KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
12 import java.io.InputStream JavaDoc;
13 import java.util.Calendar JavaDoc;
14 import java.util.GregorianCalendar JavaDoc;
15
16 /**
17  * Created y the eXo platform team
18  * User: Benjamin Mestrallet
19  * Date: 27 juil. 2004
20  */

21 public class TestValue extends BaseTest{
22
23   public void testGetString() throws RepositoryException {
24     Value value = new StringValue("text");
25     assertEquals("text", value.getString());
26   }
27
28   public void testGetDouble() throws RepositoryException {
29     Value value = new StringValue("text");
30     try {
31       value.getDouble();
32       fail("exception should have been thrown");
33     } catch (ValueFormatException e) {
34     } catch (RepositoryException e) {
35       fail("not good exception thrown");
36     }
37
38     value = new StringValue("20");
39     assertEquals(20, (int)value.getDouble());
40
41     try {
42       value.getStream();
43       fail("exception should have been thrown");
44     } catch (IllegalStateException JavaDoc e) {
45     } catch (RepositoryException e) {
46       fail("not good exception thrown");
47     }
48   }
49
50   public void testGetLong() throws RepositoryException {
51     Value value = new StringValue("text");
52     try {
53       value.getDouble();
54       fail("exception should have been thrown");
55     } catch (ValueFormatException e) {
56     } catch (RepositoryException e) {
57       fail("not good exception thrown");
58     }
59
60     value = new StringValue("15");
61     assertEquals(15, value.getLong());
62
63     try {
64       value.getStream();
65       fail("exception should have been thrown");
66     } catch (IllegalStateException JavaDoc e) {
67     } catch (RepositoryException e) {
68       fail("not good exception thrown");
69     }
70   }
71
72   public void testGetStream() throws RepositoryException, IOException JavaDoc {
73     Value value = new BinaryValue(new String JavaDoc("inputStream").getBytes());
74     InputStream JavaDoc iS = value.getStream();
75     byte[] bytes = new byte[iS.available()];
76     iS.read(bytes);
77     assertEquals("inputStream", new String JavaDoc(bytes));
78
79     assertEquals(iS, value.getStream());
80
81     value = new StringValue("text");
82     //TODO JCR API BUG
83
iS = value.getStream();
84     bytes = new byte[iS.available()];
85     iS.read(bytes);
86     assertEquals("text", new String JavaDoc(bytes));
87
88     try {
89       value.getStream();
90       fail("exception should have been thrown");
91     } catch (IllegalStateException JavaDoc e) {
92     } catch (RepositoryException e) {
93       fail("not good exception thrown");
94     }
95   }
96
97   public void testGetDate() throws RepositoryException {
98     Value value = new StringValue("text");
99     try {
100       value.getDate();
101       fail("exception should have been thrown");
102     } catch (ValueFormatException e) {
103     } catch (RepositoryException e) {
104       fail("not good exception thrown");
105     }
106
107     Calendar JavaDoc calendar = new GregorianCalendar JavaDoc();
108     value = new DateValue(calendar);
109     assertEquals(calendar, value.getDate());
110
111     try {
112       value.getStream();
113       fail("exception should have been thrown");
114     } catch (IllegalStateException JavaDoc e) {
115     } catch (RepositoryException e) {
116       fail("not good exception thrown");
117     }
118   }
119
120   public void testGetBoolean() throws RepositoryException {
121     Value value = new LongValue(10);
122     try {
123       value.getBoolean();
124       fail("exception should have been thrown");
125     } catch (ValueFormatException e) {
126     } catch (RepositoryException e) {
127       fail("not good exception thrown");
128     }
129
130     value = new BooleanValue(true);
131     assertTrue(value.getBoolean());
132
133     try {
134       value.getStream();
135       fail("exception should have been thrown");
136     } catch (IllegalStateException JavaDoc e) {
137     } catch (RepositoryException e) {
138       fail("not good exception thrown");
139     }
140   }
141
142   public void tesGetType(){
143     Value value = new StringValue("");
144     assertEquals(PropertyType.STRING, value.getType());
145     value = new LongValue(10);
146     assertEquals(PropertyType.LONG, value.getType());
147     value = new DoubleValue(10);
148     assertEquals(PropertyType.DOUBLE, value.getType());
149     value = new BooleanValue(true);
150     assertEquals(PropertyType.BOOLEAN, value.getType());
151     value = new DateValue(new GregorianCalendar JavaDoc());
152     assertEquals(PropertyType.DATE, value.getType());
153     value = new BinaryValue("");
154     assertEquals(PropertyType.BINARY, value.getType());
155     value = new ReferenceValue("uuid");
156     assertEquals(PropertyType.REFERENCE, value.getType());
157     value = new SoftLinkValue("");
158     assertEquals(PropertyType.SOFTLINK, value.getType());
159   }
160
161 }
162
Popular Tags