Skip to content

Commit

Permalink
Fixed unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
joaoandremartins committed Jul 18, 2016
1 parent 1884c5b commit 59fbac1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,45 @@
package com.google.cloud.tools.eclipse.appengine.localserver;

import static org.mockito.Mockito.when;

import java.nio.file.Paths;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IClasspathEntry;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

// Must Run as JUnit Plug-in Test in Eclipse.
import com.google.cloud.tools.appengine.cloudsdk.CloudSdk;
import com.google.cloud.tools.eclipse.sdk.CloudSdkProvider;

@RunWith(MockitoJUnitRunner.class)
public class ServletClasspathProviderTest {

private ServletClasspathProvider provider = new ServletClasspathProvider();
@Mock private CloudSdkProvider cloudSdkProvider;
@Mock private CloudSdk cloudSdk;

@Before
public void setUp() {
when(cloudSdkProvider.getCloudSdk()).thenReturn(cloudSdk);
when(cloudSdk.getJarPath("servlet-api.jar")).thenReturn(Paths.get("/path/to/servlet-api.jar"));
when(cloudSdk.getJarPath("jsp-api.jar")).thenReturn(Paths.get("/path/to/jsp-api.jar"));
}

@Test
public void testResolveClasspathContainer() {
IClasspathEntry[] result = provider.resolveClasspathContainer(null, null);
Assert.assertTrue(result[0].getPath().toString().endsWith("servlet-api.jar"));
Assert.assertTrue(result[1].getPath().toString().endsWith("jsp-api.jar"));
provider.setCloudSdkProvider(cloudSdkProvider);
IClasspathEntry[] result = provider.resolveClasspathContainer(null, null);
Assert.assertTrue(result[0].getPath().toString().endsWith("servlet-api.jar"));
Assert.assertTrue(result[1].getPath().toString().endsWith("jsp-api.jar"));
}

@Test
public void testResolveClasspathContainer_mavenProject() throws CoreException {
IProject project = Mockito.mock(IProject.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
import com.google.cloud.tools.appengine.cloudsdk.CloudSdk;
import com.google.cloud.tools.eclipse.sdk.CloudSdkProvider;
import com.google.cloud.tools.eclipse.util.MavenUtils;
import com.google.common.annotations.VisibleForTesting;

/**
* Supply Java standard classes, specifically servlet-api.jar and jsp-api.jar,
* to non-Maven projects.
*/
public class ServletClasspathProvider extends RuntimeClasspathProviderDelegate {

private CloudSdkProvider cloudSdkProvider = new CloudSdkProvider();

public ServletClasspathProvider() {
}
Expand All @@ -31,8 +34,8 @@ public IClasspathEntry[] resolveClasspathContainer(IProject project, IRuntime ru

@Override
public IClasspathEntry[] resolveClasspathContainer(IRuntime runtime) {
CloudSdk cloudSdk = new CloudSdkProvider().getCloudSdk();
if (cloudSdk == null || cloudSdk.getJavaAppEngineSdkPath() == null) {
CloudSdk cloudSdk = cloudSdkProvider.getCloudSdk();
if (cloudSdk == null) {
return new IClasspathEntry[0];
};
java.nio.file.Path servletJar = cloudSdk.getJarPath("servlet-api.jar");
Expand All @@ -43,6 +46,10 @@ public IClasspathEntry[] resolveClasspathContainer(IRuntime runtime) {

IClasspathEntry[] entries = {servletEntry, jspEntry};
return entries;
}
}

@VisibleForTesting
public void setCloudSdkProvider(CloudSdkProvider cloudSdkProvider) {
this.cloudSdkProvider = cloudSdkProvider;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@

package com.google.cloud.tools.eclipse.sdk.internal;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertTrue;

import com.google.cloud.tools.appengine.api.AppEngineException;
import com.google.cloud.tools.appengine.cloudsdk.CloudSdk;
import com.google.cloud.tools.eclipse.sdk.CloudSdkProvider;

Expand All @@ -40,15 +38,11 @@ public void setUp() {
}

/** Verify that the preference overrides auto discovery. */
@Test(expected = AppEngineException.class)
@Test
public void testSetPreferenceInvalid() {
// A path that almost certainly does not contain the SDK
File root = File.listRoots()[0];

CloudSdk instance = new CloudSdkProvider(preferences).getCloudSdk();
assertEquals(root.toPath(), instance.getSdkPath());
instance.validate();
fail("root directory should not be a valid location");
assertTrue(instance == null);
//assertEquals(root.toPath(), instance.getSdkPath());
}

private static class MockPreferences implements IPreferenceStore {
Expand Down Expand Up @@ -123,6 +117,7 @@ public long getLong(String name) {

@Override
public String getString(String name) {
// A path that almost certainly does not contain the SDK
return File.listRoots()[0].toString();
}

Expand Down

0 comments on commit 59fbac1

Please sign in to comment.