View Javadoc
1   package org.apache.maven.plugins.repository.it.support;
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 static org.codehaus.plexus.util.IOUtil.close;
23  
24  import org.apache.maven.it.VerificationException;
25  import org.apache.maven.it.Verifier;
26  import org.codehaus.plexus.util.xml.Xpp3Dom;
27  import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
28  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
29  
30  import java.io.File;
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.io.InputStreamReader;
34  import java.net.URI;
35  import java.net.URISyntaxException;
36  import java.net.URL;
37  
38  public class IntegrationTestUtils
39  {
40  
41      private static String cliPluginPrefix;
42  
43      private static String pluginVersion;
44  
45      private static String pluginGroupId;
46  
47      private static String pluginArtifactId;
48      
49      private static boolean installed = false;
50      
51      public static void bootstrap()
52          throws VerificationException, IOException, URISyntaxException
53      {
54          if ( !installed )
55          {
56              File bootstrapDir = getTestDir( "bootstrap" );
57              
58              Verifier verifier = new Verifier( bootstrapDir.getAbsolutePath() );
59              
60              verifier.executeGoal( "install" );
61              
62              verifier.verifyErrorFreeLog();
63              verifier.resetStreams();
64              
65              installed = true;
66          }
67      }
68  
69      public static File getTestDir( final String name )
70          throws IOException, URISyntaxException
71      {
72          ClassLoader cloader = Thread.currentThread().getContextClassLoader();
73          URL resource = cloader.getResource( name );
74  
75          if ( resource == null )
76          {
77              throw new IOException( "Cannot find test directory: " + name );
78          }
79  
80          return new File( new URI( resource.toExternalForm() ).normalize().getPath() );
81      }
82  
83      public static File getBaseDir()
84      {
85          File result = new File( System.getProperty( "basedir", "." ) );
86          try
87          {
88              return result.getCanonicalFile();
89          }
90          catch ( IOException e )
91          {
92              return result.getAbsoluteFile();
93          }
94      }
95  
96      public static String getPluginVersion()
97          throws IOException
98      {
99          if ( pluginVersion == null )
100         {
101             initPluginInfo();
102         }
103 
104         return pluginVersion;
105     }
106 
107     public static String getPluginGroupId()
108         throws IOException
109     {
110         if ( pluginGroupId == null )
111         {
112             initPluginInfo();
113         }
114 
115         return pluginGroupId;
116     }
117 
118     public static String getPluginArtifactId()
119         throws IOException
120     {
121         if ( pluginArtifactId == null )
122         {
123             initPluginInfo();
124         }
125 
126         return pluginArtifactId;
127     }
128 
129     public static String getCliPluginPrefix()
130         throws IOException
131     {
132         if ( cliPluginPrefix == null )
133         {
134             initPluginInfo();
135         }
136 
137         return cliPluginPrefix;
138     }
139 
140     private static void initPluginInfo()
141         throws IOException
142     {
143         URL resource = Thread.currentThread().getContextClassLoader().getResource( "META-INF/maven/plugin.xml" );
144 
145         InputStream stream = null;
146         try
147         {
148             stream = resource.openStream();
149             Xpp3Dom pluginDom;
150             try
151             {
152                 pluginDom = Xpp3DomBuilder.build( new InputStreamReader( stream ) );
153             }
154             catch ( XmlPullParserException e )
155             {
156                 IOException err = new IOException(
157                                                    "Failed to parse plugin descriptor for groupId:artifactId:version prefix. Reason: "
158                                                        + e.getMessage() );
159                 err.initCause( e );
160 
161                 throw err;
162             }
163 
164             pluginArtifactId = pluginDom.getChild( "artifactId" ).getValue();
165             pluginGroupId = pluginDom.getChild( "groupId" ).getValue();
166             pluginVersion = pluginDom.getChild( "version" ).getValue();
167 
168             cliPluginPrefix = pluginGroupId + ":" + pluginArtifactId + ":" + pluginVersion + ":";
169         }
170         finally
171         {
172             close( stream );
173         }
174     }
175 }