Improved classpath handling in ProjectConfiguration.

Change-Id: I4c645d958c73348795cafae9b88e2dd96a08a559
diff --git a/src/main/java/com/google/gwt/benchmark/project/ProjectConfiguration.java b/src/main/java/com/google/gwt/benchmark/project/ProjectConfiguration.java
index 747aef9..5d7db7f 100644
--- a/src/main/java/com/google/gwt/benchmark/project/ProjectConfiguration.java
+++ b/src/main/java/com/google/gwt/benchmark/project/ProjectConfiguration.java
@@ -14,16 +14,17 @@
 
 package com.google.gwt.benchmark.project;
 
-
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Joiner;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
+import com.google.common.collect.Iterables;
 import com.google.common.collect.Lists;
 
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.util.Collections;
 import java.util.List;
 import java.util.Properties;
 import java.util.logging.Logger;
@@ -39,9 +40,10 @@
   private static final String GWT_ENTRY_POINT_PROPERTY = "gwt.compiler.entrypoint";
   private static final String GWT_OPTS_PROPERTY = "gwt.opts";
   private static final String JVM_OPTS_PROPERTY = "jvm.opts";
-  private static final String TARGET_MODULE_PROPERTY = "module";
   private static final String OUT_JAR = "out.jar";
   private static final String AUX_JAR = "aux.jar";
+  private static final String PROJECT_JAR_PATH = "input_jars";
+  private static final String TARGET_MODULE_PROPERTY = "module";
 
   /**
    * Loads a project configuration from disk.
@@ -70,9 +72,17 @@
       }
       projectConfiguration.setTargetModule(targetModule);
       projectConfiguration.setPath(projectPath);
+
+      // Determine the input jars for this project.
+      File projectJarPath = new File(projectPath + "/" + PROJECT_JAR_PATH);
+      if (projectJarPath.exists()) {
+        for (File jar : projectJarPath.listFiles()) {
+          projectConfiguration.inputJars.add(PROJECT_JAR_PATH + "/" + jar.getName());
+        }
+      }
+
     } catch (IOException e) {
-      log.severe(String.format("Project '%s': could not read 'project.config' file",
-          projectDir));
+      log.severe(String.format("Project '%s': could not read 'project.config' file", projectDir));
       return null;
     }
     return projectConfiguration;
@@ -101,6 +111,7 @@
   private final String name;
   private String path;
   private String targetModule;
+  private final List<String> inputJars = Lists.newArrayList();
 
   /**
    * Constructs a project configuration.
@@ -113,23 +124,20 @@
   /**
    * Returns the complete command line required to compile this project.
    */
-  public String getCompleteCommandLine(String outDir) {
+  public String getCompleteCommandLine(String outDir, List<String> extraJars) {
     Preconditions.checkState(outDir != null && !outDir.isEmpty(),
         "Output directory should not be null nor empty");
     Preconditions.checkState(compilerEntryPoint != null && !compilerEntryPoint.isEmpty(),
         "Compiler entry point should not be null");
     Preconditions.checkState(targetModule != null && !targetModule.isEmpty(),
         "Target module should not be null");
+    String classPath = Joiner.on(":").join(Iterables.concat(extraJars, getInputJars()));
     return Joiner
         .on(" ")
         .skipNulls()
-        .join("java", "-cp @../classpath",
-            Strings.emptyToNull(javaCommandLineOpts),
-            compilerEntryPoint,
-            Strings.emptyToNull(gwtCommandLineOpts),
-            targetModule,
-            "-war", outDir + "/" + OUT_JAR,
-            "-extra", outDir + "/" + AUX_JAR, "-deploy",
+        .join("java", "-cp", classPath, Strings.emptyToNull(javaCommandLineOpts),
+            compilerEntryPoint, Strings.emptyToNull(gwtCommandLineOpts), targetModule, "-war",
+            outDir + "/" + OUT_JAR, "-extra", outDir + "/" + AUX_JAR, "-deploy",
             outDir + "/" + AUX_JAR);
   }
 
@@ -228,4 +236,11 @@
         + javaCommandLineOpts + ", compilerEntryPoint=" + compilerEntryPoint + ", targetModule="
         + targetModule + ", gwtCommandLineOpts=" + gwtCommandLineOpts + "]";
   }
+
+  /**
+   * Returns the list of input jars.
+   */
+  private final List<String> getInputJars() {
+    return Collections.unmodifiableList(inputJars);
+  }
 }
diff --git a/src/test/java/com/google/gwt/benchmark/project/ProjectConfigurationTest.java b/src/test/java/com/google/gwt/benchmark/project/ProjectConfigurationTest.java
index a4e1678..777007f 100644
--- a/src/test/java/com/google/gwt/benchmark/project/ProjectConfigurationTest.java
+++ b/src/test/java/com/google/gwt/benchmark/project/ProjectConfigurationTest.java
@@ -21,6 +21,7 @@
 
 import com.google.common.base.Function;
 import com.google.common.base.Joiner;
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Sets;
@@ -46,15 +47,15 @@
 
     assertEquals(3, projectConfigurations.size());
 
-    final HashSet<String> actualProjectNames = Sets.newHashSet(
-        Iterables.transform(projectConfigurations,
+    final HashSet<String> actualProjectNames =
+        Sets.newHashSet(Iterables.transform(projectConfigurations,
             new Function<ProjectConfiguration, String>() {
-          @Override
-          public String apply(ProjectConfiguration projectConfiguration) {
-            return projectConfiguration.getName();
-          }
-        }));
-    assertEquals(ImmutableSet.of("p1", "p2", "showcase"),  actualProjectNames);
+              @Override
+              public String apply(ProjectConfiguration projectConfiguration) {
+                return projectConfiguration.getName();
+              }
+            }));
+    assertEquals(ImmutableSet.of("p1", "p2", "showcase"), actualProjectNames);
   }
 
   /**
@@ -68,14 +69,17 @@
 
     assertNotNull(projectConfiguration);
     assertEquals("p1", projectConfiguration.getName());
-    assertEquals("-cp blah", projectConfiguration.getJavaCommandLineOpts());
+    assertEquals("-Dtest", projectConfiguration.getJavaCommandLineOpts());
     assertEquals("-draftCompile", projectConfiguration.getGwtCommandLineOpts());
     assertEquals("com.dummy.Compiler", projectConfiguration.getCompilerEntryPoint());
     assertEquals("com.target.Target", projectConfiguration.getTargetModule());
     assertEquals("src/test/resources/workdir/projects/p1", projectConfiguration.getPath());
-    assertEquals("java -cp @../classpath -cp blah com.dummy.Compiler "
-        + "-draftCompile com.target.Target -war outdir/out.jar -extra outdir/aux.jar "
-        + "-deploy outdir/aux.jar", projectConfiguration.getCompleteCommandLine("outdir"));
+    assertEquals(
+        "java -cp gwt-user.jar:gwt-dev.jar -Dtest com.dummy.Compiler "
+            + "-draftCompile com.target.Target -war outdir/out.jar -extra outdir/aux.jar "
+            + "-deploy outdir/aux.jar",
+        projectConfiguration.getCompleteCommandLine("outdir",
+            ImmutableList.of("gwt-user.jar", "gwt-dev.jar")));
   }
 
   /**
@@ -106,14 +110,16 @@
     assertEquals("com.google.gwt.dev.Compiler", projectConfiguration.getCompilerEntryPoint());
     assertEquals("p2target", projectConfiguration.getTargetModule());
     assertEquals("src/test/resources/workdir/projects/p2", projectConfiguration.getPath());
-    assertEquals("java -cp @../classpath com.google.gwt.dev.Compiler p2target -war outdir/out.jar"
-        + " -extra outdir/aux.jar -deploy outdir/aux.jar",
-        projectConfiguration.getCompleteCommandLine("outdir"));
+    assertEquals(
+        "java -cp gwt-user.jar:gwt-dev.jar com.google.gwt.dev.Compiler p2target -war outdir/out.jar"
+            + " -extra outdir/aux.jar -deploy outdir/aux.jar",
+        projectConfiguration.getCompleteCommandLine("outdir",
+            ImmutableList.of("gwt-user.jar", "gwt-dev.jar")));;
 
     try {
-      projectConfiguration.getCompleteCommandLine("");
+      projectConfiguration.getCompleteCommandLine("", ImmutableList.<String>of());
       fail("Should have thrown an exception");
-    }  catch (Exception e) {
+    } catch (Exception e) {
     }
   }
 }
diff --git a/src/test/resources/workdir/projects/p1/project.config b/src/test/resources/workdir/projects/p1/project.config
index 6857005..f0a582d 100644
--- a/src/test/resources/workdir/projects/p1/project.config
+++ b/src/test/resources/workdir/projects/p1/project.config
@@ -1,4 +1,4 @@
-jvm.opts=-cp blah
+jvm.opts=-Dtest
 gwt.opts=-draftCompile
 gwt.compiler.entrypoint=com.dummy.Compiler
 module=com.target.Target
\ No newline at end of file