View Javadoc
1   /*
2    * Copyright (C) 2009, 2010 Jayway AB
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.simpligility.maven.plugins.android;
17  
18  import org.apache.commons.io.IOUtils;
19  import org.apache.maven.plugin.MojoExecutionException;
20  import org.apache.maven.plugin.MojoFailureException;
21  import org.fest.reflect.core.Reflection;
22  import org.junit.Assert;
23  import org.junit.Before;
24  import org.junit.Test;
25  
26  import com.android.ddmlib.DdmPreferences;
27  import com.simpligility.maven.plugins.android.AbstractAndroidMojo;
28  import com.simpligility.maven.plugins.android.AndroidSdk;
29  
30  import java.io.File;
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.net.MalformedURLException;
34  import java.net.URI;
35  import java.net.URISyntaxException;
36  import java.net.URL;
37  
38  /**
39   * @author hugo.josefson@jayway.com
40   */
41  public class AbstractAndroidMojoTest {
42      protected AbstractAndroidMojo androidMojo;
43  
44      @Before
45      public void setUp() throws Exception {
46          androidMojo = new DefaultTestAndroidMojo();
47      }
48  
49      @Test
50      public void givenNoPathThenUseAndroidHomePath() throws MojoExecutionException
51      {
52          SdkTestSupport testSupport = new SdkTestSupport();
53          androidMojo = new EmptyAndroidMojo();
54          Reflection.field("sdkPath").ofType(File.class).in(androidMojo).set(null);
55          Reflection.field("sdkPlatform").ofType(String.class).in(androidMojo).set("19");
56          AndroidSdk sdk = androidMojo.getAndroidSdk();
57          File path = Reflection.field("sdkPath").ofType(File.class).in(sdk).get();
58          Assert.assertEquals(new File(testSupport.getEnv_ANDROID_HOME()).getAbsolutePath(), path.getAbsolutePath());
59      }
60  
61      @Test
62      public void givenAndroidManifestThenTargetPackageIsFound() throws MalformedURLException, URISyntaxException, MojoExecutionException {
63          final URL    url                = this.getClass().getResource("AndroidManifest.xml");
64          final URI    uri                = url.toURI();
65          final File   file               = new File(uri);
66          final String foundTargetPackage = androidMojo.extractPackageNameFromAndroidManifest(file);
67          Assert.assertEquals("com.example.android.apis.tests", foundTargetPackage);
68      }
69  
70      @Test
71      public void givenAndroidManifestThenInstrumentationRunnerIsFound() throws MalformedURLException, URISyntaxException, MojoExecutionException {
72          final URL    url             = this.getClass().getResource("AndroidManifest.xml");
73          final URI    uri             = url.toURI();
74          final File   file            = new File(uri);
75          final String foundInstrumentationRunner = androidMojo.extractInstrumentationRunnerFromAndroidManifest(file);
76          Assert.assertEquals("android.test.InstrumentationTestRunner", foundInstrumentationRunner);
77      }
78  
79      @Test
80      public void givenAndroidManifestWithoutInstrumentationThenInstrumentationRunnerIsNotFound() throws MalformedURLException, URISyntaxException, MojoExecutionException {
81          final URL    url             = this.getClass().getResource("AndroidManifestWithoutInstrumentation.xml");
82          final URI    uri             = url.toURI();
83          final File   file            = new File(uri);
84          final String foundInstrumentationRunner = androidMojo.extractInstrumentationRunnerFromAndroidManifest(file);
85          Assert.assertNull(foundInstrumentationRunner);
86      }
87  
88      @Test
89      public void givenValidAndroidManifestXmlTreeThenPackageIsFound() throws IOException {
90          final URL         resource               = this.getClass().getResource("AndroidManifestXmlTree.txt");
91          final InputStream inputStream            = resource.openStream();
92          final String      androidManifestXmlTree = IOUtils.toString(inputStream);
93          final String      foundPackage           = androidMojo.extractPackageNameFromAndroidManifestXmlTree(androidManifestXmlTree);
94          Assert.assertEquals("com.example.android.apis", foundPackage);
95      }
96  
97      @Test
98      public void givenApidemosApkThenPackageIsFound() throws IOException, MojoExecutionException, URISyntaxException {
99          final URL    resource     = this.getClass().getResource("apidemos-0.1.0-SNAPSHOT.apk");
100         final String foundPackage = androidMojo.extractPackageNameFromApk(new File(new URI(resource.toString())));
101         Assert.assertEquals("com.example.android.apis", foundPackage);
102     }
103 
104     @Test
105     public void givenApidemosPlatformtestsApkThenPackageIsFound() throws IOException, MojoExecutionException, URISyntaxException {
106         final URL    resource     = this.getClass().getResource("apidemos-platformtests-0.1.0-SNAPSHOT.apk");
107         final String foundPackage = androidMojo.extractPackageNameFromApk(new File(new URI(resource.toString())));
108         Assert.assertEquals("com.example.android.apis.tests", foundPackage);
109     }
110 
111     @Test
112     public void usesAdbConnectionTimeout() throws MojoExecutionException {
113         final int expectedTimeout = 1000;
114         androidMojo.adbConnectionTimeout = expectedTimeout;
115         androidMojo.initAndroidDebugBridge();
116 
117         Assert.assertEquals(DdmPreferences.getTimeOut(), expectedTimeout);
118     }
119 
120     private class DefaultTestAndroidMojo extends AbstractAndroidMojo {
121 
122         @Override
123         protected AndroidSdk getAndroidSdk() {
124             return new SdkTestSupport().getSdk_with_platform_default();
125         }
126 
127         public void execute() throws MojoExecutionException, MojoFailureException {
128 
129         }
130     }
131 
132     private class EmptyAndroidMojo extends AbstractAndroidMojo {
133         public void execute() throws MojoExecutionException, MojoFailureException {
134         }
135     }
136 }