1 5 6 package org.exoplatform.services.jcr.api.writing; 7 8 9 import javax.jcr.*; 10 import org.exoplatform.services.jcr.BaseTest; 11 import org.exoplatform.services.jcr.impl.core.PropertyImpl; 12 import java.util.GregorianCalendar ; 13 import java.io.ByteArrayInputStream ; 14 15 20 public class AddPropertyTest extends BaseTest{ 21 22 public void initRepository() throws RepositoryException { 23 Node root = ticket.getRootNode(); 24 Node propDef = root.addNode("propertyDefNode", "nt:propertyDef"); 25 propDef.setProperty("jcr:defaultValue", "testString"); 27 root.addNode("childNodeDefNode", "nt:childNodeDef"); 28 ticket.save(); 29 } 30 31 public void tearDown() throws RepositoryException { 32 ticket = repository.login(credentials, WORKSPACE); 33 Node root = ticket.getRootNode(); 34 root.remove("propertyDefNode"); 35 root.remove("childNodeDefNode"); 36 ticket.save(); 37 } 38 39 public void testSetPropertyNameValue() throws RepositoryException { 40 Node root = ticket.getRootNode(); 41 Node node = root.getNode("propertyDefNode"); 42 43 try { 44 node.setProperty("jcr:multiple", new LongValue(20)); 45 fail("exception should have been thrown"); 46 } catch (ValueFormatException e) { 47 } 48 ticket.revert(); 49 50 node.setProperty("jcr:defaultValue", new ReferenceValue("/a/b/c")); 51 assertEquals(PropertyType.REFERENCE, node.getProperty("jcr:defaultValue").getValue().getType()); 52 53 node.save(false); 54 ticket = repository.login(credentials, WORKSPACE); 55 node = ticket.getRootNode().getNode("propertyDefNode"); 56 assertEquals("/a/b/c", node.getProperty("jcr:defaultValue").getString()); 57 } 58 59 public void testSetPropertyNameValueType() throws RepositoryException { 60 Node root = ticket.getRootNode(); 61 Node node = root.getNode("propertyDefNode"); 62 try { 63 node.setProperty("jcr:multiple", new StringValue("not"), PropertyType.LONG); 64 fail("exception should have been thrown"); 65 } catch (ValueFormatException e) { 66 } 67 68 try { 69 node.setProperty("jcr:autoCreate", new DateValue(new GregorianCalendar ()), PropertyType.DATE); 70 fail("exception should have been thrown"); 71 } catch (ValueFormatException e) { 72 } 73 ticket.revert(); 74 node.setProperty("jcr:defaultValue", new StringValue("10"), PropertyType.LONG); 75 assertEquals(PropertyType.LONG, node.getProperty("jcr:defaultValue").getValue().getType()); 76 assertEquals(10, node.getProperty("jcr:defaultValue").getLong()); 77 node.save(false); 78 ticket = repository.login(credentials, WORKSPACE); 79 node = ticket.getRootNode().getNode("propertyDefNode"); 80 assertEquals(10, node.getProperty("jcr:defaultValue").getLong()); 81 } 82 83 public void testSetPropertyNameValuesType() throws RepositoryException { 84 Node root = ticket.getRootNode(); 85 Node node = root.getNode("childNodeDefNode"); 86 Value[] values = {new StringValue("not"), new StringValue("in")}; 87 try { 88 node.setProperty("jcr:requiredPrimaryTypes", values, PropertyType.LONG); 89 fail("exception should have been thrown"); 90 } catch (ValueFormatException e) { 91 } 92 try { 93 node.setProperty("jcr:onParentVersion", values, PropertyType.STRING); 94 fail("exception should have been thrown"); 95 } catch (ValueFormatException e) { 96 } 97 98 node.setProperty("jcr:requiredPrimaryTypes", values, PropertyType.STRING); 99 node.save(false); 100 101 ticket = repository.login(credentials, WORKSPACE); 102 node = ticket.getRootNode().getNode("childNodeDefNode"); 103 assertEquals(2, node.getProperty("jcr:requiredPrimaryTypes").getValues().length); 104 } 105 106 public void testSetPropertyNameStringValueType() throws RepositoryException { 107 Node root = ticket.getRootNode(); 108 Node node = root.getNode("propertyDefNode"); 109 try { 110 node.setProperty("jcr:multiple", "not", PropertyType.LONG); 111 fail("exception should have been thrown"); 112 } catch (ValueFormatException e) { 113 } 114 ticket.revert(); 115 116 node.setProperty("jcr:defaultValue", "10", PropertyType.LONG); 117 assertEquals(PropertyType.LONG, node.getProperty("jcr:defaultValue").getValue().getType()); 118 assertEquals(10, node.getProperty("jcr:defaultValue").getLong()); 119 node.save(false); 120 ticket = repository.login(credentials, WORKSPACE); 121 node = ticket.getRootNode().getNode("propertyDefNode"); 122 assertEquals(10, node.getProperty("jcr:defaultValue").getLong()); 123 } 124 125 public void testSetPropertyNameStringValuesType() throws RepositoryException { 126 Node root = ticket.getRootNode(); 127 Node node = root.getNode("childNodeDefNode"); 128 String [] values = {"not", "in"}; 129 try { 130 node.setProperty("jcr:requiredPrimaryTypes", values, PropertyType.LONG); 131 fail("exception should have been thrown"); 132 } catch (ValueFormatException e) { 133 } 134 try { 135 node.setProperty("jcr:onParentVersion", values, PropertyType.STRING); 136 fail("exception should have been thrown"); 137 } catch (ValueFormatException e) { 138 } 139 140 node.setProperty("jcr:requiredPrimaryTypes", values, PropertyType.STRING); 141 node.save(false); 142 143 ticket = repository.login(credentials, WORKSPACE); 144 node = ticket.getRootNode().getNode("childNodeDefNode"); 145 assertEquals(2, node.getProperty("jcr:requiredPrimaryTypes").getValues().length); 146 } 147 148 public void testSetPropertyNameTypedValue() throws RepositoryException { 149 Node root = ticket.getRootNode(); 150 Node node = root.getNode("propertyDefNode"); 151 152 node.setProperty("jcr:defaultValue", "default"); 153 node.setProperty("jcr:defaultValue", new ByteArrayInputStream (new String ("default").getBytes())); 154 node.setProperty("jcr:defaultValue", true); 155 node.setProperty("jcr:defaultValue", new GregorianCalendar ()); 156 node.setProperty("jcr:defaultValue", 20D); 157 node.setProperty("jcr:defaultValue", 20L); 158 159 try { 160 node.setProperty("jcr:multiple", 20D); 161 fail("exception should have been thrown"); 162 } catch (ValueFormatException e) { 163 } 164 } 165 } 166 | Popular Tags |