Skip to content

Commit

Permalink
Fix handling of null values during serialization (See updated comment…
Browse files Browse the repository at this point in the history
…s on issue 41)
  • Loading branch information
3breadt committed Feb 27, 2015
1 parent 39f7ab9 commit cea7d4e
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 41 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<groupId>com.googlecode.plist</groupId>
<artifactId>dd-plist</artifactId>
<packaging>jar</packaging>
<version>1.13</version>
<version>1.15</version>
<name>dd-plist</name>
<url>http://plist.googlecode.com</url>
<description>
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/com/dd/plist/BinaryPropertyListParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,6 @@ public static NSObject parse(InputStream is) throws IOException, PropertyListFor
* @throws PropertyListFormatException When the property list's format could not be parsed.
*/
public static NSObject parse(File f) throws IOException, PropertyListFormatException {
if (f.length() > Runtime.getRuntime().freeMemory()) {
throw new OutOfMemoryError("To little heap space available! Wanted to read " + f.length() + " bytes, but only " + Runtime.getRuntime().freeMemory() + " are available.");
//not yet even implemented in Core Foundation as of revision 855.17
}
return parse(new FileInputStream(f));
}

Expand Down
9 changes: 7 additions & 2 deletions src/main/java/com/dd/plist/NSArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ public void remove(int i) {
* @param value The object.
*/
public void setValue(int key, Object value) {
if(value == null)
throw new NullPointerException("Cannot add null values to an NSArray!");
array[key] = NSObject.wrap(value);
}

Expand Down Expand Up @@ -123,6 +121,11 @@ public int count() {
public boolean containsObject(Object obj) {
NSObject nso = NSObject.wrap(obj);
for (NSObject elem : array) {
if(elem == null) {
if(obj == null)
return true;
continue;
}
if (elem.equals(nso)) {
return true;
}
Expand Down Expand Up @@ -197,6 +200,8 @@ public NSObject[] objectsAtIndexes(int... indexes) {

@Override
public boolean equals(Object obj) {
if(obj == null)
return false;
if(obj.getClass().equals(NSArray.class)) {
return Arrays.equals(((NSArray) obj).getArray(), this.array);
} else {
Expand Down
36 changes: 3 additions & 33 deletions src/main/java/com/dd/plist/NSDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,56 +148,26 @@ public void putAll(Map<? extends String, ? extends NSObject> values) {
* or null, if no value was associated to it.
*/
public NSObject put(String key, NSObject obj) {
if(key == null)
return null;
if(obj == null)
return dict.get(key);
return dict.put(key, obj);
}

/**
* Puts a new key-value pair into this dictionary.
* If the value is null, no operation will be performed on the dictionary.
* If key or value are null, no operation will be performed on the dictionary.
*
* @param key The key.
* @param obj The value. Supported object types are numbers, byte-arrays, dates, strings and arrays or sets of those.
* @return The value previously associated to the given key,
* or null, if no value was associated to it.
*/
public NSObject put(String key, Object obj) {
if(obj == null)
return dict.get(key);
return put(key, NSObject.wrap(obj));
}

/**
* Puts a new key-value pair into this dictionary.
*
* @param key The key.
* @param obj The value.
*/
public NSObject put(String key, long obj) {
return put(key, new NSNumber(obj));
}

/**
* Puts a new key-value pair into this dictionary.
*
* @param key The key.
* @param obj The value.
*/
public NSObject put(String key, double obj) {
return put(key, new NSNumber(obj));
}

/**
* Puts a new key-value pair into this dictionary.
*
* @param key The key.
* @param obj The value.
*/
public NSObject put(String key, boolean obj) {
return put(key, new NSNumber(obj));
}

/**
* Removes a key-value pair from this dictionary.
*
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/dd/plist/NSObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public static NSSet wrap(Set<Object> value) {
*/
public static NSObject wrap(Object o) {
if(o == null)
throw new NullPointerException("A null object cannot be wrapped as a NSObject");
return null;

if(o instanceof NSObject)
return (NSObject)o;
Expand Down
69 changes: 69 additions & 0 deletions src/test/java/com/dd/plist/test/IssueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

import com.dd.plist.*;
import junit.framework.TestCase;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

public class IssueTest extends TestCase {
public static void testIssue4() throws Exception {
Expand Down Expand Up @@ -71,6 +77,69 @@ public static void testIssue38() throws Exception {
assertTrue(fileRef.equals(new NSString("65541A9B16D13B8C00A968D5")));
}

/**
* Test storing null values
*/
public static void testIssue41() {
//Dictionary
Map<String, Object> nullMap = new HashMap<String, Object>();
nullMap.put("key", null);
assertFalse(nullMap.isEmpty());
NSDictionary nullDict = NSObject.wrap(nullMap);
assertTrue(nullDict.isEmpty());

nullDict.put(null, "test");
assertTrue(nullDict.isEmpty());

nullDict.put("test", null);
assertTrue(nullDict.isEmpty());

try {
assertTrue(((NSDictionary)PropertyListParser.parse(nullDict.toXMLPropertyList().getBytes())).isEmpty());
} catch (Exception e) {
throw new AssertionError("No exception should have occurred while parsing an empty dictionary", e);
}

//Array
String[] strArr = new String[3];
strArr[0] = "";
strArr[1] = null;
strArr[2] = null;
NSArray nsArr = NSObject.wrap(strArr);
assertTrue(nsArr.containsObject(null));
assertEquals(nsArr.objectAtIndex(1), null);
assertEquals(nsArr.objectAtIndex(2), null);

try {
nsArr.toXMLPropertyList();
throw new AssertionError("Storing a NSArray containing a null value as a XML property list should throw an exception");
} catch(NullPointerException ex) {
//expected exception
}

try {
nsArr.toASCIIPropertyList();
throw new AssertionError("Storing a NSArray containing a null value as a ASCII property list should throw an exception");
} catch(NullPointerException ex) {
//expected exception
}

try {
nsArr.toGnuStepASCIIPropertyList();
throw new AssertionError("Storing a NSArray containing a null value as a GnuStep ASCII property list should throw an exception");
} catch(NullPointerException ex) {
//expected exception
}

try {
byte[] bin = BinaryPropertyListWriter.writeToArray(nsArr);
throw new AssertionError("Storing a NSArray containing a null value as a binary property list should throw an exception");
} catch(IOException ex) {
//expect IOException because binary v1.0 format (which could theoretically store null values) is not supported
//But v1.0 format is not even supported by OS X 10.10, so there is no plan as of yet to implement it
}
}

public static void testIssue49() throws Exception {
NSDictionary dict = (NSDictionary)PropertyListParser.parse(new File("test-files/issue49.plist"));
assertEquals(0, dict.count());
Expand Down

0 comments on commit cea7d4e

Please sign in to comment.