2 * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
3 * Copyright (C) 2009-2010, Google Inc.
4 * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
5 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
6 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
7 * and other copyright owners as documented in the project's IP log.
9 * This program and the accompanying materials are made available
10 * under the terms of the Eclipse Distribution License v1.0 which
11 * accompanies this distribution, is reproduced below, and is
12 * available at http://www.eclipse.org/org/documents/edl-v10.php
14 * All rights reserved.
16 * Redistribution and use in source and binary forms, with or
17 * without modification, are permitted provided that the following
20 * - Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
23 * - Redistributions in binary form must reproduce the above
24 * copyright notice, this list of conditions and the following
25 * disclaimer in the documentation and/or other materials provided
26 * with the distribution.
28 * - Neither the name of the Eclipse Foundation, Inc. nor the
29 * names of its contributors may be used to endorse or promote
30 * products derived from this software without specific prior
33 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
34 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
35 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
38 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
40 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
41 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
42 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
43 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
45 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 package org
.eclipse
.jgit
.lib
;
50 import java
.util
.Arrays
;
51 import java
.util
.LinkedList
;
54 import junit
.framework
.TestCase
;
56 import org
.eclipse
.jgit
.errors
.ConfigInvalidException
;
57 import org
.eclipse
.jgit
.junit
.MockSystemReader
;
58 import org
.eclipse
.jgit
.util
.FS
;
59 import org
.eclipse
.jgit
.util
.SystemReader
;
62 * Test reading of git config
64 public class RepositoryConfigTest
extends TestCase
{
65 public void test001_ReadBareKey() throws ConfigInvalidException
{
66 final Config c
= parse("[foo]\nbar\n");
67 assertEquals(true, c
.getBoolean("foo", null, "bar", false));
68 assertEquals("", c
.getString("foo", null, "bar"));
71 public void test002_ReadWithSubsection() throws ConfigInvalidException
{
72 final Config c
= parse("[foo \"zip\"]\nbar\n[foo \"zap\"]\nbar=false\nn=3\n");
73 assertEquals(true, c
.getBoolean("foo", "zip", "bar", false));
74 assertEquals("", c
.getString("foo","zip", "bar"));
75 assertEquals(false, c
.getBoolean("foo", "zap", "bar", true));
76 assertEquals("false", c
.getString("foo", "zap", "bar"));
77 assertEquals(3, c
.getInt("foo", "zap", "n", 4));
78 assertEquals(4, c
.getInt("foo", "zap","m", 4));
81 public void test003_PutRemote() {
82 final Config c
= new Config();
83 c
.setString("sec", "ext", "name", "value");
84 c
.setString("sec", "ext", "name2", "value2");
85 final String expText
= "[sec \"ext\"]\n\tname = value\n\tname2 = value2\n";
86 assertEquals(expText
, c
.toText());
89 public void test004_PutGetSimple() {
90 Config c
= new Config();
91 c
.setString("my", null, "somename", "false");
92 assertEquals("false", c
.getString("my", null, "somename"));
93 assertEquals("[my]\n\tsomename = false\n", c
.toText());
96 public void test005_PutGetStringList() {
97 Config c
= new Config();
98 final LinkedList
<String
> values
= new LinkedList
<String
>();
100 values
.add("value2");
101 c
.setStringList("my", null, "somename", values
);
103 final Object
[] expArr
= values
.toArray();
104 final String
[] actArr
= c
.getStringList("my", null, "somename");
105 assertTrue(Arrays
.equals(expArr
, actArr
));
107 final String expText
= "[my]\n\tsomename = value1\n\tsomename = value2\n";
108 assertEquals(expText
, c
.toText());
111 public void test006_readCaseInsensitive() throws ConfigInvalidException
{
112 final Config c
= parse("[Foo]\nBar\n");
113 assertEquals(true, c
.getBoolean("foo", null, "bar", false));
114 assertEquals("", c
.getString("foo", null, "bar"));
117 public void test007_readUserConfig() {
118 final MockSystemReader mockSystemReader
= new MockSystemReader();
119 SystemReader
.setInstance(mockSystemReader
);
120 final String hostname
= mockSystemReader
.getHostname();
121 final Config userGitConfig
= mockSystemReader
.openUserConfig(FS
.DETECTED
);
122 final Config localConfig
= new Config(userGitConfig
);
123 mockSystemReader
.clearProperties();
128 // no values defined nowhere
129 authorName
= localConfig
.get(UserConfig
.KEY
).getAuthorName();
130 authorEmail
= localConfig
.get(UserConfig
.KEY
).getAuthorEmail();
131 assertEquals(Constants
.UNKNOWN_USER_DEFAULT
, authorName
);
132 assertEquals(Constants
.UNKNOWN_USER_DEFAULT
+ "@" + hostname
, authorEmail
);
134 // the system user name is defined
135 mockSystemReader
.setProperty(Constants
.OS_USER_NAME_KEY
, "os user name");
136 localConfig
.uncache(UserConfig
.KEY
);
137 authorName
= localConfig
.get(UserConfig
.KEY
).getAuthorName();
138 assertEquals("os user name", authorName
);
140 if (hostname
!= null && hostname
.length() != 0) {
141 authorEmail
= localConfig
.get(UserConfig
.KEY
).getAuthorEmail();
142 assertEquals("os user name@" + hostname
, authorEmail
);
145 // the git environment variables are defined
146 mockSystemReader
.setProperty(Constants
.GIT_AUTHOR_NAME_KEY
, "git author name");
147 mockSystemReader
.setProperty(Constants
.GIT_AUTHOR_EMAIL_KEY
, "author@email");
148 localConfig
.uncache(UserConfig
.KEY
);
149 authorName
= localConfig
.get(UserConfig
.KEY
).getAuthorName();
150 authorEmail
= localConfig
.get(UserConfig
.KEY
).getAuthorEmail();
151 assertEquals("git author name", authorName
);
152 assertEquals("author@email", authorEmail
);
154 // the values are defined in the global configuration
155 userGitConfig
.setString("user", null, "name", "global username");
156 userGitConfig
.setString("user", null, "email", "author@globalemail");
157 authorName
= localConfig
.get(UserConfig
.KEY
).getAuthorName();
158 authorEmail
= localConfig
.get(UserConfig
.KEY
).getAuthorEmail();
159 assertEquals("global username", authorName
);
160 assertEquals("author@globalemail", authorEmail
);
162 // the values are defined in the local configuration
163 localConfig
.setString("user", null, "name", "local username");
164 localConfig
.setString("user", null, "email", "author@localemail");
165 authorName
= localConfig
.get(UserConfig
.KEY
).getAuthorName();
166 authorEmail
= localConfig
.get(UserConfig
.KEY
).getAuthorEmail();
167 assertEquals("local username", authorName
);
168 assertEquals("author@localemail", authorEmail
);
170 authorName
= localConfig
.get(UserConfig
.KEY
).getCommitterName();
171 authorEmail
= localConfig
.get(UserConfig
.KEY
).getCommitterEmail();
172 assertEquals("local username", authorName
);
173 assertEquals("author@localemail", authorEmail
);
176 public void testReadBoolean_TrueFalse1() throws ConfigInvalidException
{
177 final Config c
= parse("[s]\na = true\nb = false\n");
178 assertEquals("true", c
.getString("s", null, "a"));
179 assertEquals("false", c
.getString("s", null, "b"));
181 assertTrue(c
.getBoolean("s", "a", false));
182 assertFalse(c
.getBoolean("s", "b", true));
185 public void testReadBoolean_TrueFalse2() throws ConfigInvalidException
{
186 final Config c
= parse("[s]\na = TrUe\nb = fAlSe\n");
187 assertEquals("TrUe", c
.getString("s", null, "a"));
188 assertEquals("fAlSe", c
.getString("s", null, "b"));
190 assertTrue(c
.getBoolean("s", "a", false));
191 assertFalse(c
.getBoolean("s", "b", true));
194 public void testReadBoolean_YesNo1() throws ConfigInvalidException
{
195 final Config c
= parse("[s]\na = yes\nb = no\n");
196 assertEquals("yes", c
.getString("s", null, "a"));
197 assertEquals("no", c
.getString("s", null, "b"));
199 assertTrue(c
.getBoolean("s", "a", false));
200 assertFalse(c
.getBoolean("s", "b", true));
203 public void testReadBoolean_YesNo2() throws ConfigInvalidException
{
204 final Config c
= parse("[s]\na = yEs\nb = NO\n");
205 assertEquals("yEs", c
.getString("s", null, "a"));
206 assertEquals("NO", c
.getString("s", null, "b"));
208 assertTrue(c
.getBoolean("s", "a", false));
209 assertFalse(c
.getBoolean("s", "b", true));
212 public void testReadBoolean_OnOff1() throws ConfigInvalidException
{
213 final Config c
= parse("[s]\na = on\nb = off\n");
214 assertEquals("on", c
.getString("s", null, "a"));
215 assertEquals("off", c
.getString("s", null, "b"));
217 assertTrue(c
.getBoolean("s", "a", false));
218 assertFalse(c
.getBoolean("s", "b", true));
221 public void testReadBoolean_OnOff2() throws ConfigInvalidException
{
222 final Config c
= parse("[s]\na = ON\nb = OFF\n");
223 assertEquals("ON", c
.getString("s", null, "a"));
224 assertEquals("OFF", c
.getString("s", null, "b"));
226 assertTrue(c
.getBoolean("s", "a", false));
227 assertFalse(c
.getBoolean("s", "b", true));
230 public void testReadLong() throws ConfigInvalidException
{
233 assertReadLong(Long
.MIN_VALUE
);
234 assertReadLong(Long
.MAX_VALUE
);
235 assertReadLong(4L * 1024 * 1024 * 1024, "4g");
236 assertReadLong(3L * 1024 * 1024, "3 m");
237 assertReadLong(8L * 1024, "8 k");
240 assertReadLong(-1, "1.5g");
241 fail("incorrectly accepted 1.5g");
242 } catch (IllegalArgumentException e
) {
243 assertEquals("Invalid integer value: s.a=1.5g", e
.getMessage());
247 public void testBooleanWithNoValue() throws ConfigInvalidException
{
248 Config c
= parse("[my]\n\tempty\n");
249 assertEquals("", c
.getString("my", null, "empty"));
250 assertEquals(1, c
.getStringList("my", null, "empty").length
);
251 assertEquals("", c
.getStringList("my", null, "empty")[0]);
252 assertTrue(c
.getBoolean("my", "empty", false));
253 assertEquals("[my]\n\tempty\n", c
.toText());
256 public void testEmptyString() throws ConfigInvalidException
{
257 Config c
= parse("[my]\n\tempty =\n");
258 assertNull(c
.getString("my", null, "empty"));
260 String
[] values
= c
.getStringList("my", null, "empty");
261 assertNotNull(values
);
262 assertEquals(1, values
.length
);
263 assertNull(values
[0]);
265 // always matches the default, because its non-boolean
266 assertTrue(c
.getBoolean("my", "empty", true));
267 assertFalse(c
.getBoolean("my", "empty", false));
269 assertEquals("[my]\n\tempty =\n", c
.toText());
272 c
.setStringList("my", null, "empty", Arrays
.asList(values
));
273 assertEquals("[my]\n\tempty =\n", c
.toText());
276 public void testUnsetBranchSection() throws ConfigInvalidException
{
277 Config c
= parse("" //
278 + "[branch \"keep\"]\n"
279 + " merge = master.branch.to.keep.in.the.file\n"
281 + "[branch \"remove\"]\n"
282 + " merge = this.will.get.deleted\n"
283 + " remote = origin-for-some-long-gone-place\n"
285 + "[core-section-not-to-remove-in-test]\n"
286 + " packedGitLimit = 14\n");
287 c
.unsetSection("branch", "does.not.exist");
288 c
.unsetSection("branch", "remove");
290 + "[branch \"keep\"]\n"
291 + " merge = master.branch.to.keep.in.the.file\n"
293 + "[core-section-not-to-remove-in-test]\n"
294 + " packedGitLimit = 14\n", c
.toText());
297 public void testUnsetSingleSection() throws ConfigInvalidException
{
298 Config c
= parse("" //
299 + "[branch \"keep\"]\n"
300 + " merge = master.branch.to.keep.in.the.file\n"
303 + " merge = this.will.get.deleted\n"
304 + " remote = origin-for-some-long-gone-place\n"
306 + "[core-section-not-to-remove-in-test]\n"
307 + " packedGitLimit = 14\n");
308 c
.unsetSection("single", null);
310 + "[branch \"keep\"]\n"
311 + " merge = master.branch.to.keep.in.the.file\n"
313 + "[core-section-not-to-remove-in-test]\n"
314 + " packedGitLimit = 14\n", c
.toText());
317 public void test008_readSectionNames() throws ConfigInvalidException
{
318 final Config c
= parse("[a]\n [B]\n");
319 Set
<String
> sections
= c
.getSections();
320 assertTrue("Sections should contain \"a\"", sections
.contains("a"));
321 assertTrue("Sections should contain \"b\"", sections
.contains("b"));
324 public void test009_readNamesInSection() throws ConfigInvalidException
{
325 String configString
= "[core]\n" + "repositoryformatversion = 0\n"
326 + "filemode = false\n" + "logallrefupdates = true\n";
327 final Config c
= parse(configString
);
328 Set
<String
> names
= c
.getNames("core");
329 assertEquals("Core section size", 3, names
.size());
330 assertTrue("Core section should contain \"filemode\"", names
331 .contains("filemode"));
334 public void test010_readNamesInSubSection() throws ConfigInvalidException
{
335 String configString
= "[a \"sub1\"]\n"//
342 final Config c
= parse(configString
);
343 Set
<String
> names
= c
.getNames("a", "sub1");
344 assertEquals("Subsection size", 3, names
.size());
345 assertTrue("Subsection should contain \"x\"", names
.contains("x"));
346 assertTrue("Subsection should contain \"y\"", names
.contains("y"));
347 assertTrue("Subsection should contain \"z\"", names
.contains("z"));
348 names
= c
.getNames("a", "sub2");
349 assertEquals("Subsection size", 2, names
.size());
350 assertTrue("Subsection should contain \"a\"", names
.contains("a"));
351 assertTrue("Subsection should contain \"b\"", names
.contains("b"));
354 private void assertReadLong(long exp
) throws ConfigInvalidException
{
355 assertReadLong(exp
, String
.valueOf(exp
));
358 private void assertReadLong(long exp
, String act
)
359 throws ConfigInvalidException
{
360 final Config c
= parse("[s]\na = " + act
+ "\n");
361 assertEquals(exp
, c
.getLong("s", null, "a", 0L));
364 private Config
parse(final String content
) throws ConfigInvalidException
{
365 final Config c
= new Config(null);