Skip to content

Commit

Permalink
introduced better error handling for sdk platform as well as allowed …
Browse files Browse the repository at this point in the history
…for usage of platform as well as api level as parameter with internal automatic translation
  • Loading branch information
mosabua committed May 7, 2010
1 parent b37de1c commit db533ee
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ public abstract class AbstractAndroidMojo extends AbstractMojo {
* <p>The <code>&lt;platform&gt;</code> parameter is optional, and corresponds to the
* <code>platforms/android-*</code> directories in the Android SDK directory. Default is the latest available
* version, so you only need to set it if you for example want to use platform 1.1 from an 1.5 SDK. Has no effect
* when used on an Android SDK 1.1.</p>
* when used on an Android SDK 1.1. The parameter can also be coded as the API level. Therefore valid values are
* 1.1, 1.5, 1.6, 2.0, 2.01, 2.1 as well as 3, 4, 5, 6, 7.</p>
* <p>The <code>&lt;path&gt;</code> parameter is optional. The default is the setting of the ANDROID_HOME environment
* variable. The parameter can be used to override this setting with a different environment variable like this:</p>
* <pre>
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/jayway/maven/plugins/android/AndroidSdk.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,18 @@ public class AndroidSdk {
private final String platform;
private static final String PARAMETER_MESSAGE = "Please provide a proper Android SDK directory path as configuration parameter <sdk><path>...</path></sdk> in the plugin <configuration/>. As an alternative, you may add the parameter to commandline: -Dandroid.sdk.path=... or set environment variable " + AbstractAndroidMojo.ENV_ANDROID_HOME + ".";

public AndroidSdk(File path, String platform) {
public AndroidSdk(File path, String platformOrApiLevel) {
this.path = path;
this.platform = platform;
if (platformOrApiLevel == null) {
this.platform = platformOrApiLevel;
// letting this through to preserve compatibility for now
} else if (PlatformApiLevel.isValidApiLevel(platformOrApiLevel)) {
this.platform = platformOrApiLevel;
} else if (PlatformApiLevel.isValidPlatform(platformOrApiLevel)) {
this.platform = PlatformApiLevel.findApiLevel(platformOrApiLevel);
} else {
throw new InvalidSdkException("Invalid platform: " + platformOrApiLevel);
}
}

public enum Layout{LAYOUT_1_1, LAYOUT_1_5};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.jayway.maven.plugins.android;

/**
*
*
* @author Manfred Moser <manfred@simpligility.com>
*/
public enum PlatformApiLevel {
ONE_ONE("1.1", "2"),
ONE_FIVE("1.5", "3"),
ONE_SIX("1.6", "4"),
TWO_ZERO("2.0", "5"),
TWO_ZERO_ONE("2.01", "6"),
TWO_ONE("2.1", "7");

private String platform;
private String apilevel;

private PlatformApiLevel(String platform, String apilevel) {
this.platform = platform;
this.apilevel = apilevel;
}

public String getPlatform() {
return platform;
}

public void setPlatform(String platform) {
this.platform = platform;
}

public String getApilevel() {
return apilevel;
}

public void setApilevel(String apilevel) {
this.apilevel = apilevel;
}

public static String findPlatform(String apilevel) {
PlatformApiLevel[] allPlatformApiLevels = PlatformApiLevel.values();
for (PlatformApiLevel platformApiLevel : allPlatformApiLevels) {
if (platformApiLevel.getApilevel().equals(apilevel)) return platformApiLevel.getPlatform();
}
return null;
}

public static String findApiLevel(String platform) {
PlatformApiLevel[] allPlatformApiLevels = PlatformApiLevel.values();
for (PlatformApiLevel platformApiLevel : allPlatformApiLevels) {
if (platformApiLevel.getPlatform().equals(platform)) return platformApiLevel.getApilevel();
}
return null;
}

public static boolean isValidPlatform(String platform) {
boolean valid = false;
PlatformApiLevel[] allPlatformApiLevels = PlatformApiLevel.values();
for (PlatformApiLevel current : allPlatformApiLevels) {
if (current.getPlatform().equals(platform)) valid = true;
}
return valid;
}

public static boolean isValidApiLevel(String apiLevel) {
boolean valid = false;
PlatformApiLevel[] allPlatformApiLevels = PlatformApiLevel.values();
for (PlatformApiLevel current : allPlatformApiLevels) {
if (current.getApilevel().equals(apiLevel)) valid = true;
}
return valid;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ public void givenToolAaptAndPlatform1dot5ThenPathIsPlatform1dot5() {
}

@Test(expected = InvalidSdkException.class)
public void givenToolAaptAndPlatform1dot4ThenException() {
new AndroidSdk(new File(sdkTestSupport.getEnv_ANDROID_HOME()), "1.5").getPathForTool("aapt");
public void givenInvalidSdkPathThenException() throws IOException {
new AndroidSdk(File.createTempFile("maven-android-plugin", "test"), null).getLayout();
}

@Test(expected = InvalidSdkException.class)
public void givenInvalidSdkPathThenException() throws IOException {
new AndroidSdk(File.createTempFile("maven-android-plugin", "test"), null).getLayout();
public void givenInvalidPlatformStringThenException() throws IOException {
final AndroidSdk sdk = new AndroidSdk(new File(sdkTestSupport.getEnv_ANDROID_HOME()), "invalidplatform");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.jayway.maven.plugins.android;

import org.junit.Assert;
import org.junit.Test;

/**
*
*
* @author Manfred Moser <manfred@simpligility.com>
*/
public class PlatformApiLevelTest {

@Test
public void checkTotalNumber() {
Assert.assertEquals(6, PlatformApiLevel.values().length);
}

@Test
public void checkApiLevelLookup() {
Assert.assertEquals("1.1", PlatformApiLevel.findPlatform("2"));
Assert.assertEquals("1.5", PlatformApiLevel.findPlatform("3"));
Assert.assertEquals("1.6", PlatformApiLevel.findPlatform("4"));
Assert.assertEquals("2.0", PlatformApiLevel.findPlatform("5"));
Assert.assertEquals("2.01", PlatformApiLevel.findPlatform("6"));
Assert.assertEquals("2.1", PlatformApiLevel.findPlatform("7"));
}

@Test
public void checkPlatformLookup() {
Assert.assertEquals("2", PlatformApiLevel.findApiLevel("1.1"));
Assert.assertEquals("3", PlatformApiLevel.findApiLevel("1.5"));
Assert.assertEquals("4", PlatformApiLevel.findApiLevel("1.6"));
Assert.assertEquals("5", PlatformApiLevel.findApiLevel("2.0"));
Assert.assertEquals("6", PlatformApiLevel.findApiLevel("2.01"));
Assert.assertEquals("7", PlatformApiLevel.findApiLevel("2.1"));
}

@Test
public void checkValidPlatform() {
Assert.assertTrue(PlatformApiLevel.isValidApiLevel("2"));
Assert.assertTrue(PlatformApiLevel.isValidApiLevel("3"));
Assert.assertTrue(PlatformApiLevel.isValidApiLevel("4"));
Assert.assertTrue(PlatformApiLevel.isValidApiLevel("5"));
Assert.assertTrue(PlatformApiLevel.isValidApiLevel("6"));
Assert.assertTrue(PlatformApiLevel.isValidApiLevel("7"));

Assert.assertFalse(PlatformApiLevel.isValidApiLevel("something"));
Assert.assertFalse(PlatformApiLevel.isValidApiLevel("1.0"));
}

@Test
public void checkValidApiLevel() {

Assert.assertTrue(PlatformApiLevel.isValidPlatform("1.1"));
Assert.assertTrue(PlatformApiLevel.isValidPlatform("1.5"));
Assert.assertTrue(PlatformApiLevel.isValidPlatform("1.6"));
Assert.assertTrue(PlatformApiLevel.isValidPlatform("2.0"));
Assert.assertTrue(PlatformApiLevel.isValidPlatform("2.01"));
Assert.assertTrue(PlatformApiLevel.isValidPlatform("2.1"));

Assert.assertFalse(PlatformApiLevel.isValidPlatform("something"));
Assert.assertFalse(PlatformApiLevel.isValidPlatform("1.0"));
}
}

0 comments on commit db533ee

Please sign in to comment.