git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Shawn O. Pearce" <spearce@spearce.org>
To: Robin Rosenberg <robin.rosenberg@dewire.com>
Cc: git@vger.kernel.org
Subject: [JGIT PATCH 08/19] Extract the test specific SystemReader out of RepositoryTestCase
Date: Sat, 25 Jul 2009 11:52:51 -0700	[thread overview]
Message-ID: <1248547982-4003-9-git-send-email-spearce@spearce.org> (raw)
In-Reply-To: <1248547982-4003-8-git-send-email-spearce@spearce.org>

We may need this in tests that don't extend off RepositoryTestCase,
as not all tests require a local Git repository to be created in
the host filesystem.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../tst/org/spearce/jgit/lib/MockSystemReader.java |   78 ++++++++++++++++++++
 .../org/spearce/jgit/lib/RepositoryConfigTest.java |   49 ++++++------
 .../org/spearce/jgit/lib/RepositoryTestCase.java   |   56 +-------------
 3 files changed, 107 insertions(+), 76 deletions(-)
 create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/lib/MockSystemReader.java

diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/MockSystemReader.java b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/MockSystemReader.java
new file mode 100644
index 0000000..62862d1
--- /dev/null
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/MockSystemReader.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2009, Yann Simon <yann.simon.fr@gmail.com>
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ *   copyright notice, this list of conditions and the following
+ *   disclaimer in the documentation and/or other materials provided
+ *   with the distribution.
+ *
+ * - Neither the name of the Git Development Community nor the
+ *   names of its contributors may be used to endorse or promote
+ *   products derived from this software without specific prior
+ *   written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.spearce.jgit.lib;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.spearce.jgit.util.SystemReader;
+
+class MockSystemReader extends SystemReader {
+	final Map<String, String> values = new HashMap<String, String>();
+
+	FileBasedConfig userGitConfig;
+
+	MockSystemReader() {
+		init(Constants.OS_USER_NAME_KEY);
+		init(Constants.GIT_AUTHOR_NAME_KEY);
+		init(Constants.GIT_AUTHOR_EMAIL_KEY);
+		init(Constants.GIT_COMMITTER_NAME_KEY);
+		init(Constants.GIT_COMMITTER_EMAIL_KEY);
+		userGitConfig = new FileBasedConfig(null, null);
+	}
+
+	private void init(final String n) {
+		values.put(n, n);
+	}
+
+	public String getenv(String variable) {
+		return values.get(variable);
+	}
+
+	public String getProperty(String key) {
+		return values.get(key);
+	}
+
+	public FileBasedConfig openUserConfig() {
+		return userGitConfig;
+	}
+
+	public String getHostname() {
+		return "fake.host.example.com";
+	}
+}
diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryConfigTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryConfigTest.java
index 5bb9afb..e320679 100644
--- a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryConfigTest.java
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryConfigTest.java
@@ -45,6 +45,8 @@
 import java.util.Arrays;
 import java.util.LinkedList;
 
+import org.spearce.jgit.util.SystemReader;
+
 /**
  * Test reading of git config
  */
@@ -113,59 +115,58 @@ public void test006_readCaseInsensitive() throws IOException {
 		assertEquals("", repositoryConfig.getString("foo", null, "bar"));
 	}
 
-	public void test007_readUserInfos() throws IOException {
-		final String hostname = FAKE_HOSTNAME;
-		final File localConfig = writeTrashFile("local.config", "");
-		System.clearProperty(Constants.OS_USER_NAME_KEY);
-
-		RepositoryConfig localRepositoryConfig = new RepositoryConfig(userGitConfig, localConfig);
-		fakeSystemReader.values.clear();
+	public void test007_readUserConfig() {
+		final MockSystemReader mockSystemReader = (MockSystemReader)SystemReader.getInstance();
+		final String hostname = mockSystemReader.getHostname();
+		final Config userGitConfig = mockSystemReader.userGitConfig;
+		final RepositoryConfig localConfig = db.getConfig();
+		mockSystemReader.values.clear();
 
 		String authorName;
 		String authorEmail;
 
 		// no values defined nowhere
-		authorName = localRepositoryConfig.getAuthorName();
-		authorEmail = localRepositoryConfig.getAuthorEmail();
+		authorName = localConfig.getAuthorName();
+		authorEmail = localConfig.getAuthorEmail();
 		assertEquals(Constants.UNKNOWN_USER_DEFAULT, authorName);
 		assertEquals(Constants.UNKNOWN_USER_DEFAULT + "@" + hostname, authorEmail);
 
 		// the system user name is defined
-		fakeSystemReader.values.put(Constants.OS_USER_NAME_KEY, "os user name");
-		authorName = localRepositoryConfig.getAuthorName();
+		mockSystemReader.values.put(Constants.OS_USER_NAME_KEY, "os user name");
+		authorName = localConfig.getAuthorName();
 		assertEquals("os user name", authorName);
 
 		if (hostname != null && hostname.length() != 0) {
-			authorEmail = localRepositoryConfig.getAuthorEmail();
+			authorEmail = localConfig.getAuthorEmail();
 			assertEquals("os user name@" + hostname, authorEmail);
 		}
 
 		// the git environment variables are defined
-		fakeSystemReader.values.put(Constants.GIT_AUTHOR_NAME_KEY, "git author name");
-		fakeSystemReader.values.put(Constants.GIT_AUTHOR_EMAIL_KEY, "author@email");
-		authorName = localRepositoryConfig.getAuthorName();
-		authorEmail = localRepositoryConfig.getAuthorEmail();
+		mockSystemReader.values.put(Constants.GIT_AUTHOR_NAME_KEY, "git author name");
+		mockSystemReader.values.put(Constants.GIT_AUTHOR_EMAIL_KEY, "author@email");
+		authorName = localConfig.getAuthorName();
+		authorEmail = localConfig.getAuthorEmail();
 		assertEquals("git author name", authorName);
 		assertEquals("author@email", authorEmail);
 
 		// the values are defined in the global configuration
 		userGitConfig.setString("user", null, "name", "global username");
 		userGitConfig.setString("user", null, "email", "author@globalemail");
-		authorName = localRepositoryConfig.getAuthorName();
-		authorEmail = localRepositoryConfig.getAuthorEmail();
+		authorName = localConfig.getAuthorName();
+		authorEmail = localConfig.getAuthorEmail();
 		assertEquals("global username", authorName);
 		assertEquals("author@globalemail", authorEmail);
 
 		// the values are defined in the local configuration
-		localRepositoryConfig.setString("user", null, "name", "local username");
-		localRepositoryConfig.setString("user", null, "email", "author@localemail");
-		authorName = localRepositoryConfig.getAuthorName();
-		authorEmail = localRepositoryConfig.getAuthorEmail();
+		localConfig.setString("user", null, "name", "local username");
+		localConfig.setString("user", null, "email", "author@localemail");
+		authorName = localConfig.getAuthorName();
+		authorEmail = localConfig.getAuthorEmail();
 		assertEquals("local username", authorName);
 		assertEquals("author@localemail", authorEmail);
 
-		authorName = localRepositoryConfig.getCommitterName();
-		authorEmail = localRepositoryConfig.getCommitterEmail();
+		authorName = localConfig.getCommitterName();
+		authorEmail = localConfig.getCommitterEmail();
 		assertEquals("local username", authorName);
 		assertEquals("author@localemail", authorEmail);
 	}
diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java
index 9dfaeef..24a99ca 100644
--- a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java
@@ -47,9 +47,7 @@
 import java.io.OutputStreamWriter;
 import java.io.Reader;
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 
 import junit.framework.TestCase;
 
@@ -81,8 +79,6 @@
 
 	protected static final PersonIdent jcommitter;
 
-	protected static final String FAKE_HOSTNAME = "fake.host.example.com";
-
 	static {
 		jauthor = new PersonIdent("J. Author", "jauthor@example.com");
 		jcommitter = new PersonIdent("J. Committer", "jcommitter@example.com");
@@ -90,38 +86,6 @@
 
 	protected boolean packedGitMMAP;
 
-	protected static class FakeSystemReader extends SystemReader {
-		Map<String, String> values = new HashMap<String, String>();
-		RepositoryConfig userGitConfig;
-		public String getenv(String variable) {
-			return values.get(variable);
-		}
-		public String getProperty(String key) {
-			return values.get(key);
-		}
-		public RepositoryConfig openUserConfig() {
-			return userGitConfig;
-		}
-		public void setUserGitConfig(RepositoryConfig userGitConfig) {
-			this.userGitConfig = userGitConfig;
-		}
-		public String getHostname() {
-			return FAKE_HOSTNAME;
-		}
-	}
-
-	/**
-	 * Simulates the reading of system variables and properties.
-	 * Unit test can control the returned values by manipulating
-	 * {@link FakeSystemReader#values}.
-	 */
-	protected static FakeSystemReader fakeSystemReader;
-
-	static {
-		fakeSystemReader = new FakeSystemReader();
-		SystemReader.setInstance(fakeSystemReader);
-	}
-
 	/**
 	 * Configure JGit before setting up test repositories.
 	 */
@@ -241,12 +205,6 @@ protected static void checkFile(File f, final String checkData)
 
 	protected Repository db;
 
-	/**
-	 * mock user's global configuration used instead ~/.gitconfig.
-	 * This configuration can be modified by the tests without any
-	 * effect for ~/.gitconfig.
-	 */
-	protected RepositoryConfig userGitConfig;
 	private static Thread shutdownhook;
 	private static List<Runnable> shutDownCleanups = new ArrayList<Runnable>();
 	private static int testcount;
@@ -278,9 +236,10 @@ public void run() {
 			Runtime.getRuntime().addShutdownHook(shutdownhook);
 		}
 
-		final File userGitConfigFile = new File(trash_git, "usergitconfig").getAbsoluteFile();
-		userGitConfig = new RepositoryConfig(null, userGitConfigFile);
-		fakeSystemReader.setUserGitConfig(userGitConfig);
+		final MockSystemReader mockSystemReader = new MockSystemReader();
+		mockSystemReader.userGitConfig = new FileBasedConfig(null, new File(
+				trash_git, "usergitconfig"));
+		SystemReader.setInstance(mockSystemReader);
 
 		db = new Repository(trash_git);
 		db.create();
@@ -302,13 +261,6 @@ public void run() {
 		}
 
 		copyFile(JGitTestUtil.getTestResourceFile("packed-refs"), new File(trash_git,"packed-refs"));
-
-		fakeSystemReader.values.clear();
-		fakeSystemReader.values.put(Constants.OS_USER_NAME_KEY, Constants.OS_USER_NAME_KEY);
-		fakeSystemReader.values.put(Constants.GIT_AUTHOR_NAME_KEY, Constants.GIT_AUTHOR_NAME_KEY);
-		fakeSystemReader.values.put(Constants.GIT_AUTHOR_EMAIL_KEY, Constants.GIT_AUTHOR_EMAIL_KEY);
-		fakeSystemReader.values.put(Constants.GIT_COMMITTER_NAME_KEY, Constants.GIT_COMMITTER_NAME_KEY);
-		fakeSystemReader.values.put(Constants.GIT_COMMITTER_EMAIL_KEY, Constants.GIT_COMMITTER_EMAIL_KEY);
 	}
 
 	protected void tearDown() throws Exception {
-- 
1.6.4.rc2.216.g769fa

  reply	other threads:[~2009-07-25 18:54 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-25 18:52 [JGIT PATCH 00/19] More Config class cleanup work Shawn O. Pearce
2009-07-25 18:52 ` [JGIT PATCH 01/19] Cleanup nonstandard references to encoding strings to bytes Shawn O. Pearce
2009-07-25 18:52   ` [JGIT PATCH 02/19] Delete incorrect Javadoc from Config's getRawString method Shawn O. Pearce
2009-07-25 18:52     ` [JGIT PATCH 03/19] Make Config.escapeValue a private method Shawn O. Pearce
2009-07-25 18:52       ` [JGIT PATCH 04/19] Allow a RemoteConfig to use the more generic Config class Shawn O. Pearce
2009-07-25 18:52         ` [JGIT PATCH 05/19] Use type specific sets when creating a new RepositoryConfig Shawn O. Pearce
2009-07-25 18:52           ` [JGIT PATCH 06/19] Move SystemReader out of RepositoryConfig Shawn O. Pearce
2009-07-25 18:52             ` [JGIT PATCH 07/19] Correct user config to be of type FileBasedConfig Shawn O. Pearce
2009-07-25 18:52               ` Shawn O. Pearce [this message]
2009-07-25 18:52                 ` [JGIT PATCH 09/19] Refactor Config hierarchy to make IO more explicit Shawn O. Pearce
2009-07-25 18:52                   ` [JGIT PATCH 10/19] Test for the config file when creating a new repository Shawn O. Pearce
2009-07-25 18:52                     ` [JGIT PATCH 11/19] Remove the map lookup for values in Config Shawn O. Pearce
2009-07-25 18:52                       ` [JGIT PATCH 12/19] Return base values first from Config.getStringList() Shawn O. Pearce
2009-07-25 18:52                         ` [JGIT PATCH 13/19] Make Config thread safe by using copy-on-write semantics Shawn O. Pearce
2009-07-25 18:52                           ` [JGIT PATCH 14/19] Support cached application models in a Config Shawn O. Pearce
2009-07-25 18:52                             ` [JGIT PATCH 15/19] Cache Config subsection names when requested by application code Shawn O. Pearce
2009-07-25 18:52                               ` [JGIT PATCH 16/19] Refactor author/committer lookup to use cached data Shawn O. Pearce
2009-07-25 18:53                                 ` [JGIT PATCH 17/19] Move repository config creation fully into Repository class Shawn O. Pearce
2009-07-25 18:53                                   ` [JGIT PATCH 18/19] Use Config SectionParser cache to store daemon enable states Shawn O. Pearce
2009-07-25 18:53                                     ` [JGIT PATCH 19/19] Use Config cache for fetch and receive configuration parsing Shawn O. Pearce
2009-07-25 22:54                   ` [JGIT PATCH 09/19] Refactor Config hierarchy to make IO more explicit Robin Rosenberg
2009-07-25 22:55                     ` Shawn O. Pearce
2009-07-25 23:34                       ` Robin Rosenberg
2009-07-25 23:38                         ` Shawn O. Pearce
2009-07-25 20:32     ` [JGIT PATCH 02/19] Delete incorrect Javadoc from Config's getRawString method Robin Rosenberg
2009-07-25 20:33       ` Shawn O. Pearce

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1248547982-4003-9-git-send-email-spearce@spearce.org \
    --to=spearce@spearce.org \
    --cc=git@vger.kernel.org \
    --cc=robin.rosenberg@dewire.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).