429678: ensure key exists before testing secure store availability

Change-Id: I88eca01279778938e3869cf093544e188bc8be75
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=429678
diff --git a/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/internal/commons/repositories/core/SecureCredentialsStore.java b/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/internal/commons/repositories/core/SecureCredentialsStore.java
index bbb8ff7..4639d5a 100644
--- a/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/internal/commons/repositories/core/SecureCredentialsStore.java
+++ b/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/internal/commons/repositories/core/SecureCredentialsStore.java
@@ -174,7 +174,12 @@
 	@Override
 	public void testAvailability() throws UnavailableException {
 		try {
-			getSecurePreferences().get("org.eclipse.mylyn.commons.repositories.core.SecureCredentialsStore", null); //$NON-NLS-1$
+			String key = "org.eclipse.mylyn.commons.repositories.core.SecureCredentialsStore"; //$NON-NLS-1$
+			// in some cases, we can get the list of keys even though the secure store is broken, so if we just try to get
+			// a non-existant key, it won't try to access the secure store and we won't detect that it's broken. So, create a key
+			// and try to access it.
+			getSecurePreferences().put(key, Boolean.toString(true), true);
+			getSecurePreferences().get(key, null);
 		} catch (StorageException e) {
 			throw new UnavailableException(e);
 		}
diff --git a/org.eclipse.mylyn.commons.repositories.tests/src/org/eclipse/mylyn/commons/repositories/tests/core/SecureCredentialsStoreTest.java b/org.eclipse.mylyn.commons.repositories.tests/src/org/eclipse/mylyn/commons/repositories/tests/core/SecureCredentialsStoreTest.java
index 62cf264..cc9850b 100644
--- a/org.eclipse.mylyn.commons.repositories.tests/src/org/eclipse/mylyn/commons/repositories/tests/core/SecureCredentialsStoreTest.java
+++ b/org.eclipse.mylyn.commons.repositories.tests/src/org/eclipse/mylyn/commons/repositories/tests/core/SecureCredentialsStoreTest.java
@@ -12,6 +12,8 @@
 package org.eclipse.mylyn.commons.repositories.tests.core;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.fail;
 
 import java.io.UnsupportedEncodingException;
@@ -253,7 +255,9 @@
 	@Test
 	public void testTestAvailability() throws Exception {
 		StubSecureCredentialsStore store = createStubSecureCredentialsStore();
+		assertNull(store.get("org.eclipse.mylyn.commons.repositories.core.SecureCredentialsStore", null));
 		store.testAvailability();
+		assertNotNull(store.get("org.eclipse.mylyn.commons.repositories.core.SecureCredentialsStore", null));
 		store.setUnavailable(true);
 		try {
 			store.testAvailability();