View Javadoc
1   package com.simpligility.maven.plugins.android.standalonemojos;
2   
3   import java.util.ArrayList;
4   import java.util.Arrays;
5   import java.util.List;
6   
7   import org.junit.Ignore;
8   import org.junit.runner.RunWith;
9   import org.powermock.core.classloader.annotations.PrepareForTest;
10  import org.powermock.modules.junit4.PowerMockRunner;
11  import org.powermock.reflect.Whitebox;
12  
13  import com.simpligility.maven.plugins.android.AbstractAndroidMojoTestCase;
14  import com.simpligility.maven.plugins.android.CommandExecutor;
15  import com.simpligility.maven.plugins.android.config.ConfigHandler;
16  import com.simpligility.maven.plugins.android.configuration.Program;
17  import com.simpligility.maven.plugins.android.standalonemojos.MonkeyRunnerMojo;
18  
19  /**
20   * Test the monkeyrunner mojo. Tests options' default values and parsing. Tests the parameters passed to monkeyrunner.
21   * 
22   * @author Stéphane Nicolas - snicolas@octo.com
23   * 
24   */
25  @Ignore("This test has to be migrated to be an IntegrationTest using AbstractAndroidMojoIntegrationTest") 
26  @RunWith( PowerMockRunner.class )
27  @PrepareForTest(
28  { CommandExecutor.Factory.class, ConfigHandler.class } )
29  public class MonkeyRunnerMojoTest extends AbstractAndroidMojoTestCase< MonkeyRunnerMojo >
30  {
31      @Override
32      public String getPluginGoalName()
33      {
34          return "monkeyrunner";
35      }
36  
37      /**
38       * Tests all options, checks if their default values are correct.
39       * 
40       * @throws Exception
41       */
42      public void testDefaultMonkeyRunnerConfig() throws Exception
43      {
44          MonkeyRunnerMojo mojo = createMojo( "monkey-runner-config-project0" );
45          final ConfigHandler cfh = new ConfigHandler( mojo, this.session, this.execution );
46          cfh.parseConfiguration();
47  
48          Boolean monkeyrunnerSkip = Whitebox.getInternalState( mojo, "parsedSkip" );
49  
50          assertTrue( "monkeyrunner skip parameter should be true", monkeyrunnerSkip );
51      }
52  
53      /**
54       * Tests all options, checks if their default values are correct.
55       * 
56       * @throws Exception
57       */
58      public void testDefaultUnskippedMonkeyRunnerConfig() throws Exception
59      {
60          MonkeyRunnerMojo mojo = createMojo( "monkey-runner-config-project1" );
61          final ConfigHandler cfh = new ConfigHandler( mojo, this.session, this.execution );
62          cfh.parseConfiguration();
63  
64          Boolean monkeyrunnerSkip = Whitebox.getInternalState( mojo, "parsedSkip" );
65          String[] monkeyrunnerPlugins = Whitebox.getInternalState( mojo, "parsedPlugins" );
66          List< Program > monkeyrunnerPrograms = Whitebox.getInternalState( mojo, "parsedPrograms" );
67          Boolean monkeyrunnerCreateReport = Whitebox.getInternalState( mojo, "parsedCreateReport" );
68  
69          assertFalse( "monkeyrunner skip parameter should be false", monkeyrunnerSkip );
70          assertNull( "monkeyrunner plugins parameter should not contain plugins", monkeyrunnerPlugins );
71          assertNull( "monkeyrunner programs parameter should not contain programs", monkeyrunnerPrograms );
72          assertFalse( "monkeyrunner monkeyrunnerCreateReport parameter should be false", monkeyrunnerCreateReport );
73      }
74  
75      /**
76       * Tests all options, checks if they are parsed correctly.
77       * 
78       * @throws Exception
79       */
80      public void testCustomMonkeyRunnerConfig() throws Exception
81      {
82          MonkeyRunnerMojo mojo = createMojo( "monkey-runner-config-project2" );
83          final ConfigHandler cfh = new ConfigHandler( mojo, this.session, this.execution );
84          cfh.parseConfiguration();
85  
86          Boolean monkeyrunnerSkip = Whitebox.getInternalState( mojo, "parsedSkip" );
87          String[] monkeyrunnerPlugins = Whitebox.getInternalState( mojo, "parsedPlugins" );
88          List< Program > monkeyrunnerPrograms = Whitebox.getInternalState( mojo, "parsedPrograms" );
89          Boolean monkeyrunnerCreateReport = Whitebox.getInternalState( mojo, "parsedCreateReport" );
90  
91          assertFalse( "monkeyrunner skip parameter should be false", monkeyrunnerSkip );
92          assertNotNull( "monkeyrunner plugins parameter should not contain plugins", monkeyrunnerPlugins );
93          String[] expectedPlugins =
94          { "foo" };
95          assertTrue( Arrays.equals( expectedPlugins, monkeyrunnerPlugins ) );
96          assertNotNull( "monkeyrunner programs parameter should not contain programs", monkeyrunnerPrograms );
97          List< Program > expectedProgramList = new ArrayList< Program >();
98          expectedProgramList.add( new Program( "foo", null ) );
99          expectedProgramList.add( new Program( "bar", "qux" ) );
100         assertEquals( expectedProgramList, monkeyrunnerPrograms );
101         assertTrue( "monkeyrunner monkeyrunnerCreateReport parameter should be false", monkeyrunnerCreateReport );
102     }
103 
104     /**
105      * I don't understand why getAndroidSdk fails here when it runs fine in LintMojo Test public void
106      * testAllMonkeyRunnerCommandParametersWithCustomConfig() throws Exception { MonkeyRunnerMojo mojo = createMojo(
107      * "monkey-runner-config-project2" );
108      * 
109      * MavenProject project = EasyMock.createNiceMock( MavenProject.class ); Whitebox.setInternalState( mojo, "project",
110      * project ); File projectBaseDir = new File( "project/" ); EasyMock.expect( project.getBasedir() ).andReturn(
111      * projectBaseDir ); final CommandExecutor mockExecutor = PowerMock.createMock( CommandExecutor.class );
112      * PowerMock.replace( CommandExecutor.Factory.class.getDeclaredMethod( "createDefaultCommmandExecutor" ) ).with( new
113      * InvocationHandler() {
114      * 
115      * @Override public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable { return
116      *           mockExecutor; } } );
117      * 
118      *           Capture< List< String > > capturedArgument = new Capture< List< String > >();
119      * 
120      *           mockExecutor.setLogger( EasyMock.anyObject( Log.class ) ); mockExecutor.executeCommand(
121      *           EasyMock.anyObject( String.class ), EasyMock.capture( capturedArgument ), EasyMock.eq( false ) );
122      *           mockExecutor.setCustomShell( EasyMock.anyObject( Shell.class ) );
123      * 
124      *           IDevice mockDevice = EasyMock.createMock( IDevice.class );
125      * 
126      *           PowerMock.replay( project ); PowerMock.replay( mockExecutor ); PowerMock.replay( mockDevice );
127      * 
128      *           mojo.run( mockDevice );
129      * 
130      *           PowerMock.verify( mockExecutor ); List< String > parameters = capturedArgument.getValue(); List< String
131      *           > parametersExpected = new ArrayList< String >(); parametersExpected.add( "-plugin foo" );
132      *           parametersExpected.add( "foo" ); assertEquals( parametersExpected, parameters ); }
133      */
134 
135 }