/
SimpleXmlTester.java
256 lines (220 loc) · 9.04 KB
/
SimpleXmlTester.java
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* #%L
* Netarchivesuite - common - test
* %%
* Copyright (C) 2005 - 2018 The Royal Danish Library,
* the National Library of France and the Austrian National Library.
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
package dk.netarkivet.common.utils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.File;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import dk.netarkivet.common.exceptions.ArgumentNotValid;
import dk.netarkivet.common.exceptions.IOFailure;
import dk.netarkivet.common.exceptions.UnknownID;
import dk.netarkivet.testutils.CollectionAsserts;
import dk.netarkivet.testutils.TestFileUtils;
/**
* Unit tests for the class SimpleXml.
*/
public class SimpleXmlTester {
@Before
public void setUp() {
try {
if (!dk.netarkivet.common.utils.TestInfo.TEMPDIR.exists()) {
TestInfo.TEMPDIR.mkdir();
}
FileUtils.removeRecursively(TestInfo.TEMPDIR);
TestFileUtils.copyDirectoryNonCVS(TestInfo.DATADIR, dk.netarkivet.common.utils.TestInfo.TEMPDIR);
} catch (Exception e) {
fail("Could not setup configuration");
}
}
@After
public void tearDown() {
FileUtils.removeRecursively(TestInfo.TEMPDIR);
}
/** Verify that it is possible to load a simple XML file author. */
@Test
public void testLoadAndSave() {
SimpleXml xml = new SimpleXml(TestInfo.TESTXML);
String value = xml.getString("dk.netarkivet.test.q");
assertEquals("Loaded value must exist", "what is the question", value);
List<String> items = xml.getList("dk.netarkivet.test.list1");
CollectionAsserts.assertListEquals("Must match stored items", items, "item1", "item2", "item3");
xml.save(TestInfo.NEWXML);
SimpleXml newxml = new SimpleXml(TestInfo.NEWXML);
String newvalue = newxml.getString("dk.netarkivet.test.q");
assertEquals("Reloaded value must exist", "what is the question", newvalue);
List<String> newitems = newxml.getList("dk.netarkivet.test.list1");
CollectionAsserts.assertListEquals("Must match stored items", newitems, "item1", "item2", "item3");
List<String> answers = xml.getList("dk.netarkivet.answer");
CollectionAsserts.assertListEquals("Must have both answers", answers, "42", "43");
}
/** Verify that it is possible to delete and set a key in a simple XML file. */
@Test
public void testDeleteAndSet() {
SimpleXml xml = new SimpleXml(TestInfo.TESTXML);
/** add a key and value */
String value = "unknown";
xml.add("dk.netarkivet.user", value);
/** verify that the key exists */
assertTrue("Key must exist after adding", xml.hasKey("dk.netarkivet.user"));
/** verify that the key and value exists */
String retrievevalue = xml.getString("dk.netarkivet.user");
assertEquals("Inserted key should be correct", "unknown", retrievevalue);
}
/** Check loading of non xml file fails. */
@Test
public void testLoadNonXml() {
try {
new SimpleXml(TestInfo.INVALIDXML);
fail("Loading invalid xml should throw exception");
} catch (IOFailure e) {
// Expected
}
}
/** Check loading of unknown file or null file. */
@Test
public void testLoadNonExistingFile() {
try {
new SimpleXml((File) null);
fail("Loading null file should throw an exception");
} catch (Exception e) {
// ok
}
try {
new SimpleXml(new File("./tests/dk/netarkivet/utils/data/invalid.xml"));
fail("Loading unknown file should throw an exception");
} catch (Exception e) {
// ok
}
}
@Test
public void testAdd() {
SimpleXml simpleXml = new SimpleXml(TestInfo.SETTINGS_FILE);
String key = "settings.test.key";
String value = "a value";
assertFalse("Key should not exist before adding", simpleXml.hasKey(key));
try {
simpleXml.add(null, value);
fail("Should die on null key");
} catch (ArgumentNotValid e) {
// expected
}
try {
simpleXml.add("", value);
fail("Should die on empty key");
} catch (ArgumentNotValid e) {
// expected
}
try {
simpleXml.add(key, (String[]) null);
fail("Should die on null value");
} catch (ArgumentNotValid e) {
// expected
}
assertFalse("Key should not exist after errors", simpleXml.hasKey(key));
simpleXml.add(key, value);
assertTrue("Key should exist after adding", simpleXml.hasKey(key));
assertEquals("Key's value should be the one set", value, simpleXml.getString(key));
String value2 = "another value";
simpleXml.add(key, value2);
simpleXml.add(key, value2);
assertTrue("Key should exist after adding", simpleXml.hasKey(key));
assertEquals("Key's value should be the one set first", value, simpleXml.getString(key));
CollectionAsserts.assertListEquals("Should have values in order after second insert", simpleXml.getList(key),
value, value2, value2);
}
@Test
public void testUpdate() {
SimpleXml simpleXml = new SimpleXml(TestInfo.SETTINGS_FILE);
String key = "settings.test.reset";
String value1 = "first value";
simpleXml.add(key, value1);
simpleXml.add(key, value1);
String value2 = "second value";
simpleXml.update(key, value2);
CollectionAsserts.assertListEquals("Should have updated value only", simpleXml.getList(key), value2);
try {
simpleXml.update(key, (String[]) null);
fail("Should die on null value");
} catch (ArgumentNotValid e) {
CollectionAsserts.assertListEquals("Should still have old value", simpleXml.getList(key), value2);
}
try {
simpleXml.update(key, value1, null);
fail("Should die on null value");
} catch (ArgumentNotValid e) {
CollectionAsserts.assertListEquals("Should still have old value", simpleXml.getList(key), value2);
}
simpleXml.update(key);
assertFalse("Should not have key any more", simpleXml.hasKey(key));
}
@Test
public void testHasKey() {
SimpleXml simpleXml = new SimpleXml(TestInfo.SETTINGS_FILE);
assertTrue("Should have known key", simpleXml.hasKey("settings.common.tempDir"));
assertFalse("Should not have unknown key", simpleXml.hasKey("an.unknown.key"));
assertFalse("Should not have unknown key under root element", simpleXml.hasKey("settings.foo.bar"));
try {
simpleXml.hasKey(null);
fail("Should fail on null key");
} catch (ArgumentNotValid e) {
// expected
}
try {
simpleXml.hasKey("");
fail("Should fail on empty key");
} catch (ArgumentNotValid e) {
// expected
}
}
@Test
public void testGetTree() {
SimpleXml xml = new SimpleXml(TestInfo.TESTXML);
StringTree<String> tree1 = xml.getTree("dk.netarkivet.test.q");
assertNotNull("Getting a tree should return non-null", tree1);
assertEquals("Single-leaf tree should just have contents", "what is the question", tree1.getValue());
StringTree<String> tree2 = xml.getTree("dk.netarkivet.test");
assertNotNull("Getting a tree should return non-null", tree2);
assertEquals("Complex tree should have simple sub-tree", "what is the question", tree2.getSubTree("q")
.getValue());
CollectionAsserts.assertListEquals("Complex tree should have complex sub-tree",
tree2.getLeafMultimap().get("list1"), "item1", "item2", "item3");
try {
xml.getTree("foo.bar");
fail("Should get exception on non-existing path");
} catch (UnknownID e) {
// Expected
}
try {
xml.getTree("dk.netarkivet");
fail("Should get exception on ambiguous path");
} catch (UnknownID e) {
// Expected
}
}
}