Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #64 from salesforce/59-negative-project-import-tests
Browse files Browse the repository at this point in the history
Added negative project import tests
  • Loading branch information
beginningineer committed May 21, 2020
2 parents 8c01292 + fd21200 commit 0f66be1
Show file tree
Hide file tree
Showing 24 changed files with 383 additions and 46 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ eclipse.sh
.DS_Store

# Bazel build result
**/bazel-bazel-ls-demo-project
**/bazel-bin
**/bazel-out
**/bazel-testlogs
**/bazel-*

# Checkstyle files
*.checkstyle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;

import org.eclipse.core.runtime.CoreException;
Expand All @@ -47,6 +49,7 @@
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.osgi.framework.BundleContext;
import org.osgi.service.prefs.BackingStoreException;
import org.osgi.service.prefs.Preferences;
Expand All @@ -58,6 +61,7 @@
import com.salesforce.b2eclipse.command.CommandBuilder;
import com.salesforce.b2eclipse.command.shell.ShellCommandBuilder;
import com.salesforce.b2eclipse.config.BazelAspectLocationImpl;
import com.salesforce.b2eclipse.managers.B2EPreferncesManager;
import com.salesforce.b2eclipse.runtime.api.JavaCoreHelper;
import com.salesforce.b2eclipse.runtime.api.ResourceHelper;
import com.salesforce.b2eclipse.runtime.impl.EclipseJavaCoreHelper;
Expand Down Expand Up @@ -145,6 +149,12 @@ public void start(BundleContext bundleContext) throws Exception {

startInternal(aspectLocation, commandBuilder, eclipseResourceHelper, eclipseJavaCoreHelper);

Map<String, Object> configuration = JavaLanguageServerPlugin.getPreferencesManager().getPreferences().asMap();
if (configuration == null) {
configuration = new HashMap<>();
}
B2EPreferncesManager preferencesManager = B2EPreferncesManager.getInstance();
preferencesManager.setConfiguration(configuration);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ private static IFolder createFoldersForRelativePackagePath(IProject project, Str

if (!generatedSources) {
if (!packageSourceCodeFSRelativePath.startsWith(bazelPackageFSPath)) {
throw new IllegalStateException("src code path exepcted to be under bazel package path");
throw new IllegalStateException("src code path expected to be under bazel package path");
}
if (Paths.get(packageSourceCodeFSRelativePath).equals(Paths.get(bazelPackageFSPath))) {
throw new IllegalStateException("did not expect src code path to be equals to the bazel package path");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

import java.util.Map;

import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.handlers.MapFlattener;

@SuppressWarnings("restriction")
Expand Down Expand Up @@ -73,7 +72,10 @@ public final class B2EPreferncesManager {
private String importBazelTestPath;

private B2EPreferncesManager() {
Map<String, Object> configuration = JavaLanguageServerPlugin.getPreferencesManager().getPreferences().asMap();

}

public void setConfiguration(Map<String, Object> configuration) {
importBazelEnabled = MapFlattener.getBoolean(configuration, IMPORT_BAZEL_ENABLED, false);
importBazelSrcPath = MapFlattener.getString(configuration, BAZEL_SRC_PATH, BAZEL_DEFAULT_SRC_PATH);
importBazelTestPath = MapFlattener.getString(configuration, BAZEL_TEST_PATH, BAZEL_DEFAULT_TEST_PATH);
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
load("@rules_java//java:defs.bzl", "java_binary")

java_binary(
name = "ProjectRunner",
srcs = glob(["*.java"]),
main_class = "com.example.ProjectRunner",
deps = [":greeter"],
)

java_library(
name = "greeter",
srcs = ["Greeting.java"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example;

public class Greeting {
public static void sayHi() {
System.out.println("Hi!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example;

public class ProjectRunner {
public static void main(String args[]) {
Greeting.sayHi();
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@rules_java//java:defs.bzl", "java_binary")

java_binary(
name = "ProjectRunner",
srcs = glob(["java/src/example/ProjectRunner.java"]),
main_class = "example.ProjectRunner",
deps = ["//module/submodule:greeter"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package example;

import greeting.SayHi;

public class ProjectRunner {
public static void main(String args[]) {
SayHi.sayHi();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("@rules_java//java:defs.bzl", "java_library")

java_library(
name = "greeter",
srcs = glob(["java/src/greeting/SayHi.java"]),
visibility = ["//visibility:public"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package greeting;

public class SayHi {
public static void sayHi() {
System.out.println("Hi!");
}
}
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
load("@rules_java//java:defs.bzl", "java_binary")

java_binary(
name = "ProjectRunner",
srcs = glob(["src/main/java/com/example/ProjectRunner.java"]),
main_class = "com.example.ProjectRunner",
deps = [":greeter"],
)

java_library(
name = "greeter",
srcs = ["src/main/java/com/example/Greeting.java"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example;

public class Greeting {
public static void sayHi() {
System.out.println("Hi!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example;

public class ProjectRunner {
public static void main(String args[]) {
Greeting.sayHi();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public void testGetProject() {
BazelNature nature = new BazelNature();
IProject actual = nature.getProject();
assertEquals(actual, expected);
System.out.println(nature);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,80 +23,84 @@
*
*/

package com.salesforce.b2eclipse;
package com.salesforce.b2eclipse.importer;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager;
import org.eclipse.jdt.ls.core.internal.preferences.Preferences;
import org.junit.Before;
import org.junit.Test;

import com.salesforce.b2eclipse.config.BazelEclipseProjectFactory;
import com.salesforce.b2eclipse.importer.BazelProjectImportScanner;
import com.salesforce.b2eclipse.managers.B2EPreferncesManager;
import com.salesforce.b2eclipse.model.BazelPackageInfo;
import com.salesforce.b2eclipse.runtime.impl.EclipseWorkProgressMonitor;

@SuppressWarnings("restriction")
public class BazelImportTest {
/**
* An abstract class for importing a Bazel project.
*/
public abstract class BaseBazelImproterTest {

public static final String IMPORT_BAZEL_ENABLED = "java.import.bazel.enabled";

private static final String BAZEL_SRC_PATH = "java.import.bazel.src.path";
public static final String BAZEL_SRC_PATH = "java.import.bazel.src.path";

private static final String BAZEL_SRC_PATH_VALUE = "/java/src";
public static final String BAZEL_SRC_PATH_VALUE = "/java/src";

public static final String BAZEL_SRC_PATH_VALUE_FOR_BUILD_WITH_CLASS_TEST = "/";

private BazelPackageInfo workspaceRootPackage;
private IWorkspaceRoot workspaceRoot;
private BazelProjectImportScanner scanner;

public BazelImportTest() {
public BaseBazelImproterTest() {
IWorkspace workspace = ResourcesPlugin.getWorkspace();
this.workspaceRoot = workspace.getRoot();
scanner = new BazelProjectImportScanner();
}

@Before
public void setup() {
PreferenceManager manager = new PreferenceManager();
protected void setSettings(String path) {
Map<String, Object> settings = new HashMap<>();
settings.put(BAZEL_SRC_PATH, BAZEL_SRC_PATH_VALUE);
Preferences pref = Preferences.createFrom(settings);
manager.update(pref);
JavaLanguageServerPlugin.setPreferencesManager(manager);
BazelProjectImportScanner scanner = new BazelProjectImportScanner();
BazelPackageInfo workspaceRootPackage = scanner.getProjects("projects/bazel-ls-demo-project");
settings.put(IMPORT_BAZEL_ENABLED, true);
settings.put(BAZEL_SRC_PATH, path);
B2EPreferncesManager preferencesManager = B2EPreferncesManager.getInstance();
preferencesManager.setConfiguration(settings);
}

protected void importProject() {
List<BazelPackageInfo> bazelPackagesToImport =
workspaceRootPackage.getChildPackageInfos().stream().collect(Collectors.toList());

BazelEclipseProjectFactory.importWorkspace(workspaceRootPackage, bazelPackagesToImport,
new EclipseWorkProgressMonitor(), new NullProgressMonitor());
}

@Test
public void testImport() throws CoreException {
IProject module1Proj = workspaceRoot.getProject("module1");
IProject module2Proj = workspaceRoot.getProject("module2");
IProject module3Proj = workspaceRoot.getProject("module3");
public BazelPackageInfo getWorkspaceRootPackage() {
return workspaceRootPackage;
}

IProject[] referencedProjects = module1Proj.getReferencedProjects();
public void setWorkspaceRootPackage(BazelPackageInfo workspaceRootPackage) {
this.workspaceRootPackage = workspaceRootPackage;
}

public IWorkspaceRoot getWorkspaceRoot() {
return workspaceRoot;
}

assertEquals(2, referencedProjects.length);
public void setWorkspaceRoot(IWorkspaceRoot workspaceRoot) {
this.workspaceRoot = workspaceRoot;
}

assertNotNull("Didn't find module2 in the referenced projects list",
Arrays.stream(referencedProjects).anyMatch(proj -> proj.equals(module2Proj)));
public BazelProjectImportScanner getScanner() {
return scanner;
}

assertNotNull("Didn't find module3 in the referenced projects list",
Arrays.stream(referencedProjects).anyMatch(proj -> proj.equals(module3Proj)));
public void setScanner(BazelProjectImportScanner scanner) {
this.scanner = scanner;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Copyright (c) 2020, Salesforce.com, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
* following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
* disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of Salesforce.com nor the names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

package com.salesforce.b2eclipse.importer;

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

import java.util.Arrays;

import org.eclipse.core.resources.IProject;

import org.eclipse.core.runtime.CoreException;

import org.junit.Before;
import org.junit.Test;

/**
* Test class for importing a general Bazel test project.
*/
public class BazelImportTest extends BaseBazelImproterTest {

public BazelImportTest() {
super.setWorkspaceRootPackage(getScanner().getProjects("projects/bazel-ls-demo-project"));
}

@Before
public void setup() {
setSettings(BAZEL_SRC_PATH_VALUE);
importProject();
}

@Test
public void testImport() throws CoreException {
IProject module1Proj = getWorkspaceRoot().getProject("module1");
IProject module2Proj = getWorkspaceRoot().getProject("module2");
IProject module3Proj = getWorkspaceRoot().getProject("module3");

IProject[] referencedProjects = module1Proj.getReferencedProjects();

assertEquals(2, referencedProjects.length);

assertTrue("Didn't find module2 in the referenced projects list",
Arrays.stream(referencedProjects).anyMatch(proj -> proj.equals(module2Proj)));

assertTrue("Didn't find module3 in the referenced projects list",
Arrays.stream(referencedProjects).anyMatch(proj -> proj.equals(module3Proj)));
}
}
Loading

0 comments on commit 0f66be1

Please sign in to comment.