You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ra...@apache.org on 2016/03/12 17:15:49 UTC
svn commit: r1734715 - in /sling/trunk/bundles/scripting/sightly/engine/src:
main/java/org/apache/sling/scripting/sightly/impl/compiler/
main/java/org/apache/sling/scripting/sightly/impl/engine/
main/java/org/apache/sling/scripting/sightly/impl/engine/...
Author: radu
Date: Sat Mar 12 16:15:48 2016
New Revision: 1734715
URL: http://svn.apache.org/viewvc?rev=1734715&view=rev
Log:
SLING-5597 - Sightly generated Java classes for component scripts might not run correctly if the Sightly engine is updated on the instance
* Sightly scripts will all be generated in the org.apache.sling.scripting.sightly parent package so that newer versions of the engine
can delete the stale generated classes
Added:
sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/engine/SightlyScriptEngineFactoryTest.java
Modified:
sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyJavaCompilerService.java
sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/SightlyEngineConfiguration.java
sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/SightlyScriptEngineFactory.java
sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/UnitLoader.java
sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/compiled/SourceIdentifier.java
sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/JavaUseProvider.java
sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyJavaCompilerServiceTest.java
sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/engine/compiled/SourceIdentifierTest.java
Modified: sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyJavaCompilerService.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyJavaCompilerService.java?rev=1734715&r1=1734714&r2=1734715&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyJavaCompilerService.java (original)
+++ sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyJavaCompilerService.java Sat Mar 12 16:15:48 2016
@@ -97,7 +97,7 @@ public class SightlyJavaCompilerService
* @return object instance of the requested class
* @throws CompilerException in case of any runtime exception
*/
- public Object getInstance(RenderContext renderContext, String className, boolean pojoHint) {
+ public Object getInstance(RenderContext renderContext, String className) {
if (className.contains(".")) {
if (unitChangeMonitor.getLastModifiedDateForJavaUseObject(className) > 0) {
// it looks like the POJO comes from the repo and it was changed since it was last loaded
@@ -105,9 +105,6 @@ public class SightlyJavaCompilerService
unitChangeMonitor.clearJavaUseObject(className);
return result;
}
- if (pojoHint) {
- return compileRepositoryJavaClass(renderContext.getScriptResourceResolver(), className);
- }
try {
// the object either comes from a bundle or from the repo but it was not registered by the UnitChangeMonitor
return loadObject(className);
@@ -118,7 +115,7 @@ public class SightlyJavaCompilerService
} else {
Resource pojoResource = UseProviderUtils.locateScriptResource(renderContext, className + ".java");
if (pojoResource != null) {
- return getInstance(renderContext, Utils.getJavaNameFromPath(pojoResource.getPath()), false);
+ return getInstance(renderContext, Utils.getJavaNameFromPath(pojoResource.getPath()));
}
}
throw new SightlyException("Cannot find class " + className + ".");
@@ -128,25 +125,24 @@ public class SightlyJavaCompilerService
* Compiles a class using the passed fully qualified class name and its source code.
*
* @param sourceCode the source code from which to generate the class
- * @param fqcn fully qualified name of the class to compile
* @return object instance of the class to compile
* @throws CompilerException in case of any runtime exception
*/
- public Object compileSource(SourceIdentifier sourceIdentifier, String sourceCode, String fqcn) {
+ public Object compileSource(SourceIdentifier sourceIdentifier, String sourceCode) {
readLock.lock();
try {
- if (sourceIdentifier == null || sourceIdentifier.needsUpdate()) {
+ if (sourceIdentifier.needsUpdate()) {
readLock.unlock();
writeLock.lock();
try {
- return internalCompileSource(sourceCode, fqcn);
+ return internalCompileSource(sourceCode, sourceIdentifier.getFullyQualifiedName());
} finally {
// downgrade write lock since we've probably compiled the source code by now
readLock.lock();
writeLock.unlock();
}
} else {
- return classLoaderWriter.getClassLoader().loadClass(fqcn).newInstance();
+ return classLoaderWriter.getClassLoader().loadClass(sourceIdentifier.getFullyQualifiedName()).newInstance();
}
} catch (Exception e) {
throw new CompilerException(CompilerException.CompilerExceptionCause.COMPILER_ERRORS, e);
@@ -156,13 +152,12 @@ public class SightlyJavaCompilerService
}
private Object internalCompileSource(String sourceCode, String fqcn) throws Exception {
- if (sightlyEngineConfiguration.isDevMode()) {
+ if (sightlyEngineConfiguration.keepGenerated()) {
String path = "/" + fqcn.replaceAll("\\.", "/") + ".java";
OutputStream os = classLoaderWriter.getOutputStream(path);
IOUtils.write(sourceCode, os, "UTF-8");
IOUtils.closeQuietly(os);
}
-
String[] sourceCodeLines = sourceCode.split("\\r\\n|[\\n\\x0B\\x0C\\r\\u0085\\u2028\\u2029]");
boolean foundPackageDeclaration = false;
for (String line : sourceCodeLines) {
@@ -207,7 +202,9 @@ public class SightlyJavaCompilerService
Resource pojoResource = resolver.getResource(pojoPath);
if (pojoResource != null) {
try {
- return compileSource(null, IOUtils.toString(pojoResource.adaptTo(InputStream.class), "UTF-8"), className);
+ SourceIdentifier sourceIdentifier = new SourceIdentifier(sightlyEngineConfiguration, unitChangeMonitor,
+ classLoaderWriter, pojoResource);
+ return compileSource(sourceIdentifier, IOUtils.toString(pojoResource.adaptTo(InputStream.class), "UTF-8"));
} catch (IOException e) {
throw new SightlyException(String.format("Unable to compile class %s from %s.", className, pojoPath), e);
}
Modified: sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/SightlyEngineConfiguration.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/SightlyEngineConfiguration.java?rev=1734715&r1=1734714&r2=1734715&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/SightlyEngineConfiguration.java (original)
+++ sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/SightlyEngineConfiguration.java Sat Mar 12 16:15:48 2016
@@ -25,6 +25,7 @@ import java.util.Dictionary;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
+import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
@@ -46,9 +47,7 @@ import org.osgi.service.component.Compon
name = SightlyEngineConfiguration.SCR_PROP_NAME_DEVMODE,
boolValue = SightlyEngineConfiguration.SCR_PROP_DEFAULT_DEVMODE,
label = "Development Mode",
- description = "If enabled, Sightly components will be recompiled at every request instead of loading objects from memory." +
- " This will also write the generated Java classes' source code for Sightly templates through the ClassLoaderWriter " +
- "service."
+ description = "If enabled, Sightly components will be recompiled at every request instead of loading objects from memory."
),
@Property(
name = SightlyEngineConfiguration.SCR_PROP_NAME_ENCODING,
@@ -56,6 +55,13 @@ import org.osgi.service.component.Compon
label = "Template Files Default Encoding",
description = "The default encoding used for reading Sightly template files (this directly affects how Sightly templates" +
"are rendered)."
+ ),
+ @Property(
+ name = SightlyEngineConfiguration.SCR_PROP_NAME_KEEPGENERATED,
+ boolValue = SightlyEngineConfiguration.SCR_PROP_DEFAULT_KEEPGENERATED,
+ label = "Keep Generated Java Source Code",
+ description = "If enabled, the Java source code generated during Sightly template files compilation will be stored. " +
+ "Its location is dependent on the available org.apache.sling.commons.classloader.ClassLoaderWriter."
)
})
public class SightlyEngineConfiguration {
@@ -66,14 +72,27 @@ public class SightlyEngineConfiguration
public static final String SCR_PROP_NAME_ENCODING = "org.apache.sling.scripting.sightly.encoding";
public static final String SCR_PROP_DEFAULT_ENCODING = "UTF-8";
+ public static final String SCR_PROP_NAME_KEEPGENERATED = "org.apache.sling.scripting.sightly.keepgenerated";
+ public static final boolean SCR_PROP_DEFAULT_KEEPGENERATED = true;
+
private String engineVersion = "0";
private boolean devMode = false;
private String encoding = SCR_PROP_DEFAULT_ENCODING;
+ private boolean keepGenerated;
+ private String bundleSymbolicName = "_sightly";
public String getEngineVersion() {
return engineVersion;
}
+ public String getBundleSymbolicName() {
+ return bundleSymbolicName;
+ }
+
+ public String getScratchFolder() {
+ return "/" + bundleSymbolicName.replaceAll("\\.", "/");
+ }
+
public boolean isDevMode() {
return devMode;
}
@@ -82,6 +101,10 @@ public class SightlyEngineConfiguration
return encoding;
}
+ public boolean keepGenerated() {
+ return keepGenerated;
+ }
+
protected void activate(ComponentContext componentContext) {
InputStream ins = null;
try {
@@ -93,6 +116,10 @@ public class SightlyEngineConfiguration
if (version != null) {
engineVersion = version;
}
+ String symbolicName = attrs.getValue("Bundle-SymbolicName");
+ if (StringUtils.isNotEmpty(symbolicName)) {
+ bundleSymbolicName = symbolicName;
+ }
}
} catch (IOException ioe) {
} finally {
@@ -106,5 +133,6 @@ public class SightlyEngineConfiguration
Dictionary properties = componentContext.getProperties();
devMode = PropertiesUtil.toBoolean(properties.get(SCR_PROP_NAME_DEVMODE), SCR_PROP_DEFAULT_DEVMODE);
encoding = PropertiesUtil.toString(properties.get(SCR_PROP_NAME_ENCODING), SCR_PROP_DEFAULT_ENCODING);
+ keepGenerated = PropertiesUtil.toBoolean(properties.get(SCR_PROP_NAME_KEEPGENERATED), SCR_PROP_DEFAULT_KEEPGENERATED);
}
}
Modified: sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/SightlyScriptEngineFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/SightlyScriptEngineFactory.java?rev=1734715&r1=1734714&r2=1734715&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/SightlyScriptEngineFactory.java (original)
+++ sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/SightlyScriptEngineFactory.java Sat Mar 12 16:15:48 2016
@@ -19,16 +19,25 @@
package org.apache.sling.scripting.sightly.impl.engine;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang.StringUtils;
+import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.commons.classloader.ClassLoaderWriter;
import org.apache.sling.commons.classloader.DynamicClassLoaderManager;
import org.apache.sling.scripting.api.AbstractScriptEngineFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* Sightly template engine factory
@@ -41,6 +50,8 @@ import org.apache.sling.scripting.api.Ab
})
public class SightlyScriptEngineFactory extends AbstractScriptEngineFactory {
+ private static final Logger LOGGER = LoggerFactory.getLogger(SightlyScriptEngineFactory.class);
+
@Reference
private UnitLoader unitLoader;
@@ -50,6 +61,12 @@ public class SightlyScriptEngineFactory
@Reference
private DynamicClassLoaderManager dynamicClassLoaderManager;
+ @Reference
+ private SightlyEngineConfiguration sightlyEngineConfiguration;
+
+ @Reference
+ private ClassLoaderWriter classLoaderWriter;
+
public final static String SHORT_NAME = "sightly";
public final static String LANGUAGE_NAME = "The Sightly Templating Language";
@@ -58,6 +75,8 @@ public class SightlyScriptEngineFactory
public final static String EXTENSION = "html";
+ public static final String SIGHTLY_CONFIG_FILE = "/sightly.config";
+
public SightlyScriptEngineFactory() {
setNames(SHORT_NAME);
setExtensions(EXTENSION);
@@ -81,4 +100,44 @@ public class SightlyScriptEngineFactory
protected ClassLoader getClassLoader() {
return dynamicClassLoaderManager.getDynamicClassLoader();
}
+
+ @Activate
+ @SuppressWarnings("unused")
+ protected void activate() {
+ InputStream is;
+ boolean newVersion = true;
+ String versionInfo = null;
+ String newVersionString = sightlyEngineConfiguration.getEngineVersion();
+ try {
+ is = classLoaderWriter.getInputStream(SIGHTLY_CONFIG_FILE);
+ if (is != null) {
+ versionInfo = IOUtils.toString(is, "UTF-8");
+ if (newVersionString.equals(versionInfo)) {
+ newVersion = false;
+ } else {
+ LOGGER.info("Detected stale classes generated by Apache Sling Scripting Sightly engine version {}.", versionInfo);
+ }
+ IOUtils.closeQuietly(is);
+ }
+ } catch (IOException e) {
+ // do nothing; if we didn't find any previous version information we're considering our version to be new
+ }
+ if (newVersion) {
+ OutputStream os = classLoaderWriter.getOutputStream(SIGHTLY_CONFIG_FILE);
+ try {
+ IOUtils.write(sightlyEngineConfiguration.getEngineVersion(), os, "UTF-8");
+ } catch (IOException e) {
+ // ignore
+ } finally {
+ IOUtils.closeQuietly(os);
+ }
+ String scratchFolder = sightlyEngineConfiguration.getScratchFolder();
+ boolean scratchFolderDeleted = classLoaderWriter.delete(scratchFolder);
+ if (scratchFolderDeleted) {
+ if (StringUtils.isNotEmpty(versionInfo)) {
+ LOGGER.info("Deleted stale classes generated by Apache Sling Scripting Sightly engine version {}.", versionInfo);
+ }
+ }
+ }
+ }
}
Modified: sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/UnitLoader.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/UnitLoader.java?rev=1734715&r1=1734714&r2=1734715&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/UnitLoader.java (original)
+++ sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/UnitLoader.java Sat Mar 12 16:15:48 2016
@@ -98,9 +98,9 @@ public class UnitLoader {
String encoding = unitChangeMonitor.getScriptEncoding(scriptResource.getPath());
if (sourceIdentifier.needsUpdate()) {
String sourceCode = getSourceCodeForScript(adminResolver, sourceIdentifier, renderContext.getBindings(), encoding);
- obj = sightlyJavaCompilerService.compileSource(sourceIdentifier, sourceCode, sourceIdentifier.getFullyQualifiedName());
+ obj = sightlyJavaCompilerService.compileSource(sourceIdentifier, sourceCode);
} else {
- obj = sightlyJavaCompilerService.getInstance(renderContext, sourceIdentifier.getFullyQualifiedName(), false);
+ obj = sightlyJavaCompilerService.getInstance(renderContext, sourceIdentifier.getFullyQualifiedName());
}
if (!(obj instanceof RenderUnit)) {
throw new SightlyException("Class is not a RenderUnit instance");
Modified: sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/compiled/SourceIdentifier.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/compiled/SourceIdentifier.java?rev=1734715&r1=1734714&r2=1734715&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/compiled/SourceIdentifier.java (original)
+++ sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/compiled/SourceIdentifier.java Sat Mar 12 16:15:48 2016
@@ -19,6 +19,7 @@
package org.apache.sling.scripting.sightly.impl.engine.compiled;
+import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.commons.classloader.ClassLoaderWriter;
@@ -27,7 +28,8 @@ import org.apache.sling.scripting.sightl
import org.apache.sling.scripting.sightly.impl.utils.JavaEscapeUtils;
/**
- * Identifies a Java source file in a JCR repository.
+ * Identifies a Java source file based on a {@link Resource}. Depending on the used constructor this class might provide the abstraction
+ * for either a Java source file generated for a Sightly HTML script or for a Sightly {@link Resource}-based Java Use-API Object.
*/
public class SourceIdentifier {
@@ -38,16 +40,25 @@ public class SourceIdentifier {
private SightlyEngineConfiguration configuration;
private UnitChangeMonitor unitChangeMonitor;
private ClassLoaderWriter writer;
+ private boolean scriptSource = false;
+
+ public SourceIdentifier(SightlyEngineConfiguration configuration, UnitChangeMonitor unitChangeMonitor, ClassLoaderWriter writer,
+ Resource resource) {
+ this(configuration, unitChangeMonitor, writer, resource, null);
+ }
public SourceIdentifier(SightlyEngineConfiguration configuration, UnitChangeMonitor unitChangeMonitor, ClassLoaderWriter writer,
Resource resource, String classNamePrefix) {
this.resource = resource;
- this.className = buildClassName(resource, classNamePrefix);
- this.packageName = buildPackageName(resource);
- this.fullyQualifiedName = buildFullyQualifiedName(packageName, className);
this.configuration = configuration;
this.unitChangeMonitor = unitChangeMonitor;
this.writer = writer;
+ if (StringUtils.isNotEmpty(classNamePrefix)) {
+ scriptSource = true;
+ }
+ this.className = buildClassName(resource, classNamePrefix);
+ this.packageName = buildPackageName(resource);
+ this.fullyQualifiedName = buildFullyQualifiedName(packageName, className);
}
public String getClassName() {
@@ -84,7 +95,12 @@ public class SourceIdentifier {
private String buildClassName(Resource resource, String classNamePrefix) {
String scriptName = ResourceUtil.getName(resource.getPath());
scriptName = scriptName.substring(0, scriptName.lastIndexOf(getExtension(scriptName)));
- String className = classNamePrefix + scriptName;
+ String className;
+ if (StringUtils.isNotEmpty(classNamePrefix)) {
+ className = classNamePrefix + scriptName;
+ } else {
+ className = scriptName;
+ }
return className.replaceAll("-", "_").replaceAll("\\.", "_");
}
@@ -99,7 +115,10 @@ public class SourceIdentifier {
sb.append(".");
}
}
- return sb.toString();
+ if (!scriptSource) {
+ return sb.toString();
+ }
+ return configuration.getBundleSymbolicName() + "." + sb.toString();
}
private String getExtension(String scriptName) {
Modified: sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/JavaUseProvider.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/JavaUseProvider.java?rev=1734715&r1=1734714&r2=1734715&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/JavaUseProvider.java (original)
+++ sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/JavaUseProvider.java Sat Mar 12 16:15:48 2016
@@ -91,7 +91,7 @@ public class JavaUseProvider implements
Class<?> cls = classLoaderWriter.getClassLoader().loadClass(identifier);
if (unitChangeMonitor.getLastModifiedDateForJavaUseObject(identifier) > 0) {
// the object is a POJO that was changed in the repository but not recompiled;
- result = sightlyJavaCompilerService.getInstance(renderContext, identifier, true);
+ result = sightlyJavaCompilerService.getInstance(renderContext, identifier);
if (result instanceof Use) {
((Use) result).init(BindingsUtils.merge(globalBindings, arguments));
}
@@ -124,7 +124,7 @@ public class JavaUseProvider implements
/**
* this object is either not exported from a bundle, or it's a POJO from the repository that wasn't loaded before
*/
- result = sightlyJavaCompilerService.getInstance(renderContext, identifier, true);
+ result = sightlyJavaCompilerService.getInstance(renderContext, identifier);
if (result instanceof Use) {
((Use) result).init(BindingsUtils.merge(globalBindings, arguments));
}
Modified: sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyJavaCompilerServiceTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyJavaCompilerServiceTest.java?rev=1734715&r1=1734714&r2=1734715&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyJavaCompilerServiceTest.java (original)
+++ sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyJavaCompilerServiceTest.java Sat Mar 12 16:15:48 2016
@@ -105,6 +105,7 @@ public class SightlyJavaCompilerServiceT
private void getInstancePojoTest(String pojoPath, String className) throws Exception {
RenderContext renderContext = Mockito.mock(RenderContext.class);
Resource pojoResource = Mockito.mock(Resource.class);
+ when(pojoResource.getPath()).thenReturn(pojoPath);
ResourceResolver resolver = Mockito.mock(ResourceResolver.class);
when(renderContext.getScriptResourceResolver()).thenReturn(resolver);
when(resolver.getResource(pojoPath)).thenReturn(pojoResource);
@@ -113,12 +114,6 @@ public class SightlyJavaCompilerServiceT
CompilationResult compilationResult = Mockito.mock(CompilationResult.class);
when(compilationResult.getErrors()).thenReturn(new ArrayList<CompilerMessage>());
when(javaCompiler.compile(Mockito.any(CompilationUnit[].class), Mockito.any(Options.class))).thenReturn(compilationResult);
- when(compilationResult.loadCompiledClass(className)).thenAnswer(new Answer<Object>() {
- @Override
- public Object answer(InvocationOnMock invocation) throws Throwable {
- return MockPojo.class;
- }
- });
ClassLoaderWriter clw = Mockito.mock(ClassLoaderWriter.class);
ClassLoader classLoader = Mockito.mock(ClassLoader.class);
when(clw.getClassLoader()).thenReturn(classLoader);
@@ -130,7 +125,11 @@ public class SightlyJavaCompilerServiceT
});
Whitebox.setInternalState(compiler, "classLoaderWriter", clw);
Whitebox.setInternalState(compiler, "javaCompiler", javaCompiler);
- Object obj = compiler.getInstance(renderContext, className, false);
+ 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);
+ Object obj = compiler.getInstance(renderContext, className);
assertTrue("Expected to obtain a " + MockPojo.class.getName() + " object.", obj instanceof MockPojo);
}
}
Added: sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/engine/SightlyScriptEngineFactoryTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/engine/SightlyScriptEngineFactoryTest.java?rev=1734715&view=auto
==============================================================================
--- sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/engine/SightlyScriptEngineFactoryTest.java (added)
+++ sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/engine/SightlyScriptEngineFactoryTest.java Sat Mar 12 16:15:48 2016
@@ -0,0 +1,111 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.sling.scripting.sightly.impl.engine;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.sling.commons.classloader.ClassLoaderWriter;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.powermock.reflect.Whitebox;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class SightlyScriptEngineFactoryTest {
+
+ private SightlyEngineConfiguration sightlyEngineConfiguration;
+
+ @Before
+ public void setUp() {
+ sightlyEngineConfiguration = mock(SightlyEngineConfiguration.class);
+ when(sightlyEngineConfiguration.getEngineVersion()).thenReturn("1.0.17-SNAPSHOT");
+ when(sightlyEngineConfiguration.getScratchFolder()).thenReturn("/org/apache/sling/scripting/sightly");
+ }
+
+ @After
+ public void tearDown() {
+ sightlyEngineConfiguration = null;
+ }
+
+ @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());
+ }
+
+ @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());
+ }
+
+ @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.");
+ }
+ }
+}
Modified: sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/engine/compiled/SourceIdentifierTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/engine/compiled/SourceIdentifierTest.java?rev=1734715&r1=1734714&r2=1734715&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/engine/compiled/SourceIdentifierTest.java (original)
+++ sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/engine/compiled/SourceIdentifierTest.java Sat Mar 12 16:15:48 2016
@@ -17,6 +17,7 @@
package org.apache.sling.scripting.sightly.impl.engine.compiled;
import org.apache.sling.api.resource.Resource;
+import org.apache.sling.scripting.sightly.impl.engine.SightlyEngineConfiguration;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -27,12 +28,15 @@ import static org.mockito.Mockito.when;
public class SourceIdentifierTest {
private static SourceIdentifier sourceIdentifier;
+ private static final String BUNDLE_SYMBOLIC_NAME = "org.apache.sling.scripting.sightly";
@BeforeClass
public static void setUp() {
final Resource resource = mock(Resource.class);
when(resource.getPath()).thenReturn("/apps/blah/static/foo/foo.html");
- sourceIdentifier = new SourceIdentifier(null, null, null, resource, "SightlyJava_");
+ final SightlyEngineConfiguration configuration = mock(SightlyEngineConfiguration.class);
+ when(configuration.getBundleSymbolicName()).thenReturn(BUNDLE_SYMBOLIC_NAME);
+ sourceIdentifier = new SourceIdentifier(configuration, null, null, resource, "SightlyJava_");
}
@Test
@@ -42,11 +46,11 @@ public class SourceIdentifierTest {
@Test
public void testGetPackageName() throws Exception {
- assertEquals("apps.blah._static.foo", sourceIdentifier.getPackageName());
+ assertEquals(BUNDLE_SYMBOLIC_NAME + ".apps.blah._static.foo", sourceIdentifier.getPackageName());
}
@Test
public void testGetFullyQualifiedName() throws Exception {
- assertEquals("apps.blah._static.foo.SightlyJava_foo", sourceIdentifier.getFullyQualifiedName());
+ assertEquals(BUNDLE_SYMBOLIC_NAME + ".apps.blah._static.foo.SightlyJava_foo", sourceIdentifier.getFullyQualifiedName());
}
-}
\ No newline at end of file
+}