1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
package org.eclipse.mylyn.internal.bugzilla.rest.core.tests;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import java.util.concurrent.TimeUnit;
import org.eclipse.mylyn.commons.net.AuthenticationCredentials;
import org.eclipse.mylyn.commons.net.AuthenticationType;
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.TaskRepositoryManager;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
@RunWith(Junit4TestFixtureRunner.class)
@FixtureDefinition(fixtureClass = BugzillaRestTestFixture.class, fixtureType = "bugzillaREST")
public class BugzillaRestConnectorTest {
private final BugzillaRestTestFixture actualFixture;
private static TaskRepositoryManager manager;
private BugzillaRestConnector connector;
private BugzillaRestConfiguration configuration;
public BugzillaRestConnectorTest(BugzillaRestTestFixture fixture) {
this.actualFixture = fixture;
}
@Before
public void setUp() {
connector = new BugzillaRestConnector();
}
@Test
public void testReloadCache() throws Exception {
connector = new BugzillaRestConnectorLocal(new Duration(5, TimeUnit.SECONDS));
assertNotNull(connector);
System.out.println(new java.util.Date().toString());
configuration = connector.getRepositoryConfiguration(actualFixture.repository());
System.out.println(new java.util.Date().toString());
assertNotNull(configuration);
Thread.sleep(6000L);
System.out.println(new java.util.Date().toString());
BugzillaRestConfiguration configuration_new = connector.getRepositoryConfiguration(actualFixture.repository());
System.out.println("after configuration_new");
System.out.println(new java.util.Date().toString());
assertEquals(configuration, configuration_new);
mySync = this;
synchronized (mySync) {
try {
mySync.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
mySync = null;
System.out.println("after configuration reload");
System.out.println(new java.util.Date().toString());
BugzillaRestConfiguration configuration_new1 = connector.getRepositoryConfiguration(actualFixture.repository());
System.out.println(new java.util.Date().toString());
assertThat(configuration, not(configuration_new1));
}
@Test
public void testLoadCache() throws Exception {
BugzillaRestConfiguration configuration = connector.getRepositoryConfiguration(actualFixture.repository());
assertNotNull(configuration);
Thread.sleep(7000L);
BugzillaRestConfiguration configuration_new = connector.getRepositoryConfiguration(actualFixture.repository());
assertEquals(configuration, configuration_new);
Thread.sleep(4000L);
BugzillaRestConfiguration configuration_new1 = connector.getRepositoryConfiguration(actualFixture.repository());
assertEquals(configuration, configuration_new1);
}
@Test
public void testLoadCacheWrongRepository() throws Exception {
TaskRepository taskRepository = new TaskRepository(connector.getConnectorKind(),
"http://mylyn.org/bugzilla-rest-trunk-wrong/");
AuthenticationCredentials credentials = new AuthenticationCredentials("username", "password");
taskRepository.setCredentials(AuthenticationType.REPOSITORY, credentials, false);
BugzillaRestConfiguration configuration = connector.getRepositoryConfiguration(taskRepository);
assertNull(configuration);
}
BugzillaRestConnectorTest mySync;
class BugzillaRestConnectorLocal extends BugzillaRestConnector {
public BugzillaRestConnectorLocal(Duration refreshAfterWriteDuration) {
super(refreshAfterWriteDuration);
}
@Override
protected CacheBuilder<Object, Object> createCacheBuilder(Duration expireAfterWriteDuration,
Duration refreshAfterWriteDuration) {
return super.createCacheBuilder(expireAfterWriteDuration, refreshAfterWriteDuration).removalListener(
new RemovalListener<Object, Object>() {
@Override
public void onRemoval(RemovalNotification<Object, Object> notification) {
if (mySync != null) {
synchronized (mySync) {
mySync.notify();
}
}
}
});
}
}
}
|