Skip to content
Browse files

removed redundant code and reuse existing code

  • Loading branch information...
1 parent a2a234a commit 036227097b02a246b4e11f321e8e06c20b1fb39c Parris, A - SPLXL committed Dec 19, 2012
View
6 pom.xml
@@ -106,6 +106,12 @@
<version>1.9</version>
<scope>test</scope>
</dependency>
+ <!--<dependency>-->
+ <!--<groupId>org.codehaus.plexus</groupId>-->
+ <!--<artifactId>plexus-utils</artifactId>-->
+ <!--<version>3.0.9</version>-->
+ <!--<scope>test</scope>-->
+ <!--</dependency>-->
</dependencies>
<build>
<pluginManagement>
View
46 src/main/java/com/sirrapa/maven/plugins/nodejs/NodeJsMojo.java
@@ -12,6 +12,7 @@
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
+import org.codehaus.plexus.util.Os;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.cli.CommandLineException;
import org.codehaus.plexus.util.cli.CommandLineUtils;
@@ -70,23 +71,6 @@
@Parameter(defaultValue = "${java.io.tmpdir}/nodejs")
protected File nodejsDirectory;
- private static final String CLASSIFIER_WINDOWS_FAMILY = "win";
- private static final String CLASSIFIER_LINUX_FAMILY = "linux";
- private static final String CLASSIFIER_UNIX_FAMILY = "unix";
- private static final String CLASSIFIER_MAC_FAMILY = "mac";
-
- public static final String WINDOWS_FAMILY = "windows";
- public static final String LINUX_FAMILY = "linux";
- public static final String SOLARIS_FAMILY = "sunos";
- public static final String MAC_FAMILY = "mac";
-
- public static final String[][] OS_CLASSIFIER_FAMILIES = new String[][]{
- {WINDOWS_FAMILY, CLASSIFIER_WINDOWS_FAMILY},
- {LINUX_FAMILY, CLASSIFIER_LINUX_FAMILY},
- {SOLARIS_FAMILY, CLASSIFIER_UNIX_FAMILY},
- {MAC_FAMILY, CLASSIFIER_MAC_FAMILY}};
-
-
/**
* Execution of NodeJs
*/
@@ -127,13 +111,13 @@ public void execute() throws MojoExecutionException {
protected String getNodeJsExecutable(String osFamily) {
getLog().debug("Determing executable for osFamily = " + osFamily);
StringBuilder sb = new StringBuilder(nodejsDirectory.getAbsolutePath());
- if (osFamily.toLowerCase().startsWith(CLASSIFIER_WINDOWS_FAMILY)) {
+ if (Os.FAMILY_WINDOWS.equals(osFamily)|| Os.FAMILY_DOS.equals(osFamily)) {
sb.append(File.separator).append("node.exe");
}
else {
sb.append(File.separator).append("bin").append(File.separator).append("node");
}
- getLog().debug("Determined executable for osFamily: " + sb.toString());
+ getLog().info("Determined executable for osFamily: " + sb.toString());
return sb.toString();
}
@@ -239,34 +223,12 @@ protected Artifact resolveNodeJsArtifact() throws ArtifactNotFoundException, Art
*/
protected void initializeModulesConfiguration()
throws NodeJsPluginException {
- nodejsClassifier = getOSClassifier();
+ nodejsClassifier = Os.OS_FAMILY;
if (modules == null) {
throw new NodeJsPluginException("No NodeJs modules configured.");
}
}
- /**
- * @return the operating system name
- */
- private static String getOsName() {
- return System.getProperty("os.name").toLowerCase();
- }
- /**
- * Get the OS classifier family, this looks in the OS_FAMILIES array for a match
- *
- * @return the os family
- */
- public static String getOSClassifier() {
- String osName = getOsName();
- String classifier = null;
- for (int i = 0; i < OS_CLASSIFIER_FAMILIES.length; ++i) {
- if (osName.startsWith(OS_CLASSIFIER_FAMILIES[i][0])) {
- classifier = OS_CLASSIFIER_FAMILIES[i][1];
- break;
- }
- }
- return classifier;
- }
}
View
71 src/test/java/com/sirrapa/maven/plugins/nodejs/NodeJsMojoTest.java
@@ -11,30 +11,39 @@
import org.apache.maven.artifact.handler.manager.DefaultArtifactHandlerManager;
import org.apache.maven.artifact.repository.DefaultArtifactRepository;
import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
+import org.apache.maven.monitor.logging.DefaultLog;
+import org.apache.maven.plugin.Mojo;
+import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.apache.maven.plugin.testing.ArtifactStubFactory;
import org.apache.maven.plugin.testing.stubs.StubArtifactResolver;
+import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
+import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.FileUtils;
+import org.codehaus.plexus.util.Os;
import org.codehaus.plexus.util.cli.Commandline;
public class NodeJsMojoTest extends AbstractMojoTestCase {
NodeJsMojo mojo;
private final String localRepoDir = "target/local-repo/";
private final String workDir = System.getProperty("java.io.tmpdir") + "nodejs/";
+ private Logger log;
@Override
public void setUp() throws Exception {
// required for mojo lookups to work
super.setUp();
+ log = container.getLogger();
+
File dir;
- System.out.println(">>>Cleaning nodejsDirectory " + getBasedir() + "/" + workDir + "...");
+ log.debug(">>>Cleaning nodejsDirectory " + getBasedir() + "/" + workDir + "...");
dir = new File(workDir);
FileUtils.deleteDirectory(dir);
FileUtils.forceMkdir(dir);
- System.out.println(">>>Cleaning local repo " + getBasedir() + "/" + localRepoDir + "...");
+ log.debug(">>>Cleaning local repo " + getBasedir() + "/" + localRepoDir + "...");
dir = new File(getBasedir() + "/" + localRepoDir);
FileUtils.deleteDirectory(dir);
@@ -48,7 +57,7 @@ private void loadMojo(String pomFileName) throws Exception {
assertNotNull("Mojo is null after lookupMojo call", mojo);
final DefaultArtifactRepository repository = new DefaultArtifactRepository("local",
- new File(getBasedir(), localRepoDir).toURI().toURL().toString(), new DefaultRepositoryLayout());
+ new File(getBasedir(), localRepoDir).toURI().toURL().toString(), new DefaultRepositoryLayout());
repository.setBasedir(getBasedir() + localRepoDir);
mojo.setLocal(repository);
@@ -59,7 +68,7 @@ private void loadMojo(String pomFileName) throws Exception {
setVariableValueToObject(artifactHandlerManager, "artifactHandlers", new HashMap());
Map<String, ?> handlerDescriptors = getContainer().getComponentDescriptorMap(ArtifactHandler.ROLE);
if (handlerDescriptors != null) {
- mojo.getLog().debug("Registering all unregistered ArtifactHandlers...");
+ log.debug("Registering all unregistered ArtifactHandlers...");
Set<String> existingHints = artifactHandlerManager.getHandlerTypes();
if (existingHints != null) {
@@ -69,16 +78,18 @@ private void loadMojo(String pomFileName) throws Exception {
}
if (handlerDescriptors.isEmpty()) {
- mojo.getLog().debug("All ArtifactHandlers are registered. Continuing...");
- } else {
+ log.debug("All ArtifactHandlers are registered. Continuing...");
+ }
+ else {
Map<String, ArtifactHandler> unregisteredHandlers = new HashMap<String, ArtifactHandler>(handlerDescriptors.size());
for (String hint : handlerDescriptors.keySet()) {
try {
unregisteredHandlers.put(hint, (ArtifactHandler) lookup(ArtifactHandler.ROLE, hint));
- mojo.getLog().info("Adding ArtifactHandler for: " + hint);
- } catch (ComponentLookupException e) {
- mojo.getLog().warn("Failed to lookup ArtifactHandler with hint: " + hint + ". Reason: " + e.getMessage(), e);
+ log.debug("Adding ArtifactHandler for: " + hint);
+ }
+ catch (ComponentLookupException e) {
+ log.warn("Failed to lookup ArtifactHandler with hint: " + hint + ". Reason: " + e.getMessage(), e);
}
}
@@ -98,7 +109,8 @@ public void testMissingModules() throws Exception {
try {
mojo.initializeModulesConfiguration();
fail("An NodeJsPluginException was expected");
- } catch (NodeJsPluginException e) {
+ }
+ catch (NodeJsPluginException e) {
//this is expected
//e.printStackTrace();
assertEquals("The exception is invalid", "No NodeJs modules configured.", e.getMessage());
@@ -111,7 +123,8 @@ public void testMissingModule() throws Exception {
setVariableValueToObject(mojo, "modules", null);
mojo.initializeModulesConfiguration();
fail("An NodeJsPluginException was expected");
- } catch (NodeJsPluginException t) {
+ }
+ catch (NodeJsPluginException t) {
//this is expected
//t.printStackTrace();
assertEquals("The exception message is invalid", "No NodeJs modules configured.", t.getMessage());
@@ -122,8 +135,9 @@ public void testMissingModuleName() throws Exception {
loadMojo("plugin-config-no-module-name.xml");
try {
mojo.initializeModulesConfiguration();
-// fail("An NodeJsPluginException was expected");
- } catch (NodeJsPluginException t) {
+ // fail("An NodeJsPluginException was expected");
+ }
+ catch (NodeJsPluginException t) {
//this is expected
//t.printStackTrace();
assertNotNull("Expected an exception cause.", t.getCause());
@@ -148,11 +162,12 @@ public void testUnpackNodeJs() throws Exception {
tmpDir.mkdirs();
mojo.unpackNodeJs(artifact, tmpDir);
- String nodejsEx = mojo.getNodeJsExecutable(mojo.getOSClassifier());
+ String nodejsEx = mojo.getNodeJsExecutable(mojo.nodejsClassifier);
assertTrue(String.format("Extract of nodejs to '%s' failed", tmpDir.getAbsolutePath()), tmpDir.exists());
- } finally {
+ }
+ finally {
tmpDir.delete();
}
}
@@ -164,13 +179,15 @@ public void testGetNodeJsExecutable() throws Exception {
setVariableValueToObject(mojo, "nodejsDirectory", new File(System.getProperty("java.io.tmpdir") + "nodejs/"));
String nodejsEx;
- for (String[] family : NodeJsMojo.OS_CLASSIFIER_FAMILIES) {
- nodejsEx = mojo.getNodeJsExecutable(family[0]);
- System.out.println("nodejsEx = " + nodejsEx);
+ for (String family : Os.getValidFamilies()) {
+ Os os = new Os(family);
+ nodejsEx = mojo.getNodeJsExecutable(family);
+ log.debug(String.format("nodejsEx for %s = %s", family, nodejsEx));
- if (NodeJsMojo.WINDOWS_FAMILY.endsWith(family[0])) {
+ if (Os.FAMILY_WINDOWS.equals(family) || Os.FAMILY_DOS.equals(family)) {
assertTrue(String.format("The nodejs executable for %s family is invalid", family), nodejsEx.endsWith(File.separator + "nodejs" + File.separator + "node.exe"));
- } else {
+ }
+ else {
assertTrue(String.format("The nodejs executable for %s family is invalid", family), nodejsEx.endsWith(File.separator + "nodejs" + File.separator + "bin" + File.separator + "node"));
}
}
@@ -182,29 +199,31 @@ public void testGetCommandLine() throws Exception {
mojo.initializeModulesConfiguration();
String nodejsDir = "/tmp/nodejs";
String workDir = "/work";
- setVariableValueToObject(mojo, "nodejsDirectory", new File(nodejsDir+"/"));
+ setVariableValueToObject(mojo, "nodejsDirectory", new File(nodejsDir + "/"));
String executable = mojo.getNodeJsExecutable("win");
assertNotNull("executable is null", executable);
- Commandline cmd = mojo.getCommandLine(new File(workDir+"/"), executable + " -o foobar", null);
+ Commandline cmd = mojo.getCommandLine(new File(workDir + "/"), executable + " -o foobar", null);
assertNotNull("commandline is null", cmd);
/** the expected string differs per OS... */
String expected;
- if (NodeJsMojo.WINDOWS_FAMILY.toLowerCase().startsWith(NodeJsMojo.getOSClassifier())) {
+ if (Os.FAMILY_WINDOWS.equals(Os.OS_FAMILY) || Os.FAMILY_DOS.equals(Os.OS_FAMILY)) {
expected = "cmd.exe /X /C \"%s -o foobar\"";
assertEquals("The windows commandline string is invalid", String.format(expected, executable), cmd.toString());
- } else {
+ }
+ else {
expected = "/bin/sh -c cd %s && \"%s -o foobar\"";
assertEquals("The commandline string is invalid", String.format(expected, workDir, executable), cmd.toString());
}
cmd = mojo.getCommandLine(new File(workDir), executable, "-o foobar", null);
assertNotNull("commandline is null", cmd);
/** the expected string differs per OS... */
- if (NodeJsMojo.WINDOWS_FAMILY.toLowerCase().startsWith(NodeJsMojo.getOSClassifier())) {
+ if (Os.isFamily(mojo.nodejsClassifier)) {
expected = "cmd.exe /X /C \"%s -o foobar\"";
assertEquals("The windows commandline with argument string is invalid", String.format(expected, executable), cmd.toString());
- } else {
+ }
+ else {
expected = "/bin/sh -c cd %s && %s -o foobar";
assertEquals("The commandline with argument string is invalid", String.format(expected, workDir, executable), cmd.toString());
}
View
7 src/test/resources/log4j.properties
@@ -0,0 +1,7 @@
+log4j.rootLogger=INFO, C
+
+log4j.appender.C=org.apache.log4j.ConsoleAppender
+log4j.appender.C.layout=org.apache.log4j.PatternLayout
+log4j.appender.C.layout.ConversionPattern=%d{dd.MM.yyyy HH:mm:ss} %X{shopid} [%t] (%F:%L) [%-5p] %m%n
+
+log4j.logger.com.sirrapa.maven.plugins.nodejs=DEBUG
View
1 src/test/resources/projects/project-001/pom.xml
@@ -18,6 +18,7 @@
<artifactId>nodejs-maven-plugin</artifactId>
<version>0.8.15-001</version>
<configuration>
+
<nodejsVersion>0.8.15</nodejsVersion>
<workingDirectory>${project.build.outputDirectory}</workingDirectory>
<modules>

0 comments on commit 0362270

Please sign in to comment.
Something went wrong with that request. Please try again.