source: src/test/java/geniusweb/partiesserver/AvailablePartiesUpdaterTest.java@ 3

Last change on this file since 3 was 1, checked in by bart, 5 years ago

Initial Release

File size: 5.8 KB
Line 
1package geniusweb.partiesserver;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertTrue;
5
6import java.io.File;
7import java.io.IOException;
8import java.nio.charset.Charset;
9import java.nio.file.Files;
10import java.nio.file.Path;
11import java.nio.file.Paths;
12import java.util.Arrays;
13import java.util.Collection;
14import java.util.HashMap;
15import java.util.LinkedList;
16import java.util.List;
17import java.util.Map;
18import java.util.logging.Handler;
19import java.util.logging.Level;
20import java.util.logging.LogRecord;
21import java.util.logging.Logger;
22
23import org.junit.Test;
24
25import geniusweb.partiesserver.repository.AvailableParty;
26import tudelft.utilities.files.FileInfo;
27import tudelft.utilities.files.FileWatcher;
28import tudelft.utilities.immutablelist.Tuple;
29import tudelft.utilities.listener.Listenable;
30import tudelft.utilities.repository.Repository;
31
32public class AvailablePartiesUpdaterTest {
33 private static final int TESTRATE = 200; // check file changes every 200ms
34 @SuppressWarnings("unchecked")
35 private Repository<String, AvailableParty> repo = new TestRepo();
36 private AvailablePartiesUpdater updater;
37 private Path tmpdir;
38 private final List<LogRecord> logrecords = new LinkedList<>();
39 private Path RNDPARTY;
40
41 private void init() throws IOException {
42 tmpdir = Files.createTempDirectory("geniuswebparties");
43 RNDPARTY = tmpdir.resolve("randomparty.jar");
44 logrecords.clear();
45 Logger.getLogger("partiesserver").addHandler(new Handler() {
46
47 @Override
48 public void publish(LogRecord record) {
49 if (record.getLevel().intValue() >= Level.WARNING.intValue()) {
50 logrecords.add(record);
51 }
52 }
53
54 @Override
55 public void flush() {
56 // TODO Auto-generated method stub
57
58 }
59
60 @Override
61 public void close() throws SecurityException {
62 // TODO Auto-generated method stub
63
64 }
65 });
66
67 updater = new AvailablePartiesUpdater(repo) {
68 @Override
69 protected Path getRootDir() {
70 return tmpdir;
71 }
72
73 @Override
74 protected Listenable<Tuple<FileInfo, FileInfo>> getFileWatcher(
75 File directory) {
76 return new FileWatcher(directory, TESTRATE, 1);
77 }
78 };
79 }
80
81 @Test
82 public void smokeTest() throws IOException {
83 init();
84 }
85
86 @Test
87 public void initiallyEmptyTest() throws IOException, InterruptedException {
88 init();
89 Thread.sleep(2 * TESTRATE);
90 assertTrue(repo.list().isEmpty());
91 assertTrue(logrecords.isEmpty());
92 }
93
94 @Test
95 public void IgnoreBadFileTest() throws IOException, InterruptedException {
96 init();
97
98 Path file = tmpdir.resolve("file1.txt");
99 Files.write(file, Arrays.asList("blah blah"), Charset.forName("UTF-8"));
100
101 Thread.sleep(2 * TESTRATE);
102 assertTrue(repo.list().isEmpty());
103 assertEquals(1, logrecords.size());
104 assertTrue(logrecords.get(0).getMessage()
105 .contains("Can not load class from file"));
106 assertTrue(logrecords.get(0).getThrown().getMessage()
107 .contains("must be a file and have extension .jar"));
108
109 }
110
111 @Test
112 public void IgnoreBadJarFileTest()
113 throws IOException, InterruptedException {
114 init();
115
116 Path file = tmpdir.resolve("file1.jar");
117 Files.write(file, Arrays.asList("blah blah"), Charset.forName("UTF-8"));
118
119 Thread.sleep(2 * TESTRATE);
120 assertTrue(repo.list().isEmpty());
121 assertEquals(1, logrecords.size());
122 assertTrue(logrecords.get(0).getMessage()
123 .contains("Can not load class from file"));
124 assertTrue(logrecords.get(0).getThrown().getMessage()
125 .contains("error in opening zip file"));
126
127 }
128
129 @Test
130 public void ignoreEmptyJarFileTest()
131 throws IOException, InterruptedException {
132 init();
133 Files.copy(getClass().getResourceAsStream("/empty.jar"),
134 tmpdir.resolve("empty.jar"));
135
136 Thread.sleep(2 * TESTRATE);
137 assertTrue(repo.list().isEmpty());
138 assertEquals(1, logrecords.size());
139 assertTrue(logrecords.get(0).getMessage()
140 .contains("Can not load class from file"));
141 assertTrue(logrecords.get(0).getThrown().getMessage().contains(
142 "jar file does not contain manifest with main class"));
143 }
144
145 @Test
146 public void randomPartyJarTest() throws IOException, InterruptedException {
147 init();
148 Files.copy(Paths.get("target/jars/randomparty-1.0.0.jar"),
149 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("target/jars/randomparty-1.0.0.jar"), 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("target/jars/randomparty-1.0.0.jar"), 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 */
190class 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}
Note: See TracBrowser for help on using the repository browser.