felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1513731 - in /felix/trunk/scrplugin/generator: ./ src/main/java/org/apache/felix/scrplugin/helper/ src/test/ src/test/java/ src/test/java/org/ src/test/java/org/apache/ src/test/java/org/apache/felix/ src/test/java/org/apache/felix/scrplug...
Date Wed, 14 Aug 2013 05:52:12 GMT
Author: cziegeler
Date: Wed Aug 14 05:52:12 2013
New Revision: 1513731

URL: http://svn.apache.org/r1513731
Log:
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

Added:
    felix/trunk/scrplugin/generator/src/test/
    felix/trunk/scrplugin/generator/src/test/java/
    felix/trunk/scrplugin/generator/src/test/java/org/
    felix/trunk/scrplugin/generator/src/test/java/org/apache/
    felix/trunk/scrplugin/generator/src/test/java/org/apache/felix/
    felix/trunk/scrplugin/generator/src/test/java/org/apache/felix/scrplugin/
    felix/trunk/scrplugin/generator/src/test/java/org/apache/felix/scrplugin/SCRDescriptorGeneratorTest.java
  (with props)
    felix/trunk/scrplugin/generator/src/test/java/org/apache/felix/scrplugin/SourceImpl.java
  (with props)
    felix/trunk/scrplugin/generator/src/test/java/testComponents/
    felix/trunk/scrplugin/generator/src/test/java/testComponents/ComponentWithClassReferenceAndMissingInterface.java
  (with props)
    felix/trunk/scrplugin/generator/src/test/java/testComponents/SimpleComponent.java   (with
props)
Modified:
    felix/trunk/scrplugin/generator/changelog.txt
    felix/trunk/scrplugin/generator/pom.xml
    felix/trunk/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/helper/Validator.java

Modified: felix/trunk/scrplugin/generator/changelog.txt
URL: http://svn.apache.org/viewvc/felix/trunk/scrplugin/generator/changelog.txt?rev=1513731&r1=1513730&r2=1513731&view=diff
==============================================================================
--- felix/trunk/scrplugin/generator/changelog.txt (original)
+++ felix/trunk/scrplugin/generator/changelog.txt Wed Aug 14 05:52:12 2013
@@ -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

Modified: felix/trunk/scrplugin/generator/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/scrplugin/generator/pom.xml?rev=1513731&r1=1513730&r2=1513731&view=diff
==============================================================================
--- felix/trunk/scrplugin/generator/pom.xml (original)
+++ felix/trunk/scrplugin/generator/pom.xml Wed Aug 14 05:52:12 2013
@@ -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>

Modified: felix/trunk/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/helper/Validator.java
URL: http://svn.apache.org/viewvc/felix/trunk/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/helper/Validator.java?rev=1513731&r1=1513730&r2=1513731&view=diff
==============================================================================
--- felix/trunk/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/helper/Validator.java
(original)
+++ felix/trunk/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/helper/Validator.java
Wed Aug 14 05:52:12 2013
@@ -453,52 +453,54 @@ public class Validator {
             ref.setStrategy(ReferenceStrategy.EVENT);
         }
 
-        // 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";
-            }
-
-            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());
+        // 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";
                 }
-            } 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);
+                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 {
+                ref.setBind(null);
+                ref.setUnbind(null);
             }
-        } 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)

Added: felix/trunk/scrplugin/generator/src/test/java/org/apache/felix/scrplugin/SCRDescriptorGeneratorTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/scrplugin/generator/src/test/java/org/apache/felix/scrplugin/SCRDescriptorGeneratorTest.java?rev=1513731&view=auto
==============================================================================
--- felix/trunk/scrplugin/generator/src/test/java/org/apache/felix/scrplugin/SCRDescriptorGeneratorTest.java
(added)
+++ felix/trunk/scrplugin/generator/src/test/java/org/apache/felix/scrplugin/SCRDescriptorGeneratorTest.java
Wed Aug 14 05:52:12 2013
@@ -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;
+        }
+    }
+}

Propchange: felix/trunk/scrplugin/generator/src/test/java/org/apache/felix/scrplugin/SCRDescriptorGeneratorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: felix/trunk/scrplugin/generator/src/test/java/org/apache/felix/scrplugin/SCRDescriptorGeneratorTest.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Added: felix/trunk/scrplugin/generator/src/test/java/org/apache/felix/scrplugin/SourceImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/scrplugin/generator/src/test/java/org/apache/felix/scrplugin/SourceImpl.java?rev=1513731&view=auto
==============================================================================
--- felix/trunk/scrplugin/generator/src/test/java/org/apache/felix/scrplugin/SourceImpl.java
(added)
+++ felix/trunk/scrplugin/generator/src/test/java/org/apache/felix/scrplugin/SourceImpl.java
Wed Aug 14 05:52:12 2013
@@ -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;
+    }
+}

Propchange: felix/trunk/scrplugin/generator/src/test/java/org/apache/felix/scrplugin/SourceImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: felix/trunk/scrplugin/generator/src/test/java/org/apache/felix/scrplugin/SourceImpl.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Added: felix/trunk/scrplugin/generator/src/test/java/testComponents/ComponentWithClassReferenceAndMissingInterface.java
URL: http://svn.apache.org/viewvc/felix/trunk/scrplugin/generator/src/test/java/testComponents/ComponentWithClassReferenceAndMissingInterface.java?rev=1513731&view=auto
==============================================================================
--- felix/trunk/scrplugin/generator/src/test/java/testComponents/ComponentWithClassReferenceAndMissingInterface.java
(added)
+++ felix/trunk/scrplugin/generator/src/test/java/testComponents/ComponentWithClassReferenceAndMissingInterface.java
Wed Aug 14 05:52:12 2013
@@ -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 {
+
+}

Propchange: felix/trunk/scrplugin/generator/src/test/java/testComponents/ComponentWithClassReferenceAndMissingInterface.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: felix/trunk/scrplugin/generator/src/test/java/testComponents/ComponentWithClassReferenceAndMissingInterface.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Added: felix/trunk/scrplugin/generator/src/test/java/testComponents/SimpleComponent.java
URL: http://svn.apache.org/viewvc/felix/trunk/scrplugin/generator/src/test/java/testComponents/SimpleComponent.java?rev=1513731&view=auto
==============================================================================
--- felix/trunk/scrplugin/generator/src/test/java/testComponents/SimpleComponent.java (added)
+++ felix/trunk/scrplugin/generator/src/test/java/testComponents/SimpleComponent.java Wed
Aug 14 05:52:12 2013
@@ -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 {
+}

Propchange: felix/trunk/scrplugin/generator/src/test/java/testComponents/SimpleComponent.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: felix/trunk/scrplugin/generator/src/test/java/testComponents/SimpleComponent.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url



Mime
View raw message