KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > jcr > api > writing > fs > TestAddProperty


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.writing.fs;
7
8
9 import java.io.ByteArrayInputStream JavaDoc;
10 import java.util.GregorianCalendar JavaDoc;
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 /**
26  * Created y the eXo platform team
27  * User: Benjamin Mestrallet
28  * Date: 30 juil. 2004
29  */

30 public class TestAddProperty extends AddPropertyTest{
31
32   public void setUp() throws Exception JavaDoc {
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 // ticket.save();
44
}
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 // ticket.save();
52
}
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.save(false);
69
// ticket = repository.login(credentials, WORKSPACE);
70
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 JavaDoc()), 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.save(false);
94
// ticket = repository.login(credentials, WORKSPACE);
95
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 // node.save(false);
116

117 // ticket = repository.login(credentials, WORKSPACE);
118
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.save(false);
136
// ticket = repository.login(credentials, WORKSPACE);
137
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 JavaDoc[] 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 // node.save(false);
158

159 // ticket = repository.login(credentials, WORKSPACE);
160
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 JavaDoc(new String JavaDoc("default").getBytes()));
170     node.setProperty("jcr:defaultValue", true);
171     node.setProperty("jcr:defaultValue", new GregorianCalendar JavaDoc());
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