Search in sources :

Example 1 with ClassLoaderWriter

use of org.apache.sling.commons.classloader.ClassLoaderWriter in project sling by apache.

the class SightlyJavaCompilerServiceTest method setUp.

@Before
public void setUp() throws Exception {
    compiler = spy(new SightlyJavaCompilerService());
    resourceBackedPojoChangeMonitor = spy(new ResourceBackedPojoChangeMonitor());
    SightlyEngineConfiguration sightlyEngineConfiguration = mock(SightlyEngineConfiguration.class);
    when(sightlyEngineConfiguration.getBundleSymbolicName()).thenReturn("org.apache.sling.scripting.sightly");
    when(sightlyEngineConfiguration.getScratchFolder()).thenReturn("/org/apache/sling/scripting/sightly");
    Whitebox.setInternalState(compiler, "sightlyEngineConfiguration", sightlyEngineConfiguration);
    Whitebox.setInternalState(compiler, "resourceBackedPojoChangeMonitor", resourceBackedPojoChangeMonitor);
    classLoaderWriter = Mockito.mock(ClassLoaderWriter.class);
    ClassLoader classLoader = Mockito.mock(ClassLoader.class);
    when(classLoaderWriter.getClassLoader()).thenReturn(classLoader);
}
Also used : ClassLoaderWriter(org.apache.sling.commons.classloader.ClassLoaderWriter) SightlyEngineConfiguration(org.apache.sling.scripting.sightly.impl.engine.SightlyEngineConfiguration) ResourceBackedPojoChangeMonitor(org.apache.sling.scripting.sightly.impl.engine.ResourceBackedPojoChangeMonitor) SightlyJavaCompilerService(org.apache.sling.scripting.sightly.impl.engine.SightlyJavaCompilerService) Before(org.junit.Before)

Example 2 with ClassLoaderWriter

use of org.apache.sling.commons.classloader.ClassLoaderWriter in project sling by apache.

the class SightlyScriptEngineFactoryTest method testActivateOverSameVersion.

@Test
public void testActivateOverSameVersion() {
    SightlyScriptEngineFactory scriptEngineFactory = new SightlyScriptEngineFactory();
    ClassLoaderWriter classLoaderWriter = mock(ClassLoaderWriter.class);
    try {
        when(classLoaderWriter.getInputStream(SightlyScriptEngineFactory.SIGHTLY_CONFIG_FILE)).thenReturn(IOUtils.toInputStream("1.0.17-SNAPSHOT", "UTF-8"));
    } catch (IOException e) {
        fail("IOException while setting tests.");
    }
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ByteArrayOutputStream spyOutputStream = spy(outputStream);
    when(classLoaderWriter.getOutputStream(SightlyScriptEngineFactory.SIGHTLY_CONFIG_FILE)).thenReturn(spyOutputStream);
    Whitebox.setInternalState(scriptEngineFactory, "classLoaderWriter", classLoaderWriter);
    Whitebox.setInternalState(scriptEngineFactory, "sightlyEngineConfiguration", sightlyEngineConfiguration);
    scriptEngineFactory.activate();
    verify(classLoaderWriter, never()).delete(sightlyEngineConfiguration.getScratchFolder());
    try {
        verify(spyOutputStream, never()).write(any(byte[].class));
        verify(spyOutputStream, never()).close();
    } catch (IOException e) {
        fail("IOException in test verification.");
    }
}
Also used : ClassLoaderWriter(org.apache.sling.commons.classloader.ClassLoaderWriter) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 3 with ClassLoaderWriter

use of org.apache.sling.commons.classloader.ClassLoaderWriter in project sling by apache.

the class SightlyScriptEngineFactoryTest method testActivateNoPreviousInfo.

@Test
public void testActivateNoPreviousInfo() {
    SightlyScriptEngineFactory scriptEngineFactory = new SightlyScriptEngineFactory();
    ClassLoaderWriter classLoaderWriter = mock(ClassLoaderWriter.class);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    when(classLoaderWriter.getOutputStream(SightlyScriptEngineFactory.SIGHTLY_CONFIG_FILE)).thenReturn(outputStream);
    Whitebox.setInternalState(scriptEngineFactory, "classLoaderWriter", classLoaderWriter);
    Whitebox.setInternalState(scriptEngineFactory, "sightlyEngineConfiguration", sightlyEngineConfiguration);
    scriptEngineFactory.activate();
    verify(classLoaderWriter).delete(sightlyEngineConfiguration.getScratchFolder());
    assertEquals("1.0.17-SNAPSHOT", outputStream.toString());
}
Also used : ClassLoaderWriter(org.apache.sling.commons.classloader.ClassLoaderWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 4 with ClassLoaderWriter

use of org.apache.sling.commons.classloader.ClassLoaderWriter in project sling by apache.

the class EclipseJavaCompiler method compile.

/**
     * @see org.apache.sling.commons.compiler.JavaCompiler#compile(org.apache.sling.commons.compiler.CompilationUnit[], org.apache.sling.commons.compiler.Options)
     */
@Override
public CompilationResult compile(final CompilationUnit[] units, final Options compileOptions) {
    // make sure we have an options object (to avoid null checks all over the place)
    final Options options = (compileOptions != null ? compileOptions : EMPTY_OPTIONS);
    // get classloader and classloader writer
    final ClassLoaderWriter writer = this.getClassLoaderWriter(options);
    if (writer == null) {
        return new CompilationResultImpl("Class loader writer for compilation is not available.");
    }
    final ClassLoader loader = this.getClassLoader(options, writer);
    if (loader == null) {
        return new CompilationResultImpl("Class loader for compilation is not available.");
    }
    // check sources for compilation
    boolean needsCompilation = isForceCompilation(options);
    if (!needsCompilation) {
        for (final CompilationUnit unit : units) {
            if (this.isOutDated(unit, writer)) {
                needsCompilation = true;
                break;
            }
        }
    }
    if (!needsCompilation) {
        logger.debug("All source files are recent - no compilation required.");
        return new CompilationResultImpl(writer);
    }
    // delete old class files
    for (final CompilationUnit unit : units) {
        final String name = '/' + unit.getMainClassName().replace('.', '/') + ".class";
        writer.delete(name);
    }
    // create properties for the settings object
    final Map<String, String> props = new HashMap<>();
    if (options.isGenerateDebugInfo()) {
        props.put(CompilerOptions.OPTION_LocalVariableAttribute, "generate");
        props.put(CompilerOptions.OPTION_LineNumberAttribute, "generate");
        props.put(CompilerOptions.OPTION_SourceFileAttribute, "generate");
    }
    if (options.getSourceVersion() != null) {
        props.put(CompilerOptions.OPTION_Source, options.getSourceVersion());
        props.put(CompilerOptions.OPTION_Compliance, options.getSourceVersion());
    }
    if (options.getTargetVersion() != null) {
        props.put(CompilerOptions.OPTION_TargetPlatform, options.getTargetVersion());
    }
    props.put(CompilerOptions.OPTION_Encoding, "UTF8");
    // create the settings
    final CompilerOptions settings = new CompilerOptions(props);
    logger.debug("Compiling with settings {}.", settings);
    // create the result
    final CompilationResultImpl result = new CompilationResultImpl(isIgnoreWarnings(options), writer);
    // create the context
    final CompileContext context = new CompileContext(units, result, writer, loader);
    // create the compiler
    final org.eclipse.jdt.internal.compiler.Compiler compiler = new org.eclipse.jdt.internal.compiler.Compiler(context, this.policy, settings, context, this.problemFactory, null, null);
    // compile
    compiler.compile(context.getSourceUnits());
    return result;
}
Also used : CompilationUnit(org.apache.sling.commons.compiler.CompilationUnit) ICompilationUnit(org.eclipse.jdt.internal.compiler.env.ICompilationUnit) CompilerOptions(org.eclipse.jdt.internal.compiler.impl.CompilerOptions) Options(org.apache.sling.commons.compiler.Options) ClassLoaderWriter(org.apache.sling.commons.classloader.ClassLoaderWriter) JavaCompiler(org.apache.sling.commons.compiler.JavaCompiler) HashMap(java.util.HashMap) CompilerOptions(org.eclipse.jdt.internal.compiler.impl.CompilerOptions)

Example 5 with ClassLoaderWriter

use of org.apache.sling.commons.classloader.ClassLoaderWriter in project sling by apache.

the class SightlyScriptEngineFactoryTest method testActivateOverPreviousVersion.

@Test
public void testActivateOverPreviousVersion() {
    SightlyScriptEngineFactory scriptEngineFactory = new SightlyScriptEngineFactory();
    ClassLoaderWriter classLoaderWriter = mock(ClassLoaderWriter.class);
    try {
        when(classLoaderWriter.getInputStream(SightlyScriptEngineFactory.SIGHTLY_CONFIG_FILE)).thenReturn(IOUtils.toInputStream("1.0.16", "UTF-8"));
    } catch (IOException e) {
        fail("IOException while setting tests.");
    }
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    when(classLoaderWriter.getOutputStream(SightlyScriptEngineFactory.SIGHTLY_CONFIG_FILE)).thenReturn(outputStream);
    when(classLoaderWriter.delete(sightlyEngineConfiguration.getScratchFolder())).thenReturn(true);
    Whitebox.setInternalState(scriptEngineFactory, "classLoaderWriter", classLoaderWriter);
    Whitebox.setInternalState(scriptEngineFactory, "sightlyEngineConfiguration", sightlyEngineConfiguration);
    scriptEngineFactory.activate();
    verify(classLoaderWriter).delete(sightlyEngineConfiguration.getScratchFolder());
    assertEquals("1.0.17-SNAPSHOT", outputStream.toString());
}
Also used : ClassLoaderWriter(org.apache.sling.commons.classloader.ClassLoaderWriter) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

ClassLoaderWriter (org.apache.sling.commons.classloader.ClassLoaderWriter)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 JavaCompiler (org.apache.sling.commons.compiler.JavaCompiler)2 File (java.io.File)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 JarFile (java.util.jar.JarFile)1 CompilationUnit (org.apache.sling.commons.compiler.CompilationUnit)1 Options (org.apache.sling.commons.compiler.Options)1 EclipseJavaCompiler (org.apache.sling.commons.compiler.impl.EclipseJavaCompiler)1 IOProvider (org.apache.sling.scripting.jsp.jasper.IOProvider)1 JspConfig (org.apache.sling.scripting.jsp.jasper.compiler.JspConfig)1 JspRuntimeContext (org.apache.sling.scripting.jsp.jasper.compiler.JspRuntimeContext)1 TagPluginManager (org.apache.sling.scripting.jsp.jasper.compiler.TagPluginManager)1 ResourceBackedPojoChangeMonitor (org.apache.sling.scripting.sightly.impl.engine.ResourceBackedPojoChangeMonitor)1 SightlyEngineConfiguration (org.apache.sling.scripting.sightly.impl.engine.SightlyEngineConfiguration)1 SightlyJavaCompilerService (org.apache.sling.scripting.sightly.impl.engine.SightlyJavaCompilerService)1 ICompilationUnit (org.eclipse.jdt.internal.compiler.env.ICompilationUnit)1