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