1 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 ; 12 import java.io.InputStream ; 13 import java.util.Calendar ; 14 import java.util.GregorianCalendar ; 15 16 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 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 e) { 67 } catch (RepositoryException e) { 68 fail("not good exception thrown"); 69 } 70 } 71 72 public void testGetStream() throws RepositoryException, IOException { 73 Value value = new BinaryValue(new String ("inputStream").getBytes()); 74 InputStream iS = value.getStream(); 75 byte[] bytes = new byte[iS.available()]; 76 iS.read(bytes); 77 assertEquals("inputStream", new String (bytes)); 78 79 assertEquals(iS, value.getStream()); 80 81 value = new StringValue("text"); 82 iS = value.getStream(); 84 bytes = new byte[iS.available()]; 85 iS.read(bytes); 86 assertEquals("text", new String (bytes)); 87 88 try { 89 value.getStream(); 90 fail("exception should have been thrown"); 91 } catch (IllegalStateException 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 calendar = new GregorianCalendar (); 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 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 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 ()); 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 |