001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.plugin;
020
021import java.io.File;
022
023import org.apache.maven.scm.PlexusJUnit4TestCase;
024import org.apache.maven.scm.provider.svn.SvnScmTestUtils;
025import org.codehaus.plexus.util.FileUtils;
026import org.codehaus.plexus.util.StringUtils;
027import org.junit.Before;
028import org.junit.Test;
029import org.junit.runner.RunWith;
030import org.junit.runners.JUnit4;
031
032import static org.apache.maven.scm.ScmTestCase.checkScmPresence;
033
034/**
035 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
036 *
037 */
038@RunWith(JUnit4.class)
039public class BranchMojoTest extends AbstractJUnit4MojoTestCase {
040    File checkoutDir;
041
042    File repository;
043
044    @Before
045    public void setUp() throws Exception {
046        super.setUp();
047
048        checkoutDir = getTestFile("target/checkout");
049
050        FileUtils.forceDelete(checkoutDir);
051
052        repository = getTestFile("target/repository");
053
054        FileUtils.forceDelete(repository);
055
056        checkScmPresence(SvnScmTestUtils.SVNADMIN_COMMAND_LINE);
057
058        SvnScmTestUtils.initializeRepository(repository);
059
060        checkScmPresence(SvnScmTestUtils.SVN_COMMAND_LINE);
061
062        CheckoutMojo checkoutMojo = (CheckoutMojo)
063                lookupMojo("checkout", getTestFile("src/test/resources/mojos/checkout/checkoutWithConnectionUrl.xml"));
064        checkoutMojo.setWorkingDirectory(new File(getBasedir()));
065
066        String connectionUrl = checkoutMojo.getConnectionUrl();
067        connectionUrl = StringUtils.replace(connectionUrl, "${basedir}", getBasedir());
068        connectionUrl = StringUtils.replace(connectionUrl, "\\", "/");
069        checkoutMojo.setConnectionUrl(connectionUrl);
070
071        checkoutMojo.setCheckoutDirectory(checkoutDir);
072
073        checkoutMojo.execute();
074    }
075
076    @Test
077    public void testBranch() throws Exception {
078        checkScmPresence(SvnScmTestUtils.SVNADMIN_COMMAND_LINE);
079
080        BranchMojo mojo = (BranchMojo)
081                lookupMojo("branch", PlexusJUnit4TestCase.getTestFile("src/test/resources/mojos/branch/branch.xml"));
082        mojo.setWorkingDirectory(checkoutDir);
083
084        String connectionUrl = mojo.getConnectionUrl();
085        connectionUrl = StringUtils.replace(connectionUrl, "${basedir}", PlexusJUnit4TestCase.getBasedir());
086        connectionUrl = StringUtils.replace(connectionUrl, "\\", "/");
087        mojo.setConnectionUrl(connectionUrl);
088
089        mojo.execute();
090
091        CheckoutMojo checkoutMojo = (CheckoutMojo) lookupMojo(
092                "checkout", PlexusJUnit4TestCase.getTestFile("src/test/resources/mojos/branch/checkout.xml"));
093        checkoutMojo.setWorkingDirectory(new File(PlexusJUnit4TestCase.getBasedir()));
094
095        connectionUrl = checkoutMojo.getConnectionUrl();
096        connectionUrl = StringUtils.replace(connectionUrl, "${basedir}", PlexusJUnit4TestCase.getBasedir());
097        connectionUrl = StringUtils.replace(connectionUrl, "\\", "/");
098        checkoutMojo.setConnectionUrl(connectionUrl);
099
100        File branchCheckoutDir = PlexusJUnit4TestCase.getTestFile("target/branches/mybranch");
101        if (branchCheckoutDir.exists()) {
102            FileUtils.deleteDirectory(branchCheckoutDir);
103        }
104        checkoutMojo.setCheckoutDirectory(branchCheckoutDir);
105
106        assertFalse(new File(branchCheckoutDir, "pom.xml").exists());
107        checkoutMojo.execute();
108        assertTrue(new File(branchCheckoutDir, "pom.xml").exists());
109    }
110}