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.svn.SvnScmTestUtils;
028import org.codehaus.plexus.util.FileUtils;
029
030/**
031 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
032 *
033 */
034public class ChangeLogMojoTest extends AbstractMojoTestCase {
035    File repository;
036
037    protected void setUp() throws Exception {
038        super.setUp();
039
040        repository = getTestFile("target/repository");
041
042        FileUtils.forceDelete(repository);
043
044        if (!ScmTestCase.isSystemCmd(SvnScmTestUtils.SVNADMIN_COMMAND_LINE)) {
045            ScmTestCase.printSystemCmdUnavail(SvnScmTestUtils.SVNADMIN_COMMAND_LINE, "setUp");
046            return;
047        }
048
049        SvnScmTestUtils.initializeRepository(repository);
050    }
051
052    public void testChangeLog() throws Exception {
053        if (!ScmTestCase.isSystemCmd(SvnScmTestUtils.SVN_COMMAND_LINE)) {
054            ScmTestCase.printSystemCmdUnavail(SvnScmTestUtils.SVN_COMMAND_LINE, getName());
055            return;
056        }
057
058        ChangeLogMojo mojo = (ChangeLogMojo)
059                lookupMojo("changelog", getTestFile("src/test/resources/mojos/changelog/changelog.xml"));
060
061        String connectionUrl = mojo.getConnectionUrl();
062        connectionUrl = StringUtils.replace(connectionUrl, "${basedir}", getBasedir());
063        connectionUrl = StringUtils.replace(connectionUrl, "\\", "/");
064        mojo.setConnectionUrl(connectionUrl);
065        mojo.setWorkingDirectory(new File(getBasedir()));
066        mojo.setConnectionType("connection");
067
068        mojo.execute();
069    }
070
071    public void testChangeLogWithParameters() throws Exception {
072        if (!ScmTestCase.isSystemCmd(SvnScmTestUtils.SVN_COMMAND_LINE)) {
073            ScmTestCase.printSystemCmdUnavail(SvnScmTestUtils.SVN_COMMAND_LINE, getName());
074            return;
075        }
076
077        ChangeLogMojo mojo = (ChangeLogMojo)
078                lookupMojo("changelog", getTestFile("src/test/resources/mojos/changelog/changelogWithParameters.xml"));
079
080        String connectionUrl = mojo.getConnectionUrl();
081        connectionUrl = StringUtils.replace(connectionUrl, "${basedir}", getBasedir());
082        connectionUrl = StringUtils.replace(connectionUrl, "\\", "/");
083        mojo.setConnectionUrl(connectionUrl);
084        mojo.setWorkingDirectory(new File(getBasedir()));
085        mojo.setConnectionType("connection");
086
087        mojo.execute();
088    }
089
090    public void testChangeLogWithBadUserDateFormat() throws Exception {
091        ChangeLogMojo mojo = (ChangeLogMojo) lookupMojo(
092                "changelog", getTestFile("src/test/resources/mojos/changelog/changelogWithBadUserDateFormat.xml"));
093
094        String connectionUrl = mojo.getConnectionUrl();
095        connectionUrl = StringUtils.replace(connectionUrl, "${basedir}", getBasedir());
096        connectionUrl = StringUtils.replace(connectionUrl, "\\", "/");
097        mojo.setConnectionUrl(connectionUrl);
098        mojo.setWorkingDirectory(new File(getBasedir()));
099        mojo.setConnectionType("connection");
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 testChangeLogWithBadConnectionUrl() throws Exception {
111        ChangeLogMojo mojo = (ChangeLogMojo) lookupMojo(
112                "changelog", getTestFile("src/test/resources/mojos/changelog/changelogWithBadConnectionUrl.xml"));
113
114        String connectionUrl = mojo.getConnectionUrl();
115        connectionUrl = StringUtils.replace(connectionUrl, "${basedir}", getBasedir());
116        connectionUrl = StringUtils.replace(connectionUrl, "\\", "/");
117        mojo.setConnectionUrl(connectionUrl);
118        mojo.setWorkingDirectory(new File(getBasedir()));
119        mojo.setConnectionType("connection");
120
121        try {
122            mojo.execute();
123
124            fail("mojo execution must fail.");
125        } catch (MojoExecutionException e) {
126            assertTrue(true);
127        }
128    }
129}