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.MojoExecutionException;
025import org.apache.maven.plugin.testing.AbstractMojoTestCase;
026import org.apache.maven.scm.ScmTestCase;
027import org.apache.maven.scm.provider.ScmProviderRepositoryWithHost;
028import org.apache.maven.scm.provider.svn.SvnScmTestUtils;
029import org.codehaus.plexus.util.FileUtils;
030
031/**
032 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
033 *
034 */
035public class CheckoutMojoTest extends AbstractMojoTestCase {
036    File checkoutDir;
037
038    File repository;
039
040    protected void setUp() throws Exception {
041        super.setUp();
042
043        checkoutDir = getTestFile("target/checkout");
044
045        repository = getTestFile("target/repository");
046
047        FileUtils.forceDelete(checkoutDir);
048    }
049
050    public void testSkipCheckoutWhenCheckoutDirectoryExistsAndSkip() throws Exception {
051        FileUtils.forceDelete(checkoutDir);
052        checkoutDir.mkdirs();
053
054        CheckoutMojo mojo = (CheckoutMojo) lookupMojo(
055                "checkout",
056                getTestFile("src/test/resources/mojos/checkout/checkoutWhenCheckoutDirectoryExistsAndSkip.xml"));
057
058        mojo.setCheckoutDirectory(checkoutDir);
059
060        mojo.execute();
061
062        assertEquals(0, checkoutDir.listFiles().length);
063    }
064
065    public void testSkipCheckoutWithConnectionUrl() throws Exception {
066        if (!ScmTestCase.isSystemCmd(SvnScmTestUtils.SVNADMIN_COMMAND_LINE)) {
067            ScmTestCase.printSystemCmdUnavail(SvnScmTestUtils.SVNADMIN_COMMAND_LINE, getName());
068            return;
069        }
070
071        FileUtils.forceDelete(checkoutDir);
072
073        SvnScmTestUtils.initializeRepository(repository);
074
075        if (!ScmTestCase.isSystemCmd(SvnScmTestUtils.SVN_COMMAND_LINE)) {
076            ScmTestCase.printSystemCmdUnavail(SvnScmTestUtils.SVN_COMMAND_LINE, getName());
077            return;
078        }
079
080        CheckoutMojo mojo = (CheckoutMojo)
081                lookupMojo("checkout", getTestFile("src/test/resources/mojos/checkout/checkoutWithConnectionUrl.xml"));
082        mojo.setWorkingDirectory(new File(getBasedir()));
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.setCheckoutDirectory(checkoutDir);
090
091        mojo.execute();
092    }
093
094    public void testSkipCheckoutWithoutConnectionUrl() throws Exception {
095        FileUtils.forceDelete(checkoutDir);
096
097        checkoutDir.mkdirs();
098        CheckoutMojo mojo = (CheckoutMojo) lookupMojo(
099                "checkout", getTestFile("src/test/resources/mojos/checkout/checkoutWithoutConnectionUrl.xml"));
100
101        try {
102            mojo.execute();
103
104            fail("mojo execution must fail.");
105        } catch (MojoExecutionException e) {
106            assertTrue(true);
107        }
108    }
109
110    public void testUseExport() throws Exception {
111        if (!ScmTestCase.isSystemCmd(SvnScmTestUtils.SVN_COMMAND_LINE)) {
112            ScmTestCase.printSystemCmdUnavail(SvnScmTestUtils.SVN_COMMAND_LINE, getName());
113            return;
114        }
115
116        FileUtils.forceDelete(checkoutDir);
117
118        checkoutDir.mkdirs();
119
120        CheckoutMojo mojo = (CheckoutMojo)
121                lookupMojo("checkout", getTestFile("src/test/resources/mojos/checkout/checkoutUsingExport.xml"));
122
123        mojo.setCheckoutDirectory(checkoutDir);
124
125        mojo.execute();
126
127        assertTrue(checkoutDir.listFiles().length > 0);
128        assertFalse(new File(checkoutDir, ".svn").exists());
129    }
130
131    public void testExcludeInclude() throws Exception {
132        if (!ScmTestCase.isSystemCmd(SvnScmTestUtils.SVNADMIN_COMMAND_LINE)) {
133            ScmTestCase.printSystemCmdUnavail(SvnScmTestUtils.SVNADMIN_COMMAND_LINE, getName());
134            return;
135        }
136
137        FileUtils.forceDelete(checkoutDir);
138
139        checkoutDir.mkdirs();
140
141        SvnScmTestUtils.initializeRepository(repository);
142
143        if (!ScmTestCase.isSystemCmd(SvnScmTestUtils.SVN_COMMAND_LINE)) {
144            ScmTestCase.printSystemCmdUnavail(SvnScmTestUtils.SVN_COMMAND_LINE, getName());
145            return;
146        }
147
148        CheckoutMojo mojo = (CheckoutMojo) lookupMojo(
149                "checkout", getTestFile("src/test/resources/mojos/checkout/checkoutWithExcludesIncludes.xml"));
150
151        mojo.setCheckoutDirectory(checkoutDir);
152
153        mojo.execute();
154
155        assertTrue(checkoutDir.listFiles().length > 0);
156        assertTrue(new File(checkoutDir, ".svn").exists());
157        assertTrue(new File(checkoutDir, "pom.xml").exists());
158        assertFalse(new File(checkoutDir, "readme.txt").exists());
159        assertFalse(new File(checkoutDir, "src/test").exists());
160        assertTrue(new File(checkoutDir, "src/main/java").exists());
161        // olamy those files not exists anymore with svn 1.7
162        // assertTrue( new File( checkoutDir, "src/main/java/.svn" ).exists() );
163        // assertTrue( new File( checkoutDir, "src/main/.svn" ).exists() );
164    }
165
166    public void testEncryptedPasswordFromSettings() throws Exception {
167        File pom = getTestFile("src/test/resources/mojos/checkout/checkoutEncryptedPasswordFromSettings.xml");
168        CheckoutMojo mojo = (CheckoutMojo) lookupMojo("checkout", pom);
169        ScmProviderRepositoryWithHost repo =
170                (ScmProviderRepositoryWithHost) mojo.getScmRepository().getProviderRepository();
171
172        assertEquals("testuser", repo.getUser());
173        assertEquals("testpass", repo.getPassword());
174        assertEquals("testphrase", repo.getPassphrase());
175    }
176}