View Javadoc
1   /*
2    * Copyright (C) 2009 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 java.io.File;
19  import java.io.IOException;
20  import java.net.URISyntaxException;
21  
22  import org.codehaus.plexus.util.ReflectionUtils;
23  import org.junit.Assert;
24  import org.junit.Before;
25  import org.junit.Test;
26  
27  import com.simpligility.maven.plugins.android.AndroidSdk;
28  import com.simpligility.maven.plugins.android.InvalidSdkException;
29  import org.junit.Ignore;
30  
31  /**
32   * Excercises the {@link AndroidSdk} class.
33   *
34   * @author hugo.josefson@jayway.com
35   * @author Manfred Moser - manfred@simpligility.com
36   */
37  public class AndroidSdkTest {
38      
39      private SdkTestSupport sdkTestSupport; 
40      
41      @Before
42      public void setUp(){
43          sdkTestSupport = new SdkTestSupport();
44      }
45  
46      @Test
47      public void givenToolAdbThenPathIsPlatformTools() {
48          final String pathForTool =sdkTestSupport.getSdk_with_platform_default().getAdbPath();
49          Assert.assertEquals(new File(sdkTestSupport.getEnv_ANDROID_HOME() + "/platform-tools").getAbsolutePath(), new File(pathForTool).getParentFile().getAbsolutePath());
50      }
51  
52      @Test
53      public void givenToolAndroidThenPathIsCommon() {
54          final String pathForTool = sdkTestSupport.getSdk_with_platform_default().getAndroidPath();
55          Assert.assertEquals(new File(sdkTestSupport.getEnv_ANDROID_HOME() + "/tools").getAbsolutePath(), new File(pathForTool).getParentFile().getAbsolutePath());
56      }
57  
58  
59      @Test(expected = InvalidSdkException.class)
60      public void givenInvalidPlatformStringThenException() throws IOException {
61          final AndroidSdk sdk = new AndroidSdk(new File(sdkTestSupport.getEnv_ANDROID_HOME()), "invalidplatform");
62      }
63  
64      @Test
65      public void givenPlatformNullThenPlatformisSomethingValidLooking() throws IllegalAccessException, URISyntaxException {
66          final File sdkPath = (File) ReflectionUtils.getValueIncludingSuperclasses("sdkPath",sdkTestSupport.getSdk_with_platform_default());
67          final File platform = sdkTestSupport.getSdk_with_platform_default().getPlatform();
68          final String platformPath = platform.getAbsoluteFile().toURI().toString();
69          final String regex = "/platforms/android-.*";
70          //Strip off the sdkPath part  
71          String matcher = platformPath.substring( sdkPath.toURI().toString().length() -1 );
72          Assert.assertTrue(String.format("Platform [%s] does not match regex: [%s]", matcher,regex), matcher.matches(regex));
73      }
74  
75      /**
76       * Test all available platforms and api level versions. All have to be installed locally
77       * for this test to pass including the obsolete ones.
78       */
79      @Test
80      public void validPlatformsAndApiLevels19() {
81          // Remember to add further platforms to .travis.yml if you add more platforms here, otherwise ci build fails
82          final AndroidSdk sdk19 = new AndroidSdk(new File(sdkTestSupport.getEnv_ANDROID_HOME()), "19"); 
83      }
84  
85      @Test
86      public void validPlatformsAndApiLevels22() {
87          // Remember to add further platforms to .travis.yml if you add more platforms here, otherwise ci build fails
88          final AndroidSdk sdk22 = new AndroidSdk(new File(sdkTestSupport.getEnv_ANDROID_HOME()), "22", null);
89          Assert.assertTrue( sdk22.getAaptPath() != null && !sdk22.getAaptPath().equals( "" ) );
90      }
91  
92      @Test
93      public void validPlatformsAndApiLevels25() {
94          // Remember to add further platforms to .travis.yml if you add more platforms here, otherwise ci build fails
95          final AndroidSdk sdk25 = new AndroidSdk( new File( sdkTestSupport.getEnv_ANDROID_HOME()), "25", "25.0.3" );
96          Assert.assertTrue( sdk25.getAaptPath() != null && !sdk25.getAaptPath().equals( "" ) );
97      }
98  
99      @Test
100     public void validPlatformsAndApiLevels23() {
101         // Remember to add further platforms to .travis.yml if you add more platforms here, otherwise ci build fails
102         final AndroidSdk sdk23 = new AndroidSdk( new File( sdkTestSupport.getEnv_ANDROID_HOME() ), "23" );
103         Assert.assertTrue( sdk23.getAaptPath() != null && !sdk23.getAaptPath().equals( "" ) );
104     }
105 
106     @Test
107     public void validPlatformsAndApiLevels28() {
108         // Remember to add further platforms to .travis.yml if you add more platforms here, otherwise ci build fails
109         final AndroidSdk sdk23 = new AndroidSdk( new File( sdkTestSupport.getEnv_ANDROID_HOME() ), "28" );
110         Assert.assertTrue( sdk23.getAaptPath() != null && !sdk23.getAaptPath().equals( "" ) );
111     }
112 
113     @Test(expected = InvalidSdkException.class)
114     public void invalidPlatformAndApiLevels() {
115         final AndroidSdk invalid = new AndroidSdk (new File( sdkTestSupport.getEnv_ANDROID_HOME() ), "invalid" );
116     }
117 
118     @Test(expected = NumberFormatException.class)
119     public void invalidBuildTools() {
120         final AndroidSdk invalid = new AndroidSdk (new File( sdkTestSupport.getEnv_ANDROID_HOME() ), "19", "invalid" );
121         invalid.getAaptPath();
122     }
123 
124     @Test
125     public void validPlatformsAndApiLevelsWithDiffBuildTools1() {
126         // Remember to add further platforms to .travis.yml if you add more platforms here, otherwise ci build fails
127         final AndroidSdk sdk = new AndroidSdk( new File( sdkTestSupport.getEnv_ANDROID_HOME() ), "19", "25.0.3" );
128         Assert.assertTrue( sdk.getAaptPath() != null && !sdk.getAaptPath().equals( "" ) );
129     }
130 
131     @Test
132     public void validPlatformsAndApiLevelsWithDiffBuildTools2() {
133         // Remember to add further platforms to .travis.yml if you add more platforms here, otherwise ci build fails
134         final AndroidSdk sdk = new AndroidSdk( new File( sdkTestSupport.getEnv_ANDROID_HOME() ), "19", "24.0.3" );
135         Assert.assertTrue( sdk.getAaptPath() != null && !sdk.getAaptPath().equals( "" ) );
136     }
137 
138     @Test
139     public void validPlatformsAndApiLevelsWithDiffBuildTools3() {
140         // Remember to add further platforms to .travis.yml if you add more platforms here, otherwise ci build fails
141         final AndroidSdk sdk = new AndroidSdk( new File( sdkTestSupport.getEnv_ANDROID_HOME() ), "28", "28.0.3" );
142         Assert.assertTrue( sdk.getAaptPath() != null && !sdk.getAaptPath().equals( "" ) );
143     }
144 
145 }