View Javadoc
1   package org.apache.maven.scm.plugin;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
24  import org.apache.maven.scm.ScmTestCase;
25  import org.apache.maven.scm.provider.ScmProviderRepositoryWithHost;
26  import org.apache.maven.scm.provider.svn.SvnScmTestUtils;
27  import org.codehaus.plexus.util.FileUtils;
28  import org.codehaus.plexus.util.StringUtils;
29  
30  import java.io.File;
31  
32  /**
33   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
34   *
35   */
36  public class CheckoutMojoTest
37      extends AbstractMojoTestCase
38  {
39      File checkoutDir;
40  
41      File repository;
42  
43      protected void setUp()
44          throws Exception
45      {
46          super.setUp();
47  
48          checkoutDir = getTestFile( "target/checkout" );
49  
50          repository = getTestFile( "target/repository" );
51  
52          FileUtils.forceDelete( checkoutDir );
53      }
54  
55      public void testSkipCheckoutWhenCheckoutDirectoryExistsAndSkip()
56          throws Exception
57      {
58          FileUtils.forceDelete( checkoutDir );
59          checkoutDir.mkdirs();
60  
61          CheckoutMojo mojo = (CheckoutMojo) lookupMojo( "checkout", getTestFile(
62              "src/test/resources/mojos/checkout/checkoutWhenCheckoutDirectoryExistsAndSkip.xml" ) );
63  
64          mojo.setCheckoutDirectory( checkoutDir );
65  
66          mojo.execute();
67  
68          assertEquals( 0, checkoutDir.listFiles().length );
69      }
70  
71      public void testSkipCheckoutWithConnectionUrl()
72          throws Exception
73      {
74          if ( !ScmTestCase.isSystemCmd( SvnScmTestUtils.SVNADMIN_COMMAND_LINE ) )
75          {
76              System.err.println( "'" + SvnScmTestUtils.SVNADMIN_COMMAND_LINE
77                  + "' is not a system command. Ignored " + getName() + "." );
78              return;
79          }
80  
81          FileUtils.forceDelete( checkoutDir );
82          
83          SvnScmTestUtils.initializeRepository( repository );
84  
85          CheckoutMojo mojo = (CheckoutMojo) lookupMojo( "checkout", getTestFile(
86              "src/test/resources/mojos/checkout/checkoutWithConnectionUrl.xml" ) );
87          mojo.setWorkingDirectory( new File( getBasedir() ) );
88  
89          String connectionUrl = mojo.getConnectionUrl();
90          connectionUrl = StringUtils.replace( connectionUrl, "${basedir}", getBasedir() );
91          connectionUrl = StringUtils.replace( connectionUrl, "\\", "/" );
92          mojo.setConnectionUrl( connectionUrl );
93  
94          mojo.setCheckoutDirectory( checkoutDir );
95  
96          mojo.execute();
97      }
98  
99      public void testSkipCheckoutWithoutConnectionUrl()
100         throws Exception
101     {
102         FileUtils.forceDelete( checkoutDir );
103         
104         checkoutDir.mkdirs();
105         CheckoutMojo mojo = (CheckoutMojo) lookupMojo( "checkout", getTestFile(
106             "src/test/resources/mojos/checkout/checkoutWithoutConnectionUrl.xml" ) );
107 
108         try
109         {
110             mojo.execute();
111 
112             fail( "mojo execution must fail." );
113         }
114         catch ( MojoExecutionException e )
115         {
116             assertTrue( true );
117         }
118     }
119 
120     public void testUseExport()
121         throws Exception
122     {
123         FileUtils.forceDelete( checkoutDir );
124         
125         checkoutDir.mkdirs();
126 
127         CheckoutMojo mojo = (CheckoutMojo) lookupMojo( "checkout", getTestFile(
128             "src/test/resources/mojos/checkout/checkoutUsingExport.xml" ) );
129 
130         mojo.setCheckoutDirectory( checkoutDir );
131 
132         mojo.execute();
133 
134         assertTrue( checkoutDir.listFiles().length > 0  );
135         assertFalse( new File( checkoutDir, ".svn" ).exists() );    
136     }
137     
138     public void testExcludeInclude()
139         throws Exception
140     {
141         FileUtils.forceDelete( checkoutDir );
142         
143         checkoutDir.mkdirs();
144 
145         SvnScmTestUtils.initializeRepository( repository );
146 
147         CheckoutMojo mojo = (CheckoutMojo) lookupMojo(
148                                                        "checkout",
149                                                        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()
167         throws Exception
168     {
169         File pom = getTestFile( "src/test/resources/mojos/checkout/checkoutEncryptedPasswordFromSettings.xml" );
170         CheckoutMojo mojo = (CheckoutMojo) lookupMojo( "checkout", pom );
171         ScmProviderRepositoryWithHost repo =
172             (ScmProviderRepositoryWithHost) mojo.getScmRepository().getProviderRepository();
173 
174         assertEquals( "testuser", repo.getUser() );
175         assertEquals( "testpass", repo.getPassword() );
176         assertEquals( "testphrase", repo.getPassphrase() );
177     }
178 
179 }