24
24
* SOFTWARE.
25
25
*/
26
26
27
+ import static org .assertj .core .api .Assertions .assertThat ;
28
+
27
29
import org .apache .maven .artifact .Artifact ;
28
30
import org .apache .maven .artifact .DefaultArtifact ;
29
31
import org .apache .maven .artifact .handler .DefaultArtifactHandler ;
30
- import org .apache .maven .artifact .test .ArtifactTestCase ;
32
+ import org .apache .maven .artifact .repository .ArtifactRepository ;
33
+ import org .apache .maven .artifact .repository .DefaultArtifactRepository ;
34
+ import org .apache .maven .artifact .repository .layout .ArtifactRepositoryLayout ;
31
35
import org .apache .maven .artifact .versioning .VersionRange ;
32
-
36
+ import org .apache .maven .settings .Settings ;
37
+ import org .apache .maven .settings .io .xpp3 .SettingsXpp3Reader ;
38
+ import org .codehaus .plexus .testing .PlexusTest ;
33
39
import org .codehaus .plexus .util .FileUtils ;
40
+ import org .codehaus .plexus .util .ReaderFactory ;
34
41
import org .codehaus .plexus .util .StringUtils ;
42
+ import org .junit .jupiter .api .BeforeEach ;
43
+ import org .junit .jupiter .api .Test ;
35
44
36
45
import java .io .File ;
37
46
import java .util .ArrayList ;
38
47
import java .util .Collection ;
39
48
import java .util .Collections ;
40
49
import java .util .Iterator ;
41
50
import java .util .List ;
51
+ import java .util .Map ;
42
52
import java .util .TreeSet ;
43
53
54
+ import javax .inject .Inject ;
55
+
44
56
/**
45
57
*
46
58
*/
59
+ @ PlexusTest
47
60
public abstract class AbstractCompilerTest
48
- extends ArtifactTestCase
49
61
{
50
62
private boolean compilerDebug = false ;
51
63
52
64
private boolean compilerDeprecationWarnings = false ;
53
65
54
66
private boolean forceJavacCompilerUse = false ;
55
-
67
+
68
+ @ Inject
69
+ private Map <String , Compiler > compilers ;
70
+
71
+ @ Inject
72
+ private ArtifactRepositoryLayout repositoryLayout ;
73
+
74
+ private ArtifactRepository localRepository ;
75
+
56
76
protected abstract String getRoleHint ();
57
77
78
+ @ BeforeEach
79
+ final void setUpLocalRepo ()
80
+ throws Exception
81
+ {
82
+ String localRepo = System .getProperty ( "maven.repo.local" );
83
+
84
+ if ( localRepo == null )
85
+ {
86
+ File settingsFile = new File ( System .getProperty ( "user.home" ), ".m2/settings.xml" );
87
+ if ( settingsFile .exists () )
88
+ {
89
+ Settings settings = new SettingsXpp3Reader ().read ( ReaderFactory .newXmlReader ( settingsFile ) );
90
+ localRepo = settings .getLocalRepository ();
91
+ }
92
+ }
93
+ if ( localRepo == null )
94
+ {
95
+ localRepo = System .getProperty ( "user.home" ) + "/.m2/repository" ;
96
+ }
97
+
98
+ localRepository = new DefaultArtifactRepository ( "local" , "file://" + localRepo , repositoryLayout );
99
+ }
100
+
58
101
protected void setCompilerDebug ( boolean flag )
59
102
{
60
103
compilerDebug = flag ;
@@ -69,6 +112,11 @@ public void setForceJavacCompilerUse( boolean forceJavacCompilerUse )
69
112
{
70
113
this .forceJavacCompilerUse = forceJavacCompilerUse ;
71
114
}
115
+
116
+ protected final Compiler getCompiler ()
117
+ {
118
+ return compilers .get ( getRoleHint () );
119
+ }
72
120
73
121
protected List <String > getClasspath ()
74
122
throws Exception
@@ -77,8 +125,8 @@ protected List<String> getClasspath()
77
125
78
126
File file = getLocalArtifactPath ( "commons-lang" , "commons-lang" , "2.0" , "jar" );
79
127
80
- assertTrue ( "test prerequisite: commons-lang library must be available in local repository, expected "
81
- + file .getAbsolutePath (), file . canRead () );
128
+ assertThat ( file . canRead () ). as ( "test prerequisite: commons-lang library must be available in local repository, expected "
129
+ + file .getAbsolutePath () ). isTrue ( );
82
130
83
131
cp .add ( file .getAbsolutePath () );
84
132
@@ -90,6 +138,7 @@ protected void configureCompilerConfig( CompilerConfiguration compilerConfig )
90
138
91
139
}
92
140
141
+ @ Test
93
142
public void testCompilingSources ()
94
143
throws Exception
95
144
{
@@ -100,9 +149,7 @@ public void testCompilingSources()
100
149
{
101
150
File outputDir = new File ( compilerConfig .getOutputLocation () );
102
151
103
- Compiler compiler = (Compiler ) lookup ( Compiler .ROLE , getRoleHint () );
104
-
105
- messages .addAll ( compiler .performCompile ( compilerConfig ).getCompilerMessages () );
152
+ messages .addAll ( getCompiler ().performCompile ( compilerConfig ).getCompilerMessages () );
106
153
107
154
if ( outputDir .isDirectory () )
108
155
{
@@ -134,8 +181,8 @@ public void testCompilingSources()
134
181
errors .add ( error .getMessage () );
135
182
}
136
183
137
- assertEquals ( "Wrong number of compilation errors (" + numCompilerErrors + "/" + expectedErrors //
138
- + ") : " + displayLines ( errors ), expectedErrors , numCompilerErrors );
184
+ assertThat ( numCompilerErrors ). as ( "Wrong number of compilation errors (" + numCompilerErrors + "/" + expectedErrors //
185
+ + ") : " + displayLines ( errors ) ). isEqualTo ( expectedErrors );
139
186
}
140
187
141
188
int expectedWarnings = expectedWarnings ();
@@ -157,12 +204,12 @@ public void testCompilingSources()
157
204
warnings .add ( error .getMessage () );
158
205
}
159
206
160
- assertEquals ( "Wrong number (" + numCompilerWarnings + "/ "
161
- + expectedWarnings + ") of compilation warnings: " + displayLines ( warnings ), //
162
- expectedWarnings , numCompilerWarnings );
207
+ assertThat ( numCompilerWarnings ). as ( "Wrong number ("
208
+ + numCompilerWarnings + "/" + expectedWarnings + ") of compilation warnings: "
209
+ + displayLines ( warnings ) ). isEqualTo ( expectedWarnings );
163
210
}
164
211
165
- assertEquals ( new TreeSet <>( normalizePaths ( expectedOutputFiles () ) ), files );
212
+ assertThat ( files ). isEqualTo ( new TreeSet <>( normalizePaths ( expectedOutputFiles () ) ) );
166
213
}
167
214
168
215
protected String displayLines ( List <String > warnings )
@@ -179,7 +226,7 @@ protected String displayLines( List<String> warnings)
179
226
private List <CompilerConfiguration > getCompilerConfigurations ()
180
227
throws Exception
181
228
{
182
- String sourceDir = getBasedir () + "/ src/test-input/src/main" ;
229
+ String sourceDir = " src/test-input/src/main" ;
183
230
184
231
List <String > filenames =
185
232
FileUtils .getFileNames ( new File ( sourceDir ), "**/*.java" , null , false , true );
@@ -202,7 +249,7 @@ private List<CompilerConfiguration> getCompilerConfigurations()
202
249
203
250
compilerConfig .addSourceLocation ( sourceDir );
204
251
205
- compilerConfig .setOutputLocation ( getBasedir () + "/ target/" + getRoleHint () + "/classes-" + index );
252
+ compilerConfig .setOutputLocation ( " target/" + getRoleHint () + "/classes-" + index );
206
253
207
254
FileUtils .deleteDirectory ( compilerConfig .getOutputLocation () );
208
255
@@ -315,4 +362,8 @@ protected String getJavaVersion()
315
362
return javaVersion ;
316
363
}
317
364
365
+ protected File getLocalArtifactPath ( Artifact artifact )
366
+ {
367
+ return new File ( localRepository .getBasedir (), localRepository .pathOf ( artifact ) );
368
+ }
318
369
}
0 commit comments