Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
FileConfigurationTest.java
1 package org.bukkit.configuration.file;
2 
3 import java.io.BufferedWriter;
4 import java.io.File;
5 import java.io.FileWriter;
6 import java.util.Map;
8 import org.junit.Rule;
9 import org.junit.Test;
10 import org.junit.rules.TemporaryFolder;
11 import static org.junit.Assert.*;
12 
13 public abstract class FileConfigurationTest extends MemoryConfigurationTest {
14  @Rule
15  public TemporaryFolder testFolder = new TemporaryFolder();
16 
17  @Override
18  public abstract FileConfiguration getConfig();
19 
20  public abstract String getTestValuesString();
21 
22  public abstract String getTestHeaderInput();
23 
24  public abstract String getTestHeaderResult();
25 
26  @Test
27  public void testSave_File() throws Exception {
28  FileConfiguration config = getConfig();
29  File file = testFolder.newFile("test.config");
30 
31  for (Map.Entry<String, Object> entry : getTestValues().entrySet()) {
32  config.set(entry.getKey(), entry.getValue());
33  }
34 
35  config.save(file);
36 
37  assertTrue(file.isFile());
38  }
39 
40  @Test
41  public void testSave_String() throws Exception {
42  FileConfiguration config = getConfig();
43  File file = testFolder.newFile("test.config");
44 
45  for (Map.Entry<String, Object> entry : getTestValues().entrySet()) {
46  config.set(entry.getKey(), entry.getValue());
47  }
48 
49  config.save(file.getAbsolutePath());
50 
51  assertTrue(file.isFile());
52  }
53 
54  @Test
55  public void testSaveToString() {
56  FileConfiguration config = getConfig();
57 
58  for (Map.Entry<String, Object> entry : getTestValues().entrySet()) {
59  config.set(entry.getKey(), entry.getValue());
60  }
61 
62  String result = config.saveToString();
63  String expected = getTestValuesString();
64 
65  assertEquals(expected, result);
66  }
67 
68  @Test
69  public void testLoad_File() throws Exception {
70  FileConfiguration config = getConfig();
71  File file = testFolder.newFile("test.config");
72  BufferedWriter writer = new BufferedWriter(new FileWriter(file));
73  String saved = getTestValuesString();
74  Map<String, Object> values = getTestValues();
75 
76  try {
77  writer.write(saved);
78  } finally {
79  writer.close();
80  }
81 
82  config.load(file);
83 
84  for (Map.Entry<String, Object> entry : values.entrySet()) {
85  assertEquals(entry.getValue(), config.get(entry.getKey()));
86  }
87 
88  assertEquals(values.keySet(), config.getKeys(true));
89  }
90 
91  @Test
92  public void testLoad_String() throws Exception {
93  FileConfiguration config = getConfig();
94  File file = testFolder.newFile("test.config");
95  BufferedWriter writer = new BufferedWriter(new FileWriter(file));
96  String saved = getTestValuesString();
97  Map<String, Object> values = getTestValues();
98 
99  try {
100  writer.write(saved);
101  } finally {
102  writer.close();
103  }
104 
105  config.load(file.getAbsolutePath());
106 
107  for (Map.Entry<String, Object> entry : values.entrySet()) {
108  assertEquals(entry.getValue(), config.get(entry.getKey()));
109  }
110 
111  assertEquals(values.keySet(), config.getKeys(true));
112  }
113 
114  @Test
115  public void testLoadFromString() throws Exception {
116  FileConfiguration config = getConfig();
117  Map<String, Object> values = getTestValues();
118  String saved = getTestValuesString();
119 
120  config.loadFromString(saved);
121 
122  for (Map.Entry<String, Object> entry : values.entrySet()) {
123  assertEquals(entry.getValue(), config.get(entry.getKey()));
124  }
125 
126  assertEquals(values.keySet(), config.getKeys(true));
127  assertEquals(saved, config.saveToString());
128  }
129 
130  @Test
131  public void testSaveToStringWithHeader() {
132  FileConfiguration config = getConfig();
133  config.options().header(getTestHeaderInput());
134 
135  for (Map.Entry<String, Object> entry : getTestValues().entrySet()) {
136  config.set(entry.getKey(), entry.getValue());
137  }
138 
139  String result = config.saveToString();
140  String expected = getTestHeaderResult() + "\n" + getTestValuesString();
141 
142  assertEquals(expected, result);
143  }
144 
145  @Test
146  public void testParseHeader() throws Exception {
147  FileConfiguration config = getConfig();
148  Map<String, Object> values = getTestValues();
149  String saved = getTestValuesString();
150  String header = getTestHeaderResult();
151  String expected = getTestHeaderInput();
152 
153  config.loadFromString(header + "\n" + saved);
154 
155  assertEquals(expected, config.options().header());
156 
157  for (Map.Entry<String, Object> entry : values.entrySet()) {
158  assertEquals(entry.getValue(), config.get(entry.getKey()));
159  }
160 
161  assertEquals(values.keySet(), config.getKeys(true));
162  assertEquals(header + "\n" + saved, config.saveToString());
163  }
164 
165  @Test
166  public void testCopyHeader() throws Exception {
167  FileConfiguration config = getConfig();
168  FileConfiguration defaults = getConfig();
169  Map<String, Object> values = getTestValues();
170  String saved = getTestValuesString();
171  String header = getTestHeaderResult();
172  String expected = getTestHeaderInput();
173 
174  defaults.loadFromString(header);
175  config.loadFromString(saved);
176  config.setDefaults(defaults);
177 
178  assertNull(config.options().header());
179  assertEquals(expected, defaults.options().header());
180 
181  for (Map.Entry<String, Object> entry : values.entrySet()) {
182  assertEquals(entry.getValue(), config.get(entry.getKey()));
183  }
184 
185  assertEquals(values.keySet(), config.getKeys(true));
186  assertEquals(header + "\n" + saved, config.saveToString());
187 
188  config = getConfig();
189  config.loadFromString(getTestHeaderResult() + saved);
190  assertEquals(getTestHeaderResult() + saved, config.saveToString());
191  }
192 
193  @Test
194  public void testReloadEmptyConfig() throws Exception {
195  FileConfiguration config = getConfig();
196 
197  assertEquals("", config.saveToString());
198 
199  config = getConfig();
200  config.loadFromString("");
201 
202  assertEquals("", config.saveToString());
203 
204  config = getConfig();
205  config.loadFromString("\n\n"); // Should trim the first newlines of a header
206 
207  assertEquals("", config.saveToString());
208  }
209 }
abstract void loadFromString(String contents)
void set(String path, Object value)
Set< String > getKeys(boolean deep)