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