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
|
package com.gitblit;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.eclipse.jgit.util.FS;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import static org.junit.Assert.*;
public class StoredUserConfigTest
{
private static File file;
@Before
public void setup()
{
file = new File("./suc-test.conf");
file.delete();
}
@After
public void teardown()
{
file.delete();
}
@Test
public void testSection() throws Exception
{
StoredUserConfig config = new StoredUserConfig(file);
config.setString("USER", "norman", "key", "value");
config.setString("USER", "admin", "displayName", "marusha");
config.setString("USER", null, "role", "none");
config.setString("TEAM", "admin", "role", "admin");
config.setString("TEAM", "ci", "email", "ci@example.com");
config.setString("TEAM", null, "displayName", "noone");
config.save();
StoredConfig cfg = new FileBasedConfig(file, FS.detect());
cfg.load();
assertEquals("value", cfg.getString("USER", "norman", "key"));
assertEquals("marusha", cfg.getString("USER", "admin", "displayName"));
assertEquals("none", cfg.getString("USER", null, "role"));
assertEquals("admin", cfg.getString("TEAM", "admin", "role"));
assertEquals("ci@example.com", cfg.getString("TEAM", "ci", "email"));
assertEquals("noone", cfg.getString("TEAM", null, "displayName"));
}
@Test
public void testStringFields() throws Exception
{
StoredUserConfig config = new StoredUserConfig(file);
config.setString("USER", "admin", "password", "secret");
config.setString("USER", "admin", "displayName", "marusha");
config.setString("USER", "admin", "email", "name@example.com");
config.setString("USER", "other", "password", "password");
config.setString("USER", "other", "displayName", "mama");
config.setString("USER", "other", "email", "other@example.com");
config.setString("USER", "other", "repository", "RW+:repo1");
config.setString("USER", "other", "repository", "RW+:repo2");
config.setString("USER", null, "displayName", "default");
config.save();
StoredConfig cfg = new FileBasedConfig(file, FS.detect());
cfg.load();
assertEquals("secret", cfg.getString("USER", "admin", "password"));
assertEquals("marusha", cfg.getString("USER", "admin", "displayName"));
assertEquals("name@example.com", cfg.getString("USER", "admin", "email"));
assertEquals("password", cfg.getString("USER", "other", "password"));
assertEquals("mama", cfg.getString("USER", "other", "displayName"));
assertEquals("other@example.com", cfg.getString("USER", "other", "email"));
String[] stringList = cfg.getStringList("USER", "other", "repository");
assertNotNull(stringList);
assertEquals(2, stringList.length);
int i = 0;
for (String s : stringList) {
if (s.equalsIgnoreCase("RW+:repo1")) i += 1;
else if (s.equalsIgnoreCase("RW+:repo2")) i += 2;
}
assertEquals("Not all repository strings found", 3, i);
assertEquals("default", cfg.getString("USER", null, "displayName"));
}
@Test
public void testBooleanFields() throws Exception
{
StoredUserConfig config = new StoredUserConfig(file);
config.setBoolean("USER", "admin", "emailMeOnMyTicketChanges", true);
config.setBoolean("USER", "master", "emailMeOnMyTicketChanges", false);
config.setBoolean("TEAM", "admin", "excludeFromFederation", true);
config.setBoolean("USER", null, "excludeFromFederation", false);
config.save();
StoredConfig cfg = new FileBasedConfig(file, FS.detect());
cfg.load();
assertTrue(cfg.getBoolean("USER", "admin", "emailMeOnMyTicketChanges", false));
assertFalse(cfg.getBoolean("USER", "master", "emailMeOnMyTicketChanges", true));
assertTrue(cfg.getBoolean("TEAM", "admin", "excludeFromFederation", false));
assertFalse(cfg.getBoolean("USER", null, "excludeFromFederation", true));
}
@Test
public void testHashEscape() throws Exception
{
StoredUserConfig config = new StoredUserConfig(file);
config.setString("USER", "admin", "role", "#admin");
config.setString("USER", "other", "role", "#none");
config.setString("USER", "other", "displayName", "big#");
config.setString("USER", "other", "email", "user#name@home.de");
config.save();
StoredConfig cfg = new FileBasedConfig(file, FS.detect());
cfg.load();
assertEquals("#admin", cfg.getString("USER", "admin", "role"));
assertEquals("#none", cfg.getString("USER", "other", "role"));
assertEquals("big#", cfg.getString("USER", "other", "displayName"));
assertEquals("user#name@home.de", cfg.getString("USER", "other", "email"));
}
@Test
public void testCtrlEscape() throws Exception
{
StoredUserConfig config = new StoredUserConfig(file);
config.setString("USER", "name", "password", "bing\bbong");
config.setString("USER", "name", "role", "domain\\admin");
config.setString("USER", "name", "displayName", "horny\n\telephant");
config.setString("USER", "name", "org", "\tbig\tblue");
config.setString("USER", "name", "unit", "the end\n");
config.setString("USER", null, "unit", "the\ndefault");
config.save();
StoredConfig cfg = new FileBasedConfig(file, FS.detect());
cfg.load();
assertEquals("bing\bbong", cfg.getString("USER", "name", "password"));
assertEquals("domain\\admin", cfg.getString("USER", "name", "role"));
assertEquals("horny\n\telephant", cfg.getString("USER", "name", "displayName"));
assertEquals("\tbig\tblue", cfg.getString("USER", "name", "org"));
assertEquals("the end\n", cfg.getString("USER", "name", "unit"));
assertEquals("the\ndefault", cfg.getString("USER", null, "unit"));
}
@Test
public void testQuoteEscape() throws Exception
{
StoredUserConfig config = new StoredUserConfig(file);
config.setString("USER", "dude", "password", "going\"places");
config.setString("USER", "dude", "role", "\"dude\"");
config.setString("USER", "dude", "displayName", "John \"The Dude\" Lebowski");
config.setString("USER", "dude", "repo", "\"front matter");
config.setString("USER", "dude", "peepo", "leadout\"");
config.save();
StoredConfig cfg = new FileBasedConfig(file, FS.detect());
cfg.load();
assertEquals("going\"places", cfg.getString("USER", "dude", "password"));
assertEquals("\"dude\"", cfg.getString("USER", "dude", "role"));
assertEquals("John \"The Dude\" Lebowski", cfg.getString("USER", "dude", "displayName"));
assertEquals("\"front matter", cfg.getString("USER", "dude", "repo"));
assertEquals("leadout\"", cfg.getString("USER", "dude", "peepo"));
}
@Test
public void testUTF8() throws Exception
{
StoredUserConfig config = new StoredUserConfig(file);
config.setString("USER", "ming", "password", "一\t二\n三");
config.setString("USER", "ming", "displayName", "白老鼠");
config.setString("USER", "ming", "peepo", "Mickey \"白老鼠\" Whitfield");
config.save();
StoredConfig cfg = new FileBasedConfig(file, FS.detect());
cfg.load();
assertEquals("一\t二\n三", cfg.getString("USER", "ming", "password"));
assertEquals("白老鼠", cfg.getString("USER", "ming", "displayName"));
assertEquals("Mickey \"白老鼠\" Whitfield", cfg.getString("USER", "ming", "peepo"));
}
}
|