use of org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor in project eclipselink by eclipse-ee4j.
the class TestProcessor method verifyLogging.
/**
* Verify logging output suppression
* @param testName name of the test
* @param pu persistence unit {@code String}
* @param haveMsgs there should be logging messages in the output or not
* @param options compiler options
*/
private void verifyLogging(final String testName, final String pu, final boolean haveMsgs, final String... options) throws Exception {
File runDir = new File(System.getProperty("run.dir"), testName);
File srcOut = new File(runDir, "src");
srcOut.mkdirs();
File cpDir = new File(runDir, "cp");
cpDir.mkdirs();
File pxml = new File(cpDir, "META-INF/persistence.xml");
pxml.getParentFile().mkdirs();
try (BufferedWriter writer = Files.newBufferedWriter(pxml.toPath(), StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)) {
writer.write(pu, 0, pu.length());
} catch (IOException x) {
throw x;
}
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
StandardJavaFileManager sfm = compiler.getStandardFileManager(diagnostics, null, null);
URL apiUrl = Entity.class.getProtectionDomain().getCodeSource().getLocation();
URL generatedUrl = Generated.class.getProtectionDomain().getCodeSource().getLocation();
sfm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(new File(apiUrl.getFile()), new File(generatedUrl.getFile()), cpDir));
sfm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(srcOut));
sfm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(cpDir));
TestFO entity = new TestFO("org.Sample", "package org; import jakarta.persistence.Entity; @Entity public class Sample { public Sample() {} public int getX() {return 1;}}");
CompilationTask task = compiler.getTask(new PrintWriter(System.out), sfm, diagnostics, getJavacOptions(options), null, Arrays.asList(entity));
CanonicalModelProcessor modelProcessor = new CanonicalModelProcessor();
task.setProcessors(Collections.singleton(modelProcessor));
task.call();
for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
System.out.println(diagnostic);
}
if (haveMsgs) {
Assert.assertFalse("Log messages should be generated", diagnostics.getDiagnostics().isEmpty());
} else {
Assert.assertTrue("No log message should be generated", diagnostics.getDiagnostics().isEmpty());
}
}
use of org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor in project eclipselink by eclipse-ee4j.
the class TestProcessor method runProject.
private Result runProject(String name, List<String> options, List<JavaFileObject> sources, String pxmlStr, String oxmlStr) throws Exception {
File runDir = new File(System.getProperty("run.dir"), name);
File srcOut = new File(runDir, "src");
srcOut.mkdirs();
File cpDir = new File(runDir, "cp");
cpDir.mkdirs();
File pxml = new File(cpDir, "META-INF/persistence.xml");
pxml.getParentFile().mkdirs();
try (BufferedWriter writer = Files.newBufferedWriter(pxml.toPath(), StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)) {
writer.write(pxmlStr, 0, pxmlStr.length());
} catch (IOException x) {
throw x;
}
File oxml = new File(cpDir, "META-INF/orm.xml");
try (BufferedWriter writer = Files.newBufferedWriter(oxml.toPath(), StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)) {
writer.write(oxmlStr, 0, oxmlStr.length());
} catch (IOException x) {
throw x;
}
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
StandardJavaFileManager sfm = compiler.getStandardFileManager(diagnostics, null, null);
URL apiUrl = Entity.class.getProtectionDomain().getCodeSource().getLocation();
URL generatedUrl = Generated.class.getProtectionDomain().getCodeSource().getLocation();
sfm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(new File(apiUrl.getFile()), new File(generatedUrl.getFile()), cpDir));
sfm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(srcOut));
sfm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(cpDir));
CompilationTask task = compiler.getTask(new PrintWriter(System.out), sfm, diagnostics, options, null, sources);
CanonicalModelProcessor modelProcessor = new CanonicalModelProcessor();
task.setProcessors(Collections.singleton(modelProcessor));
boolean result = task.call();
for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
System.out.println(diagnostic);
String msg = diagnostic.getMessage(null);
Assert.assertFalse(msg, msg.contains("The following options were not recognized by any processor:"));
}
return new Result(srcOut, cpDir, result);
}
Aggregations