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

Last change on this file since 44 was 44, checked in by bart, 2 years ago

Domaineditor released.

File size: 5.9 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 String RANDOMPARTY = "src/main/webapp/partiesrepo/randomparty-2.1.4.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 // someone changed the error message deep down.
126 // assertTrue(logrecords.get(0).getThrown().getMessage().contains("error
127 // 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();
150 Files.copy(Paths.get(RANDOMPARTY), tmpdir.resolve(RNDPARTY));
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();
160 Files.copy(Paths.get(RANDOMPARTY), RNDPARTY);
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();
172 Files.copy(Paths.get(RANDOMPARTY), RNDPARTY);
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.