View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.plugin;
20  
21  import java.io.File;
22  
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.scm.provider.git.GitScmTestUtils;
25  import org.codehaus.plexus.util.FileUtils;
26  import org.codehaus.plexus.util.StringUtils;
27  import org.junit.Before;
28  import org.junit.Test;
29  import org.junit.runner.RunWith;
30  import org.junit.runners.JUnit4;
31  
32  import static org.apache.maven.scm.ScmTestCase.checkScmPresence;
33  
34  @RunWith(JUnit4.class)
35  public class UntagMojoTest extends AbstractJUnit4MojoTestCase {
36      File checkoutDir;
37  
38      File repository;
39  
40      @Before
41      public void setUp() throws Exception {
42          super.setUp();
43  
44          checkoutDir = getTestFile("target/checkout");
45  
46          repository = getTestFile("target/repository");
47  
48          checkScmPresence(GitScmTestUtils.GIT_COMMAND_LINE);
49  
50          GitScmTestUtils.initRepo("src/test/resources/git", repository, checkoutDir);
51  
52          CheckoutMojo checkoutMojo =
53                  (CheckoutMojo) lookupMojo("checkout", getTestFile("src/test/resources/mojos/untag/checkout.xml"));
54          checkoutMojo.setWorkingDirectory(checkoutDir);
55  
56          String connectionUrl = checkoutMojo.getConnectionUrl();
57          connectionUrl = StringUtils.replace(connectionUrl, "${basedir}", getBasedir());
58          connectionUrl = StringUtils.replace(connectionUrl, "\\", "/");
59          checkoutMojo.setConnectionUrl(connectionUrl);
60  
61          checkoutMojo.setCheckoutDirectory(checkoutDir);
62  
63          checkoutMojo.execute();
64  
65          // Add a default user to the config
66          GitScmTestUtils.setDefaulGitConfig(checkoutDir);
67      }
68  
69      @Test
70      public void testUntag() throws Exception {
71          checkScmPresence(GitScmTestUtils.GIT_COMMAND_LINE);
72  
73          TagMojo tagMojo = (TagMojo) lookupMojo("tag", getTestFile("src/test/resources/mojos/untag/tag.xml"));
74          tagMojo.setWorkingDirectory(checkoutDir);
75          tagMojo.setConnectionUrl(getConnectionLocalAddress(tagMojo));
76          tagMojo.execute();
77  
78          CheckoutMojo checkoutMojo =
79                  (CheckoutMojo) lookupMojo("checkout", getTestFile("src/test/resources/mojos/untag/checkout-tag.xml"));
80          checkoutMojo.setWorkingDirectory(new File(getBasedir()));
81          checkoutMojo.setConnectionUrl(getConnectionLocalAddress(checkoutMojo));
82  
83          File tagCheckoutDir = getTestFile("target/tags/mytag");
84  
85          if (tagCheckoutDir.exists()) {
86              FileUtils.deleteDirectory(tagCheckoutDir);
87          }
88  
89          checkoutMojo.setCheckoutDirectory(tagCheckoutDir);
90          checkoutMojo.execute();
91  
92          UntagMojo mojo = (UntagMojo) lookupMojo("untag", getTestFile("src/test/resources/mojos/untag/untag.xml"));
93          mojo.setWorkingDirectory(checkoutDir);
94          mojo.setConnectionUrl(getConnectionLocalAddress(mojo));
95  
96          mojo.execute();
97  
98          FileUtils.deleteDirectory(tagCheckoutDir);
99  
100         try {
101             checkoutMojo.execute();
102 
103             fail("mojo execution must fail.");
104         } catch (MojoExecutionException e) {
105             assertTrue(true);
106         }
107     }
108 
109     private String getConnectionLocalAddress(AbstractScmMojo mojo) {
110         String connectionUrl = mojo.getConnectionUrl();
111         connectionUrl = StringUtils.replace(connectionUrl, "${basedir}", getBasedir());
112         connectionUrl = StringUtils.replace(connectionUrl, "\\", "/");
113         return connectionUrl;
114     }
115 }