Author: cziegeler
Date: Wed Sep 6 17:37:30 2017
New Revision: 1807508
URL: http://svn.apache.org/viewvc?rev=1807508&view=rev
Log:
Add test case for SLING-7111
Added:
sling/trunk/bundles/commons/compiler/src/test/resources/JavaFailure
Modified:
sling/trunk/bundles/commons/compiler/src/test/java/org/apache/sling/commons/compiler/impl/CompilerJava5Test.java
Modified:
sling/trunk/bundles/commons/compiler/src/test/java/org/apache/sling/commons/compiler/impl/CompilerJava5Test.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/commons/compiler/src/test/java/org/apache/sling/commons/compiler/impl/CompilerJava5Test.java?rev=1807508&r1=1807507&r2=1807508&view=diff
==============================================================================
---
sling/trunk/bundles/commons/compiler/src/test/java/org/apache/sling/commons/compiler/impl/CompilerJava5Test.java
(original)
+++
sling/trunk/bundles/commons/compiler/src/test/java/org/apache/sling/commons/compiler/impl/CompilerJava5Test.java
Wed Sep 6 17:37:30 2017
@@ -22,21 +22,29 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
-
-import junit.framework.TestCase;
+import java.util.ArrayList;
+import java.util.List;
import org.apache.sling.commons.classloader.ClassLoaderWriter;
import org.apache.sling.commons.compiler.CompilationResult;
import org.apache.sling.commons.compiler.CompilationUnit;
import org.apache.sling.commons.compiler.Options;
+import junit.framework.TestCase;
+
/**
* Test case for java 5 support
*/
public class CompilerJava5Test extends TestCase
implements ClassLoaderWriter {
+ private final List<String> deletedPath = new ArrayList<>();
+
+ private final List<String> outputPath = new ArrayList<>();
+
public void testJava5Support() throws Exception {
+ deletedPath.clear();
+ outputPath.clear();
String sourceFile = "Java5Test";
CompilationUnit unit = createCompileUnit(sourceFile);
@@ -48,6 +56,30 @@ public class CompilerJava5Test extends T
final CompilationResult result = new EclipseJavaCompiler().compile(new
CompilationUnit[]{unit}, options);
assertNotNull(result);
assertNull(result.getErrors());
+ assertEquals(1, deletedPath.size());
+ assertEquals(1, outputPath.size());
+
assertEquals("/org/apache/sling/commons/compiler/test/Java5Test.class",
deletedPath.get(0));
+
assertEquals("/org/apache/sling/commons/compiler/test/Java5Test.class",
outputPath.get(0));
+ }
+
+ public void testFailedCompilation() throws Exception {
+ deletedPath.clear();
+ outputPath.clear();
+
+ String sourceFile = "JavaFailure";
+
+ CompilationUnit unit = createCompileUnit(sourceFile);
+ final Options options = new Options();
+ options.put(Options.KEY_SOURCE_VERSION, Options.VERSION_1_5);
+ options.put(Options.KEY_CLASS_LOADER_WRITER, this);
+ options.put(Options.KEY_CLASS_LOADER,
this.getClass().getClassLoader());
+
+ final CompilationResult result = new EclipseJavaCompiler().compile(new
CompilationUnit[]{unit}, options);
+ assertNotNull(result);
+ assertNotNull(result.getErrors());
+ assertEquals(1, deletedPath.size());
+
assertEquals("/org/apache/sling/commons/compiler/test/JavaFailure.class",
deletedPath.get(0));
+ assertEquals(0, outputPath.size());
}
//--------------------------------------------------------< misc. helpers >
@@ -58,6 +90,7 @@ public class CompilerJava5Test extends T
/**
* @see
org.apache.sling.commons.compiler.CompilationUnit#getMainClassName()
*/
+ @Override
public String getMainClassName() {
return "org.apache.sling.commons.compiler.test." + sourceFile;
}
@@ -65,6 +98,7 @@ public class CompilerJava5Test extends T
/**
* @see
org.apache.sling.commons.compiler.CompilationUnit#getSource()
*/
+ @Override
public Reader getSource() throws IOException {
InputStream in =
getClass().getClassLoader().getResourceAsStream(sourceFile);
return new InputStreamReader(in, "UTF-8");
@@ -73,6 +107,7 @@ public class CompilerJava5Test extends T
/**
* @see
org.apache.sling.commons.compiler.CompilationUnit#getLastModified()
*/
+ @Override
public long getLastModified() {
return 0;
}
@@ -82,13 +117,16 @@ public class CompilerJava5Test extends T
/**
* @see
org.apache.sling.commons.classloader.ClassLoaderWriter#delete(java.lang.String)
*/
+ @Override
public boolean delete(String path) {
+ deletedPath.add(path);
return false;
}
/**
* @see
org.apache.sling.commons.classloader.ClassLoaderWriter#getInputStream(java.lang.String)
*/
+ @Override
public InputStream getInputStream(String path) throws IOException {
return null;
}
@@ -96,6 +134,7 @@ public class CompilerJava5Test extends T
/**
* @see
org.apache.sling.commons.classloader.ClassLoaderWriter#getLastModified(java.lang.String)
*/
+ @Override
public long getLastModified(String path) {
return -1;
}
@@ -103,13 +142,16 @@ public class CompilerJava5Test extends T
/**
* @see
org.apache.sling.commons.classloader.ClassLoaderWriter#getOutputStream(java.lang.String)
*/
+ @Override
public OutputStream getOutputStream(String path) {
+ outputPath.add(path);
return new ByteArrayOutputStream();
}
/**
* @see
org.apache.sling.commons.classloader.ClassLoaderWriter#rename(java.lang.String,
java.lang.String)
*/
+ @Override
public boolean rename(String oldPath, String newPath) {
return false;
}
@@ -117,6 +159,7 @@ public class CompilerJava5Test extends T
/**
* @see
org.apache.sling.commons.classloader.ClassLoaderWriter#getClassLoader()
*/
+ @Override
public ClassLoader getClassLoader() {
return null;
}
Added: sling/trunk/bundles/commons/compiler/src/test/resources/JavaFailure
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/commons/compiler/src/test/resources/JavaFailure?rev=1807508&view=auto
==============================================================================
--- sling/trunk/bundles/commons/compiler/src/test/resources/JavaFailure (added)
+++ sling/trunk/bundles/commons/compiler/src/test/resources/JavaFailure Wed Sep
6 17:37:30 2017
@@ -0,0 +1,26 @@
+/*
+ * 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.commons.compiler.test;
+
+import java.util.List;
+
+/**
+ * Class to test compilation failure
+ */
+public class Java5Test {
+ xyz
+}