Add support for boolean config values "on", "off"
[egit/imyousuf.git] / org.spearce.jgit.test / tst / org / spearce / jgit / lib / RepositoryConfigTest.java
blobed573e137adcf0724248df9add1ae1e45fde6a70
1 /*
2 * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
3 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
4 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
5 * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the following
11 * conditions are met:
13 * - Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * - Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer in the documentation and/or other materials provided
19 * with the distribution.
21 * - Neither the name of the Git Development Community nor the
22 * names of its contributors may be used to endorse or promote
23 * products derived from this software without specific prior
24 * written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
27 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
28 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
31 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
38 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 package org.spearce.jgit.lib;
43 import java.io.File;
44 import java.io.IOException;
45 import java.net.InetAddress;
46 import java.net.UnknownHostException;
47 import java.util.Arrays;
48 import java.util.LinkedList;
50 /**
51 * Test reading of git config
53 public class RepositoryConfigTest extends RepositoryTestCase {
54 /**
55 * Read config item with no value from a section without a subsection.
57 * @throws IOException
59 public void test001_ReadBareKey() throws IOException {
60 final RepositoryConfig repositoryConfig = read("[foo]\nbar\n");
61 assertEquals(true, repositoryConfig.getBoolean("foo", null, "bar", false));
62 assertEquals("", repositoryConfig.getString("foo", null, "bar"));
65 /**
66 * Read various data from a subsection.
68 * @throws IOException
70 public void test002_ReadWithSubsection() throws IOException {
71 final RepositoryConfig repositoryConfig = read("[foo \"zip\"]\nbar\n[foo \"zap\"]\nbar=false\nn=3\n");
72 assertEquals(true, repositoryConfig.getBoolean("foo", "zip", "bar", false));
73 assertEquals("", repositoryConfig.getString("foo","zip", "bar"));
74 assertEquals(false, repositoryConfig.getBoolean("foo", "zap", "bar", true));
75 assertEquals("false", repositoryConfig.getString("foo", "zap", "bar"));
76 assertEquals(3, repositoryConfig.getInt("foo", "zap", "n", 4));
77 assertEquals(4, repositoryConfig.getInt("foo", "zap","m", 4));
80 public void test003_PutRemote() throws IOException {
81 File cfgFile = writeTrashFile("config_003", "");
82 RepositoryConfig repositoryConfig = new RepositoryConfig(null, cfgFile);
83 repositoryConfig.setString("sec", "ext", "name", "value");
84 repositoryConfig.setString("sec", "ext", "name2", "value2");
85 repositoryConfig.save();
86 checkFile(cfgFile, "[sec \"ext\"]\n\tname = value\n\tname2 = value2\n");
89 public void test004_PutGetSimple() throws IOException {
90 File cfgFile = writeTrashFile("config_004", "");
91 RepositoryConfig repositoryConfig = new RepositoryConfig(null, cfgFile);
92 repositoryConfig.setString("my", null, "somename", "false");
93 repositoryConfig.save();
94 checkFile(cfgFile, "[my]\n\tsomename = false\n");
95 assertEquals("false", repositoryConfig
96 .getString("my", null, "somename"));
99 public void test005_PutGetStringList() throws IOException {
100 File cfgFile = writeTrashFile("config_005", "");
101 RepositoryConfig repositoryConfig = new RepositoryConfig(null, cfgFile);
102 final LinkedList<String> values = new LinkedList<String>();
103 values.add("value1");
104 values.add("value2");
105 repositoryConfig.setStringList("my", null, "somename", values);
106 repositoryConfig.save();
107 assertTrue(Arrays.equals(values.toArray(), repositoryConfig
108 .getStringList("my", null, "somename")));
109 checkFile(cfgFile, "[my]\n\tsomename = value1\n\tsomename = value2\n");
112 public void test006_readCaseInsensitive() throws IOException {
113 final RepositoryConfig repositoryConfig = read("[Foo]\nBar\n");
114 assertEquals(true, repositoryConfig.getBoolean("foo", null, "bar", false));
115 assertEquals("", repositoryConfig.getString("foo", null, "bar"));
118 public void test007_readUserInfos() throws IOException {
119 String hostname;
120 try {
121 InetAddress localMachine = InetAddress.getLocalHost();
122 hostname = localMachine.getCanonicalHostName();
123 } catch (UnknownHostException e) {
124 hostname = "localhost";
127 final File localConfig = writeTrashFile("local.config", "");
128 System.clearProperty(Constants.OS_USER_NAME_KEY);
130 RepositoryConfig localRepositoryConfig = new RepositoryConfig(userGitConfig, localConfig);
131 fakeSystemReader.values.clear();
133 String authorName;
134 String authorEmail;
136 // no values defined nowhere
137 authorName = localRepositoryConfig.getAuthorName();
138 authorEmail = localRepositoryConfig.getAuthorEmail();
139 assertEquals(Constants.UNKNOWN_USER_DEFAULT, authorName);
140 assertEquals(Constants.UNKNOWN_USER_DEFAULT + "@" + hostname, authorEmail);
142 // the system user name is defined
143 fakeSystemReader.values.put(Constants.OS_USER_NAME_KEY, "os user name");
144 authorName = localRepositoryConfig.getAuthorName();
145 assertEquals("os user name", authorName);
147 if (hostname != null && hostname.length() != 0) {
148 authorEmail = localRepositoryConfig.getAuthorEmail();
149 assertEquals("os user name@" + hostname, authorEmail);
152 // the git environment variables are defined
153 fakeSystemReader.values.put(Constants.GIT_AUTHOR_NAME_KEY, "git author name");
154 fakeSystemReader.values.put(Constants.GIT_AUTHOR_EMAIL_KEY, "author@email");
155 authorName = localRepositoryConfig.getAuthorName();
156 authorEmail = localRepositoryConfig.getAuthorEmail();
157 assertEquals("git author name", authorName);
158 assertEquals("author@email", authorEmail);
160 // the values are defined in the global configuration
161 userGitConfig.setString("user", null, "name", "global username");
162 userGitConfig.setString("user", null, "email", "author@globalemail");
163 authorName = localRepositoryConfig.getAuthorName();
164 authorEmail = localRepositoryConfig.getAuthorEmail();
165 assertEquals("global username", authorName);
166 assertEquals("author@globalemail", authorEmail);
168 // the values are defined in the local configuration
169 localRepositoryConfig.setString("user", null, "name", "local username");
170 localRepositoryConfig.setString("user", null, "email", "author@localemail");
171 authorName = localRepositoryConfig.getAuthorName();
172 authorEmail = localRepositoryConfig.getAuthorEmail();
173 assertEquals("local username", authorName);
174 assertEquals("author@localemail", authorEmail);
176 authorName = localRepositoryConfig.getCommitterName();
177 authorEmail = localRepositoryConfig.getCommitterEmail();
178 assertEquals("local username", authorName);
179 assertEquals("author@localemail", authorEmail);
182 public void testReadBoolean_TrueFalse1() throws IOException {
183 final RepositoryConfig c = read("[s]\na = true\nb = false\n");
184 assertEquals("true", c.getString("s", null, "a"));
185 assertEquals("false", c.getString("s", null, "b"));
187 assertTrue(c.getBoolean("s", "a", false));
188 assertFalse(c.getBoolean("s", "b", true));
191 public void testReadBoolean_TrueFalse2() throws IOException {
192 final RepositoryConfig c = read("[s]\na = TrUe\nb = fAlSe\n");
193 assertEquals("TrUe", c.getString("s", null, "a"));
194 assertEquals("fAlSe", c.getString("s", null, "b"));
196 assertTrue(c.getBoolean("s", "a", false));
197 assertFalse(c.getBoolean("s", "b", true));
200 public void testReadBoolean_YesNo1() throws IOException {
201 final RepositoryConfig c = read("[s]\na = yes\nb = no\n");
202 assertEquals("yes", c.getString("s", null, "a"));
203 assertEquals("no", c.getString("s", null, "b"));
205 assertTrue(c.getBoolean("s", "a", false));
206 assertFalse(c.getBoolean("s", "b", true));
209 public void testReadBoolean_YesNo2() throws IOException {
210 final RepositoryConfig c = read("[s]\na = yEs\nb = NO\n");
211 assertEquals("yEs", c.getString("s", null, "a"));
212 assertEquals("NO", c.getString("s", null, "b"));
214 assertTrue(c.getBoolean("s", "a", false));
215 assertFalse(c.getBoolean("s", "b", true));
218 public void testReadBoolean_OnOff1() throws IOException {
219 final RepositoryConfig c = read("[s]\na = on\nb = off\n");
220 assertEquals("on", c.getString("s", null, "a"));
221 assertEquals("off", c.getString("s", null, "b"));
223 assertTrue(c.getBoolean("s", "a", false));
224 assertFalse(c.getBoolean("s", "b", true));
227 public void testReadBoolean_OnOff2() throws IOException {
228 final RepositoryConfig c = read("[s]\na = ON\nb = OFF\n");
229 assertEquals("ON", c.getString("s", null, "a"));
230 assertEquals("OFF", c.getString("s", null, "b"));
232 assertTrue(c.getBoolean("s", "a", false));
233 assertFalse(c.getBoolean("s", "b", true));
236 private RepositoryConfig read(final String content) throws IOException {
237 final File p = writeTrashFile(getName() + ".config", content);
238 final RepositoryConfig c = new RepositoryConfig(null, p);
239 return c;