KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > jcr > api > reading > PropertyTest


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.reading;
7
8
9 import javax.jcr.*;
10 import org.exoplatform.services.jcr.BaseTest;
11 import java.io.ByteArrayInputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.InputStream JavaDoc;
14 import java.util.GregorianCalendar JavaDoc;
15 import java.util.Calendar JavaDoc;
16
17
18 /**
19  * Created y the eXo platform team
20  * User: Benjamin Mestrallet
21  * Date: 24 juil. 2004
22  */

23 public class PropertyTest extends BaseTest{
24
25   public void initRepository() throws RepositoryException {
26     Node root = ticket.getRootNode();
27     Node file = root.addNode("childNode", "nt:file");
28     Node contentNode = file.getNode("jcr:content");
29     contentNode.setProperty("exo:content", new StringValue("this is the content"));
30     Value[] values = new Value[3];
31     values[0] = new StringValue("stringValue");
32     values[1] = new BooleanValue(true);
33     values[2] = new LongValue(12);
34     contentNode.setProperty("multi", values, PropertyType.STRING);
35 // ticket.save();
36
}
37
38   public void tearDown() throws RepositoryException {
39     Node root = ticket.getRootNode();
40     root.remove("childNode");
41 // ticket.save();
42
}
43
44   public void testGetValue() throws RepositoryException {
45     Node root = ticket.getRootNode();
46     Property property = root.getNode("childNode/jcr:content").getProperty("exo:content");
47     assertTrue(property.getValue() instanceof Value);
48
49     Node node = root.getNode("childNode/jcr:content");
50     assertEquals("stringValue", node.getProperty("multi").getValue().getString());
51
52     node.setProperty("dummy", new Value[0], PropertyType.STRING);
53     assertNull(node.getProperty("dummy").getValue());
54   }
55
56   public void testGetValues() throws RepositoryException {
57     Node root = ticket.getRootNode();
58     Node node = root.getNode("childNode/jcr:content");
59     Value[] values = node.getProperty("multi").getValues();
60     for (int i = 0; i < values.length; i++) {
61       Value value = values[i];
62       if(!("stringValue".equals(value.getString()) || "true".equals(value.getString()) ||
63           "12".equals(value.getString()) )){
64         fail("returned non expected value");
65       }
66     }
67   }
68
69   public void testGetString() throws RepositoryException {
70     Node root = ticket.getRootNode();
71     Node node = root.getNode("childNode/jcr:content");
72     String JavaDoc stringValue = node.getProperty("multi").getString();
73     assertEquals("stringValue", stringValue);
74     assertEquals("stringValue", node.getProperty("multi").getValue().getString());
75   }
76
77   public void testDouble() throws RepositoryException {
78     Node root = ticket.getRootNode();
79     Node node = root.getNode("childNode/jcr:content");
80     node.setProperty("double", new DoubleValue(15));
81     assertEquals(15, (int)node.getProperty("double").getDouble());
82     assertEquals(15, (int)node.getProperty("double").getValue().getDouble());
83   }
84
85   public void testLong() throws RepositoryException {
86     Node root = ticket.getRootNode();
87     Node node = root.getNode("childNode/jcr:content");
88     node.setProperty("long", new LongValue(15));
89     assertEquals(15, node.getProperty("long").getLong());
90     assertEquals(15, node.getProperty("long").getValue().getLong());
91   }
92
93   public void testStream() throws RepositoryException, IOException JavaDoc {
94     Node root = ticket.getRootNode();
95     Node node = root.getNode("childNode/jcr:content");
96 // node.setProperty("stream", new BinaryValue(new String("inputStream").getBytes()));
97
node.setProperty("stream", new BinaryValue("inputStream"));
98
99 System.out.println("STTTTTTTTREAM ---- "+node.getProperty("stream"));
100     InputStream JavaDoc iS = node.getProperty("stream").getStream();
101 System.out.println("IIIIIIString ---- "+node.getProperty("stream").getString());
102 System.out.println("IIIIIISREAM ---- "+iS.available());
103     byte[] bytes = new byte[iS.available()];
104     iS.read(bytes);
105     assertEquals("inputStream", new String JavaDoc(bytes));
106     iS.reset();
107     iS = node.getProperty("stream").getValue().getStream();
108     bytes = new byte[iS.available()];
109     iS.read(bytes);
110     assertEquals("inputStream", new String JavaDoc(bytes));
111   }
112
113   public void testDate() throws RepositoryException {
114     Node root = ticket.getRootNode();
115     Node node = root.getNode("childNode/jcr:content");
116     Calendar JavaDoc calendar = new GregorianCalendar JavaDoc();
117     node.setProperty("date", new DateValue(calendar));
118 // System.out.println("MILLIS ----- "+ calendar.getTimeInMillis());
119
assertEquals(calendar.getTimeInMillis(), node.getProperty("date").getDate().getTimeInMillis());
120     assertEquals(calendar.getTimeInMillis(), node.getProperty("date").getValue().getDate().getTimeInMillis());
121   }
122
123   public void tesGetBoolean() throws RepositoryException {
124     Node root = ticket.getRootNode();
125     Node node = root.getNode("childNode/jcr:content");
126     node.setProperty("boolean", new BooleanValue(true));
127     assertEquals(true, node.getProperty("boolean").getBoolean());
128     assertEquals(true, node.getProperty("boolean").getValue().getBoolean());
129   }
130
131   public void testHasValues() throws RepositoryException {
132     Node root = ticket.getRootNode();
133     Node node = root.getNode("childNode/jcr:content");
134     node.setProperty("dummy", new Value[0], PropertyType.STRING);
135     assertFalse(node.getProperty("dummy").hasValue());
136   }
137
138   public void testGetLength() throws RepositoryException, IOException JavaDoc {
139     Node root = ticket.getRootNode();
140     Node node = root.getNode("childNode/jcr:content");
141     long length = node.getProperty("multi").getLength();
142     assertEquals(11, length);
143
144     InputStream JavaDoc iS = new ByteArrayInputStream JavaDoc(new String JavaDoc("inputStream").getBytes());
145     node.setProperty("stream", new BinaryValue(iS));
146     assertEquals(11, node.getProperty("stream").getLength());
147   }
148
149 }
150
Popular Tags