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 java.io.File;
23 import java.io.IOException;
24 import java.util.Locale;
25
26 import org.apache.maven.plugin.LegacySupport;
27 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
28 import org.apache.maven.plugin.testing.ArtifactStubFactory;
29 import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
30 import org.apache.maven.project.DefaultProjectBuildingRequest;
31 import org.apache.maven.project.MavenProject;
32 import org.apache.maven.project.ProjectBuilder;
33 import org.apache.maven.project.ProjectBuildingRequest;
34 import org.apache.maven.report.projectinfo.stubs.DependencyArtifactStubFactory;
35 import org.codehaus.plexus.i18n.I18N;
36 import org.codehaus.plexus.util.StringUtils;
37 import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
38 import org.sonatype.aether.util.DefaultRepositorySystemSession;
39
40
41
42
43
44
45
46
47 public abstract class AbstractProjectInfoTestCase
48 extends AbstractMojoTestCase
49 {
50 private ArtifactStubFactory artifactStubFactory;
51
52
53
54
55 protected static final Locale DEFAULT_LOCALE = Locale.ENGLISH;
56
57
58
59
60 private MavenProject testMavenProject;
61
62
63
64
65 private I18N i18n;
66
67 @Override
68 protected void setUp()
69 throws Exception
70 {
71
72 super.setUp();
73
74 i18n = getContainer().lookup( I18N.class );
75 setVariableValueToObject( i18n, "defaultBundleName", "project-info-reports" );
76
77 artifactStubFactory = new DependencyArtifactStubFactory( getTestFile( "target" ), true, false );
78 artifactStubFactory.getWorkingDir().mkdirs();
79
80
81 Locale.setDefault( DEFAULT_LOCALE );
82 }
83
84 @Override
85 protected void tearDown()
86 throws Exception
87 {
88 super.tearDown();
89 }
90
91
92
93
94
95
96
97 protected String getString( String key )
98 {
99 if ( StringUtils.isEmpty( key ) )
100 {
101 throw new IllegalArgumentException( "The key cannot be empty" );
102 }
103
104 return i18n.getString( key, Locale.getDefault() ).trim();
105 }
106
107
108
109
110
111
112
113
114
115 protected String prepareTitle( String name, String title )
116 {
117 if ( StringUtils.isEmpty( name ) )
118 {
119 throw new IllegalArgumentException( "The name cannot be empty" );
120 }
121
122 if ( StringUtils.isEmpty( title ) )
123 {
124 throw new IllegalArgumentException( "The title cannot be empty" );
125 }
126
127 return String.format( "%s \u2013 %s", name, title );
128 }
129
130
131
132
133
134
135 protected MavenProject getTestMavenProject()
136 {
137 return testMavenProject;
138 }
139
140
141
142
143
144
145
146
147 protected File getGeneratedReport( String name )
148 throws IOException
149 {
150 String outputDirectory = getBasedir() + "/target/test-harness/" + getTestMavenProject().getArtifactId();
151
152 File report = new File( outputDirectory, name );
153 if ( !report.exists() )
154 {
155 throw new IOException( "File not found. Attempted :" + report );
156 }
157
158 return report;
159 }
160
161
162
163
164
165
166
167
168
169 protected File generateReport( String goal, String pluginXml )
170 throws Exception
171 {
172 File pluginXmlFile = new File( getBasedir(), "src/test/resources/plugin-configs/" + pluginXml );
173 AbstractProjectInfoReport mojo = createReportMojo( goal, pluginXmlFile );
174 return generateReport( mojo, pluginXmlFile);
175 }
176
177 protected AbstractProjectInfoReport createReportMojo( String goal, File pluginXmlFile )
178 throws Exception
179 {
180 AbstractProjectInfoReport mojo = (AbstractProjectInfoReport) lookupMojo( goal, pluginXmlFile );
181 assertNotNull( "Mojo found.", mojo );
182
183 LegacySupport legacySupport = lookup( LegacySupport.class );
184 legacySupport.setSession( newMavenSession( new MavenProjectStub() ) );
185 DefaultRepositorySystemSession repoSession =
186 (DefaultRepositorySystemSession) legacySupport.getRepositorySession();
187 repoSession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( artifactStubFactory.getWorkingDir() ) );
188
189 setVariableValueToObject( mojo, "session", legacySupport.getSession() );
190
191 setVariableValueToObject( mojo, "remoteRepositories", mojo.getProject().getRemoteArtifactRepositories() );
192 return mojo;
193 }
194
195 protected File generateReport( AbstractProjectInfoReport mojo, File pluginXmlFile )
196 throws Exception
197 {
198 mojo.execute();
199
200 ProjectBuilder builder = lookup( ProjectBuilder.class );
201
202 ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
203 buildingRequest.setRepositorySession( null );
204
205 assertNotNull( "Local repository", mojo.localRepository );
206 testMavenProject = builder.build( pluginXmlFile, buildingRequest ).getProject();
207
208 File outputDir = mojo.getReportOutputDirectory();
209 String filename = mojo.getOutputName() + ".html";
210
211 return new File( outputDir, filename );
212 }
213
214
215 }