1 package org.apache.maven.plugins.repository.it.support;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 }