From 9d7b30fa23d9f17d9466033fa7ca30169ec10e26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Martins?= Date: Fri, 15 Jul 2016 14:58:34 -0400 Subject: [PATCH] Make getSdkPath and getJavaToolsJar public. (#164) * Make getSdkPath public for testing. * Fixes an issue with resolving paths in CloudSdk and adds unit tests. * Replaced toFile() with Files.isDirectory()/isRegularFile(). --- .../tools/appengine/cloudsdk/CloudSdk.java | 67 +++++++++++++------ .../appengine/cloudsdk/CloudSdkTest.java | 40 +++++++++++ 2 files changed, 85 insertions(+), 22 deletions(-) create mode 100644 appengine-plugins-core/src/test/java/com/google/cloud/tools/appengine/cloudsdk/CloudSdkTest.java diff --git a/appengine-plugins-core/src/main/java/com/google/cloud/tools/appengine/cloudsdk/CloudSdk.java b/appengine-plugins-core/src/main/java/com/google/cloud/tools/appengine/cloudsdk/CloudSdk.java index b31cdc217..6603cb166 100644 --- a/appengine-plugins-core/src/main/java/com/google/cloud/tools/appengine/cloudsdk/CloudSdk.java +++ b/appengine-plugins-core/src/main/java/com/google/cloud/tools/appengine/cloudsdk/CloudSdk.java @@ -29,9 +29,11 @@ import com.google.common.collect.Maps; import java.io.File; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.logging.Logger; @@ -52,6 +54,7 @@ public class CloudSdk { "platform/google_appengine/google/appengine/tools/java/lib"; private static final String JAVA_TOOLS_JAR = "appengine-tools-api.jar"; private static final String WINDOWS_BUNDLED_PYTHON = "platform/bundledpython/python.exe"; + private static final Map JAR_LOCATIONS = new HashMap<>(); private final Path sdkPath; private final ProcessRunner processRunner; @@ -93,6 +96,14 @@ private CloudSdk(Path sdkPath, String appCommandMetricsEnvironment, this.processRunner = new DefaultProcessRunner(async, stdOutLineListeners, stdErrLineListeners, exitListeners, startListeners); + // Populate jar locations. + // TODO(joaomartins): Consider case where SDK doesn't contain these jars. Only App Engine + // SDK does. + JAR_LOCATIONS.put("servlet-api.jar", + getJavaAppEngineSdkPath().resolve("shared/servlet-api.jar")); + JAR_LOCATIONS.put("jsp-api.jar", getJavaAppEngineSdkPath().resolve("shared/jsp-api.jar")); + JAR_LOCATIONS.put(JAVA_TOOLS_JAR, + sdkPath.resolve(JAVA_APPENGINE_SDK_PATH).resolve(JAVA_TOOLS_JAR)); } /** @@ -162,7 +173,7 @@ public void runAppCfgCommand(List args) throws ProcessRunnerException { command.add( Paths.get(System.getProperty("java.home")).resolve("bin/java").toAbsolutePath().toString()); command.add("-cp"); - command.add(getJavaToolsJar().toAbsolutePath().toString()); + command.add(JAR_LOCATIONS.get(JAVA_TOOLS_JAR).toString()); command.add("com.google.appengine.tools.admin.AppCfg"); command.addAll(args); @@ -175,7 +186,7 @@ private void logCommand(List command) { logger.info("submitting command: " + WHITESPACE_JOINER.join(command)); } - private Path getSdkPath() { + public Path getSdkPath() { return sdkPath; } @@ -195,14 +206,20 @@ public Path getJavaAppEngineSdkPath() { return getSdkPath().resolve(JAVA_APPENGINE_SDK_PATH); } - private Path getJavaToolsJar() { - return getJavaAppEngineSdkPath().resolve(JAVA_TOOLS_JAR); - } - private Path getWindowsPythonPath() { return getSdkPath().resolve(WINDOWS_BUNDLED_PYTHON); } + /** + * Gets the file system location for an SDK jar. + * + * @param jarName the jar file name. For example, "servlet-api.jar" + * @return the path in the file system + */ + public Path getJarPath(String jarName) { + return JAR_LOCATIONS.get(jarName); + } + /** * Checks whether the configured Cloud SDK Path is valid. * @@ -212,26 +229,28 @@ public void validate() throws AppEngineException { if (sdkPath == null) { throw new AppEngineException("Validation Error: SDK path is null"); } - if (!sdkPath.toFile().isDirectory()) { + if (!Files.isDirectory(sdkPath)) { throw new AppEngineException( - "Validation Error: SDK directory '" + sdkPath + "' is not valid"); + "Validation Error: SDK location '" + sdkPath + "' is not a directory."); } - if (!getGCloudPath().toFile().isFile()) { + if (!Files.isRegularFile(getGCloudPath())) { throw new AppEngineException( - "Validation Error: gcloud path '" + getGCloudPath() + "' is not valid"); + "Validation Error: gcloud location '" + getGCloudPath() + "' is not a file."); } - if (!getDevAppServerPath().toFile().isFile()) { + if (!Files.isRegularFile(getDevAppServerPath())) { throw new AppEngineException( - "Validation Error: dev_appserver.py path '" + getDevAppServerPath() + "' is not valid"); + "Validation Error: dev_appserver.py location '" + + getDevAppServerPath() + "' is not a file."); } - if (!getJavaAppEngineSdkPath().toFile().isDirectory()) { + if (!Files.isDirectory(getJavaAppEngineSdkPath())) { throw new AppEngineException( - "Validation Error: Java App Engine SDK path '" + getJavaAppEngineSdkPath() - + "' is not valid"); + "Validation Error: Java App Engine SDK location '" + + getJavaAppEngineSdkPath() + "' is not a directory."); } - if (!getJavaToolsJar().toFile().isFile()) { + if (!Files.isRegularFile(JAR_LOCATIONS.get(JAVA_TOOLS_JAR))) { throw new AppEngineException( - "Validation Error: Java Tools jar path '" + getJavaToolsJar() + "' is not valid"); + "Validation Error: Java Tools jar location '" + + JAR_LOCATIONS.get(JAVA_TOOLS_JAR) + "' is not a file."); } } @@ -250,12 +269,13 @@ public static class Builder { private int runDevAppServerWaitSeconds; /** - * The home directory of Google Cloud SDK. If not set, will attempt to look for the SDK in known - * install locations. + * The home directory of Google Cloud SDK. + * + * @param sdkPath the root path for the Cloud SDK */ - public Builder sdkPath(File sdkPathFile) { - if (sdkPathFile != null) { - this.sdkPath = sdkPathFile.toPath(); + public Builder sdkPath(Path sdkPath) { + if (sdkPath != null) { + this.sdkPath = sdkPath; } return this; } @@ -352,6 +372,9 @@ public Builder runDevAppServerWait(int runDevAppServerWaitSeconds) { /** * Create a new instance of {@link CloudSdk}. + * + *

If {@code sdkPath} is not set, this method will look for the SDK in known install + * locations. */ public CloudSdk build() { diff --git a/appengine-plugins-core/src/test/java/com/google/cloud/tools/appengine/cloudsdk/CloudSdkTest.java b/appengine-plugins-core/src/test/java/com/google/cloud/tools/appengine/cloudsdk/CloudSdkTest.java new file mode 100644 index 000000000..bb7f6739d --- /dev/null +++ b/appengine-plugins-core/src/test/java/com/google/cloud/tools/appengine/cloudsdk/CloudSdkTest.java @@ -0,0 +1,40 @@ +package com.google.cloud.tools.appengine.cloudsdk; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.runners.MockitoJUnitRunner; + +import java.nio.file.Path; +import java.nio.file.Paths; + +/** + * Unit tests for {@link CloudSdk} + */ +@RunWith(MockitoJUnitRunner.class) +public class CloudSdkTest { + @Test + public void testGetSdkPath() { + Path location = Paths.get("/"); + CloudSdk sdk = new CloudSdk.Builder().sdkPath(location).build(); + assertEquals(location, sdk.getSdkPath()); + } + + @Test + public void testGetJavaAppEngineSdkPath() { + Path location = Paths.get("/"); + CloudSdk sdk = new CloudSdk.Builder().sdkPath(location).build(); + assertEquals(location.resolve("platform/google_appengine/google/appengine/tools/java/lib"), + sdk.getJavaAppEngineSdkPath()); + } + + @Test + public void testGetJarPathJavaTools() { + Path location = Paths.get("/"); + CloudSdk sdk = new CloudSdk.Builder().sdkPath(location).build(); + assertEquals(Paths.get("/platform/google_appengine/google/appengine" + + "/tools/java/lib/appengine-tools-api.jar"), + sdk.getJarPath("appengine-tools-api.jar")); + } +}