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.plugins.resources;
20  
21  import java.io.File;
22  import java.util.Collections;
23  import java.util.List;
24  
25  import org.apache.maven.model.Resource;
26  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
27  import org.apache.maven.plugins.resources.stub.MavenProjectResourcesStub;
28  import org.codehaus.plexus.util.FileUtils;
29  
30  public class TestResourcesTest extends AbstractMojoTestCase {
31      protected static final String defaultPomFilePath = "/target/test-classes/unit/resources-test/plugin-config.xml";
32  
33      protected void setUp() throws Exception {
34          super.setUp();
35      }
36  
37      /**
38       * test mojo lookup, test harness should be working fine
39       *
40       * @throws Exception
41       */
42      public void testHarnessEnvironment() throws Exception {
43          File testPom = new File(getBasedir(), defaultPomFilePath);
44          ResourcesMojo mojo = (ResourcesMojo) lookupMojo("testResources", testPom);
45  
46          assertNotNull(mojo);
47      }
48  
49      /**
50       * @throws Exception
51       */
52      public void testTestResourceDirectoryCreation() throws Exception {
53          File testPom = new File(getBasedir(), defaultPomFilePath);
54          TestResourcesMojo mojo = (TestResourcesMojo) lookupMojo("testResources", testPom);
55          MavenProjectResourcesStub project = new MavenProjectResourcesStub("testResourceDirectoryStructure");
56          List<Resource> resources = project.getBuild().getResources();
57  
58          assertNotNull(mojo);
59  
60          project.addFile("file4.txt");
61          project.addFile("package/file3.nottest");
62          project.addFile("notpackage/file1.include");
63          project.addFile("package/test/file1.txt");
64          project.addFile("notpackage/test/file2.txt");
65          project.setupBuildEnvironment();
66  
67          setVariableValueToObject(mojo, "project", project);
68          setVariableValueToObject(mojo, "resources", resources);
69          setVariableValueToObject(
70                  mojo, "outputDirectory", new File(project.getBuild().getTestOutputDirectory()));
71          setVariableValueToObject(mojo, "buildFilters", Collections.emptyList());
72          setVariableValueToObject(mojo, "useBuildFilters", Boolean.TRUE);
73          mojo.execute();
74  
75          String resorucesDir = project.getTestOutputDirectory();
76  
77          assertTrue(FileUtils.fileExists(resorucesDir + "/file4.txt"));
78          assertTrue(FileUtils.fileExists(resorucesDir + "/package/file3.nottest"));
79          assertTrue(FileUtils.fileExists(resorucesDir + "/notpackage/file1.include"));
80          assertTrue(FileUtils.fileExists(resorucesDir + "/package/test"));
81          assertTrue(FileUtils.fileExists(resorucesDir + "/notpackage/test"));
82      }
83  }