Add test case for SLING-7111
authorCarsten Ziegeler <cziegeler@apache.org>
Wed, 6 Sep 2017 17:37:30 +0000 (17:37 +0000)
committerCarsten Ziegeler <cziegeler@apache.org>
Wed, 6 Sep 2017 17:37:30 +0000 (17:37 +0000)
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1807508 13f79535-47bb-0310-9956-ffa450edef68

src/test/java/org/apache/sling/commons/compiler/impl/CompilerJava5Test.java
src/test/resources/JavaFailure [new file with mode: 0644]

index ac5f0b5..5540ce9 100644 (file)
@@ -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 TestCase
         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 TestCase
             /**
              * @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 TestCase
             /**
              * @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 TestCase
             /**
              * @see org.apache.sling.commons.compiler.CompilationUnit#getLastModified()
              */
+            @Override
             public long getLastModified() {
                 return 0;
             }
@@ -82,13 +117,16 @@ public class CompilerJava5Test extends TestCase
     /**
      * @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 TestCase
     /**
      * @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 TestCase
     /**
      * @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 TestCase
     /**
      * @see org.apache.sling.commons.classloader.ClassLoaderWriter#getClassLoader()
      */
+    @Override
     public ClassLoader getClassLoader() {
         return null;
     }
diff --git a/src/test/resources/JavaFailure b/src/test/resources/JavaFailure
new file mode 100644 (file)
index 0000000..cd4045c
--- /dev/null
@@ -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
+}