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