View Javadoc

1   package org.apache.maven.plugin.dependency.fromDependencies;
2   
3   /* 
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  import java.io.File;
23  import java.util.Set;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.plugin.dependency.AbstractDependencyMojoTestCase;
27  import org.apache.maven.plugin.dependency.fromDependencies.UnpackDependenciesMojo;
28  import org.apache.maven.project.MavenProject;
29  
30  public class TestIncludeExcludeUnpackDependenciesMojo
31  	extends AbstractDependencyMojoTestCase
32  {
33  	private final String PACKED_FILE = "test.zip";
34  
35  	private final String UNPACKED_FILE_PREFIX = "test";
36  	private final String UNPACKED_FILE_SUFFIX = ".txt";
37  
38  	private final String PACKED_FILE_PATH = "target/test-classes/unit/unpack-dependencies-test/" + PACKED_FILE;
39  
40  	UnpackDependenciesMojo mojo;
41  
42      protected void setUp()
43          throws Exception
44      {
45      	// required for mojo lookups to work
46          super.setUp( "unpack-dependencies", true );
47  
48          File testPom = new File( getBasedir(), "target/test-classes/unit/unpack-dependencies-test/plugin-config.xml" );
49          mojo = (UnpackDependenciesMojo) lookupMojo( "unpack-dependencies", testPom );
50          mojo.outputDirectory = new File( this.testDir, "outputDirectory" );
51          // mojo.silent = true;
52  
53          // it needs to get the archivermanager
54          //stubFactory.setUnpackableFile( mojo.getArchiverManager() );
55          // i'm using one file repeatedly to archive so I can test the name
56          // programmatically.
57          stubFactory.setSrcFile( new File( getBasedir() + File.separatorChar + PACKED_FILE_PATH ) );
58  
59          assertNotNull( mojo );
60          assertNotNull( mojo.getProject() );
61          MavenProject project = mojo.getProject();
62  
63          Set<Artifact> artifacts = this.stubFactory.getScopedArtifacts();
64          Set<Artifact> directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
65          artifacts.addAll( directArtifacts );
66  
67          project.setArtifacts( artifacts );
68          project.setDependencyArtifacts( directArtifacts );
69          mojo.markersDirectory = new File( this.testDir, "markers" );
70  
71      }
72  
73      protected void tearDown()
74      {
75          super.tearDown();
76  
77          mojo = null;
78          System.gc();
79      }
80  
81      private void assertUnpacked( boolean unpacked, String fileName )
82      {
83          File destFile = new File( mojo.getOutputDirectory().getAbsolutePath(), fileName );
84          assertEquals( unpacked, destFile.exists() );
85      }
86  
87      /**
88       * This test will validate that only the 1 and 11 files get unpacked
89       * @throws Exception
90       */
91      public void testUnpackIncludesManyFiles()
92  		throws Exception
93  	{
94          mojo.setIncludes( "**/*1" + UNPACKED_FILE_SUFFIX );
95          mojo.execute();
96          assertUnpacked( true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX );
97          assertUnpacked( true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX );
98          assertUnpacked( false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
99          assertUnpacked( false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
100 	}
101 
102     /**
103      * This test will verify only the 2 file gets unpacked
104      * @throws Exception
105      */
106     public void testUnpackIncludesSingleFile()
107     	throws Exception
108 	{
109         mojo.setIncludes( "**/test2" + UNPACKED_FILE_SUFFIX );
110         mojo.execute();
111         assertUnpacked( false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX );
112         assertUnpacked( false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX );
113         assertUnpacked( true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
114         assertUnpacked( false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
115 	}
116 
117     /**
118      * This test will verify all files get unpacked
119      * @throws Exception
120      */
121     public void testUnpackIncludesAllFiles()
122     	throws Exception
123 	{
124         mojo.setIncludes( "**/*" );
125         mojo.execute();
126         assertUnpacked( true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX );
127         assertUnpacked( true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX );
128         assertUnpacked( true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
129         assertUnpacked( true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
130 	}
131 
132     /**
133      * This test will validate that only the 2 and 3 files get unpacked
134      * @throws Exception
135      */
136     public void testUnpackExcludesManyFiles()
137 		throws Exception
138 	{
139         mojo.setExcludes( "**/*1" + UNPACKED_FILE_SUFFIX );
140         mojo.execute();
141         assertUnpacked( false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX );
142         assertUnpacked( false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX );
143         assertUnpacked( true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
144         assertUnpacked( true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
145 	}
146 
147     /**
148      * This test will verify only the 1, 11 & 3 files get unpacked
149      * @throws Exception
150      */
151     public void testUnpackExcludesSingleFile()
152     	throws Exception
153 	{
154         mojo.setExcludes( "**/test2" + UNPACKED_FILE_SUFFIX );
155         mojo.execute();
156         assertUnpacked( true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX );
157         assertUnpacked( true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX );
158         assertUnpacked( false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
159         assertUnpacked( true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
160 	}
161 
162     /**
163      * This test will verify no files get unpacked
164      * @throws Exception
165      */
166     public void testUnpackExcludesAllFiles()
167     	throws Exception
168 	{
169         mojo.setExcludes( "**/*" );
170         mojo.execute();
171         assertUnpacked( false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX );
172         assertUnpacked( false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX );
173         assertUnpacked( false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
174         assertUnpacked( false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
175 	}
176 
177     public void testNoIncludeExcludes()
178     	throws Exception
179 	{
180         mojo.execute();
181         assertUnpacked( true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX );
182         assertUnpacked( true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX );
183         assertUnpacked( true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
184         assertUnpacked( true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
185 	}
186 }