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:
023: import javax.persistence.*;
024:
025: import org.apache.openjpa.jdbc.conf.*;
026: import org.apache.openjpa.jdbc.meta.*;
027: import org.apache.openjpa.jdbc.meta.strats.*;
028: import org.apache.openjpa.jdbc.sql.*;
029: import org.apache.openjpa.persistence.OpenJPAEntityManager;
030:
031: //import kodo.persistence.test.*;
032:
033: import org.apache.openjpa.persistence.annotations.common.apps.annotApp.annotype.*;
034: import org.apache.openjpa.persistence.common.utils.*;
035:
036: import junit.framework.*;
037:
038: /**
039: * Test for embedded
040: *
041: * @author Steve Kim
042: */
043: public class TestEJBEmbedded extends AnnotationTestCase {
044:
045: public TestEJBEmbedded(String name) {
046: super (name, "annotationcactusapp");
047: }
048:
049: private static final String CLOB;
050:
051: static {
052: StringBuffer buf = new StringBuffer();
053: for (int i = 0; i < 1000; i++)
054: buf.append('a');
055: CLOB = buf.toString();
056: }
057:
058: public void setUp() {
059: deleteAll(EmbedOwner.class);
060: }
061:
062: public void testEmbedded() {
063: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
064: startTx(em);
065: EmbedOwner owner = new EmbedOwner();
066: owner.setBasic("foo");
067: EmbedValue embed = new EmbedValue();
068: embed.setClob(CLOB);
069: embed.setBasic("bar");
070: embed.setBlob("foobar".getBytes());
071: embed.setOwner(owner);
072: owner.setEmbed(embed);
073: em.persist(owner);
074: int pk = owner.getPk();
075: endTx(em);
076: endEm(em);
077:
078: em = (OpenJPAEntityManager) currentEntityManager();
079: owner = em.find(EmbedOwner.class, pk);
080: assertEquals("foo", owner.getBasic());
081: embed = owner.getEmbed();
082: assertNotNull(embed);
083: assertEquals(CLOB, embed.getClob());
084: assertEquals("bar", embed.getBasic());
085: assertEquals("foobar", new String(embed.getBlob()));
086: assertEquals(owner, embed.getOwner());
087: endEm(em);
088: }
089:
090: public void testNull() {
091: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
092: startTx(em);
093: EmbedOwner owner = new EmbedOwner();
094: owner.setBasic("foo");
095: em.persist(owner);
096: int pk = owner.getPk();
097: endTx(em);
098: endEm(em);
099:
100: em = (OpenJPAEntityManager) currentEntityManager();
101: owner = em.find(EmbedOwner.class, pk);
102: assertEquals("foo", owner.getBasic());
103: EmbedValue embed = owner.getEmbed();
104: assertNotNull(embed);
105: assertNull(embed.getClob());
106: assertNull(embed.getBasic());
107: assertNull(embed.getBlob());
108: startTx(em);
109: }
110:
111: // public void testMappingTransferAndOverride() {
112: // JDBCConfiguration conf = (JDBCConfiguration) getConfiguration();
113: // ClassMapping cls = conf.getMappingRepositoryInstance().getMapping
114: // (EmbedOwner.class, null, true);
115: // assertEquals("OWN_BASIC", cls.getFieldMapping("basic").
116: // getColumns()[0].getName());
117: // ClassMapping embed = cls.getFieldMapping("embed").currentEntityManager()beddedMapping();
118: // assertEquals("EMB_BLOB", embed.getFieldMapping("blob").
119: // getColumns()[0].getName());
120: // assertEquals("OVER_BASIC", embed.getFieldMapping("basic").
121: // getColumns()[0].getName());
122: // assertEquals("OVER_OWNER", embed.getFieldMapping("owner").
123: // getColumns()[0].getName());
124: //
125: // FieldMapping fm = embed.getFieldMapping("clob");
126: // DBDictionary dict = conf.getDBDictionaryInstance();
127: // if (dict.getPreferredType(Types.CLOB) == Types.CLOB) {
128: // if (dict.maxEmbeddedClobSize > 0)
129: // assertTrue(fm.getStrategy() instanceof
130: // MaxEmbeddedClobFieldStrategy);
131: // else
132: // assertTrue(fm.getHandler() instanceof ClobValueHandler);
133: // } else
134: // assertTrue(fm.getStrategy() instanceof StringFieldStrategy);
135: // }
136: }
|