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.jdbc.annotations;
020:
021: import java.sql.Blob;
022: import java.sql.Connection;
023: import java.sql.ResultSet;
024: import java.sql.SQLException;
025: import java.sql.Statement;
026: import java.util.Date;
027: import javax.persistence.EntityManager;
028:
029: import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
030: import org.apache.openjpa.jdbc.sql.DBDictionary;
031: import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
032: import org.apache.openjpa.persistence.test.SingleEMFTestCase;
033:
034: /**
035: * Test for serialized, clob, and lob types.
036: *
037: * @author Steve Kim
038: */
039: public class TestSerializedLobs extends SingleEMFTestCase {
040:
041: private static final Date DATE = new Date();
042:
043: public void setUp() {
044: setUp(AnnoTest1.class, AnnoTest2.class, Flat1.class,
045: CLEAR_TABLES);
046: }
047:
048: // Serialized fields not being read properly
049: public void testSerialized() {
050: EntityManager em = emf.createEntityManager();
051: em.getTransaction().begin();
052: AnnoTest1 pc1 = new AnnoTest1(1);
053: AnnoTest1 pc2 = new AnnoTest1(2);
054: pc1.setSerialized("ASDASD");
055: pc2.setSerialized(DATE);
056: em.persist(pc1);
057: em.persist(pc2);
058: em.getTransaction().commit();
059: em.close();
060:
061: em = emf.createEntityManager();
062: pc1 = em.find(AnnoTest1.class, new Long(1));
063: pc2 = em.find(AnnoTest1.class, new Long(2));
064: assertEquals("ASDASD", pc1.getSerialized());
065: assertEquals(DATE, pc2.getSerialized());
066: em.close();
067: }
068:
069: public void testBlob() throws Exception {
070: OpenJPAEntityManagerSPI em = emf.createEntityManager();
071: em.getTransaction().begin();
072:
073: AnnoTest1 pc = new AnnoTest1(1);
074: pc.setBlob("Not Null".getBytes());
075: em.persist(pc);
076: em.getTransaction().commit();
077: em.close();
078:
079: em = emf.createEntityManager();
080: pc = em.find(AnnoTest1.class, new Long(1));
081: assertEquals("Not Null", new String(pc.getBlob()));
082: Connection conn = (Connection) em.getConnection();
083: Statement stmnt = conn.createStatement();
084: ResultSet rs = stmnt
085: .executeQuery("SELECT BLOBVAL FROM ANNOTEST1 "
086: + "WHERE PK = 1");
087: assertTrue(rs.next());
088:
089: JDBCConfiguration conf = (JDBCConfiguration) em
090: .getConfiguration();
091: DBDictionary dict = conf.getDBDictionaryInstance();
092: if (dict.useGetBytesForBlobs)
093: rs.getBytes(1);
094: else if (dict.useGetObjectForBlobs)
095: rs.getObject(1);
096: else {
097: Blob blob = rs.getBlob(1);
098: blob.getBytes(1L, (int) blob.length());
099: }
100: assertEquals("Not Null", new String(pc.getBlob()));
101:
102: try {
103: rs.close();
104: } catch (SQLException e) {
105: }
106: try {
107: stmnt.close();
108: } catch (SQLException e) {
109: }
110: try {
111: conn.close();
112: } catch (SQLException e) {
113: }
114: em.close();
115: }
116:
117: public void testClob() throws Exception {
118: EntityManager em = emf.createEntityManager();
119: em.getTransaction().begin();
120:
121: AnnoTest1 pc = new AnnoTest1(1);
122: StringBuffer buf = new StringBuffer();
123: for (int i = 0; i < 1000; i++)
124: buf.append((char) ('a' + (i % 24)));
125: pc.setClob(buf.toString());
126: em.persist(pc);
127: em.getTransaction().commit();
128: em.close();
129:
130: em = emf.createEntityManager();
131: pc = em.find(AnnoTest1.class, new Long(1));
132: String str = pc.getClob();
133: assertEquals(1000, str.length());
134: for (int i = 0; i < str.length(); i++)
135: assertEquals('a' + (i % 24), str.charAt(i));
136: em.close();
137: }
138: }
|