KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > jcr > api > visiting > ItemVisitorTest


1 /*
2  * Copyright 2001-2003 The eXo platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  */

5
6 package org.exoplatform.services.jcr.api.visiting;
7
8
9 import javax.jcr.*;
10 import javax.jcr.util.TraversingItemVisitor;
11 import org.exoplatform.services.jcr.BaseTest;
12
13 /**
14  * Created y the eXo platform team
15  * User: Benjamin Mestrallet
16  * Date: 14 ao�t 2004
17  */

18 public class ItemVisitorTest extends BaseTest {
19
20   public void testItemVisiting() throws RepositoryException {
21     Node root = ticket.getRootNode();
22     Node file = root.addNode("childNode", "nt:folder").addNode("childNode2", "nt:file");
23     Node contentNode = file.getNode("jcr:content");
24     contentNode.setProperty("exo:content", new StringValue("this is the content"));
25     ticket.save();
26
27     ItemVisitor visitor = new MockVisitor();
28     contentNode.accept(visitor);
29     contentNode.getProperty("exo:content").accept(visitor);
30
31     visitor = new MockVisitor2();
32     root.getNode("childNode").accept(visitor);
33     assertEquals(((MockVisitor2)visitor).getI(),((MockVisitor2)visitor).getJ());
34     assertEquals(3, ((MockVisitor2)visitor).getI());
35     assertTrue(((MockVisitor2)visitor).isReached());
36   }
37
38   public class MockVisitor implements ItemVisitor {
39
40     public void visit(Property property) throws RepositoryException {
41       assertEquals(property.getName(), "exo:content");
42     }
43
44     public void visit(Node node) throws RepositoryException {
45       assertEquals(node.getName(), "jcr:content");
46     }
47
48   }
49
50
51   public class MockVisitor2 extends TraversingItemVisitor {
52
53     private boolean reached = false;
54     private int i = 0;
55     private int j = 0;
56
57     protected void entering(Property property, int level) throws RepositoryException {
58       if("exo:content".equals(property.getName()))
59         reached = true;
60     }
61
62     protected void entering(Node node, int level) throws RepositoryException {
63       i++;
64       assertTrue(isInList(node.getName()));
65     }
66
67     protected void leaving(Property property, int level) throws RepositoryException {
68     }
69
70     protected void leaving(Node node, int level) throws RepositoryException {
71       j++;
72     }
73
74     private boolean isInList(String JavaDoc name){
75       if("childNode".equals(name) || "childNode2".equals(name) || "jcr:content".equals(name) ||
76           "exo:content".equals(name)){
77         return true;
78       }
79       return false;
80     }
81
82     public int getI() {
83       return i;
84     }
85
86     public int getJ() {
87       return j;
88     }
89
90     public boolean isReached() {
91       return reached;
92     }
93   }
94
95 }
96
Popular Tags