FELIX-4192 : SCR Generator fails with a NPE in case a class level Reference doesn't define a referenceInterface. Add test cases from Daniel Kuffner

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1513731 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scrplugin/generator/changelog.txt b/scrplugin/generator/changelog.txt
index 89d0afd..dbbc673 100644
--- a/scrplugin/generator/changelog.txt
+++ b/scrplugin/generator/changelog.txt
@@ -1,3 +1,8 @@
+Changes from 1.8.2 to 1.8.0
+---------------------------
+** Bug
+    * [FELIX-4192] - SCR Generator fails with a NPE in case a class level Reference doesn't define a referenceInterface
+
 Changes from 1.8.0 to 1.7.0
 ---------------------------
 ** Improvement
diff --git a/scrplugin/generator/pom.xml b/scrplugin/generator/pom.xml
index 170e073..419ad2b 100644
--- a/scrplugin/generator/pom.xml
+++ b/scrplugin/generator/pom.xml
@@ -62,6 +62,43 @@
             <artifactId>org.osgi.compendium</artifactId>
             <version>4.2.0</version>
         </dependency>
+
+
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.8.1</version>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.easymock</groupId>
+            <artifactId>easymock</artifactId>
+            <version>3.2</version>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>commons-lang</groupId>
+            <artifactId>commons-lang</artifactId>
+            <version>2.6</version>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>commons-io</groupId>
+            <artifactId>commons-io</artifactId>
+            <version>2.4</version>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.scr.annotations</artifactId>
+            <version>1.9.6</version>
+            <scope>test</scope>
+        </dependency>
+
     </dependencies>
     
     <build>
diff --git a/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/helper/Validator.java b/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/helper/Validator.java
index 7a42089..baddef9 100644
--- a/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/helper/Validator.java
+++ b/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/helper/Validator.java
@@ -453,52 +453,54 @@
             ref.setStrategy(ReferenceStrategy.EVENT);
         }
 
-        // validate bind and unbind methods
-        if (ref.getStrategy() != ReferenceStrategy.LOOKUP) {
-            String bindName = ref.getBind();
-            String unbindName = ref.getUnbind();
+        // validate methods only if interface name is set
+        if (!StringUtils.isEmpty(ref.getInterfaceName())) {
+            // validate bind and unbind methods
+            if (ref.getStrategy() != ReferenceStrategy.LOOKUP) {
+                String bindName = ref.getBind();
+                String unbindName = ref.getUnbind();
 
-            final boolean canGenerate = this.options.isGenerateAccessors() &&
-                            ref.getField() != null
-                            && (ref.getCardinality() == ReferenceCardinality.OPTIONAL_UNARY || ref.getCardinality() == ReferenceCardinality.MANDATORY_UNARY);
-            if (bindName == null && !canGenerate ) {
-                bindName = "bind";
-            }
-            if (unbindName == null && !canGenerate ) {
-                unbindName = "unbind";
-            }
+                final boolean canGenerate = this.options.isGenerateAccessors() &&
+                                ref.getField() != null
+                                && (ref.getCardinality() == ReferenceCardinality.OPTIONAL_UNARY || ref.getCardinality() == ReferenceCardinality.MANDATORY_UNARY);
+                if (bindName == null && !canGenerate ) {
+                    bindName = "bind";
+                }
+                if (unbindName == null && !canGenerate ) {
+                    unbindName = "unbind";
+                }
 
-            if ( bindName != null ) {
-                bindName = this.validateMethod(ref, bindName, componentIsAbstract);
-                if ( bindName == null && ref.getField() != null ) {
-                    iLog.addError("Something went wrong: " + canGenerate + " - " + this.options.isGenerateAccessors() + " - " + ref.getCardinality(), ref.getField().getName());
+                if ( bindName != null ) {
+                    bindName = this.validateMethod(ref, bindName, componentIsAbstract);
+                    if ( bindName == null && ref.getField() != null ) {
+                        iLog.addError("Something went wrong: " + canGenerate + " - " + this.options.isGenerateAccessors() + " - " + ref.getCardinality(), ref.getField().getName());
+                    }
+                } else {
+                    bindName = "bind" + Character.toUpperCase(ref.getName().charAt(0)) + ref.getName().substring(1);
+                }
+                if ( unbindName != null ) {
+                    unbindName = this.validateMethod(ref, unbindName, componentIsAbstract);
+                } else {
+                    unbindName = "unbind" + Character.toUpperCase(ref.getName().charAt(0)) + ref.getName().substring(1);
+                }
+
+                if (iLog.getNumberOfErrors() == currentIssueCount) {
+                    ref.setBind(bindName);
+                    ref.setUnbind(unbindName);
                 }
             } else {
-                bindName = "bind" + Character.toUpperCase(ref.getName().charAt(0)) + ref.getName().substring(1);
-            }
-            if ( unbindName != null ) {
-                unbindName = this.validateMethod(ref, unbindName, componentIsAbstract);
-            } else {
-                unbindName = "unbind" + Character.toUpperCase(ref.getName().charAt(0)) + ref.getName().substring(1);
+                ref.setBind(null);
+                ref.setUnbind(null);
             }
 
-            if (iLog.getNumberOfErrors() == currentIssueCount) {
-                ref.setBind(bindName);
-                ref.setUnbind(unbindName);
-            }
-        } else {
-            ref.setBind(null);
-            ref.setUnbind(null);
-        }
-
-        // validate updated method
-        if (ref.getUpdated() != null) {
-            if (this.options.getSpecVersion().ordinal() < SpecVersion.VERSION_1_1_FELIX.ordinal()) {
-                this.logError(ref, "Updated method declaration requires version "
-                                + SpecVersion.VERSION_1_1_FELIX.getName() + ", " + SpecVersion.VERSION_1_2.getName() + " or newer");
+            // validate updated method
+            if (ref.getUpdated() != null) {
+                if (this.options.getSpecVersion().ordinal() < SpecVersion.VERSION_1_1_FELIX.ordinal()) {
+                    this.logError(ref, "Updated method declaration requires version "
+                                    + SpecVersion.VERSION_1_1_FELIX.getName() + ", " + SpecVersion.VERSION_1_2.getName() + " or newer");
+                }
             }
         }
-
     }
 
     private String validateMethod(final ReferenceDescription ref, final String methodName, final boolean componentIsAbstract)
diff --git a/scrplugin/generator/src/test/java/org/apache/felix/scrplugin/SCRDescriptorGeneratorTest.java b/scrplugin/generator/src/test/java/org/apache/felix/scrplugin/SCRDescriptorGeneratorTest.java
new file mode 100644
index 0000000..9272576
--- /dev/null
+++ b/scrplugin/generator/src/test/java/org/apache/felix/scrplugin/SCRDescriptorGeneratorTest.java
@@ -0,0 +1,115 @@
+/*
+ * 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.felix.scrplugin;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.UUID;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.easymock.EasyMock;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class SCRDescriptorGeneratorTest {
+
+    File folder;
+    File dest;
+
+    @Before
+    public void setup() throws IOException {
+        folder = new File(FileUtils.getTempDirectory(), UUID.randomUUID().toString());
+        FileUtils.forceMkdir(folder);
+
+        dest = new File(folder, "testComponents");
+        FileUtils.forceMkdir(dest);
+
+    }
+
+    @After
+    public void tearDown() throws IOException {
+        FileUtils.deleteDirectory(folder);
+    }
+
+    @Test
+    public void testSimpleComponent() throws SCRDescriptorException, SCRDescriptorFailureException, IOException {
+        Env env = new Env("SimpleComponent").invoke();
+        EasyMock.replay(env.log());
+        env.generator().execute();
+        EasyMock.verify(env.log());
+    }
+
+    @Test
+    public void testComponentWithClassReferenceAndMissingInterface() throws SCRDescriptorException, SCRDescriptorFailureException, IOException {
+        Env env = new Env("ComponentWithClassReferenceAndMissingInterface").invoke();
+        EasyMock.replay(env.log());
+        try {
+            env.generator().execute();
+        } catch ( final SCRDescriptorFailureException e) {
+            // this is expected as the interface for a reference is missing
+        }
+        EasyMock.verify(env.log());
+    }
+
+    private void unpackSource(String resource, File dest) throws IOException {
+        IOUtils.copy(getClass().getResourceAsStream(resource), new FileOutputStream(dest));
+    }
+
+    /**
+     * Setups a minimal environment.
+     */
+    private class Env {
+        private String className;
+        private Log log;
+        private SCRDescriptorGenerator gen;
+
+        public Env(String className) {
+            this.className = className;
+        }
+
+        public Log log() {
+            return log;
+        }
+
+        public SCRDescriptorGenerator generator() {
+            return gen;
+        }
+
+        public Env invoke() throws IOException {
+            File aFile = new File(dest, className + ".class");
+            unpackSource("/testComponents/" + className + ".class", aFile);
+
+            log = EasyMock.createNiceMock(Log.class);
+            gen = new SCRDescriptorGenerator(log);
+            Project p = new Project();
+            p.setClassLoader(getClass().getClassLoader());
+            p.setSources(Collections.<Source>singletonList(new SourceImpl("testComponents." + className, aFile)));
+
+            Options o = new Options();
+            o.setOutputDirectory(folder);
+            gen.setProject(p);
+            gen.setOptions(o);
+            return this;
+        }
+    }
+}
diff --git a/scrplugin/generator/src/test/java/org/apache/felix/scrplugin/SourceImpl.java b/scrplugin/generator/src/test/java/org/apache/felix/scrplugin/SourceImpl.java
new file mode 100644
index 0000000..2601490
--- /dev/null
+++ b/scrplugin/generator/src/test/java/org/apache/felix/scrplugin/SourceImpl.java
@@ -0,0 +1,39 @@
+/*
+ * 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.felix.scrplugin;
+
+import java.io.File;
+
+class SourceImpl implements Source {
+    String className;
+    File file;
+
+    SourceImpl(String className, File file) {
+        this.className = className;
+        this.file = file;
+    }
+
+    public String getClassName() {
+        return className;
+    }
+
+    public File getFile() {
+        return file;
+    }
+}
diff --git a/scrplugin/generator/src/test/java/testComponents/ComponentWithClassReferenceAndMissingInterface.java b/scrplugin/generator/src/test/java/testComponents/ComponentWithClassReferenceAndMissingInterface.java
new file mode 100644
index 0000000..0114444
--- /dev/null
+++ b/scrplugin/generator/src/test/java/testComponents/ComponentWithClassReferenceAndMissingInterface.java
@@ -0,0 +1,28 @@
+/*
+ * 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 testComponents;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Reference;
+
+@Component
+@Reference(bind = "setRunnable", unbind = "unsetRunnable")
+public class ComponentWithClassReferenceAndMissingInterface {
+
+}
diff --git a/scrplugin/generator/src/test/java/testComponents/SimpleComponent.java b/scrplugin/generator/src/test/java/testComponents/SimpleComponent.java
new file mode 100644
index 0000000..a093198
--- /dev/null
+++ b/scrplugin/generator/src/test/java/testComponents/SimpleComponent.java
@@ -0,0 +1,25 @@
+/*
+ * 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 testComponents;
+
+import org.apache.felix.scr.annotations.Component;
+
+@Component
+public class SimpleComponent {
+}