1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.ini4j;
17
18 import org.ini4j.sample.Dwarfs;
19
20 import org.ini4j.test.DwarfsData;
21 import org.ini4j.test.Helper;
22
23 import static org.junit.Assert.assertArrayEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertSame;
26 import static org.junit.Assert.fail;
27
28 import org.junit.Test;
29
30 import java.io.ByteArrayOutputStream;
31 import java.io.File;
32 import java.io.FileInputStream;
33 import java.io.FileNotFoundException;
34 import java.io.FileOutputStream;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.io.InputStreamReader;
38 import java.io.StringReader;
39
40 public class RegTest extends Ini4jCase
41 {
42 private static final String DWARFS_PATH = Helper.DWARFS_REG_PATH + "\\dwarfs\\";
43
44 @Test public void proba() throws Exception
45 {
46 }
47
48 @Test public void testDwarfs() throws Exception
49 {
50 Reg reg = Helper.loadDwarfsReg();
51 Dwarfs dwarfs = reg.as(Dwarfs.class, DWARFS_PATH);
52
53 assertNotNull(dwarfs);
54 Helper.assertEquals(DwarfsData.dwarfs, dwarfs);
55 }
56
57 @Test public void testInvalidFileFormatException() throws Exception
58 {
59 try
60 {
61 new Reg(Helper.getResourceReader(Helper.DWARFS_INI));
62 missing(InvalidFileFormatException.class);
63 }
64 catch (InvalidFileFormatException x)
65 {
66
67 }
68 }
69
70 @Test public void testIsWindwos()
71 {
72 assertEquals(isWindows(), Reg.isWindows());
73 }
74
75 @Test public void testLoad() throws Exception
76 {
77 Reg r1 = new Reg(new InputStreamReader(Helper.getResourceStream(Helper.DWARFS_REG), "UnicodeLittle"));
78 Reg r2 = new Reg(Helper.getResourceStream(Helper.DWARFS_REG));
79 Reg r3 = new Reg(Helper.getResourceURL(Helper.DWARFS_REG));
80 File f = Helper.getSourceFile(Helper.DWARFS_REG);
81 Reg r4 = new Reg(f);
82 Reg r5 = new Reg();
83
84 r5.setFile(f);
85 r5.load();
86 Helper.assertEquals(DwarfsData.dwarfs, r1.as(Dwarfs.class, DWARFS_PATH));
87 Helper.assertEquals(DwarfsData.dwarfs, r2.as(Dwarfs.class, DWARFS_PATH));
88 Helper.assertEquals(DwarfsData.dwarfs, r3.as(Dwarfs.class, DWARFS_PATH));
89 Helper.assertEquals(DwarfsData.dwarfs, r4.as(Dwarfs.class, DWARFS_PATH));
90 Helper.assertEquals(DwarfsData.dwarfs, r5.as(Dwarfs.class, DWARFS_PATH));
91 assertSame(f, r4.getFile());
92 }
93
94 @Test public void testLoadFileNotFoundException() throws Exception
95 {
96 Reg reg = new Reg();
97
98 try
99 {
100 reg.load();
101 missing(FileNotFoundException.class);
102 }
103 catch (FileNotFoundException x)
104 {
105
106 }
107 }
108
109 @Test public void testLoadSave() throws Exception
110 {
111 Reg reg = new Reg(Helper.getResourceURL(Helper.TEST_REG));
112
113 checkLoadSave(Helper.TEST_REG, reg);
114 }
115
116 @Test public void testMissingVersion() throws Exception
117 {
118 try
119 {
120 new Reg(new StringReader("\r\n\r\n[section]\r\n\"option\"=\"value\""));
121 missing(InvalidFileFormatException.class);
122 }
123 catch (InvalidFileFormatException x)
124 {
125
126 }
127 }
128
129 @Test public void testNonWindwosExec() throws Exception
130 {
131 if (isSkip(isWindows(), "testNonWindwosExec"))
132 {
133 return;
134 }
135
136 Reg reg = new Reg();
137
138 reg.exec(new String[] { "/bin/true" });
139 try
140 {
141 reg.exec(new String[] { "/bin/ls", "no such file" });
142 fail("IOException expected");
143 }
144 catch (IOException x)
145 {
146 assert true;
147 }
148 }
149
150 @Test public void testReadException() throws Exception
151 {
152 if (!isWindows())
153 {
154 try
155 {
156 new Reg(Reg.Hive.HKEY_CURRENT_USER.toString());
157 fail("missing UnsupportedOperationException");
158 }
159 catch (UnsupportedOperationException x)
160 {
161 assert true;
162 }
163 }
164 else
165 {
166 try
167 {
168 new Reg("no such key");
169 fail("missing IOException");
170 }
171 catch (IOException x)
172 {
173 assert true;
174 }
175 }
176 }
177
178 @Test public void testReadWrite() throws Exception
179 {
180 if (isSkip(!isWindows(), "testReadWrite"))
181 {
182 return;
183 }
184
185 Reg reg = Helper.loadDwarfsReg();
186
187 reg.write();
188 Reg dup = new Reg(Helper.DWARFS_REG_PATH);
189
190 Helper.assertEquals(reg.get(Helper.DWARFS_REG_PATH), dup.get(Helper.DWARFS_REG_PATH));
191 Dwarfs dwarfs = dup.as(Dwarfs.class, DWARFS_PATH);
192
193 assertNotNull(dwarfs);
194 Helper.assertEquals(DwarfsData.dwarfs, dwarfs);
195 }
196
197 @Test public void testStore() throws Exception
198 {
199 Reg reg = Helper.loadDwarfsReg();
200 File tmp = File.createTempFile(Reg.TMP_PREFIX, Reg.DEFAULT_SUFFIX);
201
202 tmp.deleteOnExit();
203 reg.setFile(tmp);
204 reg.store();
205 reg = new Reg(tmp);
206 Helper.assertEquals(DwarfsData.dwarfs, reg.as(Dwarfs.class, DWARFS_PATH));
207 tmp.delete();
208 }
209
210 @Test public void testStoreFileNotFoundException() throws Exception
211 {
212 try
213 {
214 new Reg().store();
215 missing(FileNotFoundException.class);
216 }
217 catch (FileNotFoundException x)
218 {
219
220 }
221 }
222
223 @Test public void testUnsupportedOperatingSystem() throws Exception
224 {
225 if (isSkip(isWindows(), "testUnsupportedOperatingSystem"))
226 {
227 return;
228 }
229
230 Reg reg = new Reg();
231
232 try
233 {
234 reg.read(Helper.DWARFS_REG_PATH);
235 fail("UnsupportedOperationException expected");
236 }
237 catch (UnsupportedOperationException x)
238 {
239 assert true;
240 }
241
242 try
243 {
244 reg.write();
245 fail("UnsupportedOperationException expected");
246 }
247 catch (UnsupportedOperationException x)
248 {
249 assert true;
250 }
251 }
252
253 private boolean isSkip(boolean flag, String testName)
254 {
255 if (!flag)
256 {
257 System.out.println("Skipping " + getClass().getName() + '#' + testName);
258 }
259
260 return flag;
261 }
262
263 private boolean isWindows()
264 {
265 String family = System.getProperty("os.family");
266
267 return (family != null) && family.equals("windows");
268 }
269
270 private void checkLoadSave(String path, Reg reg) throws Exception
271 {
272 File tmp = File.createTempFile(Reg.TMP_PREFIX, Reg.DEFAULT_SUFFIX);
273
274 tmp.deleteOnExit();
275 reg.store(new FileOutputStream(tmp));
276 assertArrayEquals(read(Helper.getResourceStream(path)), read(new FileInputStream(tmp)));
277 }
278
279 private byte[] read(InputStream input) throws Exception
280 {
281 ByteArrayOutputStream out = new ByteArrayOutputStream();
282 byte[] buff = new byte[81912];
283 int n;
284
285 while ((n = input.read(buff)) >= 0)
286 {
287 out.write(buff, 0, n);
288 }
289
290 return out.toByteArray();
291 }
292 }