View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.release.plugin.mojos;
18  
19  import static junit.framework.TestCase.assertTrue;
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertNotNull;
23  
24  import java.io.File;
25  
26  import org.apache.maven.plugin.MojoFailureException;
27  import org.apache.maven.plugin.testing.MojoRule;
28  import org.codehaus.plexus.util.FileUtils;
29  import org.junit.Before;
30  import org.junit.Rule;
31  import org.junit.Test;
32  
33  /**
34   * Unit tests for {@link CommonsSiteCompressionMojo}.
35   */
36  @SuppressWarnings("deprecation") // testing a deprecated class
37  public class CommonsSiteCompressionMojoTest {
38  
39      private static final String COMMONS_RELEASE_PLUGIN_TEST_DIR_PATH = "target/testing-commons-release-plugin";
40  
41      @Rule
42      public final MojoRule rule = new MojoRule() {
43          @Override
44          protected void before() throws Throwable {
45              // noop
46          }
47  
48          @Override
49          protected void after() {
50              // noop
51          }
52      };
53  
54      protected CommonsSiteCompressionMojo mojo;
55  
56      @Before
57      public void setUp() throws Exception {
58          final File testingDirectory = new File(COMMONS_RELEASE_PLUGIN_TEST_DIR_PATH);
59          if (testingDirectory.exists()) {
60              FileUtils.deleteDirectory(testingDirectory);
61          }
62      }
63  
64      @Test
65      public void testCompressSiteSuccess() throws Exception {
66          final File testingDirectory = new File(COMMONS_RELEASE_PLUGIN_TEST_DIR_PATH);
67          testingDirectory.mkdir();
68          final File testPom = new File("src/test/resources/mojos/compress-site/compress-site.xml");
69          assertNotNull(testPom);
70          assertTrue(testPom.exists());
71          mojo = (CommonsSiteCompressionMojo) rule.lookupMojo("compress-site", testPom);
72          mojo.execute();
73          final File siteZip = new File(COMMONS_RELEASE_PLUGIN_TEST_DIR_PATH + "/site.zip");
74          assertTrue(siteZip.exists());
75      }
76  
77      @Test
78      public void testCompressSiteDirNonExistentFailure() throws Exception {
79          final File testPom = new File("src/test/resources/mojos/compress-site/compress-site-failure.xml");
80          assertNotNull(testPom);
81          assertTrue(testPom.exists());
82          mojo = (CommonsSiteCompressionMojo) rule.lookupMojo("compress-site", testPom);
83          try {
84              mojo.execute();
85          } catch (final MojoFailureException e) {
86              assertEquals(
87                      "\"mvn site\" was not run before this goal, or a siteDirectory did not exist.", e.getMessage()
88              );
89          }
90      }
91  
92      @Test
93      public void testDisabled() throws Exception {
94          final File testPom = new File("src/test/resources/mojos/compress-site/compress-site-disabled.xml");
95          assertNotNull(testPom);
96          assertTrue(testPom.exists());
97          mojo = (CommonsSiteCompressionMojo) rule.lookupMojo("compress-site", testPom);
98          mojo.execute();
99          final File testingDirectory = new File(COMMONS_RELEASE_PLUGIN_TEST_DIR_PATH);
100         assertFalse(testingDirectory.exists());
101     }
102 }