View Javadoc
1   package com.simpligility.maven.plugins.android.standalonemojos;
2   
3   import com.simpligility.maven.plugins.android.AbstractAndroidMojoTestCase;
4   import com.simpligility.maven.plugins.android.CommandExecutor;
5   import com.simpligility.maven.plugins.android.common.AndroidExtension;
6   import com.simpligility.maven.plugins.android.config.ConfigHandler;
7   import com.simpligility.maven.plugins.android.standalonemojos.ZipalignMojo;
8   
9   import org.apache.maven.plugin.logging.Log;
10  import org.apache.maven.project.MavenProject;
11  import org.apache.maven.project.MavenProjectHelper;
12  import org.codehaus.plexus.util.FileUtils;
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 java.io.File;
23  import java.lang.reflect.InvocationHandler;
24  import java.lang.reflect.Method;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  /**
29   * User: Eugen
30   */
31  @Ignore("This test has to be migrated to be an IntegrationTest using AbstractAndroidMojoIntegrationTest") 
32  @RunWith ( PowerMockRunner.class )
33  @PrepareForTest (
34          { CommandExecutor.Factory.class, FileUtils.class, ZipalignMojo.class } )
35  public class ZipalignMojoTest extends AbstractAndroidMojoTestCase<ZipalignMojo>
36  {
37      @Override
38      public String getPluginGoalName ()
39      {
40          return "zipalign";
41      }
42  
43      /**
44       * Tests all options, checks if their default values are correct.
45       *
46       * @throws Exception
47       */
48      public void testDefaultConfig () throws Exception
49      {
50          ZipalignMojo mojo = createMojo( "zipalign-config-project0" );
51          final ConfigHandler cfh = new ConfigHandler( mojo, this.session, this.execution );
52          cfh.parseConfiguration();
53  
54          Boolean skip = Whitebox.getInternalState( mojo, "parsedSkip" );
55          assertTrue( "zipalign 'skip' parameter should be true", skip );
56  
57          Boolean verbose = Whitebox.getInternalState( mojo, "parsedVerbose" );
58          assertFalse( "zipalign 'verbose' parameter should be false", verbose );
59  
60          MavenProject project = Whitebox.getInternalState( mojo, "project" );
61  
62          String inputApk = Whitebox.getInternalState( mojo, "parsedInputApk" );
63          File inputApkFile = new File( project.getBuild().getDirectory(), project.getBuild().getFinalName() + ".apk" );
64          assertEquals( "zipalign 'inputApk' parameter should be equal", inputApkFile.getAbsolutePath(), inputApk );
65  
66          String outputApk = Whitebox.getInternalState( mojo, "parsedOutputApk" );
67          File outputApkFile = new File( project.getBuild().getDirectory(), project.getBuild().getFinalName()
68                  + "-aligned.apk" );
69          assertEquals( "zipalign 'outputApk' parameter should be equal", outputApkFile.getAbsolutePath(), outputApk );
70      }
71  
72      /**
73       * Tests all parameters parsing
74       * 
75       * Probably not needed since it is like testing maven itself
76       *
77       * @throws Exception
78       */
79      public void testConfigParse () throws Exception
80      {
81          ZipalignMojo mojo = createMojo( "zipalign-config-project1" );
82          final ConfigHandler cfh = new ConfigHandler( mojo, this.session, this.execution );
83          cfh.parseConfiguration();
84  
85          Boolean skip = Whitebox.getInternalState( mojo, "parsedSkip" );
86          assertFalse( "zipalign 'skip' parameter should be false", skip );
87  
88          Boolean verbose = Whitebox.getInternalState( mojo, "parsedVerbose" );
89          assertTrue( "zipalign 'verbose' parameter should be true", verbose );
90  
91          String inputApk = Whitebox.getInternalState( mojo, "parsedInputApk" );
92          assertEquals( "zipalign 'inputApk' parameter should be equal", "app.apk", inputApk );
93  
94          String outputApk = Whitebox.getInternalState( mojo, "parsedOutputApk" );
95          assertEquals( "zipalign 'outputApk' parameter should be equal", "app-updated.apk", outputApk );
96      }
97  
98      /**
99       * Tests run of zipalign with correct parameters as well adding aligned file to artifacts
100      *
101      * @throws Exception
102      */
103     public void testDefaultRun () throws Exception
104     {
105         ZipalignMojo mojo = createMojo( "zipalign-config-project3" );
106 
107         MavenProject project = Whitebox.getInternalState( mojo, "project" );
108         project.setPackaging( AndroidExtension.APK );
109 
110         MavenProjectHelper projectHelper = EasyMock.createNiceMock( MavenProjectHelper.class );
111         Capture<File> capturedParameter = new Capture<File>();
112         projectHelper.attachArtifact( EasyMock.eq( project ), EasyMock.eq( AndroidExtension.APK ),
113                 EasyMock.eq( "aligned" ), EasyMock.capture( capturedParameter ) );
114         Whitebox.setInternalState( mojo, "projectHelper", projectHelper );
115 
116         final CommandExecutor mockExecutor = PowerMock.createMock( CommandExecutor.class );
117         PowerMock.replace( CommandExecutor.Factory.class.getDeclaredMethod( "createDefaultCommmandExecutor" ) ).with(
118                 new InvocationHandler()
119                 {
120                     @Override
121                     public Object invoke ( Object proxy, Method method, Object[] args ) throws Throwable
122                     {
123                         return mockExecutor;
124                     }
125                 } );
126 
127         Capture<List<String>> capturedFile = new Capture<List<String>>();
128         mockExecutor.setLogger( EasyMock.anyObject( Log.class ) );
129         mockExecutor.setCaptureStdOut( EasyMock.anyBoolean() );
130         mockExecutor.executeCommand( EasyMock.anyObject( String.class ), EasyMock.capture( capturedFile ) );
131 
132         PowerMock.mockStatic( FileUtils.class );
133         EasyMock.expect( FileUtils.fileExists( "app-updated.apk" ) ).andReturn( true );
134 
135         EasyMock.replay( projectHelper );
136         PowerMock.replay( mockExecutor );
137         PowerMock.replay( FileUtils.class );
138 
139         mojo.execute();
140 
141         PowerMock.verify( mockExecutor );
142         List<String> parameters = capturedFile.getValue();
143         List<String> parametersExpected = new ArrayList<String>();
144         parametersExpected.add( "-v" );
145         parametersExpected.add( "-f" );
146         parametersExpected.add( "4" );
147         parametersExpected.add( "app.apk" );
148         parametersExpected.add( "app-updated.apk" );
149         assertEquals( "Zipalign arguments aren't as expected", parametersExpected, parameters );
150 
151         PowerMock.verify( projectHelper );
152         assertEquals( "File should be same as expected", new File( "app-updated.apk" ), capturedParameter.getValue() );
153 
154         // verify that all method were invoked
155         PowerMock.verify( FileUtils.class );
156     }
157 
158     /**
159      * Tests run of zipalign with correct parameters
160      *
161      * @throws Exception
162      */
163     public void testRunWhenInputApkIsSameAsOutput () throws Exception
164     {
165         ZipalignMojo mojo = createMojo( "zipalign-config-project2" );
166 
167         MavenProject project = Whitebox.getInternalState( mojo, "project" );
168         project.setPackaging( AndroidExtension.APK );
169 
170         MavenProjectHelper projectHelper = EasyMock.createNiceMock( MavenProjectHelper.class );
171         Whitebox.setInternalState( mojo, "projectHelper", projectHelper );
172 
173         final CommandExecutor mockExecutor = PowerMock.createMock( CommandExecutor.class );
174         PowerMock.replace( CommandExecutor.Factory.class.getDeclaredMethod( "createDefaultCommmandExecutor" ) ).with(
175                 new InvocationHandler()
176                 {
177                     @Override
178                     public Object invoke ( Object proxy, Method method, Object[] args ) throws Throwable
179                     {
180                         return mockExecutor;
181                     }
182                 } );
183 
184         Capture<List<String>> capturedFile = new Capture<List<String>>();
185         mockExecutor.setLogger( EasyMock.anyObject( Log.class ) );
186         mockExecutor.setCaptureStdOut( EasyMock.anyBoolean() );
187         mockExecutor.executeCommand( EasyMock.anyObject( String.class ), EasyMock.capture( capturedFile ) );
188 
189         PowerMock.mockStatic( FileUtils.class );
190         EasyMock.expect( FileUtils.fileExists( "app-aligned-temp.apk" ) ).andReturn( true );
191         FileUtils.rename( new File( "app-aligned-temp.apk" ) , new File( "app.apk" ) );
192         EasyMock.expectLastCall();
193 
194         PowerMock.replay( projectHelper );
195         PowerMock.replay( mockExecutor );
196         PowerMock.replay( FileUtils.class );
197 
198         mojo.execute();
199 
200         PowerMock.verify( mockExecutor );
201         List<String> parameters = capturedFile.getValue();
202         List<String> parametersExpected = new ArrayList<String>();
203         parametersExpected.add( "-v" );
204         parametersExpected.add( "-f" );
205         parametersExpected.add( "4" );
206         parametersExpected.add( "app.apk" );
207         parametersExpected.add( "app-aligned-temp.apk" );
208         assertEquals( "Zipalign arguments aren't as expected", parametersExpected, parameters );
209 
210         // no invocations to attach artifact
211         PowerMock.verify( projectHelper );
212 
213         // verify that all method were invoked
214         PowerMock.verify( FileUtils.class );
215     }
216 }
217