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 protected InputStream getCustomConfiguration()
77 throws Exception
78 {
79
80 if ( super.getConfiguration() == null )
81 {
82 String className = AbstractProjectInfoTestCase.class.getName();
83
84 String config = className.substring( className.lastIndexOf( "." ) + 1 ) + ".xml";
85
86 return AbstractProjectInfoTestCase.class.getResourceAsStream( config );
87 }
88
89 return null;
90 }
91
92 @Override
93 protected void tearDown()
94 throws Exception
95 {
96 super.tearDown();
97 }
98
99
100
101
102
103
104
105 protected String getString( String key )
106 {
107 if ( StringUtils.isEmpty( key ) )
108 {
109 throw new IllegalArgumentException( "The key cannot be empty" );
110 }
111
112 return i18n.getString( key, Locale.getDefault() ).trim();
113 }
114
115
116
117
118
119
120
121
122
123 protected String prepareTitle( String name, String title )
124 {
125 if ( StringUtils.isEmpty( name ) )
126 {
127 throw new IllegalArgumentException( "The name cannot be empty" );
128 }
129
130 if ( StringUtils.isEmpty( title ) )
131 {
132 throw new IllegalArgumentException( "The title cannot be empty" );
133 }
134
135 return String.format( "%s \u2013 %s", name, title );
136 }
137
138
139
140
141
142
143 protected MavenProject getTestMavenProject()
144 {
145 return testMavenProject;
146 }
147
148
149
150
151
152
153
154
155 protected File getGeneratedReport( String name )
156 throws IOException
157 {
158 String outputDirectory = getBasedir() + "/target/test-harness/" + getTestMavenProject().getArtifactId();
159
160 File report = new File( outputDirectory, name );
161 if ( !report.exists() )
162 {
163 throw new IOException( "File not found. Attempted :" + report );
164 }
165
166 return report;
167 }
168
169
170
171
172
173
174
175
176
177 protected File generateReport( String goal, String pluginXml )
178 throws Exception
179 {
180 File pluginXmlFile = new File( getBasedir(), "src/test/resources/plugin-configs/" + pluginXml );
181 AbstractProjectInfoReport mojo = createReportMojo( goal, pluginXmlFile );
182 return generateReport( mojo, pluginXmlFile);
183 }
184
185 protected AbstractProjectInfoReport createReportMojo( String goal, File pluginXmlFile )
186 throws Exception
187 {
188 AbstractProjectInfoReport mojo = (AbstractProjectInfoReport) lookupMojo( goal, pluginXmlFile );
189 assertNotNull( "Mojo found.", mojo );
190
191 setVariableValueToObject( mojo, "remoteRepositories", mojo.project.getRemoteArtifactRepositories() );
192 return mojo;
193 }
194
195 protected File generateReport( AbstractProjectInfoReport mojo, File pluginXmlFile )
196 throws Exception
197 {
198 mojo.execute();
199
200 MavenProjectBuilder builder = (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE );
201 ProfileManager profileManager = new DefaultProfileManager( getContainer(), null, null );
202
203 assertNotNull( "Local repository", mojo.localRepository );
204 testMavenProject = builder.buildWithDependencies( pluginXmlFile, mojo.localRepository, profileManager );
205
206 File outputDir = mojo.getReportOutputDirectory();
207 String filename = mojo.getOutputName() + ".html";
208
209 return new File( outputDir, filename );
210 }
211
212
213 }