Compute the author/committer name/email from git configuration
[jgit.git] / org.spearce.jgit.test / tst / org / spearce / jgit / lib / RepositoryConfigTest.java
blob259bc057faed3d81992f613919a1247ee74505cb
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 File path = writeTrashFile("config_001", "[foo]\nbar\n");
61 RepositoryConfig repositoryConfig = new RepositoryConfig(null, path);
62 System.out.println(repositoryConfig.getString("foo", null, "bar"));
63 assertEquals(true, repositoryConfig.getBoolean("foo", null, "bar", false));
64 assertEquals("", repositoryConfig.getString("foo", null, "bar"));
67 /**
68 * Read various data from a subsection.
70 * @throws IOException
72 public void test002_ReadWithSubsection() throws IOException {
73 final File path = writeTrashFile("config_002", "[foo \"zip\"]\nbar\n[foo \"zap\"]\nbar=false\nn=3\n");
74 RepositoryConfig repositoryConfig = new RepositoryConfig(null, path);
75 assertEquals(true, repositoryConfig.getBoolean("foo", "zip", "bar", false));
76 assertEquals("", repositoryConfig.getString("foo","zip", "bar"));
77 assertEquals(false, repositoryConfig.getBoolean("foo", "zap", "bar", true));
78 assertEquals("false", repositoryConfig.getString("foo", "zap", "bar"));
79 assertEquals(3, repositoryConfig.getInt("foo", "zap", "n", 4));
80 assertEquals(4, repositoryConfig.getInt("foo", "zap","m", 4));
83 public void test003_PutRemote() throws IOException {
84 File cfgFile = writeTrashFile("config_003", "");
85 RepositoryConfig repositoryConfig = new RepositoryConfig(null, cfgFile);
86 repositoryConfig.setString("sec", "ext", "name", "value");
87 repositoryConfig.setString("sec", "ext", "name2", "value2");
88 repositoryConfig.save();
89 checkFile(cfgFile, "[sec \"ext\"]\n\tname = value\n\tname2 = value2\n");
92 public void test004_PutGetSimple() throws IOException {
93 File cfgFile = writeTrashFile("config_004", "");
94 RepositoryConfig repositoryConfig = new RepositoryConfig(null, cfgFile);
95 repositoryConfig.setString("my", null, "somename", "false");
96 repositoryConfig.save();
97 checkFile(cfgFile, "[my]\n\tsomename = false\n");
98 assertEquals("false", repositoryConfig
99 .getString("my", null, "somename"));
102 public void test005_PutGetStringList() throws IOException {
103 File cfgFile = writeTrashFile("config_005", "");
104 RepositoryConfig repositoryConfig = new RepositoryConfig(null, cfgFile);
105 final LinkedList<String> values = new LinkedList<String>();
106 values.add("value1");
107 values.add("value2");
108 repositoryConfig.setStringList("my", null, "somename", values);
109 repositoryConfig.save();
110 assertTrue(Arrays.equals(values.toArray(), repositoryConfig
111 .getStringList("my", null, "somename")));
112 checkFile(cfgFile, "[my]\n\tsomename = value1\n\tsomename = value2\n");
115 public void test006_readCaseInsensitive() throws IOException {
116 final File path = writeTrashFile("config_001", "[Foo]\nBar\n");
117 RepositoryConfig repositoryConfig = new RepositoryConfig(null, path);
118 assertEquals(true, repositoryConfig.getBoolean("foo", null, "bar", false));
119 assertEquals("", repositoryConfig.getString("foo", null, "bar"));
122 public void test007_readUserInfos() throws IOException {
123 String hostname;
124 try {
125 InetAddress localMachine = InetAddress.getLocalHost();
126 hostname = localMachine.getCanonicalHostName();
127 } catch (UnknownHostException e) {
128 hostname = "localhost";
131 final File globalConfig = writeTrashFile("global.config", "");
132 final File localConfig = writeTrashFile("local.config", "");
133 System.clearProperty(Constants.OS_USER_NAME_KEY);
135 RepositoryConfig globalRepositoryConfig = new RepositoryConfig(null, globalConfig);
136 RepositoryConfig localRepositoryConfig = new RepositoryConfig(globalRepositoryConfig, localConfig);
137 fakeSystemReader.values.clear();
139 String authorName;
140 String authorEmail;
142 // no values defined nowhere
143 authorName = localRepositoryConfig.getAuthorName();
144 authorEmail = localRepositoryConfig.getAuthorEmail();
145 assertEquals(Constants.UNKNOWN_USER_DEFAULT, authorName);
146 assertEquals(Constants.UNKNOWN_USER_DEFAULT + "@" + hostname, authorEmail);
148 // the system user name is defined
149 fakeSystemReader.values.put(Constants.OS_USER_NAME_KEY, "os user name");
150 authorName = localRepositoryConfig.getAuthorName();
151 assertEquals("os user name", authorName);
153 if (hostname != null && hostname.length() != 0) {
154 authorEmail = localRepositoryConfig.getAuthorEmail();
155 assertEquals("os user name@" + hostname, authorEmail);
158 // the git environment variables are defined
159 fakeSystemReader.values.put(Constants.GIT_AUTHOR_NAME_KEY, "git author name");
160 fakeSystemReader.values.put(Constants.GIT_AUTHOR_EMAIL_KEY, "author@email");
161 authorName = localRepositoryConfig.getAuthorName();
162 authorEmail = localRepositoryConfig.getAuthorEmail();
163 assertEquals("git author name", authorName);
164 assertEquals("author@email", authorEmail);
166 // the values are defined in the global configuration
167 globalRepositoryConfig.setString("user", null, "name", "global username");
168 globalRepositoryConfig.setString("user", null, "email", "author@globalemail");
169 authorName = localRepositoryConfig.getAuthorName();
170 authorEmail = localRepositoryConfig.getAuthorEmail();
171 assertEquals("global username", authorName);
172 assertEquals("author@globalemail", authorEmail);
174 // the values are defined in the local configuration
175 localRepositoryConfig.setString("user", null, "name", "local username");
176 localRepositoryConfig.setString("user", null, "email", "author@localemail");
177 authorName = localRepositoryConfig.getAuthorName();
178 authorEmail = localRepositoryConfig.getAuthorEmail();
179 assertEquals("local username", authorName);
180 assertEquals("author@localemail", authorEmail);
182 authorName = localRepositoryConfig.getCommitterName();
183 authorEmail = localRepositoryConfig.getCommitterEmail();
184 assertEquals("local username", authorName);
185 assertEquals("author@localemail", authorEmail);