diff options
author | Frank Becker | 2014-11-23 19:46:27 +0000 |
---|---|---|
committer | Gerrit Code Review @ Eclipse.org | 2015-03-25 17:02:09 +0000 |
commit | b81427cddc51e98026915a626a97e5e41b82270d (patch) | |
tree | 0f9bdbf7f97071476153a6cc75cfa3d38581e672 | |
parent | f15bbb75adcc175ff36114f90059a2678ce89693 (diff) | |
download | org.eclipse.mylyn.tasks-b81427cddc51e98026915a626a97e5e41b82270d.tar.gz org.eclipse.mylyn.tasks-b81427cddc51e98026915a626a97e5e41b82270d.tar.xz org.eclipse.mylyn.tasks-b81427cddc51e98026915a626a97e5e41b82270d.zip |
426644: add notifyChangeListeners to remove invalid cache entries
Change-Id: I25208137f013d9b6097bd66986903d907632d28e
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=426644
4 files changed, 158 insertions, 3 deletions
diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core.tests/META-INF/MANIFEST.MF b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core.tests/META-INF/MANIFEST.MF index 425dc786e..cd3e8e0eb 100644 --- a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core.tests/META-INF/MANIFEST.MF +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core.tests/META-INF/MANIFEST.MF @@ -7,9 +7,11 @@ Bundle-Vendor: Eclipse Mylyn Fragment-Host: org.eclipse.mylyn.bugzilla.rest.core Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Require-Bundle: org.junit;bundle-version="4.8.2", - org.eclipse.mylyn.commons.sdk.util -Require-Bundle: org.junit;bundle-version="4.8.2", + org.hamcrest;bundle-version="[1.0.0,1.2.0)", + org.hamcrest.library;bundle-version="[1.0.0,1.2.0)", org.eclipse.mylyn.commons.sdk.util, com.google.guava;bundle-version="10.0.1", org.apache.commons.io -Export-Package: org.eclipse.mylyn.internal.bugzilla.rest.core;x-internal:=true +Export-Package: org.eclipse.mylyn.internal.bugzilla.rest.core;x-internal:=true, + org.eclipse.mylyn.internal.bugzilla.rest.core.tests;x-internal:=true, + org.eclipse.mylyn.internal.bugzilla.rest.test.support;x-internal:=true diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core.tests/src/org/eclipse/mylyn/internal/bugzilla/rest/core/tests/BugzillaRestConnectorTest.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core.tests/src/org/eclipse/mylyn/internal/bugzilla/rest/core/tests/BugzillaRestConnectorTest.java index 11cb739a6..aabb2f29a 100644 --- a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core.tests/src/org/eclipse/mylyn/internal/bugzilla/rest/core/tests/BugzillaRestConnectorTest.java +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core.tests/src/org/eclipse/mylyn/internal/bugzilla/rest/core/tests/BugzillaRestConnectorTest.java @@ -1,6 +1,7 @@ package org.eclipse.mylyn.internal.bugzilla.rest.core.tests; import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @@ -11,12 +12,17 @@ import java.util.concurrent.TimeUnit; import org.eclipse.core.runtime.CoreException; import org.eclipse.mylyn.commons.net.AuthenticationCredentials; import org.eclipse.mylyn.commons.net.AuthenticationType; +import org.eclipse.mylyn.commons.repositories.core.RepositoryLocation; +import org.eclipse.mylyn.commons.repositories.core.auth.UserCredentials; +import org.eclipse.mylyn.commons.sdk.util.CommonTestUtil; +import org.eclipse.mylyn.commons.sdk.util.CommonTestUtil.PrivilegeLevel; import org.eclipse.mylyn.commons.sdk.util.Junit4TestFixtureRunner; import org.eclipse.mylyn.commons.sdk.util.Junit4TestFixtureRunner.FixtureDefinition; import org.eclipse.mylyn.internal.bugzilla.rest.core.BugzillaRestConfiguration; import org.eclipse.mylyn.internal.bugzilla.rest.core.BugzillaRestConnector; import org.eclipse.mylyn.internal.bugzilla.rest.core.Duration; import org.eclipse.mylyn.internal.bugzilla.rest.test.support.BugzillaRestTestFixture; +import org.eclipse.mylyn.internal.tasks.core.IRepositoryConstants; import org.eclipse.mylyn.tasks.core.TaskRepository; import org.junit.Before; import org.junit.Test; @@ -81,6 +87,97 @@ public class BugzillaRestConnectorTest { } @Test + public void testRepositoryCacheNotChanged() throws Exception { + TaskRepository repository = new TaskRepository(actualFixture.repository().getConnectorKind(), + actualFixture.repository().getRepositoryUrl()); + UserCredentials credentials = CommonTestUtil.getCredentials(PrivilegeLevel.USER); + repository.setCredentials(org.eclipse.mylyn.commons.net.AuthenticationType.REPOSITORY, + new AuthenticationCredentials(credentials.getUserName(), credentials.getPassword()), true); + + // load a configuration into the cache + BugzillaRestConfiguration configuration = connector.getRepositoryConfiguration(repository); + assertNotNull(configuration); + + // test that with an wrong user the configuration is removed from the cache + String orgUserName = repository.getProperty("org.eclipse.mylyn.tasklist.repositories.username"); + repository.setProperty("org.eclipse.mylyn.tasklist.repositories.username", "xxx"); + BugzillaRestConfiguration configurationForCompare = connector.getRepositoryConfiguration(repository); + assertNull(configurationForCompare); + + // load a configuration into the cache + repository.setProperty("org.eclipse.mylyn.tasklist.repositories.username", orgUserName); + configurationForCompare = connector.getRepositoryConfiguration(repository); + assertThat(configuration, not(equalTo(configurationForCompare))); + // the following three properties do not remove the entry from the cache + repository.setProperty(RepositoryLocation.PROPERTY_LABEL, "nolabel"); + BugzillaRestConfiguration newConfiguration = connector.getRepositoryConfiguration(repository); + assertNotNull(newConfiguration); + assertEquals(configurationForCompare, newConfiguration); + repository.setProperty(TaskRepository.OFFLINE, "false"); + newConfiguration = connector.getRepositoryConfiguration(repository); + assertNotNull(newConfiguration); + assertEquals(configurationForCompare, newConfiguration); + repository.setProperty(TaskRepository.OFFLINE, "false"); + newConfiguration = connector.getRepositoryConfiguration(repository); + assertNotNull(newConfiguration); + assertEquals(configurationForCompare, newConfiguration); + repository.setProperty(IRepositoryConstants.PROPERTY_ENCODING, "UTF-16"); + newConfiguration = connector.getRepositoryConfiguration(repository); + assertNotNull(newConfiguration); + assertEquals(configurationForCompare, newConfiguration); + repository.setProperty(TaskRepository.PROXY_HOSTNAME, "nohost"); + newConfiguration = connector.getRepositoryConfiguration(repository); + assertNotNull(newConfiguration); + assertEquals(configurationForCompare, newConfiguration); + repository.setProperty(TaskRepository.PROXY_PORT, "noport"); + newConfiguration = connector.getRepositoryConfiguration(repository); + assertNotNull(newConfiguration); + assertEquals(configurationForCompare, newConfiguration); + repository.setProperty("org.eclipse.mylyn.tasklist.repositories.savePassword", "true"); + newConfiguration = connector.getRepositoryConfiguration(repository); + assertNotNull(newConfiguration); + assertEquals(configurationForCompare, newConfiguration); + } + + @Test + public void testRepositoryCacheChanged() throws Exception { + TaskRepository repository = new TaskRepository(actualFixture.repository().getConnectorKind(), + actualFixture.repository().getRepositoryUrl()); + UserCredentials credentials = CommonTestUtil.getCredentials(PrivilegeLevel.USER); + repository.setCredentials(org.eclipse.mylyn.commons.net.AuthenticationType.REPOSITORY, + new AuthenticationCredentials(credentials.getUserName(), credentials.getPassword()), true); + + // load a configuration into the cache + BugzillaRestConfiguration configuration = connector.getRepositoryConfiguration(repository); + assertNotNull(configuration); + + // test that with an wrong user the configuration is removed from the cache + String orgUserName = repository.getProperty("org.eclipse.mylyn.tasklist.repositories.username"); + repository.setProperty("org.eclipse.mylyn.tasklist.repositories.username", "xxx"); + BugzillaRestConfiguration configurationForCompare = connector.getRepositoryConfiguration(repository); + assertNull(configurationForCompare); + repository.setProperty("org.eclipse.mylyn.tasklist.repositories.username", orgUserName); + configurationForCompare = connector.getRepositoryConfiguration(repository); + assertNotNull(configurationForCompare); + + repository.setProperty(IRepositoryConstants.PROPERTY_TIMEZONE, "Europe/Brussels"); + BugzillaRestConfiguration newConfiguration = connector.getRepositoryConfiguration(repository); + assertNotNull(newConfiguration); + assertThat(configurationForCompare, not(equalTo(newConfiguration))); + configurationForCompare = newConfiguration; + + repository.setProperty("version", "xxx"); + newConfiguration = connector.getRepositoryConfiguration(repository); + assertNotNull(newConfiguration); + assertThat(configurationForCompare, not(equalTo(newConfiguration))); + + int i = 9; + i++; + + //version=unknown, encoding=UTF-8, timezone=Europe/Berlin + } + + @Test public void testLoadCacheWrongRepository() throws Exception { TaskRepository taskRepository = new TaskRepository(connector.getConnectorKind(), "http://mylyn.org/bugzilla-rest-trunk-wrong/"); diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestConnector.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestConnector.java index 0e1c62700..3d1c69058 100644 --- a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestConnector.java +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestConnector.java @@ -11,6 +11,8 @@ package org.eclipse.mylyn.internal.bugzilla.rest.core; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -27,6 +29,7 @@ import org.eclipse.mylyn.commons.repositories.core.RepositoryLocation; import org.eclipse.mylyn.commons.repositories.core.auth.AuthenticationType; import org.eclipse.mylyn.commons.repositories.core.auth.UserCredentials; import org.eclipse.mylyn.internal.commons.core.operations.NullOperationMonitor; +import org.eclipse.mylyn.internal.tasks.core.IRepositoryConstants; import org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector; import org.eclipse.mylyn.tasks.core.IRepositoryQuery; import org.eclipse.mylyn.tasks.core.ITask; @@ -57,6 +60,46 @@ public class BugzillaRestConnector extends AbstractRepositoryConnector { private static final ThreadLocal<IOperationMonitor> context = new ThreadLocal<IOperationMonitor>(); + private boolean ignoredProperty(String propertyName) { + if (propertyName.equals(RepositoryLocation.PROPERTY_LABEL) || propertyName.equals(TaskRepository.OFFLINE) + || propertyName.equals(IRepositoryConstants.PROPERTY_ENCODING) + || propertyName.equals(TaskRepository.PROXY_HOSTNAME) || propertyName.equals(TaskRepository.PROXY_PORT) + || propertyName.equals("org.eclipse.mylyn.tasklist.repositories.savePassword") + || propertyName.equals("org.eclipse.mylyn.tasklist.repositories.proxy.usedefault") + || propertyName.equals("org.eclipse.mylyn.tasklist.repositories.proxy.savePassword") + || propertyName.equals("org.eclipse.mylyn.tasklist.repositories.proxy.username") + || propertyName.equals("org.eclipse.mylyn.tasklist.repositories.proxy.password") + || propertyName.equals("org.eclipse.mylyn.tasklist.repositories.proxy.enabled")) { + return true; + } + return false; + } + + private final PropertyChangeListener repositoryChangeListener4ClientCache = new PropertyChangeListener() { + + @Override + public void propertyChange(PropertyChangeEvent evt) { + if (ignoredProperty(evt.getPropertyName())) { + return; + } + TaskRepository taskRepository = (TaskRepository) evt.getSource(); + clientCache.invalidate(new RepositoryKey(taskRepository)); + } + }; + + private final PropertyChangeListener repositoryChangeListener4ConfigurationCache = new PropertyChangeListener() { + + @Override + public void propertyChange(PropertyChangeEvent evt) { + if (ignoredProperty(evt.getPropertyName()) + || evt.getPropertyName().equals("org.eclipse.mylyn.tasklist.repositories.password")) { + return; + } + TaskRepository taskRepository = (TaskRepository) evt.getSource(); + configurationCache.invalidate(new RepositoryKey(taskRepository)); + } + }; + private final LoadingCache<RepositoryKey, BugzillaRestClient> clientCache = CacheBuilder.newBuilder() .expireAfterAccess(CLIENT_CACHE_DURATION.getValue(), CLIENT_CACHE_DURATION.getUnit()) .build(new CacheLoader<RepositoryKey, BugzillaRestClient>() { @@ -64,6 +107,7 @@ public class BugzillaRestConnector extends AbstractRepositoryConnector { @Override public BugzillaRestClient load(RepositoryKey key) throws Exception { TaskRepository repository = key.getRepository(); + repository.addChangeListener(repositoryChangeListener4ClientCache); return createClient(repository); } }); @@ -82,6 +126,8 @@ public class BugzillaRestConnector extends AbstractRepositoryConnector { @Override public Optional<BugzillaRestConfiguration> load(RepositoryKey key) throws Exception { BugzillaRestClient client = clientCache.get(key); + TaskRepository repository = key.getRepository(); + repository.addChangeListener(repositoryChangeListener4ConfigurationCache); return Optional.fromNullable(client.getConfiguration(key.getRepository(), context.get())); } diff --git a/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/tasks/core/TaskRepository.java b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/tasks/core/TaskRepository.java index 253084e1e..cb420fd17 100644 --- a/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/tasks/core/TaskRepository.java +++ b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/tasks/core/TaskRepository.java @@ -35,6 +35,8 @@ import org.eclipse.mylyn.internal.commons.repositories.core.LocationService; import org.eclipse.mylyn.internal.tasks.core.IRepositoryConstants; import org.eclipse.mylyn.internal.tasks.core.RepositoryPerson; +import com.google.common.base.Objects; + /** * Note that task repositories use Strings for storing time stamps because using Date objects led to the following * problems: @@ -261,9 +263,17 @@ public final class TaskRepository extends PlatformObject { if (userProperty.equals(getKeyPrefix(AuthenticationType.REPOSITORY) + USERNAME)) { this.setProperty(userProperty, username); } else { + String oldUserValue = credentialsStore.get(userProperty, ""); credentialsStore.put(userProperty, username, false); + if (!Objects.equal(oldUserValue, username)) { + notifyChangeListeners(userProperty, oldUserValue, username); + } } + String oldPasswordValue = credentialsStore.get(passwordProperty, ""); credentialsStore.put(passwordProperty, password, true); + if (!Objects.equal(oldPasswordValue, password)) { + notifyChangeListeners(passwordProperty, "", ""); + } } /** |