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.provider.svn.SvnScmTestUtils;
024import org.codehaus.plexus.util.FileUtils;
025import org.junit.Before;
026import org.junit.Test;
027import org.junit.runner.RunWith;
028import org.junit.runners.JUnit4;
029
030import static org.apache.maven.scm.ScmTestCase.checkScmPresence;
031
032/**
033 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
034 *
035 */
036@RunWith(JUnit4.class)
037public class TagMojoTest extends AbstractJUnit4MojoTestCase {
038    File checkoutDir;
039
040    File repository;
041
042    @Before
043    public void setUp() throws Exception {
044        super.setUp();
045
046        checkoutDir = getTestFile("target/checkout");
047
048        FileUtils.forceDelete(checkoutDir);
049
050        repository = getTestFile("target/repository");
051
052        FileUtils.forceDelete(repository);
053
054        checkScmPresence(SvnScmTestUtils.SVNADMIN_COMMAND_LINE);
055
056        SvnScmTestUtils.initializeRepository(repository);
057
058        checkScmPresence(SvnScmTestUtils.SVN_COMMAND_LINE);
059
060        CheckoutMojo checkoutMojo = (CheckoutMojo)
061                lookupMojo("checkout", getTestFile("src/test/resources/mojos/checkout/checkoutWithConnectionUrl.xml"));
062        checkoutMojo.setWorkingDirectory(new File(getBasedir()));
063
064        setupConnectionUrl(checkoutMojo);
065
066        checkoutMojo.setCheckoutDirectory(checkoutDir);
067
068        checkoutMojo.execute();
069    }
070
071    private static void setupConnectionUrl(AbstractScmMojo mojo) {
072        String connectionUrl = mojo.getConnectionUrl();
073        connectionUrl = connectionUrl.replace("${basedir}", getBasedir());
074        connectionUrl = connectionUrl.replace('\\', '/');
075        mojo.setConnectionUrl(connectionUrl);
076    }
077
078    @Test
079    public void testTag() throws Exception {
080        checkScmPresence(SvnScmTestUtils.SVN_COMMAND_LINE);
081
082        TagMojo mojo = (TagMojo) lookupMojo("tag", getTestFile("src/test/resources/mojos/tag/tag.xml"));
083        mojo.setWorkingDirectory(checkoutDir);
084
085        setupConnectionUrl(mojo);
086
087        mojo.execute();
088
089        CheckoutMojo checkoutMojo =
090                (CheckoutMojo) lookupMojo("checkout", getTestFile("src/test/resources/mojos/tag/checkout.xml"));
091        checkoutMojo.setWorkingDirectory(new File(getBasedir()));
092
093        setupConnectionUrl(checkoutMojo);
094
095        File tagCheckoutDir = getTestFile("target/tags/mytag");
096        if (tagCheckoutDir.exists()) {
097            FileUtils.deleteDirectory(tagCheckoutDir);
098        }
099        checkoutMojo.setCheckoutDirectory(tagCheckoutDir);
100
101        assertFalse(new File(tagCheckoutDir, "pom.xml").exists());
102        checkoutMojo.execute();
103        assertTrue(new File(tagCheckoutDir, "pom.xml").exists());
104    }
105
106    @Test
107    public void testTagWithTimestamp() throws Exception {
108        checkScmPresence(SvnScmTestUtils.SVN_COMMAND_LINE);
109
110        TagMojo mojo = (TagMojo) lookupMojo("tag", getTestFile("src/test/resources/mojos/tag/tagWithTimestamp.xml"));
111        mojo.setWorkingDirectory(checkoutDir);
112
113        setupConnectionUrl(mojo);
114
115        mojo.execute();
116    }
117}