1   package org.apache.maven.plugin;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  
21  import java.io.File;
22  import java.io.FileInputStream;
23  
24  import junit.framework.TestCase;
25  
26  import org.apache.maven.MavenConstants;
27  import org.apache.maven.MavenSession;
28  import org.apache.maven.MavenUtils;
29  import org.apache.maven.jelly.MavenJellyContext;
30  import org.apache.maven.project.Project;
31  
32  import com.werken.forehead.Forehead;
33  
34  /**
35   * @author Brett Porter <a href="mailto:brett@apache.org">brett@apache.org</a>
36   *
37   */
38  public class PluginManagerTest
39      extends TestCase
40  {
41  
42      /** instance being tested */
43      private PluginManager pluginManager;
44  
45      /** project to test with */
46      private Project project;
47  
48      /** directory for fake data */
49      private static final String FAKE_BASE = "/target/PluginManagerTest/";
50  
51      private static String FS = File.separator;
52  
53      /** source base */
54      private static final String SOURCE_BASE = FS + "src" + FS + "test" + FS + "java" + FS + "org" + FS + "apache" + FS
55          + "maven" + FS + "plugin" + FS;
56  
57      /** base directory */
58      private String basedir;
59  
60      /** fake maven.home */
61      private String fakeHome;
62  
63      /**
64       * Constructor for PluginManagerTest.
65       * @param name the name of the test case
66       */
67      public PluginManagerTest( String name )
68      {
69          super( name );
70      }
71  
72      /**
73       * Set up the test case
74       * @throws Exception when any error occurs
75       */
76      public void setUp()
77          throws Exception
78      {
79          super.setUp();
80  
81          basedir = System.getProperty( "user.dir" );
82          assertNotNull( basedir );
83  
84          // 1) need to fake out maven home, so that the unpacked plugin dir in the test
85          //    directory
86          fakeHome = basedir + SOURCE_BASE + "maven.home";
87          System.setProperty( "maven.home", fakeHome );
88  
89          MavenJellyContext context = MavenUtils.createContext( new File( basedir ) );
90          assertNotNull( context );
91  
92          // 2) need to fake out maven home local
93          String fakeHomeLocal = basedir + FAKE_BASE + "maven.home.local";
94          new File( fakeHomeLocal ).mkdirs();
95          context.setVariable( MavenConstants.MAVEN_HOME_LOCAL, fakeHomeLocal );
96  
97          // 3) Need to fake out unpacked plugins dir so that the plugins are
98          //    unpacked locally in the test directory
99          String fakeUnpackedPlugins = basedir + FAKE_BASE + "maven.plugin.unpacked.dir";
100         new File( fakeUnpackedPlugins ).mkdirs();
101         context.setVariable( MavenConstants.MAVEN_UNPACKED_PLUGINS_DIR, fakeUnpackedPlugins );
102 
103         // fake forehead up - must be done before MavenSession creation
104         FileInputStream fis = new FileInputStream( basedir + SOURCE_BASE + "forehead.conf" );
105         Forehead.getInstance().config( fis );
106         fis.close();
107 
108         // setup maven session
109         MavenSession mavenSession = new MavenSession();
110         mavenSession.setRootContext( context );
111         File projectFile = new File( basedir + "/src/test/touchstone-build/project.xml" );
112 
113         // set the project being run
114         MavenSession.setRootDescriptorFile( projectFile );
115 
116         project = MavenUtils.getProject( projectFile, context );
117 
118         // This seems like a glitch in the context assembly that doesn't affect the real world, so work around it
119         project.getContext().setMavenRepoLocal( fakeHomeLocal + "/repository" );
120 
121         assertNotNull( project );
122 
123         pluginManager = mavenSession.getPluginManager();
124     }
125 
126     /**
127      * Delete the fake directory
128      * @throws Exception when any error occurs
129      * @todo uncomment
130      */
131     public void tearDown()
132         throws Exception
133     {
134         //        try
135         //        {
136         //            // clean up directories
137         //            String fake = basedir + FAKE_BASE;
138         //            FileUtils.deleteDirectory(fake);
139         //        }
140         //        catch (IOException e)
141         //        {
142         //            // ignore
143         //        }
144     }
145 
146     /**
147      * Make sure the plugin manager has loaded the plugins from the directory specified
148      * @throws Exception when any error occurs
149      */
150     public void testLoading()
151         throws Exception
152     {
153         assertNotNull( pluginManager );
154         pluginManager.initialize();
155         assertTrue( "clean plugin is not loaded properly", pluginManager.getGoalNames().contains( "clean" ) );
156         assertTrue( "clean plugin is not loaded properly", pluginManager.getGoalNames().contains( "clean:clean" ) );
157         assertTrue( "clean plugin is not loaded properly", pluginManager.getGoalNames().contains( "clean:original" ) );
158     }
159 
160     /**
161      * Make sure the plugin manager can install a plugin
162      * @throws Exception when any error occurs
163      */
164     public void testInstall()
165         throws Exception
166     {
167         installPlugin( "maven-java-plugin-1.3.jar" );
168 
169         assertTrue( "java plugin is not loaded properly", pluginManager.getGoalNames().contains( "java:compile" ) );
170     }
171 
172     /**
173      * Make sure the plugin manager can install a plugin twice without bad
174      * effects.
175      * @throws Exception when any error occurs
176      */
177     public void testInstallTwice()
178         throws Exception
179     {
180         installPlugin( "maven-java-plugin-1.3.jar" );
181 
182         assertTrue( "java plugin is not loaded properly", pluginManager.getGoalNames().contains( "java:compile" ) );
183 
184         installPlugin( "maven-java-plugin-1.3.jar" );
185 
186         assertTrue( "java plugin is not loaded properly", pluginManager.getGoalNames().contains( "java:compile" ) );
187     }
188 
189     /**
190      * Make sure the plugin manager can upgrade a plugin
191      * @throws Exception when any error occurs
192      * @todo what is a valid way to check the correct plugin will be used?
193      */
194     public void testUpgrade()
195         throws Exception
196     {
197         installPlugin( "maven-clean-plugin-1.2-SNAPSHOT.jar" );
198 
199         assertTrue( "upgraded clean plugin is not loaded properly", pluginManager.getGoalNames()
200             .contains( "clean:other" ) );
201         //assertFalse("original clean plugin is not removed properly",
202         //pluginManager.getGoalNames().contains("clean:original"));
203     }
204 
205     /**
206      * Make sure the plugin manager can downgrade a plugin
207      * @throws Exception when any error occurs
208      * @todo what is a valid way to check the correct plugin will be used?
209      */
210     public void testDowngrade()
211         throws Exception
212     {
213         installPlugin( "maven-clean-plugin-1.0.jar" );
214 
215         assertTrue( "downgraded clean plugin is not loaded properly", pluginManager.getGoalNames()
216             .contains( "clean:first" ) );
217         //assertFalse("original clean plugin is not removed properly",
218         //pluginManager.getGoalNames().contains("clean:original"));
219     }
220 
221     /**
222      * install a plugin
223      * @param name the file name of the plugin to install relative to the source base
224      * @throws Exception when any error occurs
225      */
226     private void installPlugin( String name )
227         throws Exception
228     {
229         assertNotNull( pluginManager );
230         pluginManager.initialize();
231         File pluginToInstall = new File( basedir + SOURCE_BASE + name );
232         pluginManager.installPlugin( pluginToInstall, project );
233     }
234 
235     /**
236      * delete a plugin
237      * @param name the file name of the plugin to install relative to the source base
238      * @throws Exception when any error occurs
239      */
240     private void deletePlugin( String name )
241         throws Exception
242     {
243         File plugin = new File( fakeHome + "/plugins/" + name );
244         if ( plugin.exists() )
245         {
246             plugin.delete();
247         }
248     }
249 
250     /**
251      *  Test extracted plugins are loaded from maven.home - for the bootstrap.
252      *  @throws Exception on failure
253      */
254     public void testExtractedPluginInMavenHome()
255         throws Exception
256     {
257         assertNotNull( pluginManager );
258         pluginManager.initialize();
259 
260         assertTrue( "check extracted jar plugin is loaded", pluginManager.getGoalNames().contains( "jar:jar" ) );
261     }
262 
263     /**
264      * Test plugin property substitution.
265      */
266     public void testPluginPropertySubstitution()
267         throws Exception
268     {
269         assertNotNull( pluginManager );
270         pluginManager.initialize();
271         pluginManager.setupBaseContext( project );
272 
273         MavenJellyContext context = pluginManager.getPluginContext( "maven-jar-plugin" );
274         assertNotNull( "check context exists", context );
275 
276         assertEquals( "check plugin.resources", fakeHome + FS + "plugins" + FS + "jar" + FS + "plugin-resources",
277                       ( (File) context.getVariable( "plugin.resources" ) ).getAbsolutePath() );
278         assertEquals( "check plugin property",
279                       fakeHome + FS + "plugins" + FS + "jar" + FS + "plugin-resources/foo.bar", context
280                           .getVariable( "maven.jar.foo" ) );
281     }
282 }