1 | package geniusweb.partiesserver;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.junit.Assert.assertTrue;
|
---|
5 |
|
---|
6 | import java.io.File;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.nio.charset.Charset;
|
---|
9 | import java.nio.file.Files;
|
---|
10 | import java.nio.file.Path;
|
---|
11 | import java.nio.file.Paths;
|
---|
12 | import java.util.Arrays;
|
---|
13 | import java.util.Collection;
|
---|
14 | import java.util.HashMap;
|
---|
15 | import java.util.LinkedList;
|
---|
16 | import java.util.List;
|
---|
17 | import java.util.Map;
|
---|
18 | import java.util.logging.Handler;
|
---|
19 | import java.util.logging.Level;
|
---|
20 | import java.util.logging.LogRecord;
|
---|
21 | import java.util.logging.Logger;
|
---|
22 |
|
---|
23 | import org.junit.Test;
|
---|
24 |
|
---|
25 | import geniusweb.partiesserver.repository.AvailableParty;
|
---|
26 | import tudelft.utilities.files.FileInfo;
|
---|
27 | import tudelft.utilities.files.FileWatcher;
|
---|
28 | import tudelft.utilities.immutablelist.Tuple;
|
---|
29 | import tudelft.utilities.listener.Listenable;
|
---|
30 | import tudelft.utilities.repository.Repository;
|
---|
31 |
|
---|
32 | public class AvailablePartiesUpdaterTest {
|
---|
33 | private static final String RANDOMPARTY = "target/jars/randomparty-1.5.0.jar";
|
---|
34 | private static final int TESTRATE = 200; // check file changes every 200ms
|
---|
35 | @SuppressWarnings("unchecked")
|
---|
36 | private Repository<String, AvailableParty> repo = new TestRepo();
|
---|
37 | private AvailablePartiesUpdater updater;
|
---|
38 | private Path tmpdir;
|
---|
39 | private final List<LogRecord> logrecords = new LinkedList<>();
|
---|
40 | private Path RNDPARTY;
|
---|
41 |
|
---|
42 | private void init() throws IOException {
|
---|
43 | tmpdir = Files.createTempDirectory("geniuswebparties");
|
---|
44 | RNDPARTY = tmpdir.resolve("randomparty.jar");
|
---|
45 | logrecords.clear();
|
---|
46 | Logger.getLogger("partiesserver").addHandler(new Handler() {
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | public void publish(LogRecord record) {
|
---|
50 | if (record.getLevel().intValue() >= Level.WARNING.intValue()) {
|
---|
51 | logrecords.add(record);
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public void flush() {
|
---|
57 | // TODO Auto-generated method stub
|
---|
58 |
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override
|
---|
62 | public void close() throws SecurityException {
|
---|
63 | // TODO Auto-generated method stub
|
---|
64 |
|
---|
65 | }
|
---|
66 | });
|
---|
67 |
|
---|
68 | updater = new AvailablePartiesUpdater(repo) {
|
---|
69 | @Override
|
---|
70 | protected Path getRootDir() {
|
---|
71 | return tmpdir;
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | protected Listenable<Tuple<FileInfo, FileInfo>> getFileWatcher(
|
---|
76 | File directory) {
|
---|
77 | return new FileWatcher(directory, TESTRATE, 1);
|
---|
78 | }
|
---|
79 | };
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Test
|
---|
83 | public void smokeTest() throws IOException {
|
---|
84 | init();
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Test
|
---|
88 | public void initiallyEmptyTest() throws IOException, InterruptedException {
|
---|
89 | init();
|
---|
90 | Thread.sleep(2 * TESTRATE);
|
---|
91 | assertTrue(repo.list().isEmpty());
|
---|
92 | assertTrue(logrecords.isEmpty());
|
---|
93 | }
|
---|
94 |
|
---|
95 | @Test
|
---|
96 | public void IgnoreBadFileTest() throws IOException, InterruptedException {
|
---|
97 | init();
|
---|
98 |
|
---|
99 | Path file = tmpdir.resolve("file1.txt");
|
---|
100 | Files.write(file, Arrays.asList("blah blah"), Charset.forName("UTF-8"));
|
---|
101 |
|
---|
102 | Thread.sleep(2 * TESTRATE);
|
---|
103 | assertTrue(repo.list().isEmpty());
|
---|
104 | assertEquals(1, logrecords.size());
|
---|
105 | assertTrue(logrecords.get(0).getMessage()
|
---|
106 | .contains("Can not load class from file"));
|
---|
107 | assertTrue(logrecords.get(0).getThrown().getMessage()
|
---|
108 | .contains("must be a file and have extension .jar"));
|
---|
109 |
|
---|
110 | }
|
---|
111 |
|
---|
112 | @Test
|
---|
113 | public void IgnoreBadJarFileTest()
|
---|
114 | throws IOException, InterruptedException {
|
---|
115 | init();
|
---|
116 |
|
---|
117 | Path file = tmpdir.resolve("file1.jar");
|
---|
118 | Files.write(file, Arrays.asList("blah blah"), Charset.forName("UTF-8"));
|
---|
119 |
|
---|
120 | Thread.sleep(2 * TESTRATE);
|
---|
121 | assertTrue(repo.list().isEmpty());
|
---|
122 | assertEquals(1, logrecords.size());
|
---|
123 | assertTrue(logrecords.get(0).getMessage()
|
---|
124 | .contains("Can not load class from file"));
|
---|
125 | assertTrue(logrecords.get(0).getThrown().getMessage()
|
---|
126 | .contains("error in opening zip file"));
|
---|
127 |
|
---|
128 | }
|
---|
129 |
|
---|
130 | @Test
|
---|
131 | public void ignoreEmptyJarFileTest()
|
---|
132 | throws IOException, InterruptedException {
|
---|
133 | init();
|
---|
134 | Files.copy(getClass().getResourceAsStream("/empty.jar"),
|
---|
135 | tmpdir.resolve("empty.jar"));
|
---|
136 |
|
---|
137 | Thread.sleep(2 * TESTRATE);
|
---|
138 | assertTrue(repo.list().isEmpty());
|
---|
139 | assertEquals(1, logrecords.size());
|
---|
140 | assertTrue(logrecords.get(0).getMessage()
|
---|
141 | .contains("Can not load class from file"));
|
---|
142 | assertTrue(logrecords.get(0).getThrown().getMessage().contains(
|
---|
143 | "jar file does not contain manifest with main class"));
|
---|
144 | }
|
---|
145 |
|
---|
146 | @Test
|
---|
147 | public void randomPartyJarTest() throws IOException, InterruptedException {
|
---|
148 | init();
|
---|
149 | Files.copy(Paths.get(RANDOMPARTY), tmpdir.resolve(RNDPARTY));
|
---|
150 |
|
---|
151 | Thread.sleep(2 * TESTRATE);
|
---|
152 | assertTrue(logrecords.isEmpty());
|
---|
153 | assertEquals(1, repo.list().size());
|
---|
154 | }
|
---|
155 |
|
---|
156 | @Test
|
---|
157 | public void removeJarTest() throws IOException, InterruptedException {
|
---|
158 | init();
|
---|
159 | Files.copy(Paths.get(RANDOMPARTY), RNDPARTY);
|
---|
160 | Thread.sleep(2 * TESTRATE);
|
---|
161 | assertEquals(1, repo.list().size());
|
---|
162 |
|
---|
163 | Files.delete(RNDPARTY);
|
---|
164 | Thread.sleep(2 * TESTRATE);
|
---|
165 | assertEquals(0, repo.list().size());
|
---|
166 | }
|
---|
167 |
|
---|
168 | @Test
|
---|
169 | public void removeRootTest() throws IOException, InterruptedException {
|
---|
170 | init();
|
---|
171 | Files.copy(Paths.get(RANDOMPARTY), RNDPARTY);
|
---|
172 | Thread.sleep(2 * TESTRATE);
|
---|
173 | assertEquals(1, repo.list().size());
|
---|
174 |
|
---|
175 | Files.delete(RNDPARTY);
|
---|
176 | Files.delete(tmpdir);
|
---|
177 | Thread.sleep(2 * TESTRATE);
|
---|
178 | assertEquals(0, repo.list().size());
|
---|
179 | assertEquals(1, logrecords.size());
|
---|
180 | assertTrue(logrecords.get(0).getMessage()
|
---|
181 | .contains("Can not read repository root directory"));
|
---|
182 |
|
---|
183 | }
|
---|
184 |
|
---|
185 | }
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Basic repo to track what the updater is doing.
|
---|
189 | */
|
---|
190 | class TestRepo implements Repository<String, AvailableParty> {
|
---|
191 | private Map<String, AvailableParty> map = new HashMap<String, AvailableParty>();
|
---|
192 |
|
---|
193 | @Override
|
---|
194 | public Collection<AvailableParty> list() {
|
---|
195 | return map.values();
|
---|
196 | }
|
---|
197 |
|
---|
198 | @Override
|
---|
199 | public AvailableParty get(String id) {
|
---|
200 | return map.get(id);
|
---|
201 | }
|
---|
202 |
|
---|
203 | @Override
|
---|
204 | public void put(AvailableParty entity) {
|
---|
205 | if (map.containsKey(entity.getID())) {
|
---|
206 | throw new IllegalStateException("repo already contains entity");
|
---|
207 | }
|
---|
208 | map.put(entity.getID(), entity);
|
---|
209 | }
|
---|
210 |
|
---|
211 | @Override
|
---|
212 | public void remove(String id) {
|
---|
213 | map.remove(id);
|
---|
214 | }
|
---|
215 |
|
---|
216 | @Override
|
---|
217 | public void replace(AvailableParty entity) {
|
---|
218 | if (!map.containsKey(entity.getID())) {
|
---|
219 | throw new IllegalStateException("repo does not contain entity");
|
---|
220 | }
|
---|
221 | map.put(entity.getID(), entity);
|
---|
222 | }
|
---|
223 |
|
---|
224 | } |
---|