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.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1653464 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/metatype/src/main/java/org/apache/felix/metatype/Attribute.java b/metatype/src/main/java/org/apache/felix/metatype/Attribute.java
index 6458e6d..a62a91d 100644
--- a/metatype/src/main/java/org/apache/felix/metatype/Attribute.java
+++ b/metatype/src/main/java/org/apache/felix/metatype/Attribute.java
@@ -18,7 +18,6 @@
*/
package org.apache.felix.metatype;
-
/**
* The <code>Attribute</code> TODO
*
@@ -30,58 +29,53 @@
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 });
}
}
}
-
}
diff --git a/metatype/src/test/java/org/apache/felix/metatype/ADTest.java b/metatype/src/test/java/org/apache/felix/metatype/ADTest.java
index 82c9c6d..b271c82 100644
--- a/metatype/src/test/java/org/apache/felix/metatype/ADTest.java
+++ b/metatype/src/test/java/org/apache/felix/metatype/ADTest.java
@@ -157,6 +157,7 @@
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"));
diff --git a/metatype/src/test/java/org/apache/felix/metatype/AttributeTest.java b/metatype/src/test/java/org/apache/felix/metatype/AttributeTest.java
new file mode 100644
index 0000000..1ff5349
--- /dev/null
+++ b/metatype/src/test/java/org/apache/felix/metatype/AttributeTest.java
@@ -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());
+ }
+}