[jbosstools-commits] JBoss Tools SVN: r35929 - in trunk/openshift/tests: org.jboss.tools.openshift.egit.test and 11 other directories.

jbosstools-commits at lists.jboss.org jbosstools-commits at lists.jboss.org
Mon Oct 24 07:48:54 EDT 2011


Author: rob.stryker at jboss.com
Date: 2011-10-24 07:48:54 -0400 (Mon, 24 Oct 2011)
New Revision: 35929

Added:
   trunk/openshift/tests/org.jboss.tools.openshift.egit.test/
   trunk/openshift/tests/org.jboss.tools.openshift.egit.test/.classpath
   trunk/openshift/tests/org.jboss.tools.openshift.egit.test/.project
   trunk/openshift/tests/org.jboss.tools.openshift.egit.test/.settings/
   trunk/openshift/tests/org.jboss.tools.openshift.egit.test/.settings/org.eclipse.jdt.core.prefs
   trunk/openshift/tests/org.jboss.tools.openshift.egit.test/META-INF/
   trunk/openshift/tests/org.jboss.tools.openshift.egit.test/META-INF/MANIFEST.MF
   trunk/openshift/tests/org.jboss.tools.openshift.egit.test/build.properties
   trunk/openshift/tests/org.jboss.tools.openshift.egit.test/pom.xml
   trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/
   trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/
   trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/
   trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/
   trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/
   trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/
   trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/
   trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/
   trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/EGitTestActivator.java
   trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/EGitUtilsTest.java
   trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/util/
   trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/util/MockSystemReader.java
   trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/util/TestProject.java
   trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/util/TestRepository.java
   trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/util/TestUtils.java
Log:
JBIDE-10013 - missed egit tests

Added: trunk/openshift/tests/org.jboss.tools.openshift.egit.test/.classpath
===================================================================
--- trunk/openshift/tests/org.jboss.tools.openshift.egit.test/.classpath	                        (rev 0)
+++ trunk/openshift/tests/org.jboss.tools.openshift.egit.test/.classpath	2011-10-24 11:48:54 UTC (rev 35929)
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+	<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>

Added: trunk/openshift/tests/org.jboss.tools.openshift.egit.test/.project
===================================================================
--- trunk/openshift/tests/org.jboss.tools.openshift.egit.test/.project	                        (rev 0)
+++ trunk/openshift/tests/org.jboss.tools.openshift.egit.test/.project	2011-10-24 11:48:54 UTC (rev 35929)
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>org.jboss.tools.openshift.egit.test</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.eclipse.pde.ManifestBuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.eclipse.pde.SchemaBuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.pde.PluginNature</nature>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>

Added: trunk/openshift/tests/org.jboss.tools.openshift.egit.test/.settings/org.eclipse.jdt.core.prefs
===================================================================
--- trunk/openshift/tests/org.jboss.tools.openshift.egit.test/.settings/org.eclipse.jdt.core.prefs	                        (rev 0)
+++ trunk/openshift/tests/org.jboss.tools.openshift.egit.test/.settings/org.eclipse.jdt.core.prefs	2011-10-24 11:48:54 UTC (rev 35929)
@@ -0,0 +1,8 @@
+#Tue Aug 16 13:32:00 CEST 2011
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.6

Added: trunk/openshift/tests/org.jboss.tools.openshift.egit.test/META-INF/MANIFEST.MF
===================================================================
--- trunk/openshift/tests/org.jboss.tools.openshift.egit.test/META-INF/MANIFEST.MF	                        (rev 0)
+++ trunk/openshift/tests/org.jboss.tools.openshift.egit.test/META-INF/MANIFEST.MF	2011-10-24 11:48:54 UTC (rev 35929)
@@ -0,0 +1,16 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: EGit Tests
+Bundle-SymbolicName: org.jboss.ide.eclipse.as.egit.test
+Bundle-Version: 2.3.0.qualifier
+Bundle-Activator: org.jboss.tools.openshift.egit.internal.test.EGitTestActivator
+Bundle-Vendor: JBoss by Red Hat
+Require-Bundle: org.eclipse.egit.core;bundle-version="1.0.0",
+ org.eclipse.jgit;bundle-version="[1.0.0,2.0.1]",
+ org.eclipse.core.runtime,
+ org.eclipse.core.resources;bundle-version="[3.7.100,4.0.0)",
+ org.eclipse.core.filesystem;bundle-version="1.3.100",
+ org.junit;bundle-version="[4.8.2,5.0.0)",
+ org.jboss.tools.openshift.egit.core;bundle-version="1.0.0"
+Bundle-RequiredExecutionEnvironment: JavaSE-1.6
+Bundle-ActivationPolicy: lazy

Added: trunk/openshift/tests/org.jboss.tools.openshift.egit.test/build.properties
===================================================================
--- trunk/openshift/tests/org.jboss.tools.openshift.egit.test/build.properties	                        (rev 0)
+++ trunk/openshift/tests/org.jboss.tools.openshift.egit.test/build.properties	2011-10-24 11:48:54 UTC (rev 35929)
@@ -0,0 +1,4 @@
+source.. = src/
+output.. = bin/
+bin.includes = META-INF/,\
+               .

Added: trunk/openshift/tests/org.jboss.tools.openshift.egit.test/pom.xml
===================================================================
--- trunk/openshift/tests/org.jboss.tools.openshift.egit.test/pom.xml	                        (rev 0)
+++ trunk/openshift/tests/org.jboss.tools.openshift.egit.test/pom.xml	2011-10-24 11:48:54 UTC (rev 35929)
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+	<modelVersion>4.0.0</modelVersion> 
+	<parent>
+		<groupId>org.jboss.tools.openshift</groupId>
+		<artifactId>tests</artifactId>
+		<version>1.0.0-SNAPSHOT</version>
+	</parent>
+	<groupId>org.jboss.tools.openshift.tests</groupId>
+	<artifactId>org.jboss.tools.openshift.egit.test</artifactId> 
+	<packaging>eclipse-test-plugin</packaging>
+</project>

Added: trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/EGitTestActivator.java
===================================================================
--- trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/EGitTestActivator.java	                        (rev 0)
+++ trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/EGitTestActivator.java	2011-10-24 11:48:54 UTC (rev 35929)
@@ -0,0 +1,30 @@
+package org.jboss.tools.openshift.egit.internal.test;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+
+public class EGitTestActivator implements BundleActivator {
+
+	private static BundleContext context;
+
+	static BundleContext getContext() {
+		return context;
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
+	 */
+	public void start(BundleContext bundleContext) throws Exception {
+		EGitTestActivator.context = bundleContext;
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
+	 */
+	public void stop(BundleContext bundleContext) throws Exception {
+		EGitTestActivator.context = null;
+	}
+
+}

Added: trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/EGitUtilsTest.java
===================================================================
--- trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/EGitUtilsTest.java	                        (rev 0)
+++ trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/EGitUtilsTest.java	2011-10-24 11:48:54 UTC (rev 35929)
@@ -0,0 +1,148 @@
+package org.jboss.tools.openshift.egit.internal.test;
+
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.net.URISyntaxException;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.egit.core.Activator;
+import org.jboss.tools.openshift.egit.core.EGitUtils;
+import org.jboss.tools.openshift.egit.internal.test.util.TestProject;
+import org.jboss.tools.openshift.egit.internal.test.util.TestRepository;
+import org.jboss.tools.openshift.egit.internal.test.util.TestUtils;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class EGitUtilsTest {
+
+	private static final String GIT_EMAIL = "dummyUser at redhat.com";
+	private static final String GIT_USER = "dummyUser";
+	private static final String REPO2_REMOTE_NAME = "openshift";
+	
+	protected final TestUtils testUtils = new TestUtils();
+
+	private TestRepository testRepository;
+	private TestRepository testRepository2;
+	private TestProject testProject;
+	private TestProject testProject2;
+	private TestRepository clonedTestRepository;
+
+	@Before
+	public void setUp() throws Exception {
+		Activator.getDefault().getRepositoryCache().clear();
+
+		this.testProject = new TestProject(true);
+
+		this.testRepository = new TestRepository(TestUtils.createGitDir(testProject));
+		testRepository.createMockSystemReader(ResourcesPlugin.getWorkspace().getRoot().getLocation());
+		testRepository.setUserAndEmail(GIT_USER, GIT_EMAIL);
+		testRepository.connect(testProject.getProject());
+
+		this.testProject2 = new TestProject(true);
+
+		this.testRepository2 = new TestRepository(TestUtils.createGitDir(testProject2));
+		testRepository2.setUserAndEmail(GIT_USER, GIT_EMAIL);
+		testRepository2.connect(testProject2.getProject());
+		
+		this.clonedTestRepository = cloneRepository(testRepository);
+		clonedTestRepository.addRemoteTo(REPO2_REMOTE_NAME, testRepository2.getRepository());
+	}
+
+	private TestRepository cloneRepository(TestRepository repository) throws URISyntaxException,
+			InvocationTargetException, InterruptedException, IOException {
+		File workspaceDir = ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile();
+		File clonedRepositoryFile =
+				new File(workspaceDir, "clonedRepository-" + String.valueOf(System.currentTimeMillis()));
+		return testRepository.cloneRepository(clonedRepositoryFile);
+	}
+
+	@After
+	public void tearDown() throws Exception {
+		testRepository.dispose();
+		clonedTestRepository.dispose();
+		testRepository2.dispose();
+		Activator.getDefault().getRepositoryCache().clear();
+		
+		testProject.dispose();
+		testProject2.dispose();		
+	}
+
+	@Test
+	public void canCommitFileInProject() throws Exception {
+		String fileName = "a.txt";
+		String fileContent = "adietish at redhat.com";
+
+		IFile file = testUtils.addFileToProject(
+				testProject.getProject(),
+				fileName,
+				fileContent);
+		testRepository.track(file);
+
+		EGitUtils.commit(testProject.getProject(), null);
+
+		testUtils.assertRepositoryContainsFilesWithContent(
+				testRepository.getRepository(),
+				new String[] { testUtils.getPathInRepository(file), fileContent });
+	}
+
+	@Test
+	public void fileAddedToCloneIsInOriginAfterPush() throws Exception {
+		String fileName = "b.txt";
+		String fileContent = "adietish at redhat.com";
+
+		File file = clonedTestRepository.createFile(fileName, fileContent);
+		clonedTestRepository.addAndCommit(file, "adding a file");
+
+		EGitUtils.push(clonedTestRepository.getRepository(), null);
+
+		// does origin contain file added to clone?
+		testUtils.assertRepositoryContainsFilesWithContent(
+				clonedTestRepository.getRepository(),
+				fileName,
+				fileContent);
+	}
+
+	@Test
+	public void fileAddedToCloneIsInRemoteAfterPush() throws Exception {
+		String fileName = "c.txt";
+		String fileContent = "adietish at redhat.com";
+
+		File file = clonedTestRepository.createFile(fileName, fileContent);
+		clonedTestRepository.addAndCommit(file, "adding a file");
+
+		EGitUtils.push(REPO2_REMOTE_NAME, clonedTestRepository.getRepository(), null);
+
+		// does origin contain file added to clone?
+		testUtils.assertRepositoryContainsFilesWithContent(
+				testRepository2.getRepository(),
+				fileName,
+				fileContent);
+	}
+
+	@Test
+	public void forcedPushRemovesFileInRemote() throws Exception {
+		String fileName = "c.txt";
+		String fileContent = "adietish at redhat.com";
+
+		IFile fileInRepo2 = testUtils.addFileToProject(
+				testProject2.getProject(),
+				fileName,
+				fileContent);
+		testRepository2.track(fileInRepo2);
+
+		File fileInClone = clonedTestRepository.createFile(fileName, fileContent);
+		clonedTestRepository.addAndCommit(fileInClone, "adding a file");
+
+		EGitUtils.push(REPO2_REMOTE_NAME, clonedTestRepository.getRepository(), null);
+
+		// does origin contain file added to clone?
+		testUtils.assertRepositoryContainsFilesWithContent(
+				clonedTestRepository.getRepository(),
+				fileName,
+				fileContent);
+	}
+
+}

Added: trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/util/MockSystemReader.java
===================================================================
--- trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/util/MockSystemReader.java	                        (rev 0)
+++ trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/util/MockSystemReader.java	2011-10-24 11:48:54 UTC (rev 35929)
@@ -0,0 +1,143 @@
+/*
+ * Copyright (C) 2009, Google Inc.
+ * Copyright (C) 2009, Robin Rosenberg <robin.rosenberg at dewire.com>
+ * Copyright (C) 2009, Yann Simon <yann.simon.fr at gmail.com>
+ * and other copyright owners as documented in the project's IP log.
+ *
+ * This program and the accompanying materials are made available
+ * under the terms of the Eclipse Distribution License v1.0 which
+ * accompanies this distribution, is reproduced below, and is
+ * available at http://www.eclipse.org/org/documents/edl-v10.php
+ *
+ * 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 Eclipse Foundation, Inc. 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.jboss.tools.openshift.egit.internal.test.util;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.TimeZone;
+
+import org.eclipse.jgit.errors.ConfigInvalidException;
+import org.eclipse.jgit.lib.Config;
+import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.storage.file.FileBasedConfig;
+import org.eclipse.jgit.util.FS;
+import org.eclipse.jgit.util.SystemReader;
+
+public class MockSystemReader extends SystemReader {
+	private final class MockConfig extends FileBasedConfig {
+		private MockConfig(File cfgLocation, FS fs) {
+			super(cfgLocation, fs);
+		}
+
+		@Override
+		public void load() throws IOException, ConfigInvalidException {
+			// Do nothing
+		}
+
+		@Override
+		public boolean isOutdated() {
+			return false;
+		}
+	}
+
+	final Map<String, String> values = new HashMap<String, String>();
+
+	FileBasedConfig userGitConfig;
+
+	FileBasedConfig systemGitConfig;
+
+	public 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 MockConfig(null, null);
+		systemGitConfig = new MockConfig(null, null);
+	}
+
+	private void init(final String n) {
+		setProperty(n, n);
+	}
+
+	public void clearProperties() {
+		values.clear();
+	}
+
+	public void setProperty(String key, String value) {
+		values.put(key, value);
+	}
+
+	@Override
+	public String getenv(String variable) {
+		return values.get(variable);
+	}
+
+	@Override
+	public String getProperty(String key) {
+		return values.get(key);
+	}
+
+	@Override
+	public FileBasedConfig openUserConfig(Config parent, FS fs) {
+		assert parent == null || parent == systemGitConfig;
+		return userGitConfig;
+	}
+
+	@Override
+	public FileBasedConfig openSystemConfig(Config parent, FS fs) {
+		assert parent == null;
+		return systemGitConfig;
+	}
+
+	@Override
+	public String getHostname() {
+		return "fake.host.example.com";
+	}
+
+	@Override
+	public long getCurrentTime() {
+		return 1250379778668L; // Sat Aug 15 20:12:58 GMT-03:30 2009
+	}
+
+	@Override
+	public int getTimezone(long when) {
+		return TimeZone.getTimeZone("GMT-03:30").getOffset(when) / (60 * 1000);
+	}
+
+}

Added: trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/util/TestProject.java
===================================================================
--- trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/util/TestProject.java	                        (rev 0)
+++ trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/util/TestProject.java	2011-10-24 11:48:54 UTC (rev 35929)
@@ -0,0 +1,97 @@
+/*******************************************************************************
+ * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg at dewire.com>
+ * Copyright (C) 2006, Shawn O. Pearce <spearce at spearce.org>
+ * Copyright (C) 2011, Andre Dietisheim <adietish at redhat.com>
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+package org.jboss.tools.openshift.egit.internal.test.util;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IFolder;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IWorkspaceRoot;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.jgit.util.FileUtils;
+
+public class TestProject {
+	public IProject project;
+
+	private String location;
+	private TestUtils testUtils = new TestUtils();
+
+	/**
+	 * @throws CoreException
+	 *             If project already exists
+	 */
+	public TestProject() throws CoreException {
+		this(false);
+	}
+
+	public TestProject(boolean remove) throws CoreException {
+		this(remove, "Project-" + System.currentTimeMillis());
+	}
+
+	/**
+	 * @param remove
+	 *            should project be removed if already exists
+	 * @param projectName
+	 * @throws CoreException
+	 */
+	public TestProject(final boolean remove, String projectName) throws CoreException {
+		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
+		project = root.getProject(projectName);
+		if (remove)
+			project.delete(true, null);
+		project.create(null);
+		project.open(null);
+		location = project.getLocation().toOSString();
+	}
+
+	public IProject getProject() {
+		return project;
+	}
+
+	public IFile createFile(String name, byte[] content) throws Exception {
+		IFile file = project.getFile(name);
+		InputStream inputStream = new ByteArrayInputStream(content);
+		file.create(inputStream, true, null);
+
+		return file;
+	}
+
+	public IFolder createFolder(String name) throws Exception {
+		IFolder folder = project.getFolder(name);
+		folder.create(true, true, null);
+
+		IFile keep = project.getFile(name + "/keep");
+		keep.create(new ByteArrayInputStream(new byte[] {0}), true, null);
+
+		return folder;
+	}
+
+	public void dispose() throws CoreException, IOException {
+		if (project.exists())
+			project.delete(true, true, null);
+		else {
+			File f = new File(location);
+			if (f.exists())
+				FileUtils.delete(f, FileUtils.RECURSIVE | FileUtils.RETRY);
+		}
+	}
+
+	public String getFileContent(String filepath) throws Exception {
+		IFile file = project.getFile(filepath);
+		InputStream stream = file.getContents();
+		return testUtils.slurpAndClose(stream);
+	}
+}

Added: trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/util/TestRepository.java
===================================================================
--- trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/util/TestRepository.java	                        (rev 0)
+++ trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/util/TestRepository.java	2011-10-24 11:48:54 UTC (rev 35929)
@@ -0,0 +1,608 @@
+/*******************************************************************************
+ * Copyright (C) 2011, Mathias Kinzler <mathias.kinzler at sap.com>
+ * Copyright (C) 2010, Jens Baumgart <jens.baumgart at sap.com>
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+package org.jboss.tools.openshift.egit.internal.test.util;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import java.lang.reflect.InvocationTargetException;
+import java.net.MalformedURLException;
+import java.net.URISyntaxException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.regex.Pattern;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.egit.core.Activator;
+import org.eclipse.egit.core.RepositoryCache;
+import org.eclipse.egit.core.op.BranchOperation;
+import org.eclipse.egit.core.op.CloneOperation;
+import org.eclipse.egit.core.op.ConnectProviderOperation;
+import org.eclipse.egit.core.op.DisconnectProviderOperation;
+import org.eclipse.jgit.api.CommitCommand;
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
+import org.eclipse.jgit.api.errors.JGitInternalException;
+import org.eclipse.jgit.api.errors.NoFilepatternException;
+import org.eclipse.jgit.api.errors.NoHeadException;
+import org.eclipse.jgit.api.errors.NoMessageException;
+import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
+import org.eclipse.jgit.dircache.DirCache;
+import org.eclipse.jgit.dircache.DirCacheEntry;
+import org.eclipse.jgit.errors.UnmergedPathException;
+import org.eclipse.jgit.lib.ConfigConstants;
+import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.lib.ObjectId;
+import org.eclipse.jgit.lib.Ref;
+import org.eclipse.jgit.lib.RefUpdate;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.lib.StoredConfig;
+import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevWalk;
+import org.eclipse.jgit.storage.file.FileRepository;
+import org.eclipse.jgit.transport.RemoteConfig;
+import org.eclipse.jgit.transport.URIish;
+import org.eclipse.jgit.treewalk.TreeWalk;
+import org.eclipse.jgit.util.FileUtils;
+import org.eclipse.jgit.util.SystemReader;
+
+/**
+ * Helper class for creating and filling a test repository
+ * 
+ */
+public class TestRepository {
+
+	Repository repository;
+
+	String workdirPrefix;
+
+	private File gitDir;
+
+	/**
+	 * Creates a new test repository
+	 * 
+	 * @param gitDir
+	 * @throws IOException
+	 */
+	public TestRepository(File gitDir) throws IOException {
+		FileRepository tmpRepository = new FileRepository(gitDir);
+		tmpRepository.create();
+		tmpRepository.close();
+		// use repository instance from RepositoryCache!
+		this.gitDir = gitDir;
+		this.repository = Activator.getDefault().getRepositoryCache().lookupRepository(gitDir);
+		this.workdirPrefix = getWorkdirPrefix(repository);
+		workdirPrefix = workdirPrefix.replace('\\', '/');
+		if (!workdirPrefix.endsWith("/")) //$NON-NLS-1$
+			workdirPrefix += "/"; //$NON-NLS-1$
+	}
+
+	private String getWorkdirPrefix(Repository repository) {
+		String workdirPrefix = repository.getWorkTree().getAbsolutePath();
+		try {
+			workdirPrefix = repository.getWorkTree().getCanonicalPath();
+		} catch (IOException err) {
+			// ignore;
+		}
+		return workdirPrefix;
+	}
+
+	/**
+	 * Creates a test repository from an existing Repository
+	 * 
+	 * @param repository
+	 * @throws IOException
+	 */
+	public TestRepository(Repository repository) throws IOException {
+		this.repository = repository;
+		try {
+			workdirPrefix = repository.getWorkTree().getCanonicalPath();
+		} catch (IOException err) {
+			workdirPrefix = repository.getWorkTree().getAbsolutePath();
+		}
+		workdirPrefix = workdirPrefix.replace('\\', '/');
+		if (!workdirPrefix.endsWith("/")) //$NON-NLS-1$
+			workdirPrefix += "/"; //$NON-NLS-1$
+	}
+
+	/**
+	 * @return the wrapped repository
+	 */
+	public Repository getRepository() {
+		return repository;
+	}
+
+	/**
+	 * create an initial commit containing a file "dummy" in the
+	 * 
+	 * @param message
+	 *            commit message
+	 * @return commit object
+	 * @throws IOException
+	 * @throws NoHeadException
+	 * @throws NoMessageException
+	 * @throws ConcurrentRefUpdateException
+	 * @throws JGitInternalException
+	 * @throws WrongRepositoryStateException
+	 */
+	public RevCommit createInitialCommit(String message) throws IOException,
+			NoHeadException, NoMessageException, ConcurrentRefUpdateException,
+			JGitInternalException, WrongRepositoryStateException {
+		String repoPath = repository.getWorkTree().getAbsolutePath();
+		File file = new File(repoPath, "dummy");
+		if (!file.exists())
+			FileUtils.createNewFile(file);
+		track(file);
+		return commit(message);
+	}
+
+	/**
+	 * Create a file or get an existing one
+	 * 
+	 * @param project
+	 *            instance of project inside with file will be created
+	 * @param name
+	 *            name of file
+	 * @return nearly created file
+	 * @throws IOException
+	 */
+	public File createFile(IProject project, String name) throws IOException {
+		String path = project.getLocation().append(name).toOSString();
+		int lastSeparator = path.lastIndexOf(File.separator);
+		FileUtils.mkdirs(new File(path.substring(0, lastSeparator)), true);
+
+		File file = new File(path);
+		if (!file.exists())
+			FileUtils.createNewFile(file);
+
+		return file;
+	}
+
+	public File createFile(String name, String data) throws IOException {
+		File file = new File(repository.getWorkTree(), name);
+		write(file, data);
+		return file;
+	}
+
+	private void write(final File file, final String data) throws IOException {
+		FileUtils.mkdirs(file.getParentFile(), true);
+		Writer w = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
+		try {
+			w.write(data);
+		} finally {
+			w.close();
+		}
+	}
+
+	/**
+	 * Track, add to index and finally commit given file
+	 * 
+	 * @param project
+	 * @param file
+	 * @param commitMessage
+	 * @return commit object
+	 * @throws Exception
+	 */
+	public RevCommit addAndCommit(IProject project, File file, String commitMessage)
+			throws Exception {
+		track(file);
+		addToIndex(project, file);
+
+		return commit(commitMessage);
+	}
+
+	public RevCommit addAndCommit(File file, String commitMessage)
+			throws Exception {
+		track(file);
+		addToIndex(file);
+
+		return commit(commitMessage);
+	}
+
+	/**
+	 * Appends file content to given file, then track, add to index and finally
+	 * commit it.
+	 * 
+	 * @param project
+	 * @param file
+	 * @param content
+	 * @param commitMessage
+	 * @return commit object
+	 * @throws Exception
+	 */
+	public RevCommit appendContentAndCommit(IProject project, File file,
+			byte[] content, String commitMessage) throws Exception {
+		return appendContentAndCommit(project, file, new String(content),
+				commitMessage);
+	}
+
+	/**
+	 * Appends file content to given file, then track, add to index and finally
+	 * commit it.
+	 * 
+	 * @param project
+	 * @param file
+	 * @param content
+	 * @param commitMessage
+	 * @return commit object
+	 * @throws Exception
+	 */
+	public RevCommit appendContentAndCommit(IProject project, File file,
+			String content, String commitMessage) throws Exception {
+		appendFileContent(file, content);
+		track(file);
+		addToIndex(project, file);
+
+		return commit(commitMessage);
+	}
+
+	/**
+	 * Commits the current index
+	 * 
+	 * @param message
+	 *            commit message
+	 * @return commit object
+	 * 
+	 * @throws NoHeadException
+	 * @throws NoMessageException
+	 * @throws UnmergedPathException
+	 * @throws ConcurrentRefUpdateException
+	 * @throws JGitInternalException
+	 * @throws WrongRepositoryStateException
+	 */
+	public RevCommit commit(String message) throws NoHeadException,
+			NoMessageException, UnmergedPathException,
+			ConcurrentRefUpdateException, JGitInternalException,
+			WrongRepositoryStateException {
+		Git git = new Git(repository);
+		CommitCommand commitCommand = git.commit();
+		commitCommand.setAuthor("J. Git", "j.git at egit.org");
+		commitCommand.setCommitter(commitCommand.getAuthor());
+		commitCommand.setMessage(message);
+		return commitCommand.call();
+	}
+
+	public void track(IFile file) throws IOException {
+		track(new File(file.getLocation().toOSString()));
+	}
+
+	/**
+	 * Adds file to version control
+	 * 
+	 * @param file
+	 * @throws IOException
+	 */
+	public void track(File file) throws IOException {
+		String repoPath = getRepoRelativePath(
+				new Path(file.getPath()).toString());
+		try {
+			new Git(repository).add().addFilepattern(repoPath).call();
+		} catch (NoFilepatternException e) {
+			throw new IOException(e.getMessage());
+		}
+	}
+
+	/**
+	 * Creates a new branch and immediately checkout it.
+	 * 
+	 * @param refName
+	 *            starting point for the new branch
+	 * @param newRefName
+	 * @throws Exception
+	 */
+	public void createAndCheckoutBranch(String refName, String newRefName) throws Exception {
+		createBranch(refName, newRefName);
+		checkoutBranch(newRefName);
+	}
+
+	/**
+	 * Creates a new branch
+	 * 
+	 * @param refName
+	 *            starting point for the new branch
+	 * @param newRefName
+	 * @throws IOException
+	 */
+	public void createBranch(String refName, String newRefName)
+			throws IOException {
+		RefUpdate updateRef;
+		updateRef = repository.updateRef(newRefName);
+		Ref startRef = repository.getRef(refName);
+		ObjectId startAt = repository.resolve(refName);
+		String startBranch;
+		if (startRef != null)
+			startBranch = refName;
+		else
+			startBranch = startAt.name();
+		startBranch = Repository.shortenRefName(startBranch);
+		updateRef.setNewObjectId(startAt);
+		updateRef
+				.setRefLogMessage("branch: Created from " + startBranch, false); //$NON-NLS-1$
+		updateRef.update();
+	}
+
+	/**
+	 * Checkouts branch
+	 * 
+	 * @param refName
+	 *            full name of branch
+	 * @throws CoreException
+	 */
+	public void checkoutBranch(String refName) throws CoreException {
+		new BranchOperation(repository, refName).execute(null);
+	}
+
+	/**
+	 * Adds the given file to the index
+	 * 
+	 * @param project
+	 * @param file
+	 * @throws Exception
+	 */
+	public void addToIndex(IProject project, File file) throws Exception {
+		IFile iFile = getIFile(project, file);
+		addToIndex(iFile);
+	}
+
+	/**
+	 * Adds the given file to the index
+	 * 
+	 * @param file
+	 * @throws CoreException
+	 * @throws IOException
+	 */
+	public void addToIndex(IFile file) throws CoreException, IOException {
+		String repoPath = getRepoRelativePath(file.getLocation().toOSString());
+		try {
+			new Git(repository).add().addFilepattern(repoPath).call();
+		} catch (NoFilepatternException e) {
+			throw new IOException(e.getMessage());
+		}
+	}
+
+	public void addToIndex(File file) throws CoreException, IOException {
+		String repoPath = getRepoRelativePath(file.getAbsolutePath());
+		try {
+			new Git(repository).add().addFilepattern(repoPath).call();
+		} catch (NoFilepatternException e) {
+			throw new IOException(e.getMessage());
+		}
+	}
+
+	/**
+	 * Appends content to end of given file.
+	 * 
+	 * @param file
+	 * @param content
+	 * @throws IOException
+	 */
+	public void appendFileContent(File file, byte[] content) throws IOException {
+		appendFileContent(file, new String(content), true);
+	}
+
+	/**
+	 * Appends content to end of given file.
+	 * 
+	 * @param file
+	 * @param content
+	 * @throws IOException
+	 */
+	public void appendFileContent(File file, String content) throws IOException {
+		appendFileContent(file, content, true);
+	}
+
+	/**
+	 * Appends content to given file.
+	 * 
+	 * @param file
+	 * @param content
+	 * @param append
+	 *            if true, then bytes will be written to the end of the file
+	 *            rather than the beginning
+	 * @throws IOException
+	 */
+	public void appendFileContent(File file, byte[] content, boolean append)
+			throws IOException {
+		appendFileContent(file, new String(content), append);
+	}
+
+	/**
+	 * Appends content to given file.
+	 * 
+	 * @param file
+	 * @param content
+	 * @param append
+	 *            if true, then bytes will be written to the end of the file
+	 *            rather than the beginning
+	 * @throws IOException
+	 */
+	public void appendFileContent(File file, String content, boolean append)
+			throws IOException {
+		FileWriter fw = null;
+		try {
+			fw = new FileWriter(file, append);
+			fw.append(content);
+		} finally {
+			if (fw != null)
+				fw.close();
+		}
+	}
+
+	/**
+	 * Checks if a file with the given path exists in the HEAD tree
+	 * 
+	 * @param path
+	 * @return true if the file exists
+	 * @throws IOException
+	 */
+	public boolean inHead(String path) throws IOException {
+		ObjectId headId = repository.resolve(Constants.HEAD);
+		RevWalk rw = new RevWalk(repository);
+		TreeWalk tw = null;
+		try {
+			tw = TreeWalk.forPath(repository, path, rw.parseTree(headId));
+			return tw != null;
+		} finally {
+			rw.release();
+			rw.dispose();
+			if (tw != null)
+				tw.release();
+		}
+	}
+
+	public boolean inIndex(String path) throws IOException {
+		return getDirCacheEntry(path) != null;
+	}
+
+	public boolean removedFromIndex(String path) throws IOException {
+		DirCacheEntry dc = getDirCacheEntry(path);
+		if (dc == null)
+			return true;
+
+		Ref ref = repository.getRef(Constants.HEAD);
+		RevCommit c = new RevWalk(repository).parseCommit(ref.getObjectId());
+		TreeWalk tw = TreeWalk.forPath(repository, path, c.getTree());
+
+		return tw == null || dc.getObjectId().equals(tw.getObjectId(0));
+	}
+
+	public long lastModifiedInIndex(String path) throws IOException {
+		String repoPath = getRepoRelativePath(path);
+		DirCache dc = DirCache.read(repository.getIndexFile(), repository.getFS());
+
+		return dc.getEntry(repoPath).getLastModified();
+	}
+
+	public int getDirCacheEntryLength(String path) throws IOException {
+		String repoPath = getRepoRelativePath(path);
+		DirCache dc = DirCache.read(repository.getIndexFile(), repository.getFS());
+
+		return dc.getEntry(repoPath).getLength();
+	}
+
+	public String getRepoRelativePath(String path) {
+		final int pfxLen = workdirPrefix.length();
+		final int pLen = path.length();
+		if (pLen > pfxLen)
+			return path.substring(pfxLen);
+		else if (path.length() == pfxLen - 1)
+			return ""; //$NON-NLS-1$
+		return null;
+	}
+
+	public IFile getIFile(IProject project, File file) throws CoreException {
+		String relativePath = getRepoRelativePath(file.getAbsolutePath());
+
+		String quotedProjectName = Pattern.quote(project.getName());
+		relativePath = relativePath.replaceFirst(quotedProjectName, "");
+
+		IFile iFile = project.getFile(relativePath);
+		iFile.refreshLocal(0, null);
+
+		return iFile;
+	}
+
+	public void dispose() throws IOException {
+		repository.close();
+		remove();
+		repository = null;
+	}
+
+	public void remove() throws IOException {
+		File repositoryDirectory = repository.getDirectory();
+		File repositoryParent = repositoryDirectory.getParentFile();
+		if (repositoryParent.exists()) {
+			FileUtils.delete(repositoryParent, FileUtils.RECURSIVE | FileUtils.RETRY);
+		}
+	}
+
+	/**
+	 * Connect a project to this repository
+	 * 
+	 * @param project
+	 * @throws CoreException
+	 */
+	public void connect(IProject project) throws CoreException {
+		ConnectProviderOperation op = new ConnectProviderOperation(project,
+				this.getRepository().getDirectory());
+		op.execute(null);
+	}
+
+	/**
+	 * Disconnects provider from project
+	 * 
+	 * @param project
+	 * @throws CoreException
+	 */
+	public void disconnect(IProject project) throws CoreException {
+		Collection<IProject> projects = Collections.singleton(project
+				.getProject());
+		DisconnectProviderOperation disconnect = new DisconnectProviderOperation(
+				projects);
+		disconnect.execute(null);
+	}
+
+	private DirCacheEntry getDirCacheEntry(String path) throws IOException {
+		String repoPath = getRepoRelativePath(path);
+		DirCache dc = DirCache.read(repository.getIndexFile(), repository.getFS());
+
+		return dc.getEntry(repoPath);
+	}
+
+	public TestRepository cloneRepository(File path) throws URISyntaxException, InvocationTargetException,
+			InterruptedException, IOException {
+		URIish uri = new URIish("file:///" + repository.getDirectory().toString());
+		CloneOperation clop =
+				new CloneOperation(uri, true, null, path, Constants.R_HEADS + Constants.MASTER,
+						Constants.DEFAULT_REMOTE_NAME, 0);
+		clop.run(null);
+		RepositoryCache repositoryCache = Activator.getDefault().getRepositoryCache();
+		Repository clonedRepository = repositoryCache
+				.lookupRepository(new File(path, Constants.DOT_GIT));
+		return new TestRepository(clonedRepository);
+	}
+
+	public void setUserAndEmail(String user, String email) {
+		StoredConfig config = repository.getConfig();
+		config.setString(
+				ConfigConstants.CONFIG_USER_SECTION, null, ConfigConstants.CONFIG_KEY_NAME, user);
+		config.setString(
+				ConfigConstants.CONFIG_USER_SECTION, null, ConfigConstants.CONFIG_KEY_EMAIL, email);
+	}
+
+	public void createMockSystemReader(IPath ceilingPath) {
+		MockSystemReader mockSystemReader = new MockSystemReader();
+		SystemReader.setInstance(mockSystemReader);
+		mockSystemReader.setProperty(Constants.GIT_CEILING_DIRECTORIES_KEY, ceilingPath.toOSString());
+	}
+	
+	public File getGitDir() {
+		return gitDir;
+	}
+
+	public void addRemoteTo(String remoteName, Repository remoteRepository)
+			throws URISyntaxException, MalformedURLException,
+			IOException {
+		StoredConfig config = repository.getConfig();
+		RemoteConfig remoteConfig = new RemoteConfig(config, remoteName);
+		URIish uri = new URIish(remoteRepository.getDirectory().toURI().toURL());
+		remoteConfig.addURI(uri);
+		remoteConfig.update(config);
+		config.save();
+	}
+
+}

Added: trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/util/TestUtils.java
===================================================================
--- trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/util/TestUtils.java	                        (rev 0)
+++ trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/util/TestUtils.java	2011-10-24 11:48:54 UTC (rev 35929)
@@ -0,0 +1,271 @@
+/*******************************************************************************
+ * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg at dewire.com>
+ * Copyright (C) 2010, Jens Baumgart <jens.baumgart at sap.com>
+ * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler at sap.com>
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+package org.jboss.tools.openshift.egit.internal.test.util;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IFolder;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IProjectDescription;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.egit.core.project.RepositoryMapping;
+import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.lib.ObjectId;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.treewalk.TreeWalk;
+import org.eclipse.jgit.util.FS;
+import org.eclipse.jgit.util.FileUtils;
+
+public class TestUtils {
+
+	public final static String AUTHOR = "The Author <The.author at some.com>";
+
+	public final static String COMMITTER = "The Commiter <The.committer at some.com>";
+
+	/**
+	 * Create a "temporary" directory
+	 *
+	 * @param name
+	 *            the name of the directory
+	 * @return a directory as child of a "temporary" folder in the user home
+	 *         directory; may or may not exist
+	 * @throws IOException
+	 */
+	public File createTempDir(String name) throws IOException {
+		File userHome = FS.DETECTED.userHome();
+		File rootDir = new File(userHome, "EGitCoreTestTempDir");
+		File result = new File(rootDir, name);
+		if (result.exists())
+			FileUtils.delete(result, FileUtils.RECURSIVE | FileUtils.RETRY);
+		return result;
+	}
+
+	public static File createGitDir(TestProject testProject) throws IOException {
+		return new File(testProject.getProject().getLocation().toFile(), Constants.DOT_GIT);
+	}
+
+	/**
+	 * Cleanup: delete the "temporary" folder and all children
+	 *
+	 * @throws IOException
+	 */
+	public void deleteTempDirs() throws IOException {
+		File userHome = FS.DETECTED.userHome();
+		File rootDir = new File(userHome, "EGitCoreTestTempDir");
+		if (rootDir.exists())
+			FileUtils.delete(rootDir, FileUtils.RECURSIVE | FileUtils.RETRY);
+	}
+
+	/**
+	 * Read the stream into a String
+	 *
+	 * @param inputStream
+	 * @return the contents of the stream
+	 * @throws IOException
+	 */
+	public String slurpAndClose(InputStream inputStream) throws IOException {
+		StringBuilder stringBuilder = new StringBuilder();
+		try {
+			int ch;
+			while ((ch = inputStream.read()) != -1) {
+				stringBuilder.append((char) ch);
+			}
+		} finally {
+			inputStream.close();
+		}
+		return stringBuilder.toString();
+	}
+
+	/**
+	 * Add a file to an existing project
+	 *
+	 * @param project
+	 *            the project
+	 * @param path
+	 *            e.g. "folder1/folder2/test.txt"
+	 * @param content
+	 *            the contents
+	 * @return the file
+	 * @throws Exception
+	 *             if the file can not be created
+	 */
+	public IFile addFileToProject(IProject project, String path, String content)
+			throws Exception {
+		IPath filePath = new Path(path);
+		IFolder folder = null;
+		for (int i = 0; i < filePath.segmentCount() - 1; i++) {
+			if (folder == null) {
+				folder = project.getFolder(filePath.segment(i));
+			} else {
+				folder = folder.getFolder(filePath.segment(i));
+			}
+			if (!folder.exists())
+				folder.create(false, true, null);
+		}
+		IFile file = project.getFile(filePath);
+		file.create(new ByteArrayInputStream(content.getBytes(project
+				.getDefaultCharset())), true, null);
+		return file;
+	}
+
+	/**
+	 * Change the content of a file
+	 *
+	 * @param project
+	 * @param file
+	 * @param newContent
+	 * @return the file
+	 * @throws Exception
+	 */
+	public IFile changeContentOfFile(IProject project, IFile file, String newContent)
+			throws Exception {
+		file.setContents(new ByteArrayInputStream(newContent.getBytes(project
+				.getDefaultCharset())), 0, null);
+		return file;
+	}
+
+	/**
+	 * Create a project in the local file system
+	 *
+	 * @param parentFile
+	 *            the parent
+	 * @param projectName
+	 *            project name
+	 * @return the project with a location pointing to the local file system
+	 * @throws Exception
+	 */
+	public IProject createProjectInLocalFileSystem(File parentFile,
+			String projectName) throws Exception {
+		IProject project = ResourcesPlugin.getWorkspace().getRoot()
+				.getProject(projectName);
+		if (project.exists()) {
+			project.delete(true, null);
+		}
+		File testFile = new File(parentFile, projectName);
+		if (testFile.exists())
+			FileUtils.delete(testFile, FileUtils.RECURSIVE | FileUtils.RETRY);
+
+		IProjectDescription desc = ResourcesPlugin.getWorkspace()
+				.newProjectDescription(projectName);
+		desc.setLocation(new Path(new File(parentFile, projectName).getPath()));
+		project.create(desc, null);
+		project.open(null);
+		return project;
+	}
+
+	/**
+	 * verifies that repository contains exactly the given files.
+	 * @param repository
+	 * @param paths
+	 * @throws Exception
+	 */
+	public void assertRepositoryContainsFiles(Repository repository,
+			String[] paths) throws Exception {
+		Set<String> expectedfiles = new HashSet<String>();
+		for (String path : paths)
+			expectedfiles.add(path);
+		TreeWalk treeWalk = new TreeWalk(repository);
+		treeWalk.addTree(repository.resolve("HEAD^{tree}"));
+		treeWalk.setRecursive(true);
+		while (treeWalk.next()) {
+			String path = treeWalk.getPathString();
+			if (!expectedfiles.contains(path))
+				fail("Repository contains unexpected expected file " + path);
+			expectedfiles.remove(path);
+		}
+		if (expectedfiles.size() > 0) {
+			StringBuilder message = new StringBuilder(
+					"Repository does not contain expected files: ");
+			for (String path : expectedfiles) {
+				message.append(path);
+				message.append(" ");
+			}
+			fail(message.toString());
+		}
+	}
+
+	/**
+	 * verifies that repository contains exactly the given files with the given
+	 * content. Usage example:<br>
+	 *
+	 * <code>
+	 * assertRepositoryContainsFiles(repository, "foo/a.txt", "content of A",
+	 *                                           "foo/b.txt", "content of B")
+	 * </code>
+	 * @param repository
+	 * @param args
+	 * @throws Exception
+	 */
+	public void assertRepositoryContainsFilesWithContent(Repository repository,
+			String... args) throws Exception {
+		HashMap<String, String> expectedfiles = mkmap(args);
+		TreeWalk treeWalk = new TreeWalk(repository);
+		treeWalk.addTree(repository.resolve("HEAD^{tree}"));
+		treeWalk.setRecursive(true);
+		while (treeWalk.next()) {
+			String path = treeWalk.getPathString();
+			assertTrue(expectedfiles.containsKey(path));
+			ObjectId objectId = treeWalk.getObjectId(0);
+			byte[] expectedContent = expectedfiles.get(path).getBytes();
+			byte[] repoContent = treeWalk.getObjectReader().open(objectId)
+					.getBytes();
+			if (!Arrays.equals(repoContent, expectedContent)) {
+				fail("File " + path + " has repository content "
+						+ new String(repoContent)
+						+ " instead of expected content "
+						+ new String(expectedContent));
+			}
+			expectedfiles.remove(path);
+		}
+		if (expectedfiles.size() > 0) {
+			StringBuilder message = new StringBuilder(
+					"Repository does not contain expected files: ");
+			for (String path : expectedfiles.keySet()) {
+				message.append(path);
+				message.append(" ");
+			}
+			fail(message.toString());
+		}
+	}
+
+	private static HashMap<String, String> mkmap(String... args) {
+		if ((args.length % 2) > 0)
+			throw new IllegalArgumentException("needs to be pairs");
+		HashMap<String, String> map = new HashMap<String, String>();
+		for (int i = 0; i < args.length; i += 2) {
+			map.put(args[i], args[i+1]);
+		}
+		return map;
+	}
+	
+	public String getPathInRepository(IResource resource) {
+		RepositoryMapping mapping = RepositoryMapping.getMapping(resource);
+		if (mapping == null) {
+			throw new IllegalArgumentException(resource + " is not in any repository");
+		}
+		return mapping.getRepoRelativePath(resource);
+	}
+
+}



More information about the jbosstools-commits mailing list