001package org.apache.maven.scm.plugin;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.plugin.MojoExecutionException;
023import org.apache.maven.plugin.testing.AbstractMojoTestCase;
024import org.apache.maven.scm.ScmTestCase;
025import org.codehaus.plexus.util.StringUtils;
026
027import java.io.File;
028
029/**
030 * @author <a href="paul@webotech.co.uk">Paul Mackinlay</a>
031 */
032public abstract class RemoveMojoTest //see https://jira.codehaus.org/browse/SCM-760
033    extends AbstractMojoTestCase
034{
035
036    public void testShouldInvokeP4Delete()
037        throws Exception
038    {
039
040        if ( !ScmTestCase.isSystemCmd( "p4" ) )
041        {
042            System.out.println( "'skip test as p4 is not available" );
043            return;
044        }
045
046        String testConfig = "src/test/resources/mojos/remove/removeWithPerforce.xml";
047        try
048        {
049            RemoveMojo removeMojo = (RemoveMojo) lookupMojo( "remove", getTestFile( testConfig ) );
050            String connectionUrl = removeMojo.getConnectionUrl();
051            connectionUrl = StringUtils.replace( connectionUrl, "${basedir}", getBasedir() );
052            connectionUrl = StringUtils.replace( connectionUrl, "\\", "/" );
053            removeMojo.setWorkingDirectory( new File( getBasedir() ) );
054            removeMojo.setConnectionUrl( connectionUrl );
055
056            removeMojo.execute();
057        }
058        finally
059        {
060            // Just to be sure unedit anything that has been marked for delete
061            UnEditMojo unEditMojo = (UnEditMojo) lookupMojo( "unedit", getTestFile( testConfig ) );
062            String connectionUrl = unEditMojo.getConnectionUrl();
063            connectionUrl = StringUtils.replace( connectionUrl, "${basedir}", getBasedir() );
064            connectionUrl = StringUtils.replace( connectionUrl, "\\", "/" );
065            unEditMojo.setWorkingDirectory( new File( getBasedir() ) );
066            unEditMojo.setConnectionUrl( connectionUrl );
067            unEditMojo.execute();
068        }
069    }
070
071    public void testShouldFailToInvokeP4Delete()
072        throws Exception
073    {
074        if ( !ScmTestCase.isSystemCmd( "p4" ) )
075        {
076            System.out.println( "'skip test as p4 is not available" );
077            return;
078        }
079        String testConfig = "src/test/resources/mojos/remove/removeWithPerforceNoIncludes.xml";
080        try
081        {
082            RemoveMojo removeMojo = (RemoveMojo) lookupMojo( "remove", getTestFile( testConfig ) );
083            String connectionUrl = removeMojo.getConnectionUrl();
084            connectionUrl = StringUtils.replace( connectionUrl, "${basedir}", getBasedir() );
085            connectionUrl = StringUtils.replace( connectionUrl, "\\", "/" );
086            removeMojo.setWorkingDirectory( new File( getBasedir() ) );
087            removeMojo.setConnectionUrl( connectionUrl );
088
089            try
090            {
091                removeMojo.execute();
092                fail( "At least one file needs to be included for removal" );
093            }
094            catch ( MojoExecutionException e )
095            {
096                // we're expecting this exception
097            }
098        }
099        finally
100        {
101            // Just to be sure unedit anything that has been marked for delete
102            UnEditMojo unEditMojo = (UnEditMojo) lookupMojo( "unedit", getTestFile( testConfig ) );
103            String connectionUrl = unEditMojo.getConnectionUrl();
104            connectionUrl = StringUtils.replace( connectionUrl, "${basedir}", getBasedir() );
105            connectionUrl = StringUtils.replace( connectionUrl, "\\", "/" );
106            unEditMojo.setWorkingDirectory( new File( getBasedir() ) );
107            unEditMojo.setConnectionUrl( connectionUrl );
108            unEditMojo.execute();
109        }
110    }
111
112}