001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.persistence.annotations;
020:
021: import java.sql.*;
022: import java.util.Date;
023:
024: import javax.persistence.*;
025:
026: import org.apache.openjpa.jdbc.conf.*;
027: import org.apache.openjpa.jdbc.sql.*;
028: import org.apache.openjpa.persistence.OpenJPAEntityManager;
029:
030: import org.apache.openjpa.persistence.annotations.common.apps.annotApp.annotype.*;
031: import junit.framework.*;
032:
033: import org.apache.openjpa.persistence.common.utils.*;
034:
035: /**
036: * Test for serialized, clob, and lob types.
037: *
038: * @author Steve Kim
039: */
040: public class TestSerializedLobs extends AnnotationTestCase {
041:
042: public TestSerializedLobs(String name) {
043: super (name, "annotationcactusapp");
044: }
045:
046: private static final Date DATE = new Date();
047:
048: public void setUp() {
049: deleteAll(AnnoTest1.class);
050: }
051:
052: // Serialized fields not being read properly
053: public void testSerialized() {
054: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
055: startTx(em);
056: AnnoTest1 pc1 = new AnnoTest1(1);
057: AnnoTest1 pc2 = new AnnoTest1(2);
058: pc1.setSerialized("ASDASD");
059: pc2.setSerialized(DATE);
060: em.persist(pc1);
061: em.persist(pc2);
062: endTx(em);
063: endEm(em);
064:
065: em = (OpenJPAEntityManager) currentEntityManager();
066: pc1 = em.find(AnnoTest1.class, em.getObjectId(pc1));
067: pc2 = em.find(AnnoTest1.class, em.getObjectId(pc2));
068: assertEquals("ASDASD", pc1.getSerialized());
069: assertEquals(DATE, pc2.getSerialized());
070: endEm(em);
071: }
072:
073: public void testBlob() throws Exception {
074: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
075: startTx(em);
076:
077: AnnoTest1 pc = new AnnoTest1(1);
078: pc.setBlob("Not Null".getBytes());
079: em.persist(pc);
080: endTx(em);
081: endEm(em);
082:
083: em = (OpenJPAEntityManager) currentEntityManager();
084: pc = em.find(AnnoTest1.class, em.getObjectId(pc));
085: assertEquals("Not Null", new String(pc.getBlob()));
086: Connection conn = (Connection) em.getConnection();
087: Statement stmnt = conn.createStatement();
088: ResultSet rs = stmnt
089: .executeQuery("SELECT BLOBVAL FROM ANNOTEST1 "
090: + "WHERE PK = 1");
091: assertTrue(rs.next());
092:
093: /** JDBCConfiguration conf = (JDBCConfiguration) em.getConfiguration();
094: DBDictionary dict = conf.getDBDictionaryInstance();
095: if (dict.useGetBytesForBlobs)
096: rs.getBytes(1);
097: else if (dict.useGetObjectForBlobs)
098: rs.getObject(1);
099: else {
100: Blob blob = rs.getBlob(1);
101: blob.getBytes(1L, (int) blob.length());
102: }
103: assertEquals("Not Null", new String(pc.getBlob()));
104:
105: try {
106: rs.close();
107: } catch (SQLException e) {
108: }
109: try {
110: stmnt.close();
111: } catch (SQLException e) {
112: }
113: try {
114: conn.close();
115: } catch (SQLException e) {
116: }**/
117: endEm(em);
118: }
119:
120: public void testClob() throws Exception {
121: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
122: startTx(em);
123:
124: AnnoTest1 pc = new AnnoTest1(1);
125: StringBuffer buf = new StringBuffer();
126: for (int i = 0; i < 1000; i++)
127: buf.append((char) ('a' + (i % 24)));
128: pc.setClob(buf.toString());
129: em.persist(pc);
130: endTx(em);
131: endEm(em);
132:
133: em = (OpenJPAEntityManager) currentEntityManager();
134: pc = em.find(AnnoTest1.class, em.getObjectId(pc));
135: String str = pc.getClob();
136: assertEquals(1000, str.length());
137: for (int i = 0; i < str.length(); i++)
138: assertEquals('a' + (i % 24), str.charAt(i));
139: endEm(em);
140: }
141: }
|