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.resolver.internal.ant.tasks;
20  
21  import java.io.File;
22  import java.io.PrintStream;
23  
24  import junit.framework.JUnit4TestAdapter;
25  import org.apache.tools.ant.BuildException;
26  import org.apache.tools.ant.BuildFileRule;
27  import org.apache.tools.ant.DefaultLogger;
28  import org.apache.tools.ant.Project;
29  import org.junit.Assert;
30  import org.junit.Rule;
31  import org.junit.Test;
32  
33  public class CreatePomRainyDayTest {
34  
35      public static junit.framework.Test suite() {
36          return new JUnit4TestAdapter(CreatePomRainyDayTest.class);
37      }
38  
39      @Rule
40      public final BuildFileRule buildRule = new BuildFileRule();
41  
42      public void configureProject(String filename, int logLevel) throws BuildException {
43          buildRule.configureProject(filename, logLevel);
44          DefaultLogger logger = new DefaultLogger() {
45              @Override
46              protected void printMessage(String message, PrintStream stream, int priority) {
47                  message = System.currentTimeMillis() + " " + message;
48                  super.printMessage(message, stream, priority);
49              }
50          };
51          logger.setMessageOutputLevel(logLevel);
52          logger.setOutputPrintStream(System.out);
53          logger.setErrorPrintStream(System.err);
54          buildRule.getProject().addBuildListener(logger);
55      }
56  
57      /**
58       * Test that the task fails if no groupId is specified and the pom task is called (default).
59       * Since the failure happens when the task registers the POM, the user can still create the POM
60       * if the skipPomRegistration property is set to true.
61       */
62      @Test
63      public void testNoGroupId() {
64          configureProject("target/test-classes/ant/createPom/noGroupId.xml", Project.MSG_VERBOSE);
65          // Expect a BuildException when trying to execute the target
66          Assert.assertThrows(BuildException.class, () -> buildRule.executeTarget("setup"));
67  
68          // This should work since the setSkipPomRegistration property is set to true
69          configureProject("target/test-classes/ant/createPom/noGroupIdSkipRegistration.xml", Project.MSG_VERBOSE);
70          buildRule.executeTarget("setup");
71          File pomFile = getPomFile();
72          Assert.assertTrue("The pom file should have been created at " + pomFile, pomFile.exists());
73      }
74  
75      /**
76       * Test that the task fails if no artifactId is specified and the pom task is called (default).
77       * Since the failure happens when the task registers the POM, the user can still create the POM
78       * if the skipPomRegistration property is set to true.
79       */
80      @Test
81      public void testNoArtifactId() {
82          configureProject("target/test-classes/ant/createPom/noArtifactId.xml", Project.MSG_VERBOSE);
83          // Expect a BuildException when trying to execute the target
84          Assert.assertThrows(BuildException.class, () -> buildRule.executeTarget("setup"));
85  
86          // This should work since the setSkipPomRegistration property is set to true
87          configureProject("target/test-classes/ant/createPom/noArtifactIdSkipRegistration.xml", Project.MSG_VERBOSE);
88          buildRule.executeTarget("setup");
89          File pomFile = getPomFile();
90          Assert.assertTrue("The pom file should have been created at " + pomFile, pomFile.exists());
91      }
92  
93      private File getPomFile() {
94          Project project = buildRule.getProject();
95          String pomPath = project.replaceProperties(project.getProperty("pomFile"));
96          Assert.assertNotNull("pomFile property should not be null", pomPath);
97          return new File(pomPath);
98      }
99  }