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.plugin.MojoExecutionException;
024import org.apache.maven.scm.provider.git.GitScmTestUtils;
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@RunWith(JUnit4.class)
035public class UntagMojoTest extends AbstractJUnit4MojoTestCase {
036    File checkoutDir;
037
038    File repository;
039
040    @Before
041    public void setUp() throws Exception {
042        super.setUp();
043
044        checkoutDir = getTestFile("target/checkout");
045
046        repository = getTestFile("target/repository");
047
048        checkScmPresence(GitScmTestUtils.GIT_COMMAND_LINE);
049
050        GitScmTestUtils.initRepo("src/test/resources/git", repository, checkoutDir);
051
052        CheckoutMojo checkoutMojo =
053                (CheckoutMojo) lookupMojo("checkout", getTestFile("src/test/resources/mojos/untag/checkout.xml"));
054        checkoutMojo.setWorkingDirectory(checkoutDir);
055
056        String connectionUrl = checkoutMojo.getConnectionUrl();
057        connectionUrl = StringUtils.replace(connectionUrl, "${basedir}", getBasedir());
058        connectionUrl = StringUtils.replace(connectionUrl, "\\", "/");
059        checkoutMojo.setConnectionUrl(connectionUrl);
060
061        checkoutMojo.setCheckoutDirectory(checkoutDir);
062
063        checkoutMojo.execute();
064
065        // Add a default user to the config
066        GitScmTestUtils.setDefaulGitConfig(checkoutDir);
067    }
068
069    @Test
070    public void testUntag() throws Exception {
071        checkScmPresence(GitScmTestUtils.GIT_COMMAND_LINE);
072
073        TagMojo tagMojo = (TagMojo) lookupMojo("tag", getTestFile("src/test/resources/mojos/untag/tag.xml"));
074        tagMojo.setWorkingDirectory(checkoutDir);
075        tagMojo.setConnectionUrl(getConnectionLocalAddress(tagMojo));
076        tagMojo.execute();
077
078        CheckoutMojo checkoutMojo =
079                (CheckoutMojo) lookupMojo("checkout", getTestFile("src/test/resources/mojos/untag/checkout-tag.xml"));
080        checkoutMojo.setWorkingDirectory(new File(getBasedir()));
081        checkoutMojo.setConnectionUrl(getConnectionLocalAddress(checkoutMojo));
082
083        File tagCheckoutDir = getTestFile("target/tags/mytag");
084
085        if (tagCheckoutDir.exists()) {
086            FileUtils.deleteDirectory(tagCheckoutDir);
087        }
088
089        checkoutMojo.setCheckoutDirectory(tagCheckoutDir);
090        checkoutMojo.execute();
091
092        UntagMojo mojo = (UntagMojo) lookupMojo("untag", getTestFile("src/test/resources/mojos/untag/untag.xml"));
093        mojo.setWorkingDirectory(checkoutDir);
094        mojo.setConnectionUrl(getConnectionLocalAddress(mojo));
095
096        mojo.execute();
097
098        FileUtils.deleteDirectory(tagCheckoutDir);
099
100        try {
101            checkoutMojo.execute();
102
103            fail("mojo execution must fail.");
104        } catch (MojoExecutionException e) {
105            assertTrue(true);
106        }
107    }
108
109    private String getConnectionLocalAddress(AbstractScmMojo mojo) {
110        String connectionUrl = mojo.getConnectionUrl();
111        connectionUrl = StringUtils.replace(connectionUrl, "${basedir}", getBasedir());
112        connectionUrl = StringUtils.replace(connectionUrl, "\\", "/");
113        return connectionUrl;
114    }
115}