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    *      https://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  
23  import java.io.File;
24  
25  import org.apache.maven.api.plugin.testing.InjectMojo;
26  import org.apache.maven.api.plugin.testing.MojoTest;
27  import org.apache.maven.plugin.MojoFailureException;
28  import org.codehaus.plexus.util.FileUtils;
29  import org.junit.jupiter.api.BeforeEach;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Unit tests for {@link CommonsSiteCompressionMojo}.
34   */
35  @SuppressWarnings("deprecation") // testing a deprecated class
36  @MojoTest
37  public class CommonsSiteCompressionMojoTest {
38  
39      private static final String COMMONS_RELEASE_PLUGIN_TEST_DIR_PATH = "target/testing-commons-release-plugin";
40  
41      @BeforeEach
42      public void setUp() throws Exception {
43          final File testingDirectory = new File(COMMONS_RELEASE_PLUGIN_TEST_DIR_PATH);
44          if (testingDirectory.exists()) {
45              FileUtils.deleteDirectory(testingDirectory);
46          }
47      }
48  
49      @Test
50      @InjectMojo(goal = "compress-site", pom = "src/test/resources/mojos/compress-site/compress-site-failure.xml")
51      public void testCompressSiteDirNonExistentFailure(final CommonsSiteCompressionMojo mojo) throws Exception {
52          try {
53              mojo.execute();
54          } catch (final MojoFailureException e) {
55              assertEquals(
56                      "\"mvn site\" was not run before this goal, or a siteDirectory did not exist.", e.getMessage()
57              );
58          }
59      }
60  
61      @Test
62      @InjectMojo(goal = "compress-site", pom = "src/test/resources/mojos/compress-site/compress-site.xml")
63      public void testCompressSiteSuccess(final CommonsSiteCompressionMojo mojo) throws Exception {
64          final File testingDirectory = new File(COMMONS_RELEASE_PLUGIN_TEST_DIR_PATH);
65          testingDirectory.mkdir();
66          mojo.execute();
67          final File siteZip = new File(COMMONS_RELEASE_PLUGIN_TEST_DIR_PATH + "/site.zip");
68          assertTrue(siteZip.exists());
69      }
70  
71      @Test
72      @InjectMojo(goal = "compress-site", pom = "src/test/resources/mojos/compress-site/compress-site-disabled.xml")
73      public void testDisabled(final CommonsSiteCompressionMojo mojo) throws Exception {
74          mojo.execute();
75          final File testingDirectory = new File(COMMONS_RELEASE_PLUGIN_TEST_DIR_PATH);
76          assertFalse(testingDirectory.exists());
77      }
78  }