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/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 {
+}