Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
ConfigurationSectionTest.java
1 package org.bukkit.configuration;
2 
3 import org.bukkit.Material;
4 import java.util.Arrays;
5 import java.util.HashMap;
6 import java.util.LinkedHashMap;
7 import java.util.List;
8 import java.util.Map;
10 import org.bukkit.util.Vector;
11 import org.junit.Test;
12 import static org.junit.Assert.*;
13 
14 public abstract class ConfigurationSectionTest {
15  public abstract ConfigurationSection getConfigurationSection();
16 
17  @Test
18  public void testGetKeys() {
19  ConfigurationSection section = getConfigurationSection();
20 
21  section.set("key", true);
22  section.set("subsection.subkey", true);
23  section.set("subsection.subkey2", true);
24  section.set("subsection.subsubsection.key", true);
25  section.set("key2", true);
26  section.set("42", true);
27 
28  assertArrayEquals(new String[] { "key", "subsection", "key2", "42" }, section.getKeys(false).toArray());
29  assertArrayEquals(new String[] { "key", "subsection", "subsection.subkey", "subsection.subkey2", "subsection.subsubsection", "subsection.subsubsection.key", "key2", "42" }, section.getKeys(true).toArray());
30  assertArrayEquals(new String[] { "subkey", "subkey2", "subsubsection", "subsubsection.key" }, section.getConfigurationSection("subsection").getKeys(true).toArray());
31  }
32 
33  @Test
34  public void testGetKeysWithDefaults() {
35  ConfigurationSection section = getConfigurationSection();
36  section.getRoot().options().copyDefaults(true);
37 
38  section.set("key", true);
39  section.addDefault("subsection.subkey", true);
40  section.addDefault("subsection.subkey2", true);
41  section.addDefault("subsection.subsubsection.key", true);
42  section.addDefault("key2", true);
43 
44  assertArrayEquals(new String[] { "subsection", "key2", "key" }, section.getKeys(false).toArray());
45  assertArrayEquals(new String[] { "subsection", "subsection.subkey", "subsection.subkey2", "subsection.subsubsection", "subsection.subsubsection.key", "key2", "key" }, section.getKeys(true).toArray());
46  assertArrayEquals(new String[] { "subkey", "subkey2", "subsubsection", "subsubsection.key" }, section.getConfigurationSection("subsection").getKeys(true).toArray());
47  }
48 
49  @Test
50  public void testGetValues() {
51  ConfigurationSection section = getConfigurationSection();
52 
53  section.set("bool", true);
54  section.set("subsection.string", "test");
55  section.set("subsection.long", Long.MAX_VALUE);
56  section.set("int", 42);
57 
58  Map<String, Object> shallowValues = section.getValues(false);
59  assertArrayEquals(new String[] { "bool", "subsection", "int" }, shallowValues.keySet().toArray());
60  assertArrayEquals(new Object[] { true, section.getConfigurationSection("subsection"), 42 }, shallowValues.values().toArray());
61 
62  Map<String, Object> deepValues = section.getValues(true);
63  assertArrayEquals(new String[] { "bool", "subsection", "subsection.string", "subsection.long", "int" }, deepValues.keySet().toArray());
64  assertArrayEquals(new Object[] { true, section.getConfigurationSection("subsection"), "test", Long.MAX_VALUE, 42 }, deepValues.values().toArray());
65  }
66 
67  @Test
68  public void testGetValuesWithDefaults() {
69  ConfigurationSection section = getConfigurationSection();
70  section.getRoot().options().copyDefaults(true);
71 
72  section.set("bool", true);
73  section.set("subsection.string", "test");
74  section.addDefault("subsection.long", Long.MAX_VALUE);
75  section.addDefault("int", 42);
76 
77  Map<String, Object> shallowValues = section.getValues(false);
78  assertArrayEquals(new String[] { "subsection", "int", "bool" }, shallowValues.keySet().toArray());
79  assertArrayEquals(new Object[] { section.getConfigurationSection("subsection"), 42, true }, shallowValues.values().toArray());
80 
81  Map<String, Object> deepValues = section.getValues(true);
82  assertArrayEquals(new String[] { "subsection", "subsection.long", "int", "bool", "subsection.string" }, deepValues.keySet().toArray());
83  assertArrayEquals(new Object[] { section.getConfigurationSection("subsection"), Long.MAX_VALUE, 42, true, "test" }, deepValues.values().toArray());
84  }
85 
86  @Test
87  public void testContains() {
88  ConfigurationSection section = getConfigurationSection();
89 
90  section.set("exists", true);
91 
92  assertTrue(section.contains("exists"));
93  assertFalse(section.contains("doesnt-exist"));
94  }
95 
96  @Test
97  public void testIsSet() {
98  ConfigurationSection section = getConfigurationSection();
99 
100  section.set("notDefault", true);
101  section.getRoot().addDefault("default", true);
102  section.getRoot().addDefault("defaultAndSet", true);
103  section.set("defaultAndSet", true);
104 
105  assertTrue(section.isSet("notDefault"));
106  assertFalse(section.isSet("default"));
107  assertTrue(section.isSet("defaultAndSet"));
108  }
109 
110  @Test
111  public void testGetCurrentPath() {
112  ConfigurationSection section = getConfigurationSection();
113 
114  assertEquals(section.getName(), section.getCurrentPath());
115  }
116 
117  @Test
118  public void testGetName() {
119  ConfigurationSection section = getConfigurationSection().createSection("subsection");
120 
121  assertEquals("subsection", section.getName());
122  assertEquals("", section.getRoot().getName());
123  }
124 
125  @Test
126  public void testGetRoot() {
127  ConfigurationSection section = getConfigurationSection();
128 
129  assertNotNull(section.getRoot());
130  assertTrue(section.getRoot().contains(section.getCurrentPath()));
131  }
132 
133  @Test
134  public void testGetParent() {
135  ConfigurationSection section = getConfigurationSection();
136  ConfigurationSection subsection = section.createSection("subsection");
137 
138  assertEquals(section.getRoot(), section.getParent());
139  assertEquals(section, subsection.getParent());
140  }
141 
142  @Test
143  public void testGet_String() {
144  ConfigurationSection section = getConfigurationSection();
145 
146  section.set("exists", "hello world");
147 
148  assertEquals("hello world", section.getString("exists"));
149  assertNull(section.getString("doesntExist"));
150  }
151 
152  @Test
153  public void testGet_String_Object() {
154  ConfigurationSection section = getConfigurationSection();
155 
156  section.set("exists", "Set Value");
157 
158  assertEquals("Set Value", section.get("exists", "Default Value"));
159  assertEquals("Default Value", section.get("doesntExist", "Default Value"));
160  }
161 
162  @Test
163  public void testSet() {
164  ConfigurationSection section = getConfigurationSection();
165 
166  section.set("exists", "hello world");
167 
168  assertTrue(section.contains("exists"));
169  assertTrue(section.isSet("exists"));
170  assertEquals("hello world", section.get("exists"));
171 
172  section.set("exists", null);
173 
174  assertFalse(section.contains("exists"));
175  assertFalse(section.isSet("exists"));
176  }
177 
178  @Test
179  public void testCreateSection() {
180  ConfigurationSection section = getConfigurationSection();
181  ConfigurationSection subsection = section.createSection("subsection");
182 
183  assertEquals("subsection", subsection.getName());
184  }
185 
186  @Test
187  public void testSectionMap() {
188  ConfigurationSection config = getConfigurationSection();
189  Map<String, Object> testMap = new LinkedHashMap<String, Object>();
190 
191  testMap.put("string", "Hello World");
192  testMap.put("integer", 15);
193 
194  config.createSection("test.path", testMap);
195 
196  assertEquals(testMap, config.getConfigurationSection("test.path").getValues(false));
197  }
198 
199  @Test
200  public void testGetString_String() {
201  ConfigurationSection section = getConfigurationSection();
202  String key = "exists";
203  String value = "Hello World";
204 
205  section.set(key, value);
206 
207  assertEquals(value, section.getString(key));
208  assertNull(section.getString("doesntExist"));
209  }
210 
211  @Test
212  public void testGetString_String_String() {
213  ConfigurationSection section = getConfigurationSection();
214  String key = "exists";
215  String value = "Hello World";
216  String def = "Default Value";
217 
218  section.set(key, value);
219 
220  assertEquals(value, section.getString(key, def));
221  assertEquals(def, section.getString("doesntExist", def));
222  }
223 
224  @Test
225  public void testIsString() {
226  ConfigurationSection section = getConfigurationSection();
227  String key = "exists";
228  String value = "Hello World";
229 
230  section.set(key, value);
231 
232  assertTrue(section.isString(key));
233  assertFalse(section.isString("doesntExist"));
234  }
235 
236  @Test
237  public void testGetInt_String() {
238  ConfigurationSection section = getConfigurationSection();
239  String key = "exists";
240  int value = Integer.MAX_VALUE;
241 
242  section.set(key, value);
243 
244  assertEquals(value, section.getInt(key));
245  assertNull(section.getString("doesntExist"));
246  }
247 
248  @Test
249  public void testGetInt_String_Int() {
250  ConfigurationSection section = getConfigurationSection();
251  String key = "exists";
252  int value = Integer.MAX_VALUE;
253  int def = Integer.MIN_VALUE;
254 
255  section.set(key, value);
256 
257  assertEquals(value, section.getInt(key, def));
258  assertEquals(def, section.getInt("doesntExist", def));
259  }
260 
261  @Test
262  public void testIsInt() {
263  ConfigurationSection section = getConfigurationSection();
264  String key = "exists";
265  int value = Integer.MAX_VALUE;
266 
267  section.set(key, value);
268 
269  assertTrue(section.isInt(key));
270  assertFalse(section.isInt("doesntExist"));
271  }
272 
273  @Test
274  public void testGetBoolean_String() {
275  ConfigurationSection section = getConfigurationSection();
276  String key = "exists";
277  boolean value = true;
278 
279  section.set(key, value);
280 
281  assertEquals(value, section.getBoolean(key));
282  assertNull(section.getString("doesntExist"));
283  }
284 
285  @Test
286  public void testGetBoolean_String_Boolean() {
287  ConfigurationSection section = getConfigurationSection();
288  String key = "exists";
289  boolean value = true;
290  boolean def = false;
291 
292  section.set(key, value);
293 
294  assertEquals(value, section.getBoolean(key, def));
295  assertEquals(def, section.getBoolean("doesntExist", def));
296  }
297 
298  @Test
299  public void testIsBoolean() {
300  ConfigurationSection section = getConfigurationSection();
301  String key = "exists";
302  boolean value = true;
303 
304  section.set(key, value);
305 
306  assertTrue(section.isBoolean(key));
307  assertFalse(section.isBoolean("doesntExist"));
308  }
309 
310  @Test
311  public void testGetDouble_String() {
312  ConfigurationSection section = getConfigurationSection();
313  String key = "exists";
314  double value = Double.MAX_VALUE;
315 
316  section.set(key, value);
317 
318  assertEquals(value, section.getDouble(key), 1);
319  assertNull(section.getString("doesntExist"));
320  }
321 
322  @Test
323  public void testGetDoubleFromInt() {
324  ConfigurationSection section = getConfigurationSection();
325  String key = "exists";
326  double value = 123;
327 
328  section.set(key, (int) value);
329 
330  assertEquals(value, section.getDouble(key), 1);
331  assertNull(section.getString("doesntExist"));
332  }
333 
334  @Test
335  public void testGetDouble_String_Double() {
336  ConfigurationSection section = getConfigurationSection();
337  String key = "exists";
338  double value = Double.MAX_VALUE;
339  double def = Double.MIN_VALUE;
340 
341  section.set(key, value);
342 
343  assertEquals(value, section.getDouble(key, def), 1);
344  assertEquals(def, section.getDouble("doesntExist", def), 1);
345  }
346 
347  @Test
348  public void testIsDouble() {
349  ConfigurationSection section = getConfigurationSection();
350  String key = "exists";
351  double value = Double.MAX_VALUE;
352 
353  section.set(key, value);
354 
355  assertTrue(section.isDouble(key));
356  assertFalse(section.isDouble("doesntExist"));
357  }
358 
359  @Test
360  public void testGetLong_String() {
361  ConfigurationSection section = getConfigurationSection();
362  String key = "exists";
363  long value = Long.MAX_VALUE;
364 
365  section.set(key, value);
366 
367  assertEquals(value, section.getLong(key));
368  assertNull(section.getString("doesntExist"));
369  }
370 
371  @Test
372  public void testGetLong_String_Long() {
373  ConfigurationSection section = getConfigurationSection();
374  String key = "exists";
375  long value = Long.MAX_VALUE;
376  long def = Long.MIN_VALUE;
377 
378  section.set(key, value);
379 
380  assertEquals(value, section.getLong(key, def));
381  assertEquals(def, section.getLong("doesntExist", def));
382  }
383 
384  @Test
385  public void testIsLong() {
386  ConfigurationSection section = getConfigurationSection();
387  String key = "exists";
388  long value = Long.MAX_VALUE;
389 
390  section.set(key, value);
391 
392  assertTrue(section.isLong(key));
393  assertFalse(section.isLong("doesntExist"));
394  }
395 
396  @Test
397  public void testGetList_String() {
398  ConfigurationSection section = getConfigurationSection();
399  String key = "exists";
400  Map<String, Object> map = new HashMap<String, Object>();
401 
402  map.put("one", 1);
403  map.put("two", "two");
404  map.put("three", 3.14);
405 
406  List<Object> value = Arrays.asList("One", "Two", "Three", 4, "5", 6.0, true, "false", map);
407 
408  section.set(key, value);
409 
410  assertEquals(value, section.getList(key));
411  assertEquals(Arrays.asList((Object) "One", "Two", "Three", "4", "5", "6.0", "true", "false"), section.getStringList(key));
412  assertEquals(Arrays.asList((Object) 4, 5, 6), section.getIntegerList(key));
413  assertEquals(Arrays.asList((Object) true, false), section.getBooleanList(key));
414  assertEquals(Arrays.asList((Object) 4.0, 5.0, 6.0), section.getDoubleList(key));
415  assertEquals(Arrays.asList((Object) 4.0f, 5.0f, 6.0f), section.getFloatList(key));
416  assertEquals(Arrays.asList((Object) 4l, 5l, 6l), section.getLongList(key));
417  assertEquals(Arrays.asList((Object) (byte) 4, (byte) 5, (byte) 6), section.getByteList(key));
418  assertEquals(Arrays.asList((Object) (short) 4, (short) 5, (short) 6), section.getShortList(key));
419  assertEquals(map, section.getMapList(key).get(0));
420  assertNull(section.getString("doesntExist"));
421  }
422 
423  @Test
424  public void testGetList_String_List() {
425  ConfigurationSection section = getConfigurationSection();
426  String key = "exists";
427  List<String> value = Arrays.asList("One", "Two", "Three");
428  List<String> def = Arrays.asList("A", "B", "C");
429 
430  section.set(key, value);
431 
432  assertEquals(value, section.getList(key, def));
433  assertEquals(def, section.getList("doesntExist", def));
434  }
435 
436  @Test
437  public void testIsList() {
438  ConfigurationSection section = getConfigurationSection();
439  String key = "exists";
440  List<String> value = Arrays.asList("One", "Two", "Three");
441 
442  section.set(key, value);
443 
444  assertTrue(section.isList(key));
445  assertFalse(section.isList("doesntExist"));
446  }
447 
448  @Test
449  public void testGetVector_String() {
450  ConfigurationSection section = getConfigurationSection();
451  String key = "exists";
452  Vector value = new Vector(Double.MIN_VALUE, Double.MAX_VALUE, 5);
453 
454  section.set(key, value);
455 
456  assertEquals(value, section.getVector(key));
457  assertNull(section.getString("doesntExist"));
458  }
459 
460  @Test
461  public void testGetVector_String_Vector() {
462  ConfigurationSection section = getConfigurationSection();
463  String key = "exists";
464  Vector value = new Vector(Double.MIN_VALUE, Double.MAX_VALUE, 5);
465  Vector def = new Vector(100, Double.MIN_VALUE, Double.MAX_VALUE);
466 
467  section.set(key, value);
468 
469  assertEquals(value, section.getVector(key, def));
470  assertEquals(def, section.getVector("doesntExist", def));
471  }
472 
473  @Test
474  public void testIsVector() {
475  ConfigurationSection section = getConfigurationSection();
476  String key = "exists";
477  Vector value = new Vector(Double.MIN_VALUE, Double.MAX_VALUE, 5);
478 
479  section.set(key, value);
480 
481  assertTrue(section.isVector(key));
482  assertFalse(section.isVector("doesntExist"));
483  }
484 
485  @Test
486  public void testGetItemStack_String() {
487  ConfigurationSection section = getConfigurationSection();
488  String key = "exists";
489  ItemStack value = new ItemStack(Material.WOOD, 50, (short) 2);
490 
491  section.set(key, value);
492 
493  assertEquals(value, section.getItemStack(key));
494  assertNull(section.getString("doesntExist"));
495  }
496 
497  @Test
498  public void testGetItemStack_String_ItemStack() {
499  ConfigurationSection section = getConfigurationSection();
500  String key = "exists";
501  ItemStack value = new ItemStack(Material.WOOD, 50, (short) 2);
502  ItemStack def = new ItemStack(Material.STONE, 1);
503 
504  section.set(key, value);
505 
506  assertEquals(value, section.getItemStack(key, def));
507  assertEquals(def, section.getItemStack("doesntExist", def));
508  }
509 
510  @Test
511  public void testIsItemStack() {
512  ConfigurationSection section = getConfigurationSection();
513  String key = "exists";
514  ItemStack value = new ItemStack(Material.WOOD, 50, (short) 2);
515 
516  section.set(key, value);
517 
518  assertTrue(section.isItemStack(key));
519  assertFalse(section.isItemStack("doesntExist"));
520  }
521 
522  @Test
523  public void testGetConfigurationSection() {
524  ConfigurationSection section = getConfigurationSection();
525  String key = "exists";
526 
527  ConfigurationSection subsection = section.createSection(key);
528 
529  assertEquals(subsection, section.getConfigurationSection(key));
530  }
531 
532  @Test
533  public void testIsConfigurationSection() {
534  ConfigurationSection section = getConfigurationSection();
535  String key = "exists";
536 
537  section.createSection(key);
538 
539  assertTrue(section.isConfigurationSection(key));
540  assertFalse(section.isConfigurationSection("doesntExist"));
541  }
542 
543  public enum TestEnum {
544  HELLO,
545  WORLD,
546  BANANAS
547  }
548 }
List< Map<?,?> > getMapList(String path)
List< Boolean > getBooleanList(String path)
void set(String path, Object value)
List< Integer > getIntegerList(String path)
ConfigurationSection createSection(String path)
Map< String, Object > getValues(boolean deep)
ConfigurationSection getConfigurationSection(String path)
void addDefault(String path, Object value)
List< Float > getFloatList(String path)
List< Double > getDoubleList(String path)
void addDefault(String path, Object value)
List< String > getStringList(String path)
List< Short > getShortList(String path)