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