1 package com.simpligility.maven.plugins.android.standalonemojos;
2
3 import java.io.File;
4 import java.lang.reflect.InvocationHandler;
5 import java.lang.reflect.Method;
6 import java.util.ArrayList;
7 import java.util.List;
8
9 import org.apache.commons.io.FilenameUtils;
10 import org.apache.maven.model.Build;
11 import org.apache.maven.plugin.logging.Log;
12 import org.apache.maven.project.MavenProject;
13 import org.easymock.Capture;
14 import org.easymock.EasyMock;
15 import org.junit.Ignore;
16 import org.junit.runner.RunWith;
17 import org.powermock.api.easymock.PowerMock;
18 import org.powermock.core.classloader.annotations.PrepareForTest;
19 import org.powermock.modules.junit4.PowerMockRunner;
20 import org.powermock.reflect.Whitebox;
21
22 import com.simpligility.maven.plugins.android.AbstractAndroidMojoTestCase;
23 import com.simpligility.maven.plugins.android.AndroidSdk;
24 import com.simpligility.maven.plugins.android.CommandExecutor;
25 import com.simpligility.maven.plugins.android.SdkTestSupport;
26 import com.simpligility.maven.plugins.android.config.ConfigHandler;
27 import com.simpligility.maven.plugins.android.standalonemojos.LintMojo;
28
29
30
31
32
33
34
35 @Ignore("This test has to be migrated to be an IntegrationTest using AbstractAndroidMojoIntegrationTest")
36 @RunWith( PowerMockRunner.class )
37 @PrepareForTest(
38 { CommandExecutor.Factory.class, ConfigHandler.class } )
39 public class LintMojoTest extends AbstractAndroidMojoTestCase< LintMojo >
40 {
41 @Override
42 public String getPluginGoalName()
43 {
44 return "lint";
45 }
46
47
48
49
50
51
52 public void testDefaultLintConfig() throws Exception
53 {
54 LintMojo mojo = createMojo( "lint-config-project0" );
55 final ConfigHandler cfh = new ConfigHandler( mojo, this.session, this.execution );
56 cfh.parseConfiguration();
57
58 Boolean lintSkip = Whitebox.getInternalState( mojo, "parsedSkip" );
59
60 assertTrue( "lint skip parameter should be true", lintSkip );
61 }
62
63
64
65
66
67
68 public void testDefaultUnskippedLintConfig() throws Exception
69 {
70 LintMojo mojo = createMojo( "lint-config-project1" );
71 final ConfigHandler cfh = new ConfigHandler( mojo, this.session, this.execution );
72 cfh.parseConfiguration();
73 MavenProject project = Whitebox.getInternalState( mojo, "project" );
74
75 Boolean lintSkip = Whitebox.getInternalState( mojo, "parsedSkip" );
76 Boolean lintFailOnError = Whitebox.getInternalState( mojo, "parsedFailOnError" );
77 Boolean lintIgnoreWarnings = Whitebox.getInternalState( mojo, "parsedIgnoreWarnings" );
78 Boolean lintWarnAll = Whitebox.getInternalState( mojo, "parsedWarnAll" );
79 Boolean lintWarningsAsErrors = Whitebox.getInternalState( mojo, "parsedWarningsAsErrors" );
80 String lintConfig = Whitebox.getInternalState( mojo, "parsedConfig" );
81
82 Boolean lintFullPath = Whitebox.getInternalState( mojo, "parsedFullPath" );
83 Boolean lintShowAll = Whitebox.getInternalState( mojo, "parsedShowAll" );
84 Boolean lintDisableSourceLines = Whitebox.getInternalState( mojo, "parsedDisableSourceLines" );
85 String lintUrl = Whitebox.getInternalState( mojo, "parsedUrl" );
86
87 Boolean lintEnableXml = Whitebox.getInternalState( mojo, "parsedEnableXml" );
88 String lintXmlOutputPath = Whitebox.getInternalState( mojo, "parsedXmlOutputPath" );
89 Boolean lintEnableHtml = Whitebox.getInternalState( mojo, "parsedEnableHtml" );
90 String lintHtmlOutputPath = Whitebox.getInternalState( mojo, "parsedHtmlOutputPath" );
91 Boolean lintEnableSimpleHtml = Whitebox.getInternalState( mojo, "parsedEnableSimpleHtml" );
92 String lintSimpleHtmlOutputPath = Whitebox.getInternalState( mojo, "parsedSimpleHtmlOutputPath" );
93
94 Boolean lintEnableSources = Whitebox.getInternalState( mojo, "parsedEnableSources" );
95 String lintSources = Whitebox.getInternalState( mojo, "parsedSources" );
96 Boolean lintEnableClasspath = Whitebox.getInternalState( mojo, "parsedEnableClasspath" );
97 String lintClasspath = Whitebox.getInternalState( mojo, "parsedClasspath" );
98 Boolean lintEnableLibraries = Whitebox.getInternalState( mojo, "parsedEnableLibraries" );
99 String lintLibraries = Whitebox.getInternalState( mojo, "parsedLibraries" );
100
101 assertFalse( "lint skip parameter should be false", lintSkip );
102 assertFalse( "lint failOnError parameter should be false", lintFailOnError );
103 assertFalse( "lint ignoreWarning parameter should be false", lintIgnoreWarnings );
104 assertFalse( "lint warnAll parameter should be false", lintWarnAll );
105 assertFalse( "lint warningsAsErrors parameter should be false", lintWarningsAsErrors );
106 assertEquals( "lint config parameter should be null", "null", lintConfig );
107
108 assertFalse( "lint fullPath parameter should be false", lintFullPath );
109 assertTrue( "lint showAll parameter should be true", lintShowAll );
110 assertFalse( "lint disableSourceLines parameter should be false", lintDisableSourceLines );
111 assertEquals( "lint url parameter should be none", "none", lintUrl );
112
113 assertTrue( "lint enableXml parameter should be true", lintEnableXml );
114 File lintXmlOutputFile = new File( project.getBuild().getDirectory(), "lint-results/lint-results.xml" );
115 assertEquals( "lint xmlOutputPath parameter should point to lint-results.xml", lintXmlOutputFile.getAbsolutePath(),
116 lintXmlOutputPath );
117 assertFalse( "lint enableHtml parameter should be false", lintEnableHtml );
118 File lintHtmlOutputFile = new File( project.getBuild().getDirectory(), "lint-results/lint-results-html" );
119 assertEquals( "lint htmlOutputPath parameter should point to lint-html", lintHtmlOutputFile.getAbsolutePath(),
120 lintHtmlOutputPath );
121 assertFalse( "lint enableSimplHtml parameter should be false", lintEnableSimpleHtml );
122 File lintSimpleHtmlOutputFile = new File( project.getBuild().getDirectory(), "lint-results/lint-results-simple-html" );
123 assertEquals( "lint simpleHtmlOutputPath parameter should point to lint-simple-html",
124 lintSimpleHtmlOutputFile.getAbsolutePath(), lintSimpleHtmlOutputPath );
125
126 assertTrue( "lint enableSources parameter should be true", lintEnableSources );
127 assertEquals( "lint sources parameter should point to src/", project.getBuild().getSourceDirectory(),
128 lintSources );
129 assertFalse( "lint enableClasspath parameter should be false", lintEnableClasspath );
130 assertEquals( "lint classpath parameter should point to target/classes", project.getBuild()
131 .getOutputDirectory(), lintClasspath );
132 assertFalse( "lint enableLibraries parameter should be false", lintEnableLibraries );
133 assertNull( "lint libraries parameter should point not contain dependencies", lintLibraries );
134 }
135
136
137
138
139
140
141 public void testCustomLintConfig() throws Exception
142 {
143 LintMojo mojo = createMojo( "lint-config-project2" );
144 final ConfigHandler cfh = new ConfigHandler( mojo, this.session, this.execution );
145 cfh.parseConfiguration();
146
147 Boolean lintSkip = Whitebox.getInternalState( mojo, "parsedSkip" );
148 Boolean lintFailOnError = Whitebox.getInternalState( mojo, "parsedFailOnError" );
149 Boolean lintIgnoreWarnings = Whitebox.getInternalState( mojo, "parsedIgnoreWarnings" );
150 Boolean lintWarnAll = Whitebox.getInternalState( mojo, "parsedWarnAll" );
151 Boolean lintWarningsAsErrors = Whitebox.getInternalState( mojo, "parsedWarningsAsErrors" );
152 String lintConfig = Whitebox.getInternalState( mojo, "parsedConfig" );
153
154 Boolean lintFullPath = Whitebox.getInternalState( mojo, "parsedFullPath" );
155 Boolean lintShowAll = Whitebox.getInternalState( mojo, "parsedShowAll" );
156 Boolean lintDisableSourceLines = Whitebox.getInternalState( mojo, "parsedDisableSourceLines" );
157 String lintUrl = Whitebox.getInternalState( mojo, "parsedUrl" );
158
159 Boolean lintEnableXml = Whitebox.getInternalState( mojo, "parsedEnableXml" );
160 String lintXmlOutputPath = Whitebox.getInternalState( mojo, "parsedXmlOutputPath" );
161 Boolean lintEnableHtml = Whitebox.getInternalState( mojo, "parsedEnableHtml" );
162 String lintHtmlOutputPath = Whitebox.getInternalState( mojo, "parsedHtmlOutputPath" );
163 Boolean lintEnableSimpleHtml = Whitebox.getInternalState( mojo, "parsedEnableSimpleHtml" );
164 String lintSimpleHtmlOutputPath = Whitebox.getInternalState( mojo, "parsedSimpleHtmlOutputPath" );
165
166 Boolean lintEnableSources = Whitebox.getInternalState( mojo, "parsedEnableSources" );
167 String lintSources = Whitebox.getInternalState( mojo, "parsedSources" );
168 Boolean lintEnableClasspath = Whitebox.getInternalState( mojo, "parsedEnableClasspath" );
169 String lintClasspath = Whitebox.getInternalState( mojo, "parsedClasspath" );
170 Boolean lintEnableLibraries = Whitebox.getInternalState( mojo, "parsedEnableLibraries" );
171 String lintLibraries = Whitebox.getInternalState( mojo, "parsedLibraries" );
172
173 assertFalse( "lint skip parameter should be false", lintSkip );
174 assertTrue( "lint failOnError parameter should be true", lintFailOnError );
175 assertTrue( "lint ignoreWarning parameter should be true", lintIgnoreWarnings );
176 assertTrue( "lint warnAll parameter should be true", lintWarnAll );
177 assertTrue( "lint warningsAsErrors parameter should be true", lintWarningsAsErrors );
178 assertNotNull( "lint config parameter should be non null", lintConfig );
179 assertEquals( "lint config parameter should point to lint", "lint", lintConfig );
180
181 assertTrue( "lint fullPath parameter should be true", lintFullPath );
182 assertFalse( "lint showAll parameter should be false", lintShowAll );
183 assertTrue( "lint disableSourceLines parameter should be true", lintDisableSourceLines );
184 assertEquals( "lint url parameter should be url", "url", lintUrl );
185
186 assertFalse( "lint enableXml parameter should be false", lintEnableXml );
187 assertEquals( "lint xmlOutputPath parameter should point to xml", "xml", lintXmlOutputPath );
188 assertTrue( "lint enableHtml parameter should be true", lintEnableHtml );
189 assertEquals( "lint htmlOutputPath parameter should point to html", "html", lintHtmlOutputPath );
190 assertTrue( "lint enableSimplHtml parameter should be true", lintEnableSimpleHtml );
191 assertEquals( "lint simpleHtmlOutputPath parameter should point to simple", "simple", lintSimpleHtmlOutputPath );
192
193 assertFalse( "lint enableSources parameter should be false", lintEnableSources );
194 assertTrue( "lint enableClasspath parameter should be true", lintEnableClasspath );
195 assertTrue( "lint enableLibraries parameter should be true", lintEnableLibraries );
196
197 assertEquals( "lint sources parameter should point to src2", "src2", lintSources );
198 assertEquals( "lint classpath parameter should point to cla2", "cla2", lintClasspath );
199 assertEquals( "lint libraries parameter should point to lib2", "lib2", lintLibraries );
200
201 }
202
203 public void testAllLintCommandParametersWithDefaultUnskippedConfig() throws Exception
204 {
205 LintMojo mojo = createMojo( "lint-config-project1" );
206
207 MavenProject project = EasyMock.createNiceMock( MavenProject.class );
208 Whitebox.setInternalState( mojo, "project", project );
209 File projectBaseDir = new File( getBasedir() );
210 Build projectBuild = new Build();
211 projectBuild.setDirectory( "target/" );
212 projectBuild.setSourceDirectory( "src/" );
213 projectBuild.setOutputDirectory( "classes/" );
214 EasyMock.expect( project.getBasedir() ).andReturn( projectBaseDir ).anyTimes();
215 EasyMock.expect( project.getBuild() ).andReturn( projectBuild ).anyTimes();
216 final CommandExecutor mockExecutor = PowerMock.createMock( CommandExecutor.class );
217 PowerMock.replace( CommandExecutor.Factory.class.getDeclaredMethod( "createDefaultCommmandExecutor" ) ).with(
218 new InvocationHandler()
219 {
220
221 @Override
222 public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable
223 {
224 return mockExecutor;
225 }
226 } );
227
228 PowerMock.expectNew( ConfigHandler.class, mojo, this.session, this.execution )
229 .andReturn( EasyMock.createNiceMock( ConfigHandler.class ) );
230 Capture< List< String > > capturedArgument = new Capture< List< String > >();
231
232 mockExecutor.setLogger( EasyMock.anyObject( Log.class ) );
233 mockExecutor.executeCommand( EasyMock.anyObject( String.class ), EasyMock.capture( capturedArgument ),
234 EasyMock.eq( false ) );
235 PowerMock.replay( project );
236 PowerMock.replay( mockExecutor );
237
238 mojo.execute();
239
240 PowerMock.verify( mockExecutor );
241 List< String > parameters = capturedArgument.getValue();
242 List< String > parametersExpected = new ArrayList< String >();
243 parametersExpected.add( "--showall" );
244 parametersExpected.add( "--xml" );
245 parametersExpected.add( projectBaseDir.getAbsolutePath()
246 + FilenameUtils.separatorsToSystem( "/target/lint-results/lint-results.xml" ) );
247 parametersExpected.add( "--sources" );
248 parametersExpected.add( projectBaseDir.getAbsolutePath() + File.separator + "src" );
249 parametersExpected.add( projectBaseDir.getAbsolutePath() );
250 parametersExpected.add( "--exitcode" );
251 assertEquals( parametersExpected, parameters );
252 }
253
254 public void testAllLintCommandParametersWithCustomConfig() throws Exception
255 {
256 LintMojo mojo = createMojo( "lint-config-project2" );
257
258 MavenProject project = EasyMock.createNiceMock( MavenProject.class );
259 Whitebox.setInternalState( mojo, "project", project );
260 File projectBaseDir = new File( "project/" );
261 EasyMock.expect( project.getBasedir() ).andReturn( projectBaseDir );
262 final CommandExecutor mockExecutor = PowerMock.createMock( CommandExecutor.class );
263 PowerMock.replace( CommandExecutor.Factory.class.getDeclaredMethod( "createDefaultCommmandExecutor" ) )
264 .with( new InvocationHandler()
265 {
266
267 @Override
268 public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable
269 {
270 return mockExecutor;
271 }
272 } );
273
274 PowerMock.expectNew( ConfigHandler.class, mojo, this.session, this.execution )
275 .andReturn( EasyMock.createNiceMock( ConfigHandler.class ) );
276 Whitebox.setInternalState( mojo, "parsedSkip", Boolean.FALSE );
277 Capture< List< String > > capturedArgument = new Capture< List< String > >();
278
279 mockExecutor.setLogger( EasyMock.anyObject( Log.class ) );
280 mockExecutor.executeCommand( EasyMock.anyObject( String.class ), EasyMock.capture( capturedArgument ),
281 EasyMock.eq( false ) );
282 PowerMock.replay( project );
283 PowerMock.replay( mockExecutor );
284
285 mojo.execute();
286
287 PowerMock.verify( mockExecutor );
288 List< String > parameters = capturedArgument.getValue();
289 List< String > parametersExpected = new ArrayList< String >();
290 parametersExpected.add( "-w" );
291 parametersExpected.add( "-Wall" );
292 parametersExpected.add( "-Werror" );
293 parametersExpected.add( "--config" );
294 parametersExpected.add( "lint" );
295 parametersExpected.add( "--fullpath" );
296 parametersExpected.add( "--nolines" );
297 parametersExpected.add( "--html" );
298 parametersExpected.add( "html" );
299 parametersExpected.add( "--url" );
300 parametersExpected.add( "url" );
301 parametersExpected.add( "--simplehtml" );
302 parametersExpected.add( "simple" );
303 parametersExpected.add( "--classpath" );
304 parametersExpected.add( "cla2" );
305 parametersExpected.add( "--libraries" );
306 parametersExpected.add( "lib2" );
307 parametersExpected.add( projectBaseDir.getAbsolutePath() );
308 parametersExpected.add( "--exitcode" );
309 assertEquals( parametersExpected, parameters );
310 }
311
312 public void testAllParametersOffConfig() throws Exception
313 {
314 LintMojo mojo = new LintMojo() {
315 @Override
316 protected AndroidSdk getAndroidSdk() {
317 return new SdkTestSupport().getSdk_with_platform_default();
318 }
319 };
320 MavenProject project = EasyMock.createNiceMock( MavenProject.class );
321 Whitebox.setInternalState( mojo, "project", project );
322 File projectBaseDir = new File( "project/" );
323 EasyMock.expect( project.getBasedir() ).andReturn( projectBaseDir );
324
325 final CommandExecutor mockExecutor = PowerMock.createMock( CommandExecutor.class );
326 PowerMock.replace( CommandExecutor.Factory.class.getDeclaredMethod( "createDefaultCommmandExecutor" ) ).with(
327 new InvocationHandler()
328 {
329
330 @Override
331 public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable
332 {
333 return mockExecutor;
334 }
335 } );
336
337 Capture< List< String > > capturedArgument = new Capture< List< String > >();
338
339 mockExecutor.setLogger( EasyMock.anyObject( Log.class ) );
340 mockExecutor.executeCommand( EasyMock.anyObject( String.class ), EasyMock.capture( capturedArgument ),
341 EasyMock.eq( false ) );
342 PowerMock.replay( mockExecutor );
343 PowerMock.replay( project );
344 Whitebox.setInternalState( mojo, "parsedConfig", "null" );
345 Whitebox.setInternalState( mojo, "parsedClasspath", "null" );
346 Whitebox.setInternalState( mojo, "parsedLibraries", "null" );
347 Whitebox.invokeMethod( mojo, "executeWhenConfigured" );
348
349 PowerMock.verify( mockExecutor );
350 PowerMock.verify( project );
351 List< String > parameters = capturedArgument.getValue();
352 List< String > parametersExpected = new ArrayList< String >();
353 parametersExpected.add( projectBaseDir.getAbsolutePath() );
354 parametersExpected.add( "--exitcode" );
355 assertEquals( parametersExpected, parameters );
356 }
357 }