You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by ja...@apache.org on 2015/01/21 10:35:35 UTC

svn commit: r1653464 - in /felix/trunk/metatype/src: main/java/org/apache/felix/metatype/Attribute.java test/java/org/apache/felix/metatype/ADTest.java test/java/org/apache/felix/metatype/AttributeTest.java

Author: jawi
Date: Wed Jan 21 09:35:35 2015
New Revision: 1653464

URL: http://svn.apache.org/r1653464
Log:
FELIX-4771 - Avoid NPE in Attribute#getContent:

- no longer clone the content field, as it is already a local
  clone, and this pattern isn't used in the other parts of the
  API (such as AD);
- added small test case to verify this behaviour.


Added:
    felix/trunk/metatype/src/test/java/org/apache/felix/metatype/AttributeTest.java   (with props)
Modified:
    felix/trunk/metatype/src/main/java/org/apache/felix/metatype/Attribute.java
    felix/trunk/metatype/src/test/java/org/apache/felix/metatype/ADTest.java

Modified: felix/trunk/metatype/src/main/java/org/apache/felix/metatype/Attribute.java
URL: http://svn.apache.org/viewvc/felix/trunk/metatype/src/main/java/org/apache/felix/metatype/Attribute.java?rev=1653464&r1=1653463&r2=1653464&view=diff
==============================================================================
--- felix/trunk/metatype/src/main/java/org/apache/felix/metatype/Attribute.java (original)
+++ felix/trunk/metatype/src/main/java/org/apache/felix/metatype/Attribute.java Wed Jan 21 09:35:35 2015
@@ -18,7 +18,6 @@
  */
 package org.apache.felix.metatype;
 
-
 /**
  * The <code>Attribute</code> TODO
  *
@@ -30,58 +29,53 @@ public class Attribute extends OptionalA
     private String adRef;
     private String[] content;
 
-
     public String getAdRef()
     {
         return adRef;
     }
 
-
-    public void setAdRef( String adRef )
+    public void setAdRef(String adRef)
     {
         this.adRef = adRef;
     }
 
-
     public String[] getContent()
     {
-        return ( String[] ) content.clone();
+        // FELIX-4771 - removed the clone as we're already working on a local 
+        // copy and this pattern isn't used in other parts of the API...
+        return this.content;
     }
 
-
-    public void addContent( String[] added )
+    public void addContent(String[] added)
     {
-        if ( added != null && added.length > 0 )
+        if (added != null && added.length > 0)
         {
-            if ( content == null )
+            if (content == null)
             {
-                content = ( String[] ) added.clone();
+                content = (String[]) added.clone();
             }
             else
             {
                 String[] newContent = new String[content.length + added.length];
-                System.arraycopy( content, 0, newContent, 0, content.length );
-                System.arraycopy( added, 0, newContent, content.length, added.length );
+                System.arraycopy(content, 0, newContent, 0, content.length);
+                System.arraycopy(added, 0, newContent, content.length, added.length);
                 content = newContent;
             }
         }
     }
 
-
-    public void addContent( String content, boolean split )
+    public void addContent(String content, boolean split)
     {
-        if ( content != null )
+        if (content != null)
         {
-            if ( split )
+            if (split)
             {
-                addContent( AD.splitList( content ) );
+                addContent(AD.splitList(content));
             }
             else
             {
-                addContent( new String[]
-                    { content } );
+                addContent(new String[] { content });
             }
         }
     }
-
 }

Modified: felix/trunk/metatype/src/test/java/org/apache/felix/metatype/ADTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/metatype/src/test/java/org/apache/felix/metatype/ADTest.java?rev=1653464&r1=1653463&r2=1653464&view=diff
==============================================================================
--- felix/trunk/metatype/src/test/java/org/apache/felix/metatype/ADTest.java (original)
+++ felix/trunk/metatype/src/test/java/org/apache/felix/metatype/ADTest.java Wed Jan 21 09:35:35 2015
@@ -157,6 +157,7 @@ public class ADTest extends TestCase
         assertEquals(AttributeDefinition.INTEGER, AD.toType("Integer"));
         assertEquals(AttributeDefinition.BYTE, AD.toType("Byte"));
         assertEquals(AttributeDefinition.CHARACTER, AD.toType("Char"));
+        assertEquals(AttributeDefinition.CHARACTER, AD.toType("Character"));
         assertEquals(AttributeDefinition.BOOLEAN, AD.toType("Boolean"));
         assertEquals(AttributeDefinition.SHORT, AD.toType("Short"));
         assertEquals(AttributeDefinition.PASSWORD, AD.toType("Password"));

Added: felix/trunk/metatype/src/test/java/org/apache/felix/metatype/AttributeTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/metatype/src/test/java/org/apache/felix/metatype/AttributeTest.java?rev=1653464&view=auto
==============================================================================
--- felix/trunk/metatype/src/test/java/org/apache/felix/metatype/AttributeTest.java (added)
+++ felix/trunk/metatype/src/test/java/org/apache/felix/metatype/AttributeTest.java Wed Jan 21 09:35:35 2015
@@ -0,0 +1,63 @@
+/*
+ * 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.metatype;
+
+import junit.framework.TestCase;
+
+/**
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
+public class AttributeTest extends TestCase
+{
+
+    /**
+     * FELIX-4771 - attributes can be content-less.
+     */
+    public void testAttributeWithoutContentOk() throws Exception
+    {
+        Attribute attr = new Attribute();
+
+        assertNull(attr.getContent());
+
+        // Null-values are ignored, so attribute remains empty...
+        attr.addContent(null, false);
+
+        assertNull(attr.getContent());
+    }
+
+    /**
+     * FELIX-4771 - attributes can be content-less.
+     */
+    public void testAttributeWithNullContentOk() throws Exception
+    {
+        Attribute attr = new Attribute();
+        // Null-values are ignored, so attribute remains empty...
+        attr.addContent(null, false);
+
+        assertNull(attr.getContent());
+    }
+
+    public void testAttributeWithContentOk() throws Exception
+    {
+        Attribute attr = new Attribute();
+        attr.addContent("foo", false /* split */);
+
+        assertNotNull(attr.getContent());
+    }
+}

Propchange: felix/trunk/metatype/src/test/java/org/apache/felix/metatype/AttributeTest.java
------------------------------------------------------------------------------
    svn:eol-style = native