Search in sources :

Example 1 with CanonicalModelProcessor

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());
    }
}
Also used : JavaCompiler(javax.tools.JavaCompiler) IOException(java.io.IOException) CanonicalModelProcessor(org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor) URL(java.net.URL) CompilationTask(javax.tools.JavaCompiler.CompilationTask) BufferedWriter(java.io.BufferedWriter) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 2 with CanonicalModelProcessor

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);
}
Also used : JavaCompiler(javax.tools.JavaCompiler) IOException(java.io.IOException) CanonicalModelProcessor(org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor) URL(java.net.URL) CompilationTask(javax.tools.JavaCompiler.CompilationTask) BufferedWriter(java.io.BufferedWriter) FileVisitResult(java.nio.file.FileVisitResult) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector) File(java.io.File) PrintWriter(java.io.PrintWriter)

Aggregations

BufferedWriter (java.io.BufferedWriter)2 File (java.io.File)2 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 URL (java.net.URL)2 DiagnosticCollector (javax.tools.DiagnosticCollector)2 JavaCompiler (javax.tools.JavaCompiler)2 CompilationTask (javax.tools.JavaCompiler.CompilationTask)2 JavaFileObject (javax.tools.JavaFileObject)2 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)2 StandardJavaFileManager (javax.tools.StandardJavaFileManager)2 CanonicalModelProcessor (org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor)2 FileVisitResult (java.nio.file.FileVisitResult)1