1 5 6 package org.exoplatform.services.jcr.api.reading; 7 8 9 import javax.jcr.*; 10 import javax.jcr.nodetype.NodeType; 11 import org.exoplatform.services.jcr.BaseTest; 12 import org.exoplatform.services.jcr.impl.core.NodeImpl; 13 import java.util.Calendar ; 14 import java.util.Date ; 15 import java.util.GregorianCalendar ; 16 import java.io.ByteArrayInputStream ; 17 18 23 public class NodeTest extends BaseTest{ 24 25 public void initRepository() throws RepositoryException { 26 Node root = ticket.getRootNode(); 27 Node file = root.addNode("childNode", "nt:folder").addNode("childNode2", "nt:file"); 28 Node contentNode = file.getNode("jcr:content"); 29 contentNode.setProperty("exo:content", new StringValue("this is the content")); 30 ticket.save(); 31 } 32 33 public void tearDown() throws RepositoryException { 34 38 Node root = ticket.getRootNode(); 39 root.remove("childNode"); 40 ticket.save(); 41 42 } 44 45 public void testGetNode() throws RepositoryException { 46 Node root = ticket.getRootNode(); 47 Node node = root.getNode("/childNode/childNode2"); 48 assertNotNull(node); 49 assertEquals("nt:file", node.getPrimaryNodeType().getName()); 50 Property property = node.getNode("jcr:content").getProperty("exo:content"); 51 property.setValue(new StringValue("this is the NEW content")); 52 53 node = root.getNode("/childNode"); 54 node.addNode("childNode3", "nt:file"); 55 56 ticket = repository.login(credentials, WORKSPACE); 57 root = ticket.getRootNode(); 58 try { 59 root.getNode("/childNode/childNode3"); 60 fail("exception should have been thrown"); 61 } catch (RepositoryException e) { 62 } 63 property = root.getNode("/childNode/childNode2/jcr:content"). 64 getProperty("exo:content"); 65 66 assertEquals("this is the content", property.getString()); 67 Value val = new StringValue("this is the NEW content"); 68 property.setValue(val); 69 node = root.getNode("/childNode"); 71 72 System.out.println("SAVE"); 74 ticket.save(); 75 System.out.println("END SAVE"); 76 77 root = repository.login(credentials, WORKSPACE).getRootNode(); 78 property = root.getNode("/childNode/childNode2/jcr:content"). 80 getProperty("exo:content"); 81 assertEquals("this is the NEW content", property.getString()); 82 83 ticket = repository.login(credentials, WORKSPACE); 84 root = ticket.getRootNode(); 85 node = root.getNode("/childNode"); 86 assertEquals(node, root.getNode("/childNode")); 87 root.getNode("childNode/childNode2/jcr:content").setProperty("myapp:temp", new StringValue("Temp")); 88 89 Ticket ticket2 = repository.login(credentials, WORKSPACE); 90 Node root2 = ticket2.getRootNode(); 91 Node node2 = root2.getNode("childNode/childNode2/jcr:content"); 92 node2.setProperty("exo:content", new StringValue("Temp")); 93 ticket2.save(); 94 95 ticket.revert(); 96 root = ticket.getRootNode(); 97 node = root.getNode("childNode/childNode2/jcr:content"); 98 assertNotNull(node); 99 assertNotNull(node.getProperty("exo:content")); 100 assertEquals("Temp", node.getProperty("exo:content").getString()); 101 try { 102 node.getProperty("myapp:temp"); 103 fail("exception should have been thrown"); 104 } catch (RepositoryException e) { 105 } 106 107 } 108 109 public void testGetNodes() throws RepositoryException { 110 ticket = repository.login(credentials, WORKSPACE); 111 Node root = ticket.getRootNode(); 112 Node node = root.getNode("/childNode"); 113 node.addNode("childNode4", "nt:folder"); 114 115 NodeIterator nodeIterator = node.getNodes(); 116 while(nodeIterator.hasNext()){ 117 node = (Node) nodeIterator.next(); 118 assertNotNull(node.getTicket()); 119 if(!("childNode4".equals(node.getName()) || "childNode2".equals(node.getName()))) 120 fail("returned non expected nodes"); 121 } 122 123 Ticket ticket2 = repository.login(credentials, WORKSPACE); 124 Node root2 = ticket2.getRootNode(); 125 Node node2 = root2.getNode("/childNode"); 126 node2.addNode("childNode5", "nt:folder"); 127 ticket2.save(); 128 129 ticket.revert(); 130 131 node = root.getNode("/childNode"); 132 nodeIterator = node.getNodes(); 133 while(nodeIterator.hasNext()){ 134 node = (Node) nodeIterator.next(); 135 137 if(!("childNode5".equals(node.getName()) || "childNode2".equals(node.getName()))) 138 fail("returned non expected nodes "+node.getName()); 139 } 140 } 141 142 public void testGetNodesWithNamePattern() throws RepositoryException{ 143 ticket = repository.login(credentials, WORKSPACE); 144 Node root = ticket.getRootNode(); 145 Node node = root.getNode("/childNode"); 146 node.addNode("childNode4", "nt:folder"); 147 node.addNode("otherNode", "nt:folder"); 148 node.addNode("lastNode", "nt:folder"); 149 150 Node result = (Node) node.getNodes("lastNode").next(); 151 assertEquals("lastNode", result.getName()); 152 153 NodeIterator iterator = node.getNodes("otherNode | lastNode"); 154 if(!iterator.hasNext()) 155 fail("nodes should have been found"); 156 while(iterator.hasNext()){ 157 Node nodeTmp = iterator.nextNode(); 158 if(!("otherNode".equals(nodeTmp.getName()) || "lastNode".equals(nodeTmp.getName()))) 159 fail("returned non expected nodes"); 160 } 161 162 iterator = node.getNodes("childNode*"); 163 if(!iterator.hasNext()) 164 fail("nodes should have been found"); 165 while(iterator.hasNext()){ 166 Node nodeTmp = iterator.nextNode(); 167 if(!("childNode2".equals(nodeTmp.getName()) || "childNode4".equals(nodeTmp.getName()))) 168 fail("returned non expected nodes"); 169 } 170 171 Ticket ticket2 = repository.login(credentials, WORKSPACE); 172 Node root2 = ticket2.getRootNode(); 173 Node node2 = root2.getNode("/childNode"); 174 node2.addNode("childNode5", "nt:folder"); 175 ticket2.save(); 176 177 ticket.revert(); 178 node = root.getNode("/childNode"); 179 iterator = node.getNodes("childNode*"); 180 if(!iterator.hasNext()) 181 fail("nodes should have been found"); 182 while(iterator.hasNext()){ 183 Node nodeTmp = iterator.nextNode(); 184 if(!("childNode2".equals(nodeTmp.getName()) || "childNode5".equals(nodeTmp.getName()))) 185 fail("returned non expected nodes"); 186 } 187 } 188 189 public void testGetProperty() throws RepositoryException { 190 ticket = repository.login(credentials, WORKSPACE); 191 Node root = ticket.getRootNode(); 192 Node node = root.getNode("/childNode/childNode2/jcr:content"); 193 Property property = node.getProperty("exo:content"); 194 assertEquals("this is the content", property.getString()); 195 196 Ticket ticket2 = repository.login(credentials, WORKSPACE); 197 Node root2 = ticket2.getRootNode(); 198 Node node2 = root2.getNode("/childNode/childNode2/jcr:content"); 199 node2.getProperty("exo:content").setValue("this is the NEW value"); 200 ticket2.save(); 201 202 ticket.revert(); 203 property = root.getNode("/childNode/childNode2/jcr:content").getProperty("exo:content"); 204 assertEquals("/childNode/childNode2/jcr:content/exo:content", property.getPath()); 205 assertEquals("this is the NEW value", property.getString()); 206 } 207 208 public void testGetProperties() throws RepositoryException { 209 ticket = repository.login(credentials, WORKSPACE); 210 Node root = ticket.getRootNode(); 211 Node node = root.getNode("/childNode"); 212 213 PropertyIterator iterator = node.getProperties(); 214 while(iterator.hasNext()){ 215 Property property = iterator.nextProperty(); 216 if(!("jcr:primaryType".equals(property.getName()) || "jcr:created".equals(property.getName()) || 217 "jcr:lastModified".equals(property.getName()))) 218 fail("returned non expected nodes"); 219 } 220 221 Ticket ticket2 = repository.login(credentials, WORKSPACE); 222 Node root2 = ticket2.getRootNode(); 223 Node node2 = root2.getNode("/childNode/childNode2/jcr:content"); 224 node2.setProperty("exo:prop", new StringValue("hehe")); 225 ticket2.save(); 226 227 ticket.revert(); 228 node = root.getNode("/childNode/childNode2/jcr:content"); 229 iterator = node.getProperties(); 230 while(iterator.hasNext()){ 231 Property property = iterator.nextProperty(); 232 if(!("jcr:primaryType".equals(property.getName()) || "jcr:created".equals(property.getName()) || 233 "jcr:lastModified".equals(property.getName()) || "exo:prop".equals(property.getName()) || 234 "exo:content".equals(property.getName()) || "jcr:uuid".equals(property.getName()))) 235 fail("returned non expected properties"); 236 } 237 } 238 239 public void testGetPropertiesWithNamePattern() throws RepositoryException { 240 ticket = repository.login(credentials, WORKSPACE); 241 Node root = ticket.getRootNode(); 242 Node node = root.getNode("/childNode/childNode2/jcr:content"); 243 node.setProperty("property1", "prop1Value"); 244 node.setProperty("property2", "prop2Value"); 245 246 PropertyIterator iterator = node.getProperties("property1 | property2"); 247 while(iterator.hasNext()){ 248 Property property = iterator.nextProperty(); 249 if(!("property1".equals(property.getName()) || "property2".equals(property.getName()))) 250 fail("returned non expected properties"); 251 } 252 253 iterator = node.getProperties("property1 | jcr:*"); 254 while(iterator.hasNext()){ 255 Property property = iterator.nextProperty(); 256 if(!("property1".equals(property.getName()) || "jcr:primaryType".equals(property.getName()) 257 || "jcr:created".equals(property.getName()) || "jcr:lastModified".equals(property.getName()) || 258 "exo:content".equals(property.getName()) || "jcr:uuid".equals(property.getName()) )) 259 fail("returned non expected properties"); 260 } 261 262 Ticket ticket2 = repository.login(credentials, WORKSPACE); 263 Node root2 = ticket2.getRootNode(); 264 Node node2 = root2.getNode("/childNode/childNode2/jcr:content"); 265 node2.setProperty("exo:prop", new StringValue("hehe")); 266 ticket2.save(); 267 268 ticket.revert(); 269 node = root.getNode("/childNode/childNode2/jcr:content"); 270 iterator = node.getProperties("exo:* | property1"); 271 while(iterator.hasNext()){ 272 Property property = iterator.nextProperty(); 273 if(!("property1".equals(property.getName()) || "exo:prop".equals(property.getName()) || 274 "exo:content".equals(property.getName()) )) 275 fail("returned non expected properties"); 276 } 277 } 278 279 public void testFindProperty() throws RepositoryException { 280 ticket = repository.login(credentials, WORKSPACE); 281 Node root = ticket.getRootNode(); 282 Node node = root.getNode("/childNode/childNode2/jcr:content"); 283 node.setProperty("property1", "prop1Value"); 284 assertEquals("property1", node.findProperty(new StringValue("prop1Value")).getName()); 285 assertNull(node.findProperty(new StringValue("prop2Value"))); 286 node.setProperty("property2", "prop2Value"); 287 assertEquals("property1", node.findProperty(new StringValue("prop1Value")).getName()); 288 ticket.save(); 289 290 ticket = repository.login(credentials, WORKSPACE); 291 root = ticket.getRootNode(); 292 node = root.getNode("/childNode/childNode2/jcr:content"); 293 294 assertNotNull(node.findProperty(new StringValue("prop1Value"))); 295 assertEquals("property1", node.findProperty(new StringValue("prop1Value")).getName()); 296 297 assertNull(node.findProperty(new LongValue(15))); 298 node.setProperty("longProp", new LongValue(15)); 299 assertEquals("longProp", node.findProperty(new LongValue(15)).getName()); 300 301 assertNull(node.findProperty(new DoubleValue(16))); 302 node.setProperty("doubleProp", new DoubleValue(16)); 303 assertEquals("doubleProp", node.findProperty(new DoubleValue(16)).getName()); 304 305 Calendar calendar = new GregorianCalendar (); 306 DateValue dateValue = new DateValue(calendar); 307 assertNull(node.findProperty(dateValue)); 308 node.setProperty("dateProp", dateValue); 309 assertEquals("dateProp", node.findProperty(dateValue).getName()); 311 312 BooleanValue booleanValue = new BooleanValue(true); 313 assertNull(node.findProperty(booleanValue)); 314 node.setProperty("booleanProp", booleanValue); 315 assertEquals("booleanProp", node.findProperty(booleanValue).getName()); 316 317 SoftLinkValue softLinkValue = new SoftLinkValue("/childNode"); 318 assertNull(node.findProperty(softLinkValue)); 319 node.setProperty("softLinkProp", softLinkValue); 320 assertEquals("softLinkProp", node.findProperty(softLinkValue).getName()); 321 BinaryValue binaryValue = new BinaryValue(new String ("binaryValue").getBytes()); 322 node.setProperty("binaryProp", binaryValue); 323 324 System.out.println("Binary PRRRROP --"+node.getProperty("binaryProp")+" "); 325 328 Ticket ticket2 = repository.login(credentials, WORKSPACE); 329 Node root2 = ticket2.getRootNode(); 330 Node node2 = root2.getNode("/childNode/childNode2/jcr:content"); 331 node2.setProperty("exo:prop", new StringValue("hehe")); 332 ticket2.save(); 333 334 ticket.revert(); 335 node = root.getNode("/childNode/childNode2/jcr:content"); 336 PropertyIterator iterator = node.getProperties(); 337 while(iterator.hasNext()){ 338 Property property = iterator.nextProperty(); 339 if(!("property1".equals(property.getName()) || "property2".equals(property.getName()) 340 || "jcr:created".equals(property.getName()) || "jcr:lastModified".equals(property.getName()) 341 || "jcr:primaryType".equals(property.getName()) || "exo:prop".equals(property.getName()) || 342 "exo:content".equals(property.getName()) || "jcr:uuid".equals(property.getName()))) 343 fail("returned non expected properties"); 344 } 345 } 346 347 public void testFindProperties() throws RepositoryException { 348 Node root = ticket.getRootNode(); 349 Node node = root.getNode("/childNode/childNode2/jcr:content"); 350 node.setProperty("property1", "prop1Value"); 351 node.setProperty("property2", "prop1Value"); 352 node.setProperty("property3", "prop1Value"); 353 int i = 0; 354 PropertyIterator iterator = node.findProperties(new StringValue("prop1Value")); 355 while(iterator.hasNext()){ 356 Property property = iterator.nextProperty(); 357 if(!("property1".equals(property.getName()) || "property2".equals(property.getName()) 358 || "property3".equals(property.getName()) )) 359 fail("returned non expected properties"); 360 i++; 361 } 362 assertEquals(3, i); 363 364 Ticket ticket2 = repository.login(credentials, WORKSPACE); 365 Node root2 = ticket2.getRootNode(); 366 Node node2 = root2.getNode("/childNode/childNode2/jcr:content"); 367 node2.setProperty("exo:prop", new StringValue("prop1Value")); 368 node2.setProperty("exo:prop1", new StringValue("prop1Value")); 369 ticket2.save(); 370 371 ticket.revert(); 372 node = root.getNode("/childNode/childNode2/jcr:content"); 373 i = 0; 374 iterator = node.findProperties(new StringValue("prop1Value")); 375 while(iterator.hasNext()){ 376 Property property = iterator.nextProperty(); 377 if(!("exo:prop".equals(property.getName()) || "exo:prop1".equals(property.getName()) )) 378 fail("returned non expected properties"); 379 i++; 380 } 381 assertEquals(2, i); 382 } 383 384 public void testGetPrimaryItem() throws RepositoryException { 385 Node root = ticket.getRootNode(); 386 try { 387 root.getPrimaryItem(); 388 fail("exception should have been thrown"); 389 } catch (RepositoryException e) { 390 assertTrue(e instanceof ItemNotFoundException); 391 } 392 393 Node node = root.getNode("/childNode/childNode2"); 394 Item item = node.getPrimaryItem(); 395 assertNotNull(item); 396 assertEquals("jcr:content", item.getName()); 397 } 398 399 public void testGetUUID() throws RepositoryException { 400 Node root = ticket.getRootNode(); 401 try { 402 root.getUUID(); 403 fail("exception should have been thrown"); 404 } catch (UnsupportedRepositoryOperationException e) { 405 } 406 Node node = root.getNode("/childNode/childNode2/jcr:content"); 407 assertNotNull(node.getUUID()); 408 } 409 410 public void testHasNode() throws RepositoryException { 411 Node root = ticket.getRootNode(); 412 assertFalse(root.hasNode("/dummyNode")); 413 assertTrue(root.hasNode("/childNode")); 414 } 415 416 public void testHasNodes() throws RepositoryException { 417 Node root = ticket.getRootNode(); 418 assertTrue(root.hasNodes()); 419 Node node = root.getNode("/childNode/childNode2/jcr:content"); 420 node = node.addNode("tempNode"); 421 assertFalse(node.hasNodes()); 422 } 423 424 public void testHasProperty() throws RepositoryException { 425 Node root = ticket.getRootNode(); 426 assertFalse(root.hasProperty("dummyProperty")); 427 assertTrue(root.getNode("/childNode").hasProperty("jcr:created")); 428 } 429 430 public void testHasProperties() throws RepositoryException { 431 Node root = ticket.getRootNode(); 432 Node node = root.getNode("/childNode"); 433 assertTrue(root.hasProperties()); 434 } 435 436 } 437 | Popular Tags |