1 package org.apache.ojb.broker; 2 3 17 18 import org.apache.ojb.junit.PBTestCase; 19 import org.apache.ojb.broker.platforms.PlatformHsqldbImpl; 20 import org.apache.ojb.broker.platforms.PlatformPostgreSQLImpl; 21 22 import java.io.Serializable ; 23 24 28 public class BlobTest extends PBTestCase 29 { 30 private static final String SKIP_MESSAGE = 31 "# Skip "+BlobTest.class.getName()+", DB does not support Blob/Clob #"; 32 33 37 private boolean knownIssue; 38 private boolean skipTest; 39 40 public BlobTest(String name) 41 { 42 super(name); 43 } 44 45 public static void main(String [] args) 46 { 47 String [] arr = {BlobTest.class.getName()}; 48 junit.textui.TestRunner.main(arr); 49 } 50 51 public void setUp() throws Exception 52 { 53 super.setUp(); 54 55 String platformClass = getPlatformClass(); 56 59 knownIssue = false; 60 if (platformClass.equals(PlatformHsqldbImpl.class.getName())) 61 { 62 skipTest = true; 63 } 64 else if (platformClass.equals(PlatformPostgreSQLImpl.class.getName()) 65 && ojbSkipKnownIssueProblem("LOB handling is not yet implemented for PostgreSQL")) 66 { 67 knownIssue = true; 68 skipTest = true; 69 } 70 } 71 72 public void testBlobInsertion() throws Exception 73 { 74 if (skipTest) 75 { 76 if (!knownIssue) 77 { 78 System.out.println(SKIP_MESSAGE); 79 } 80 return; 81 } 82 83 int size = 5000; 84 85 ObjectWithBlob obj = new ObjectWithBlob(); 86 87 byte[] barr = new byte[size]; 88 char[] carr = new char[size]; 89 for (int i = 0; i < size; i++) 90 { 91 barr[i] = (byte) 'x'; 92 carr[i] = 'y'; 93 } 94 95 obj.setBlob(barr); 97 obj.setClob(new String (carr)); 98 broker.beginTransaction(); 99 broker.store(obj); 100 broker.commitTransaction(); 101 broker.clearCache(); 102 103 Identity oid = new Identity(obj, broker); 104 ObjectWithBlob obj1 = (ObjectWithBlob) broker.getObjectByIdentity(oid); 105 assertNotNull("BLOB was not stored", obj1.getBlob()); 106 assertNotNull("CLOB was not stored", obj1.getClob()); 107 assertEquals(obj.getBlob().length, obj1.getBlob().length); 108 assertEquals(obj.getClob().length(), obj1.getClob().length()); 109 110 } 111 112 public void testReadNullBlob() 113 { 114 if (skipTest) 115 { 116 if (!knownIssue) 117 { 118 System.out.println(SKIP_MESSAGE); 119 } 120 return; 121 } 122 123 ObjectWithBlob obj = new ObjectWithBlob(); 124 125 obj.setBlob(null); 127 obj.setClob(null); 128 broker.beginTransaction(); 129 broker.store(obj); 130 broker.commitTransaction(); 131 broker.clearCache(); 132 133 Identity oid = new Identity(obj, broker); 134 ObjectWithBlob obj1 = (ObjectWithBlob) broker.getObjectByIdentity(oid); 135 136 assertEquals(null, obj1.getBlob()); 137 assertEquals(null, obj1.getClob()); 138 } 139 140 141 public static class ObjectWithBlob implements Serializable 145 { 146 private int id; 147 148 private byte[] blob; 149 150 private String clob; 151 152 153 157 public byte[] getBlob() 158 { 159 return blob; 160 } 161 162 166 public void setBlob(byte[] blob) 167 { 168 this.blob = blob; 169 } 170 171 175 public String getClob() 176 { 177 return clob; 178 } 179 180 184 public void setClob(String clob) 185 { 186 this.clob = clob; 187 } 188 189 193 public int getId() 194 { 195 return id; 196 } 197 198 202 public void setId(int id) 203 { 204 this.id = id; 205 } 206 } 207 208 } 209 | Popular Tags |