1d60001c8SKetan Padegaonkar /* 2*5c5f7c6bSMatthias Sohn * Copyright (C) 2011, Ketan Padegaonkar <KetanPadegaonkar@gmail.com> and others 3d60001c8SKetan Padegaonkar * 4*5c5f7c6bSMatthias Sohn * This program and the accompanying materials are made available under the 5*5c5f7c6bSMatthias Sohn * terms of the Eclipse Distribution License v. 1.0 which is available at 6*5c5f7c6bSMatthias Sohn * https://www.eclipse.org/org/documents/edl-v10.php. 7d60001c8SKetan Padegaonkar * 8*5c5f7c6bSMatthias Sohn * SPDX-License-Identifier: BSD-3-Clause 9d60001c8SKetan Padegaonkar */ 10d60001c8SKetan Padegaonkar package org.eclipse.jgit.ant.tasks; 11d60001c8SKetan Padegaonkar 12d60001c8SKetan Padegaonkar import static org.junit.Assert.assertTrue; 13d60001c8SKetan Padegaonkar 14d60001c8SKetan Padegaonkar import java.io.File; 15ee90d6afSMatthias Sohn import java.io.IOException; 16d60001c8SKetan Padegaonkar 17d60001c8SKetan Padegaonkar import org.apache.tools.ant.BuildException; 18d60001c8SKetan Padegaonkar import org.apache.tools.ant.DefaultLogger; 19d60001c8SKetan Padegaonkar import org.apache.tools.ant.Project; 20d60001c8SKetan Padegaonkar import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase; 21526b6266SRobin Rosenberg import org.eclipse.jgit.lib.Repository; 22d60001c8SKetan Padegaonkar import org.eclipse.jgit.lib.RepositoryCache; 23d60001c8SKetan Padegaonkar import org.eclipse.jgit.util.FS; 24d60001c8SKetan Padegaonkar import org.junit.Before; 25d60001c8SKetan Padegaonkar import org.junit.Test; 26d60001c8SKetan Padegaonkar 27d60001c8SKetan Padegaonkar public class GitCloneTaskTest extends LocalDiskRepositoryTestCase { 28d60001c8SKetan Padegaonkar 29d60001c8SKetan Padegaonkar private GitCloneTask task; 30d60001c8SKetan Padegaonkar private Project project; 31ee90d6afSMatthias Sohn private File dest; 32d60001c8SKetan Padegaonkar 33d60001c8SKetan Padegaonkar @Before before()34ee90d6afSMatthias Sohn public void before() throws IOException { 35eda2e95fSMatthias Sohn dest = createTempFile(); 36d45219baSMatthias Sohn FS.getFileStoreAttributes(dest.toPath().getParent()); 37d60001c8SKetan Padegaonkar project = new Project(); 38ee90d6afSMatthias Sohn project.init(); 39d60001c8SKetan Padegaonkar enableLogging(); 40d60001c8SKetan Padegaonkar project.addTaskDefinition("git-clone", GitCloneTask.class); 41d60001c8SKetan Padegaonkar task = (GitCloneTask) project.createTask("git-clone"); 42ee90d6afSMatthias Sohn task.setDest(dest); 43d60001c8SKetan Padegaonkar } 44d60001c8SKetan Padegaonkar 45d60001c8SKetan Padegaonkar @Test(expected = BuildException.class) shouldRaiseErrorOnNoUrl()46d60001c8SKetan Padegaonkar public void shouldRaiseErrorOnNoUrl() throws Exception { 47d60001c8SKetan Padegaonkar task.execute(); 48d60001c8SKetan Padegaonkar } 49d60001c8SKetan Padegaonkar 50d60001c8SKetan Padegaonkar @Test(expected = BuildException.class) shouldRaiseErrorOnEmptyUrl()51d60001c8SKetan Padegaonkar public void shouldRaiseErrorOnEmptyUrl() throws Exception { 52d60001c8SKetan Padegaonkar task.setUri(""); 53d60001c8SKetan Padegaonkar task.execute(); 54d60001c8SKetan Padegaonkar } 55d60001c8SKetan Padegaonkar 56d60001c8SKetan Padegaonkar @Test(expected = BuildException.class) shouldRaiseErrorOnBadUrl()57d60001c8SKetan Padegaonkar public void shouldRaiseErrorOnBadUrl() throws Exception { 58d60001c8SKetan Padegaonkar task.setUri("foo://bar"); 59d60001c8SKetan Padegaonkar task.execute(); 60d60001c8SKetan Padegaonkar } 61d60001c8SKetan Padegaonkar 62d60001c8SKetan Padegaonkar @Test(expected = BuildException.class) shouldRaiseErrorOnBadSourceURL()63d60001c8SKetan Padegaonkar public void shouldRaiseErrorOnBadSourceURL() throws Exception { 64d60001c8SKetan Padegaonkar task.setUri("http://localhost:9090/does-not-exist.git"); 65d60001c8SKetan Padegaonkar task.execute(); 66d60001c8SKetan Padegaonkar } 67d60001c8SKetan Padegaonkar 68d60001c8SKetan Padegaonkar @Test shouldCloneAValidGitRepository()69d60001c8SKetan Padegaonkar public void shouldCloneAValidGitRepository() throws Exception { 70526b6266SRobin Rosenberg Repository repo = createBareRepository(); 71d60001c8SKetan Padegaonkar File directory = repo.getDirectory(); 7275cfa03eSShawn Pearce task.setUri("file://" + directory.getAbsolutePath()); 73d60001c8SKetan Padegaonkar task.execute(); 74d60001c8SKetan Padegaonkar 75d60001c8SKetan Padegaonkar assertTrue(RepositoryCache.FileKey.isGitRepository(new File(dest, ".git"), FS.DETECTED)); 76d60001c8SKetan Padegaonkar } 77d60001c8SKetan Padegaonkar 78d60001c8SKetan Padegaonkar @Test shouldCreateABareCloneOfAValidGitRepository()79d60001c8SKetan Padegaonkar public void shouldCreateABareCloneOfAValidGitRepository() throws Exception { 80526b6266SRobin Rosenberg Repository repo = createBareRepository(); 81d60001c8SKetan Padegaonkar File directory = repo.getDirectory(); 8275cfa03eSShawn Pearce task.setUri("file://" + directory.getAbsolutePath()); 83d60001c8SKetan Padegaonkar task.setBare(true); 84d60001c8SKetan Padegaonkar task.execute(); 85d60001c8SKetan Padegaonkar 86d60001c8SKetan Padegaonkar assertTrue(RepositoryCache.FileKey.isGitRepository(dest, FS.DETECTED)); 87d60001c8SKetan Padegaonkar } 88d60001c8SKetan Padegaonkar enableLogging()89d60001c8SKetan Padegaonkar private void enableLogging() { 90d60001c8SKetan Padegaonkar DefaultLogger logger = new DefaultLogger(); 91d60001c8SKetan Padegaonkar logger.setOutputPrintStream(System.out); 92d60001c8SKetan Padegaonkar logger.setErrorPrintStream(System.err); 93d60001c8SKetan Padegaonkar logger.setMessageOutputLevel(Project.MSG_INFO); 94d60001c8SKetan Padegaonkar project.addBuildListener(logger); 95d60001c8SKetan Padegaonkar } 96d60001c8SKetan Padegaonkar 97d60001c8SKetan Padegaonkar } 98