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