android: Add some (older) unit tests
[strongswan.git] / src / frontends / android / app / src / test / java / org / strongswan / android / test / SettingsWriterTest.java
1 /*
2 * Copyright (C) 2015 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 package org.strongswan.android.test;
17
18 import org.junit.Test;
19 import org.strongswan.android.utils.SettingsWriter;
20
21 import static org.junit.Assert.assertEquals;
22
23 public class SettingsWriterTest
24 {
25 @Test
26 public void testString()
27 {
28 SettingsWriter writer = new SettingsWriter();
29 assertEquals("serialized", "", writer.serialize());
30 writer.setValue("key", "value");
31 assertEquals("serialized", "key=\"value\"\n", writer.serialize());
32 }
33
34 @Test
35 public void testInt()
36 {
37 SettingsWriter writer = new SettingsWriter();
38 writer.setValue("key", 1234);
39 assertEquals("serialized", "key=\"1234\"\n", writer.serialize());
40 writer.setValue("key", (Integer)null);
41 assertEquals("serialized", "key=\n", writer.serialize());
42 }
43
44 @Test
45 public void testBoolean()
46 {
47 SettingsWriter writer = new SettingsWriter();
48 writer.setValue("key1", true);
49 writer.setValue("key2", false);
50 assertEquals("serialized", "key1=\"1\"\nkey2=\"0\"\n", writer.serialize());
51 writer.setValue("key2", (Boolean)null);
52 assertEquals("serialized", "key1=\"1\"\nkey2=\n", writer.serialize());
53 }
54
55 @Test
56 public void testReplace()
57 {
58 SettingsWriter writer = new SettingsWriter();
59 writer.setValue("key", "value");
60 writer.setValue("key", "other");
61 assertEquals("serialized", "key=\"other\"\n", writer.serialize());
62 }
63
64 @Test
65 public void testUnset()
66 {
67 SettingsWriter writer = new SettingsWriter();
68 writer.setValue("key", (String)null);
69 assertEquals("serialized", "key=\n", writer.serialize());
70 }
71
72 @Test
73 public void testChain()
74 {
75 SettingsWriter writer = new SettingsWriter();
76 writer.setValue("key1", "value").setValue("key2", 1234).setValue("key3", true);
77 assertEquals("serialized", "key1=\"value\"\nkey2=\"1234\"\nkey3=\"1\"\n", writer.serialize());
78 }
79
80 @Test
81 public void testInvalid()
82 {
83 SettingsWriter writer = new SettingsWriter();
84 writer.setValue("", "value");
85 writer.setValue(null, "value");
86 writer.setValue("se{c.key", "value");
87 writer.setValue("sec}.key", "value");
88 writer.setValue("sec\n.key", "value");
89 assertEquals("serialized", "", writer.serialize());
90 }
91
92 @Test
93 public void testEscape()
94 {
95 SettingsWriter writer = new SettingsWriter();
96 writer.setValue("key", "val\"ue");
97 writer.setValue("nl", "val\nue");
98 assertEquals("serialized", "key=\"val\\\"ue\"\nnl=\"val\nue\"\n", writer.serialize());
99 }
100
101 @Test
102 public void testSections()
103 {
104 SettingsWriter writer = new SettingsWriter();
105 writer.setValue("sec.key1", "value");
106 writer.setValue("sec.sub.key", true);
107 writer.setValue("sec.key2", 1234);
108 assertEquals("serialized", "sec {\nkey1=\"value\"\nkey2=\"1234\"\nsub {\nkey=\"1\"\n}\n}\n", writer.serialize());
109 }
110
111 @Test
112 public void testOrder()
113 {
114 SettingsWriter writer = new SettingsWriter();
115 writer.setValue("key1", "value");
116 writer.setValue("key2", 1234);
117 writer.setValue("key1", "other");
118 assertEquals("serialized", "key1=\"other\"\nkey2=\"1234\"\n", writer.serialize());
119 }
120 }