1 package org.apache.maven.report.projectinfo;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
23 import org.apache.maven.profiles.DefaultProfileManager;
24 import org.apache.maven.profiles.ProfileManager;
25 import org.apache.maven.project.MavenProject;
26 import org.apache.maven.project.MavenProjectBuilder;
27 import org.codehaus.plexus.i18n.I18N;
28 import org.codehaus.plexus.util.StringUtils;
29
30 import java.io.File;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.util.Locale;
34
35
36
37
38
39
40
41
42 public abstract class AbstractProjectInfoTestCase
43 extends AbstractMojoTestCase
44 {
45
46
47
48 protected static final Locale DEFAULT_LOCALE = Locale.ENGLISH;
49
50
51
52
53 private MavenProject testMavenProject;
54
55
56
57
58 private I18N i18n;
59
60 @Override
61 protected void setUp()
62 throws Exception
63 {
64
65 super.setUp();
66
67 i18n = (I18N) getContainer().lookup( I18N.ROLE );
68
69 File f = new File( getBasedir(), "target/local-repo/" );
70 f.mkdirs();
71
72
73 Locale.setDefault( DEFAULT_LOCALE );
74 }
75
76 @Override
77 protected InputStream getCustomConfiguration()
78 throws Exception
79 {
80
81 if ( super.getConfiguration() == null )
82 {
83 String className = AbstractProjectInfoTestCase.class.getName();
84
85 String config = className.substring( className.lastIndexOf( "." ) + 1 ) + ".xml";
86
87 return AbstractProjectInfoTestCase.class.getResourceAsStream( config );
88 }
89
90 return null;
91 }
92
93 @Override
94 protected void tearDown()
95 throws Exception
96 {
97 super.tearDown();
98 }
99
100
101
102
103
104
105
106 protected String getString( String key )
107 {
108 if ( StringUtils.isEmpty( key ) )
109 {
110 throw new IllegalArgumentException( "The key cannot be empty" );
111 }
112
113 return i18n.getString( key, Locale.getDefault() ).trim();
114 }
115
116
117
118
119
120
121 protected MavenProject getTestMavenProject()
122 {
123 return testMavenProject;
124 }
125
126
127
128
129
130
131
132
133 protected File getGeneratedReport( String name )
134 throws IOException
135 {
136 String outputDirectory = getBasedir() + "/target/test-harness/" + getTestMavenProject().getArtifactId();
137
138 File report = new File( outputDirectory, name );
139 if ( !report.exists() )
140 {
141 throw new IOException( "File not found. Attempted :" + report );
142 }
143
144 return report;
145 }
146
147
148
149
150
151
152
153
154
155 protected File generateReport( String goal, String pluginXml )
156 throws Exception
157 {
158 File pluginXmlFile = new File( getBasedir(), "src/test/resources/plugin-configs/" + pluginXml );
159 AbstractProjectInfoReport mojo = createReportMojo( goal, pluginXmlFile );
160 return generateReport( mojo, pluginXmlFile);
161 }
162
163 protected AbstractProjectInfoReport createReportMojo( String goal, File pluginXmlFile )
164 throws Exception
165 {
166 AbstractProjectInfoReport mojo = (AbstractProjectInfoReport) lookupMojo( goal, pluginXmlFile );
167 assertNotNull( "Mojo found.", mojo );
168
169 setVariableValueToObject( mojo, "remoteRepositories", mojo.project.getRemoteArtifactRepositories() );
170 return mojo;
171 }
172
173 protected File generateReport( AbstractProjectInfoReport mojo, File pluginXmlFile )
174 throws Exception
175 {
176 mojo.execute();
177
178 MavenProjectBuilder builder = (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE );
179 ProfileManager profileManager = new DefaultProfileManager( getContainer(), null, null );
180
181 testMavenProject = builder.buildWithDependencies( pluginXmlFile, mojo.localRepository, profileManager );
182
183 File outputDir = mojo.getReportOutputDirectory();
184 String filename = mojo.getOutputName() + ".html";
185
186 return new File( outputDir, filename );
187 }
188
189
190 }