|
2 | 2 |
|
3 | 3 | import com.dd.plist.*; |
4 | 4 | import junit.framework.TestCase; |
| 5 | +import org.xml.sax.SAXException; |
5 | 6 |
|
| 7 | +import javax.xml.parsers.ParserConfigurationException; |
6 | 8 | import java.io.File; |
| 9 | +import java.io.IOException; |
| 10 | +import java.text.ParseException; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.Map; |
7 | 13 |
|
8 | 14 | public class IssueTest extends TestCase { |
9 | 15 | public static void testIssue4() throws Exception { |
@@ -71,6 +77,69 @@ public static void testIssue38() throws Exception { |
71 | 77 | assertTrue(fileRef.equals(new NSString("65541A9B16D13B8C00A968D5"))); |
72 | 78 | } |
73 | 79 |
|
| 80 | + /** |
| 81 | + * Test storing null values |
| 82 | + */ |
| 83 | + public static void testIssue41() { |
| 84 | + //Dictionary |
| 85 | + Map<String, Object> nullMap = new HashMap<String, Object>(); |
| 86 | + nullMap.put("key", null); |
| 87 | + assertFalse(nullMap.isEmpty()); |
| 88 | + NSDictionary nullDict = NSObject.wrap(nullMap); |
| 89 | + assertTrue(nullDict.isEmpty()); |
| 90 | + |
| 91 | + nullDict.put(null, "test"); |
| 92 | + assertTrue(nullDict.isEmpty()); |
| 93 | + |
| 94 | + nullDict.put("test", null); |
| 95 | + assertTrue(nullDict.isEmpty()); |
| 96 | + |
| 97 | + try { |
| 98 | + assertTrue(((NSDictionary)PropertyListParser.parse(nullDict.toXMLPropertyList().getBytes())).isEmpty()); |
| 99 | + } catch (Exception e) { |
| 100 | + throw new AssertionError("No exception should have occurred while parsing an empty dictionary", e); |
| 101 | + } |
| 102 | + |
| 103 | + //Array |
| 104 | + String[] strArr = new String[3]; |
| 105 | + strArr[0] = ""; |
| 106 | + strArr[1] = null; |
| 107 | + strArr[2] = null; |
| 108 | + NSArray nsArr = NSObject.wrap(strArr); |
| 109 | + assertTrue(nsArr.containsObject(null)); |
| 110 | + assertEquals(nsArr.objectAtIndex(1), null); |
| 111 | + assertEquals(nsArr.objectAtIndex(2), null); |
| 112 | + |
| 113 | + try { |
| 114 | + nsArr.toXMLPropertyList(); |
| 115 | + throw new AssertionError("Storing a NSArray containing a null value as a XML property list should throw an exception"); |
| 116 | + } catch(NullPointerException ex) { |
| 117 | + //expected exception |
| 118 | + } |
| 119 | + |
| 120 | + try { |
| 121 | + nsArr.toASCIIPropertyList(); |
| 122 | + throw new AssertionError("Storing a NSArray containing a null value as a ASCII property list should throw an exception"); |
| 123 | + } catch(NullPointerException ex) { |
| 124 | + //expected exception |
| 125 | + } |
| 126 | + |
| 127 | + try { |
| 128 | + nsArr.toGnuStepASCIIPropertyList(); |
| 129 | + throw new AssertionError("Storing a NSArray containing a null value as a GnuStep ASCII property list should throw an exception"); |
| 130 | + } catch(NullPointerException ex) { |
| 131 | + //expected exception |
| 132 | + } |
| 133 | + |
| 134 | + try { |
| 135 | + byte[] bin = BinaryPropertyListWriter.writeToArray(nsArr); |
| 136 | + throw new AssertionError("Storing a NSArray containing a null value as a binary property list should throw an exception"); |
| 137 | + } catch(IOException ex) { |
| 138 | + //expect IOException because binary v1.0 format (which could theoretically store null values) is not supported |
| 139 | + //But v1.0 format is not even supported by OS X 10.10, so there is no plan as of yet to implement it |
| 140 | + } |
| 141 | + } |
| 142 | + |
74 | 143 | public static void testIssue49() throws Exception { |
75 | 144 | NSDictionary dict = (NSDictionary)PropertyListParser.parse(new File("test-files/issue49.plist")); |
76 | 145 | assertEquals(0, dict.count()); |
|
0 commit comments