View Javadoc

1   package org.apache.maven.plugin.dependency;
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.project.MavenProject;
27  
28  public class TestIncludeExcludeUnpackDependenciesMojo
29  	extends AbstractDependencyMojoTestCase
30  {
31  	private final String PACKED_FILE = "test.zip";
32  
33  	private final String UNPACKED_FILE_PREFIX = "test";
34  	private final String UNPACKED_FILE_SUFFIX = ".txt";
35  
36  	private final String PACKED_FILE_PATH = "target/test-classes/unit/unpack-dependencies-test/" + PACKED_FILE;
37  
38  	UnpackDependenciesMojo mojo;
39  
40      protected void setUp()
41          throws Exception
42      {
43      	// required for mojo lookups to work
44          super.setUp( "unpack-dependencies", true );
45  
46          File testPom = new File( getBasedir(), "target/test-classes/unit/unpack-dependencies-test/plugin-config.xml" );
47          mojo = (UnpackDependenciesMojo) lookupMojo( "unpack-dependencies", testPom );
48          mojo.outputDirectory = new File( this.testDir, "outputDirectory" );
49          // mojo.silent = true;
50  
51          // it needs to get the archivermanager
52          //stubFactory.setUnpackableFile( mojo.getArchiverManager() );
53          // i'm using one file repeatedly to archive so I can test the name
54          // programmatically.
55          stubFactory.setSrcFile( new File( getBasedir() + File.separatorChar + PACKED_FILE_PATH ) );
56  
57          assertNotNull( mojo );
58          assertNotNull( mojo.getProject() );
59          MavenProject project = mojo.getProject();
60  
61          Set<Artifact> artifacts = this.stubFactory.getScopedArtifacts();
62          Set<Artifact> directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
63          artifacts.addAll( directArtifacts );
64  
65          project.setArtifacts( artifacts );
66          project.setDependencyArtifacts( directArtifacts );
67          mojo.markersDirectory = new File( this.testDir, "markers" );
68  
69      }
70  
71      protected void tearDown()
72      {
73          super.tearDown();
74  
75          mojo = null;
76          System.gc();
77      }
78  
79      private void assertUnpacked( boolean unpacked, String fileName )
80      {
81          File destFile = new File( mojo.getOutputDirectory().getAbsolutePath(), fileName );
82          assertEquals( unpacked, destFile.exists() );
83      }
84  
85      /**
86       * This test will validate that only the 1 and 11 files get unpacked
87       * @throws Exception
88       */
89      public void testUnpackIncludesManyFiles()
90  		throws Exception
91  	{
92          mojo.setIncludes( "**/*1" + UNPACKED_FILE_SUFFIX );
93          mojo.execute();
94          assertUnpacked( true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX );
95          assertUnpacked( true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX );
96          assertUnpacked( false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
97          assertUnpacked( false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
98  	}
99  
100     /**
101      * This test will verify only the 2 file gets unpacked
102      * @throws Exception
103      */
104     public void testUnpackIncludesSingleFile()
105     	throws Exception
106 	{
107         mojo.setIncludes( "**/test2" + UNPACKED_FILE_SUFFIX );
108         mojo.execute();
109         assertUnpacked( false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX );
110         assertUnpacked( false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX );
111         assertUnpacked( true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
112         assertUnpacked( false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
113 	}
114 
115     /**
116      * This test will verify all files get unpacked
117      * @throws Exception
118      */
119     public void testUnpackIncludesAllFiles()
120     	throws Exception
121 	{
122         mojo.setIncludes( "**/*" );
123         mojo.execute();
124         assertUnpacked( true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX );
125         assertUnpacked( true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX );
126         assertUnpacked( true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
127         assertUnpacked( true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
128 	}
129 
130     /**
131      * This test will validate that only the 2 and 3 files get unpacked
132      * @throws Exception
133      */
134     public void testUnpackExcludesManyFiles()
135 		throws Exception
136 	{
137         mojo.setExcludes( "**/*1" + UNPACKED_FILE_SUFFIX );
138         mojo.execute();
139         assertUnpacked( false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX );
140         assertUnpacked( false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX );
141         assertUnpacked( true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
142         assertUnpacked( true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
143 	}
144 
145     /**
146      * This test will verify only the 1, 11 & 3 files get unpacked
147      * @throws Exception
148      */
149     public void testUnpackExcludesSingleFile()
150     	throws Exception
151 	{
152         mojo.setExcludes( "**/test2" + UNPACKED_FILE_SUFFIX );
153         mojo.execute();
154         assertUnpacked( true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX );
155         assertUnpacked( true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX );
156         assertUnpacked( false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
157         assertUnpacked( true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
158 	}
159 
160     /**
161      * This test will verify no files get unpacked
162      * @throws Exception
163      */
164     public void testUnpackExcludesAllFiles()
165     	throws Exception
166 	{
167         mojo.setExcludes( "**/*" );
168         mojo.execute();
169         assertUnpacked( false, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX );
170         assertUnpacked( false, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX );
171         assertUnpacked( false, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
172         assertUnpacked( false, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
173 	}
174 
175     public void testNoIncludeExcludes()
176     	throws Exception
177 	{
178         mojo.execute();
179         assertUnpacked( true, UNPACKED_FILE_PREFIX + 1 + UNPACKED_FILE_SUFFIX );
180         assertUnpacked( true, UNPACKED_FILE_PREFIX + 11 + UNPACKED_FILE_SUFFIX );
181         assertUnpacked( true, UNPACKED_FILE_PREFIX + 2 + UNPACKED_FILE_SUFFIX );
182         assertUnpacked( true, UNPACKED_FILE_PREFIX + 3 + UNPACKED_FILE_SUFFIX );
183 	}
184 }