KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > jcr > api > saving > SaveTest


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.saving;
7
8
9 import javax.jcr.Node;
10 import javax.jcr.RepositoryException;
11 import javax.jcr.PathNotFoundException;
12 import javax.jcr.nodetype.ConstraintViolationException;
13 import org.exoplatform.services.jcr.BaseTest;
14
15
16 /**
17  * Created y the eXo platform team
18  * User: Benjamin Mestrallet
19  * Date: 2 ao�t 2004
20  */

21 public class SaveTest extends BaseTest{
22
23   public void testTicketSave() throws RepositoryException {
24     Node root = ticket.getRootNode();
25     root.addNode("childNode", "nt:folder").addNode("childNode2", "nt:propertyDef");
26     try {
27       ticket.save();
28       fail("exception should have been thrown");
29     } catch (ConstraintViolationException e) {
30     }
31
32     assertEquals("/childNode/childNode2", root.getNode("childNode/childNode2").getPath());
33
34     ticket = repository.login(credentials, WORKSPACE);
35     root = ticket.getRootNode();
36     try {
37       root.getNode("childNode/childNode2");
38       fail("exception should have been thrown");
39     } catch (PathNotFoundException e) {
40     }
41
42     Node node = root.addNode("nodeType", "exo:mockNodeType");
43     node.addNode("jcr:propertyDef", "nt:propertyDef").setProperty("jcr:defaultValue", "testString");
44     try {
45       ticket.save();
46       fail("exception should have been thrown");
47     } catch (ConstraintViolationException e) {
48     }
49     node.addNode("jcr:childNodeDef", "nt:childNodeDef");
50     ticket.save();
51     root.remove("nodeType");
52     ticket.save();
53   }
54
55   public void testRevert() throws RepositoryException {
56     ticket = repository.login(credentials, WORKSPACE);
57     Node root = ticket.getRootNode();
58     Node node = root.addNode("nodeType", "exo:mockNodeType");
59     node.addNode("jcr:propertyDef", "nt:propertyDef");
60     ticket.revert();
61     try {
62       root.getNode("nodeType");
63       fail("exception should have been thrown");
64     } catch (PathNotFoundException e) {
65     }
66     ticket.save();
67   }
68
69   public void testPropertiesManipThenSave() throws RepositoryException {
70     Node root = ticket.getRootNode();
71     Node node = root.addNode("childNode", "nt:default");
72     node.addNode("node2BRem");
73     node.setProperty("existingProp", "existingValue");
74     node.setProperty("existingProp2", "existingValue2");
75     ticket.save();
76     node.setProperty("prop", "propValue");
77     node.setProperty("existingProp", "existingValueBis");
78     node.remove("existingProp2");
79     node.remove("node2BRem");
80     node.addNode("addedNode");
81     ticket.save();
82
83     ticket = repository.login(credentials, WORKSPACE);
84     node = ticket.getRootNode().getNode("childNode");
85     root = ticket.getRootNode();
86     try {
87       node.getProperty("prop");
88     } catch (PathNotFoundException e) {
89       e.printStackTrace();
90       fail("exception should not be thrown");
91     }
92     assertEquals("existingValueBis", node.getProperty("existingProp").getString());
93     try {
94       node.getProperty("existingProp2");
95       fail("exception should have been thrown");
96     } catch (PathNotFoundException e) {
97     }
98     try {
99       node.getNode("node2BRem");
100       fail("exception should have been thrown");
101     } catch (PathNotFoundException e) {
102     }
103     node.getNode("addedNode");
104
105 // System.out.println("REMOVE childNode");
106
root.remove("childNode");
107 // System.out.println("SAVE childNode");
108
ticket.save();
109 // System.out.println("REMOVED");
110
}
111
112   public void testNodeSave() throws RepositoryException {
113     Node root = ticket.getRootNode();
114     root.addNode("childNode", "nt:folder").addNode("childNode2", "nt:propertyDef");
115     try {
116       root.save(false);
117       fail("exception should have been thrown");
118     } catch (ConstraintViolationException e) {
119     }
120
121     assertEquals("/childNode/childNode2", root.getNode("childNode/childNode2").getPath());
122     ticket.revert();
123
124     ticket = repository.login(credentials, WORKSPACE);
125     root = ticket.getRootNode();
126
127     try {
128       root.getNode("childNode/childNode2");
129       fail("exception should have been thrown");
130     } catch (PathNotFoundException e) {
131     }
132
133     Node node = root.addNode("nodeType", "exo:mockNodeType");
134     node.addNode("jcr:propertyDef", "nt:propertyDef").setProperty("jcr:defaultValue", "testString");
135     try {
136       node.save(false);
137       fail("exception should have been thrown");
138     } catch (ConstraintViolationException e) {
139     }
140     node.addNode("jcr:childNodeDef", "nt:childNodeDef");
141     node.save(false);
142
143     node = root.addNode("childNode", "nt:default");
144     node.addNode("node2BRem");
145     node.setProperty("existingProp", "existingValue");
146     node.setProperty("existingProp2", "existingValue2");
147     root.save(false);
148     node.setProperty("prop", "propValue");
149     node.setProperty("existingProp", "existingValueBis");
150     node.remove("existingProp2");
151     node.remove("node2BRem");
152     node.addNode("addedNode");
153     node.save(true);
154
155     ticket = repository.login(credentials, WORKSPACE);
156     node = ticket.getRootNode().getNode("childNode");
157     root = ticket.getRootNode();
158     try {
159       node.getProperty("prop");
160     } catch (PathNotFoundException e) {
161       e.printStackTrace();
162       fail("exception should not be thrown");
163     }
164     assertEquals("existingValueBis", node.getProperty("existingProp").getString());
165     try {
166       node.getProperty("existingProp2");
167       fail("exception should have been thrown");
168     } catch (PathNotFoundException e) {
169     }
170     try {
171       node.getNode("node2BRem");
172       fail("exception should have been thrown");
173     } catch (PathNotFoundException e) {
174     }
175     try {
176       node.getNode("addedNode");
177       fail("exception should have been thrown");
178     } catch (PathNotFoundException e) {
179     }
180
181     root.remove("nodeType");
182     root.remove("childNode");
183     ticket.save();
184   }
185
186   public void testSaveNodeWithNonPersistedParent() throws RepositoryException {
187     Node root = ticket.getRootNode();
188     Node n = root.addNode("childNode", "nt:folder").addNode("childNode2", "nt:file");
189     try {
190       n.save(false);
191       fail("exception should have been thrown");
192     } catch (ConstraintViolationException e) {
193     }
194
195     root.remove("childNode");
196     ticket.save();
197   }
198
199 }
200
Popular Tags