Fix: https://issues.apache.org/jira/browse/FLEX-34839
- Make use of Apache Commons Lang3.SystemUtils for OS detection.
- Make SdkConverterCLI to use the detection.
- Removed the PowerMock lib, couldn't mock static final field, I remove dynamically the final modifier instead.
diff --git a/mavenizer/cli/src/main/java/org/apache/flex/utilities/converter/core/SdkConverterCLI.java b/mavenizer/cli/src/main/java/org/apache/flex/utilities/converter/core/SdkConverterCLI.java
index 9f3d160..918dd70 100644
--- a/mavenizer/cli/src/main/java/org/apache/flex/utilities/converter/core/SdkConverterCLI.java
+++ b/mavenizer/cli/src/main/java/org/apache/flex/utilities/converter/core/SdkConverterCLI.java
@@ -2,7 +2,6 @@
import org.apache.commons.cli.*;
import org.apache.commons.io.FileUtils;
-import org.apache.commons.lang3.SystemUtils;
import org.apache.flex.utilities.converter.air.AirConverter;
import org.apache.flex.utilities.converter.deployer.aether.AetherDeployer;
import org.apache.flex.utilities.converter.flash.FlashConverter;
@@ -122,14 +121,11 @@
platforms.add(PlatformType.valueOf(platformName));
}
}
+
if(platforms.isEmpty()) {
- if(SystemUtils.IS_OS_WINDOWS) {
- platforms.add(PlatformType.WINDOWS);
- } else if(SystemUtils.IS_OS_MAC) {
- platforms.add(PlatformType.MAC);
- } else if(SystemUtils.IS_OS_LINUX) {
- platforms.add(PlatformType.LINUX);
- } else {
+ try {
+ platforms.add(PlatformType.getCurrent());
+ } catch (Exception e) {
System.err.println("Unsupported OS type. Provide manually using 'platform' parameter.");
System.exit(1);
}
diff --git a/mavenizer/retrievers/base/pom.xml b/mavenizer/retrievers/base/pom.xml
index d13b4cb..ace86c6 100644
--- a/mavenizer/retrievers/base/pom.xml
+++ b/mavenizer/retrievers/base/pom.xml
@@ -32,7 +32,7 @@
<properties>
<powermock.version>1.6.2</powermock.version>
- <junit.version>4.8.2</junit.version>
+ <junit.version>4.11</junit.version>
</properties>
<dependencies>
@@ -47,9 +47,9 @@
<version>1.8.1</version>
</dependency>
<dependency>
- <groupId>commons-lang</groupId>
- <artifactId>commons-lang</artifactId>
- <version>2.6</version>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-lang3</artifactId>
+ <version>3.3.2</version>
</dependency>
<!--TEST-->
@@ -60,27 +60,9 @@
<scope>test</scope>
</dependency>
<dependency>
- <groupId>org.powermock</groupId>
- <artifactId>powermock-module-junit4-rule-agent</artifactId>
- <version>${powermock.version}</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.powermock</groupId>
- <artifactId>powermock-module-junit4</artifactId>
- <version>${powermock.version}</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.powermock</groupId>
- <artifactId>powermock-api-easymock</artifactId>
- <version>${powermock.version}</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.easymock</groupId>
- <artifactId>easymock</artifactId>
- <version>3.3.1</version>
+ <groupId>pl.pragmatists</groupId>
+ <artifactId>JUnitParams</artifactId>
+ <version>1.0.4</version>
<scope>test</scope>
</dependency>
</dependencies>
diff --git a/mavenizer/retrievers/base/src/main/java/org/apache/flex/utilities/converter/retrievers/types/PlatformType.java b/mavenizer/retrievers/base/src/main/java/org/apache/flex/utilities/converter/retrievers/types/PlatformType.java
index 2dceba0..d7320d4 100644
--- a/mavenizer/retrievers/base/src/main/java/org/apache/flex/utilities/converter/retrievers/types/PlatformType.java
+++ b/mavenizer/retrievers/base/src/main/java/org/apache/flex/utilities/converter/retrievers/types/PlatformType.java
@@ -16,6 +16,8 @@
*/
package org.apache.flex.utilities.converter.retrievers.types;
+import org.apache.commons.lang3.SystemUtils;
+
/**
* Created by cdutz on 18.05.2014.
*/
@@ -25,95 +27,23 @@
LINUX,
MAC;
- public static PlatformType getCurrent()
- {
+ public static PlatformType getCurrent() throws Exception {
PlatformType platformType = null;
- if (isWindows())
+ if (SystemUtils.IS_OS_WINDOWS)
{
platformType = PlatformType.WINDOWS;
}
- else if (isMac())
+ else if (SystemUtils.IS_OS_MAC || SystemUtils.IS_OS_MAC_OSX)
{
platformType = PlatformType.MAC;
}
- else if (isUnixBased())
+ else if (SystemUtils.IS_OS_UNIX)
{
platformType = PlatformType.LINUX;
}
+ else throw new Exception("Unsupported OS.");
return platformType;
}
-
- static final String NET_BSD = "netbsd";
-
- static final String FREE_BSD = "freebsd";
-
- static final String WINDOWS_OS = "windows";
-
- static final String MAC_OS = "mac os x";
-
- static final String MAC_OS_DARWIN = "darwin";
-
- static final String LINUX_OS = "linux";
-
- static final String SOLARIS_OS = "sunos";
-
- private static String osString()
- {
- return System.getProperty( "os.name" ).toLowerCase();
- }
-
- /**
- * Return a boolean to show if we are running on Windows.
- *
- * @return true if we are running on Windows.
- */
- private static boolean isWindows()
- {
- return osString().startsWith( WINDOWS_OS );
- }
-
- /**
- * Return a boolean to show if we are running on Linux.
- *
- * @return true if we are running on Linux.
- */
- private static boolean isLinux()
- {
- return osString().startsWith( LINUX_OS ) ||
- // I know, but people said that workds...
- osString().startsWith( NET_BSD ) ||
- osString().startsWith( FREE_BSD );
- }
-
- /**
- * Return a boolean to show if we are running on Solaris.
- *
- * @return true if we are running on Solaris.
- */
- private static boolean isSolaris()
- {
- return osString().startsWith( SOLARIS_OS );
- }
-
- /**
- * Return a boolean to show if we are running on a unix-based OS.
- *
- * @return true if we are running on a unix-based OS.
- */
- private static boolean isUnixBased()
- {
- return isLinux() || isSolaris();
- }
-
- /**
- * Return a boolean to show if we are running on Mac OS X.
- *
- * @return true if we are running on Mac OS X.
- */
- private static boolean isMac()
- {
- return osString().startsWith( MAC_OS ) || osString().startsWith( MAC_OS_DARWIN );
- }
}
diff --git a/mavenizer/retrievers/base/src/main/java/org/apache/flex/utilities/converter/retrievers/utils/ProgressBar.java b/mavenizer/retrievers/base/src/main/java/org/apache/flex/utilities/converter/retrievers/utils/ProgressBar.java
index 72ee855..c15d26b 100644
--- a/mavenizer/retrievers/base/src/main/java/org/apache/flex/utilities/converter/retrievers/utils/ProgressBar.java
+++ b/mavenizer/retrievers/base/src/main/java/org/apache/flex/utilities/converter/retrievers/utils/ProgressBar.java
@@ -16,7 +16,7 @@
*/
package org.apache.flex.utilities.converter.retrievers.utils;
-import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang3.StringUtils;
/**
* Created by cdutz on 24.05.2014.
diff --git a/mavenizer/retrievers/base/src/test/java/org/apache/flex/utilities/converter/retrievers/types/PlatformTypeTest.java b/mavenizer/retrievers/base/src/test/java/org/apache/flex/utilities/converter/retrievers/types/PlatformTypeTest.java
index d8571bc..eeb6a22 100644
--- a/mavenizer/retrievers/base/src/test/java/org/apache/flex/utilities/converter/retrievers/types/PlatformTypeTest.java
+++ b/mavenizer/retrievers/base/src/test/java/org/apache/flex/utilities/converter/retrievers/types/PlatformTypeTest.java
@@ -1,61 +1,69 @@
package org.apache.flex.utilities.converter.retrievers.types;
-import org.junit.Rule;
+import junitparams.JUnitParamsRunner;
+import junitparams.Parameters;
+import org.apache.commons.lang3.SystemUtils;
+import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.rule.PowerMockRule;
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Collection;
-import static org.easymock.EasyMock.expect;
import static org.junit.Assert.assertEquals;
-import static org.powermock.api.easymock.PowerMock.*;
/**
* @author: Frederic Thomas
* Date: 12/05/2015
* Time: 01:34
*/
-@PrepareForTest( { PlatformType.class })
-@RunWith(Parameterized.class)
+@RunWith(JUnitParamsRunner.class)
public class PlatformTypeTest {
- private String osName;
- private PlatformType platformType;
+ private Class<SystemUtils> systemUtilsClass;
- @Rule
- public PowerMockRule powerMockRule = new PowerMockRule();
-
- @Parameterized.Parameters
- public static Collection<Object[]> data() {
-
+ public static Collection<Object[]> platformParameters() {
return Arrays.asList(new Object[][]{
- {PlatformType.WINDOWS_OS, PlatformType.WINDOWS},
- {PlatformType.MAC_OS, PlatformType.MAC},
- {PlatformType.MAC_OS_DARWIN, PlatformType.MAC},
- {PlatformType.FREE_BSD, PlatformType.LINUX},
- {PlatformType.LINUX_OS, PlatformType.LINUX},
- {PlatformType.NET_BSD, PlatformType.LINUX},
- {PlatformType.SOLARIS_OS, PlatformType.LINUX}
+ {"IS_OS_WINDOWS", PlatformType.WINDOWS},
+ {"IS_OS_MAC", PlatformType.MAC},
+ {"IS_OS_MAC_OSX", PlatformType.MAC},
+ {"IS_OS_UNIX", PlatformType.LINUX}
});
}
- public PlatformTypeTest(String osName, PlatformType platformType) {
- this.osName = osName;
- this.platformType = platformType;
+ @Before
+ public void setUp() throws Exception {
+ systemUtilsClass = SystemUtils.class;
+
+ setFinalStatic(systemUtilsClass.getField("IS_OS_WINDOWS"), false);
+ setFinalStatic(systemUtilsClass.getField("IS_OS_MAC"), false);
+ setFinalStatic(systemUtilsClass.getField("IS_OS_MAC_OSX"), false);
+ setFinalStatic(systemUtilsClass.getField("IS_OS_UNIX"), false);
}
@Test
- public void it_returns_the_current_platform_type() throws Exception {
- mockStatic(System.class);
+ @Parameters(method = "platformParameters")
+ public void it_detects_the_current_platform_type(String fieldName, PlatformType platformType) throws Exception {
- expect(System.getProperty("os.name")).andReturn(osName).anyTimes();
- replayAll();
-
+ setFinalStatic(systemUtilsClass.getField(fieldName), true);
assertEquals(platformType, PlatformType.getCurrent());
- verifyAll();
+ }
+
+ @Test(expected = Exception.class)
+ public void it_throws_an_exception_when_it_can_not_detect_the_current_platform_type() throws Exception {
+ PlatformType.getCurrent();
+ }
+
+ private static void setFinalStatic(Field field, Object newValue) throws Exception {
+ field.setAccessible(true);
+
+ // remove final modifier from field
+ Field modifiersField = Field.class.getDeclaredField("modifiers");
+ modifiersField.setAccessible(true);
+ modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
+
+ field.set(null, newValue);
}
}