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

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

Tournament overview table, few bug fixes

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