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.commons.lang3.StringUtils;
024import org.apache.maven.plugin.testing.AbstractMojoTestCase;
025import org.apache.maven.scm.ScmTestCase;
026import org.apache.maven.scm.provider.svn.SvnScmTestUtils;
027import org.codehaus.plexus.util.FileUtils;
028
029/**
030 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
031 *
032 */
033public class BranchMojoTest extends AbstractMojoTestCase {
034    File checkoutDir;
035
036    File repository;
037
038    protected void setUp() throws Exception {
039        super.setUp();
040
041        checkoutDir = getTestFile("target/checkout");
042
043        FileUtils.forceDelete(checkoutDir);
044
045        repository = getTestFile("target/repository");
046
047        FileUtils.forceDelete(repository);
048
049        if (!ScmTestCase.isSystemCmd(SvnScmTestUtils.SVNADMIN_COMMAND_LINE)) {
050            ScmTestCase.printSystemCmdUnavail(SvnScmTestUtils.SVNADMIN_COMMAND_LINE, "setUp");
051            return;
052        }
053
054        SvnScmTestUtils.initializeRepository(repository);
055
056        if (!ScmTestCase.isSystemCmd(SvnScmTestUtils.SVN_COMMAND_LINE)) {
057            ScmTestCase.printSystemCmdUnavail(SvnScmTestUtils.SVN_COMMAND_LINE, "setUp");
058            return;
059        }
060
061        CheckoutMojo checkoutMojo = (CheckoutMojo)
062                lookupMojo("checkout", getTestFile("src/test/resources/mojos/checkout/checkoutWithConnectionUrl.xml"));
063        checkoutMojo.setWorkingDirectory(new File(getBasedir()));
064
065        String connectionUrl = checkoutMojo.getConnectionUrl();
066        connectionUrl = StringUtils.replace(connectionUrl, "${basedir}", getBasedir());
067        connectionUrl = StringUtils.replace(connectionUrl, "\\", "/");
068        checkoutMojo.setConnectionUrl(connectionUrl);
069
070        checkoutMojo.setCheckoutDirectory(checkoutDir);
071
072        checkoutMojo.execute();
073    }
074
075    public void testBranch() throws Exception {
076        if (!ScmTestCase.isSystemCmd(SvnScmTestUtils.SVN_COMMAND_LINE)) {
077            ScmTestCase.printSystemCmdUnavail(SvnScmTestUtils.SVN_COMMAND_LINE, getName());
078            return;
079        }
080
081        BranchMojo mojo = (BranchMojo) lookupMojo("branch", getTestFile("src/test/resources/mojos/branch/branch.xml"));
082        mojo.setWorkingDirectory(checkoutDir);
083
084        String connectionUrl = mojo.getConnectionUrl();
085        connectionUrl = StringUtils.replace(connectionUrl, "${basedir}", getBasedir());
086        connectionUrl = StringUtils.replace(connectionUrl, "\\", "/");
087        mojo.setConnectionUrl(connectionUrl);
088
089        mojo.execute();
090
091        CheckoutMojo checkoutMojo =
092                (CheckoutMojo) lookupMojo("checkout", getTestFile("src/test/resources/mojos/branch/checkout.xml"));
093        checkoutMojo.setWorkingDirectory(new File(getBasedir()));
094
095        connectionUrl = checkoutMojo.getConnectionUrl();
096        connectionUrl = StringUtils.replace(connectionUrl, "${basedir}", getBasedir());
097        connectionUrl = StringUtils.replace(connectionUrl, "\\", "/");
098        checkoutMojo.setConnectionUrl(connectionUrl);
099
100        File branchCheckoutDir = 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}