Bug 574097 - Test case

Change-Id: Ia8b9679d06956c4cb6ee220e18570d7fbbf79623
Signed-off-by: Jay Arthanareeswaran <jarthana@in.ibm.com>
Reviewed-on: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/184330
Tested-by: JDT Bot <jdt-bot@eclipse.org>
diff --git a/org.eclipse.jdt.compiler.apt.tests/lib/apttestprocessors8.jar b/org.eclipse.jdt.compiler.apt.tests/lib/apttestprocessors8.jar
index a16235f..6425037 100644
--- a/org.eclipse.jdt.compiler.apt.tests/lib/apttestprocessors8.jar
+++ b/org.eclipse.jdt.compiler.apt.tests/lib/apttestprocessors8.jar
Binary files differ
diff --git a/org.eclipse.jdt.compiler.apt.tests/processors8/org/eclipse/jdt/compiler/apt/tests/processors/elements/Java12ElementProcessor.java b/org.eclipse.jdt.compiler.apt.tests/processors8/org/eclipse/jdt/compiler/apt/tests/processors/elements/Java12ElementProcessor.java
index 1519d0c..7a80f35 100644
--- a/org.eclipse.jdt.compiler.apt.tests/processors8/org/eclipse/jdt/compiler/apt/tests/processors/elements/Java12ElementProcessor.java
+++ b/org.eclipse.jdt.compiler.apt.tests/processors8/org/eclipse/jdt/compiler/apt/tests/processors/elements/Java12ElementProcessor.java
@@ -327,6 +327,9 @@
 		// Deliberately ignoring the extra bogus module to let this pass, so we can test the binary mode in next round
 		assertTrue("incorrect no of modules in root elements in "+ this.mode + " mode", (3 <= modules.size()));
 	}
+	public void testBug574097() {
+		// Nothing here. Just reaching here is not expected
+	}
 	private Element getRoot(Element elem) {
 		Element enclosingElement = elem.getEnclosingElement();
 		while (enclosingElement != null) {
diff --git a/org.eclipse.jdt.compiler.apt.tests/resources/mod_locations/bug574097/mod.one/abc/internal/A.java b/org.eclipse.jdt.compiler.apt.tests/resources/mod_locations/bug574097/mod.one/abc/internal/A.java
new file mode 100644
index 0000000..628d82a
--- /dev/null
+++ b/org.eclipse.jdt.compiler.apt.tests/resources/mod_locations/bug574097/mod.one/abc/internal/A.java
@@ -0,0 +1,6 @@
+package abc.internal;
+public class A {
+	// Without this being part of -classpath, the following
+	// should be reported, even if it's part of --processor-module-path
+	org.eclipse.jdt.compiler.apt.tests.processors.elements.Java12ElementProcessor prc = null;
+}
diff --git a/org.eclipse.jdt.compiler.apt.tests/resources/mod_locations/bug574097/mod.one/module-info.java b/org.eclipse.jdt.compiler.apt.tests/resources/mod_locations/bug574097/mod.one/module-info.java
new file mode 100644
index 0000000..c56f8e7
--- /dev/null
+++ b/org.eclipse.jdt.compiler.apt.tests/resources/mod_locations/bug574097/mod.one/module-info.java
@@ -0,0 +1,5 @@
+@java.lang.Deprecated()
+open module mod.one {
+	exports abc.internal;
+	requires apttestprocessors8;
+}
diff --git a/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/Java12ElementsTests.java b/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/Java12ElementsTests.java
index 3607c40..2adde3a 100644
--- a/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/Java12ElementsTests.java
+++ b/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/Java12ElementsTests.java
@@ -77,6 +77,10 @@
 		JavaCompiler compiler = BatchTestUtils.getEclipseCompiler();
 		internalTestWithBinary(compiler, MODULE_PROC, "12", "testRootElements5", null, "modules5", true);
 	}
+	public void testBug574097() throws IOException {
+		JavaCompiler compiler = BatchTestUtils.getEclipseCompiler();
+		internalTestWithBinary(compiler, MODULE_PROC, "12", "testBug574097", null, "bug574097", false);
+	}
 	@SuppressWarnings({ "rawtypes", "unchecked" })
 	protected void internalTestWithBinary(JavaCompiler compiler, String processor, String compliance, String testMethod, String testClass, String resourceArea,
 				boolean processBinariesAgain) throws IOException {
@@ -91,7 +95,6 @@
 			BatchTestUtils.copyResource("mod_locations/" + resourceArea + "/" + testClass, targetFolder);
 		}
 
-
 		List<String> options = new ArrayList<String>();
 		options.add("-A" + processor);
 		options.add("-A" + testMethod);
diff --git a/org.eclipse.jdt.compiler.tool.tests/resources/ bug.574097.jar b/org.eclipse.jdt.compiler.tool.tests/resources/ bug.574097.jar
new file mode 100644
index 0000000..d004096
--- /dev/null
+++ b/org.eclipse.jdt.compiler.tool.tests/resources/ bug.574097.jar
Binary files differ
diff --git a/org.eclipse.jdt.compiler.tool.tests/resources/module_locations/bug574097/mod.one/abc/A.java b/org.eclipse.jdt.compiler.tool.tests/resources/module_locations/bug574097/mod.one/abc/A.java
new file mode 100644
index 0000000..d0f1994
--- /dev/null
+++ b/org.eclipse.jdt.compiler.tool.tests/resources/module_locations/bug574097/mod.one/abc/A.java
@@ -0,0 +1,6 @@
+package abc;
+public class A {
+	// Without this being part of -classpath, the following
+	// should be reported, even if it's part of --processor-module-path
+	test.processor.TestProcessor prc = null;
+}
diff --git a/org.eclipse.jdt.compiler.tool.tests/resources/module_locations/bug574097/mod.one/module-info.java b/org.eclipse.jdt.compiler.tool.tests/resources/module_locations/bug574097/mod.one/module-info.java
new file mode 100644
index 0000000..93fb35b
--- /dev/null
+++ b/org.eclipse.jdt.compiler.tool.tests/resources/module_locations/bug574097/mod.one/module-info.java
@@ -0,0 +1,5 @@
+@java.lang.Deprecated()
+module mod.one {
+	exports abc;
+	requires bug574097;
+}
diff --git a/org.eclipse.jdt.compiler.tool.tests/src/org/eclipse/jdt/compiler/tool/tests/CompilerToolJava9Tests.java b/org.eclipse.jdt.compiler.tool.tests/src/org/eclipse/jdt/compiler/tool/tests/CompilerToolJava9Tests.java
index 03f003b..ba3ca3d 100644
--- a/org.eclipse.jdt.compiler.tool.tests/src/org/eclipse/jdt/compiler/tool/tests/CompilerToolJava9Tests.java
+++ b/org.eclipse.jdt.compiler.tool.tests/src/org/eclipse/jdt/compiler/tool/tests/CompilerToolJava9Tests.java
@@ -130,7 +130,7 @@
 		Files.createDirectories(_tmpSrcDir.toPath());
 		assertTrue("couldn't mkdirs " + _tmpSrcFolderName, _tmpSrcDir.exists());
 
-		modules_directory = getPluginDirectoryPath() + File.separator + "resources" + File.separator + "module_locations";
+		modules_directory = getPluginDirectoryPath() + File.separator + RESOURCES_DIR + File.separator + "module_locations";
 
 		Path moduleInfo = Paths.get(modules_directory, "source", "SimpleModules", "module.one", "module-info.java");
 		assertTrue("File should exist: " + moduleInfo, Files.isReadable(moduleInfo));
@@ -854,6 +854,51 @@
  	 		assertTrue("Compilation did not fail as expected ", false);
  		}
 	}
+	public void testBug574097() throws IOException {
+		if (this.isJREBelow9) return;
+		JavaCompiler compiler = this.compilers[1];
+		String tmpFolder = _tmpFolder;
+		StandardJavaFileManager manager = compiler.getStandardFileManager(null, Locale.getDefault(), Charset.defaultCharset());
+
+		// create new list containing input file
+		List<File> files = new ArrayList<File>();
+		files.add(new File(modules_directory + File.separator + "bug574097" + File.separator + 
+ 				"mod.one" + File.separator + "module-info.java"));
+		files.add(new File(modules_directory + File.separator + "bug574097" + File.separator + 
+ 				"mod.one" + File.separator + "abc" + File.separator + "A.java"));
+		
+		Iterable<? extends JavaFileObject> units = manager.getJavaFileObjectsFromFiles(files);
+		StringWriter stringWriter = new StringWriter();
+		PrintWriter printWriter = new PrintWriter(stringWriter);
+
+		List<String> options = new ArrayList<String>();
+		options = new ArrayList<String>();
+ 		options.add("-d");
+ 		options.add(tmpFolder);
+ 		options.add("--module-path");
+ 		options.add(tmpFolder);
+ 		options.add("--module-source-path");
+ 		options.add(modules_directory + File.separator + "bug574097");
+ 		options.add("--processor-module-path");
+ 		options.add(getPluginDirectoryPath() + File.separator + RESOURCES_DIR + File.separator + "bug574097.jar");
+		ByteArrayOutputStream errBuffer = new ByteArrayOutputStream();
+		PrintWriter err = new PrintWriter(errBuffer);
+		CompilerInvocationDiagnosticListener listener = new CompilerInvocationDiagnosticListener(err) {
+			@Override
+			public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
+				super.report(diagnostic);
+			}
+		};
+ 		CompilationTask task = compiler.getTask(printWriter, manager, listener, options, null, units);
+ 		// check the classpath location
+		Boolean result = task.call();
+		printWriter.flush();
+		printWriter.close();
+ 		if (result.booleanValue()) {
+ 			System.err.println("Compilation did not fail as expected: " + stringWriter.getBuffer().toString());
+ 	 		assertTrue("Compilation did not fail as expected ", false);
+ 		}
+	}
 	public void testGetJavaFileObjects() {
 		if (this.isJREBelow9) return;
 	}
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ModuleCompilationTests.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ModuleCompilationTests.java
index 4ac3593..59e9d06 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ModuleCompilationTests.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ModuleCompilationTests.java
@@ -5537,4 +5537,99 @@
 	     "",
 	     true);
 	}
+	public void testBug574097() {
+		Util.flushDirectoryContent(new File(OUTPUT_DIR));
+		String outDir = OUTPUT_DIR + File.separator + "bin";
+		String srcDir = OUTPUT_DIR + File.separator + "src";
+		File modDir = new File(OUTPUT_DIR + File.separator + "mod");
+		String moduleLoc = srcDir + File.separator + "mod.one";
+		List<String> files = new ArrayList<>();
+		writeFileCollecting(files, moduleLoc, "module-info.java",
+						"module mod.one { \n" +
+						"	exports p;\n" +
+						"	requires transitive java.compiler;\n" +
+						"}");
+		writeFileCollecting(files, moduleLoc + File.separator + "p", "TestProcessor.java",
+						"package p;\n"
+						+ "import java.util.Set;\n"
+						+ "import javax.annotation.processing.AbstractProcessor;\n"
+						+ "import javax.annotation.processing.RoundEnvironment;\n"
+						+ "import javax.lang.model.element.TypeElement;\n"
+						+ "public class TestProcessor extends AbstractProcessor {\n"
+						+ "	@Override\n"
+						+ "	public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {\n"
+						+ "		return false;\n"
+						+ "	}\n"
+						+ "}");
+
+		StringBuilder buffer = new StringBuilder();
+		buffer.append("-d " + outDir )
+		.append(" -9 ")
+		.append(" --module-path \"")
+		.append(Util.getJavaClassLibsAsString())
+		.append("\" ")
+		.append(" --module-source-path " + "\"" + srcDir + "\"");
+		for (String fileName : files)
+			buffer.append(" \"").append(fileName).append("\"");
+
+		runConformTest(new String[]{},
+			buffer.toString(),
+			"",
+			"",
+			false);
+		String jarName = modDir + File.separator + "mod.one.jar";
+		try {
+			Util.zip(new File(outDir + File.separator + "mod.one"),
+								jarName);
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		if (!modDir.exists()) {
+			if (!modDir.mkdirs()) {
+				fail("Coult not create folder " + modDir);
+			}
+		}
+		Util.flushDirectoryContent(new File(srcDir));
+		files = new ArrayList<>();
+		moduleLoc = srcDir + File.separator + "mod.two";
+		writeFileCollecting(files, moduleLoc, "module-info.java",
+						"module mod.two { \n" +
+						"	exports q;\n" +
+						"	requires java.base;\n" +
+						"	requires mod.one;\n" +
+						"}");
+		writeFileCollecting(files, moduleLoc + File.separator + "q", "A.java",
+						"package q;\n" +
+						"public class A {\n" +
+						"   p.TestProcessor prc = null;\n" +
+						"}");
+		buffer = new StringBuilder();
+		buffer.append("-d " + outDir )
+		.append(" -9 ")
+		.append(" --module-path \"")
+		.append(Util.getJavaClassLibsAsString())
+		.append("\" ")
+		.append(" --module-source-path " + "\"" + srcDir + "\"")
+		.append(" --processor-module-path " + "\"" + jarName + "\"");
+		for (String name : files)
+			buffer.append(" \"").append(name).append("\"");
+
+		runNegativeTest(new String[]{},
+			buffer.toString(),
+			"",
+			"----------\n" +
+			"1. ERROR in ---OUTPUT_DIR_PLACEHOLDER---/src/mod.two/module-info.java (at line 4)\n" +
+			"	requires mod.one;\n" +
+			"	         ^^^^^^^\n" +
+			"mod.one cannot be resolved to a module\n" +
+			"----------\n" +
+			"----------\n" +
+			"2. ERROR in ---OUTPUT_DIR_PLACEHOLDER---/src/mod.two/q/A.java (at line 3)\n" +
+			"	p.TestProcessor prc = null;\n" +
+			"	^\n" +
+			"p cannot be resolved to a type\n" +
+			"----------\n" +
+			"2 problems (2 errors)\n",
+			false);
+	}
 }