001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.test.poly;
018:
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import org.compass.core.CompassSession;
023: import org.compass.core.CompassTransaction;
024: import org.compass.core.test.AbstractTestCase;
025:
026: /**
027: * @author kimchy
028: */
029: public class PolyTests extends AbstractTestCase {
030:
031: protected String[] getMappings() {
032: return new String[] { "poly/Poly.cpm.xml" };
033: }
034:
035: public void testPoly() {
036: CompassSession session = openSession();
037: CompassTransaction tr = session.beginTransaction();
038:
039: PolyImpl1 impl1 = new PolyImpl1();
040: impl1.setId(new Long(1));
041: impl1.setValue("test1");
042: session.save("poly", impl1);
043:
044: PolyImpl2 impl2 = new PolyImpl2();
045: impl2.setId(new Long(2));
046: impl2.setValue("test2");
047: session.save("poly", impl2);
048:
049: impl1 = (PolyImpl1) session.load("poly", new Long(1));
050: assertEquals("test1", impl1.getValue());
051:
052: impl2 = (PolyImpl2) session.load("poly", new Long(2));
053: assertEquals("test2", impl2.getValue());
054:
055: tr.commit();
056: }
057:
058: public void testComponent() {
059: CompassSession session = openSession();
060: CompassTransaction tr = session.beginTransaction();
061:
062: Component comp = new Component();
063: comp.setId(new Long(1));
064:
065: PolyImpl1 impl1 = new PolyImpl1();
066: impl1.setId(new Long(1));
067: impl1.setValue("test1");
068: comp.setPi1(impl1);
069:
070: PolyImpl2 impl2 = new PolyImpl2();
071: impl2.setId(new Long(2));
072: impl2.setValue("test2");
073: comp.setPi2(impl2);
074:
075: session.save(comp);
076:
077: comp = (Component) session.load(Component.class, new Long(1));
078: assertEquals(1, comp.getId().longValue());
079: assertNotNull(comp.getPi1());
080: assertEquals(PolyImpl1.class.getName(), comp.getPi1()
081: .getClass().getName());
082: assertEquals("test1", comp.getPi1().getValue());
083: assertNotNull(comp.getPi2());
084: assertEquals(PolyImpl2.class.getName(), comp.getPi2()
085: .getClass().getName());
086: assertEquals("test2", comp.getPi2().getValue());
087:
088: tr.commit();
089: }
090:
091: public void testCol() {
092: CompassSession session = openSession();
093: CompassTransaction tr = session.beginTransaction();
094:
095: Col col = new Col();
096: col.setId(new Long(1));
097: List list = new ArrayList();
098:
099: PolyImpl1 impl1 = new PolyImpl1();
100: impl1.setId(new Long(1));
101: impl1.setValue("test1");
102: list.add(impl1);
103:
104: PolyImpl2 impl2 = new PolyImpl2();
105: impl2.setId(new Long(2));
106: impl2.setValue("test2");
107: list.add(impl2);
108:
109: col.setList(list);
110: session.save(col);
111:
112: col = (Col) session.load(Col.class, new Long(1));
113: assertEquals(1, col.getId().longValue());
114: assertNotNull(col.getList());
115: assertEquals(2, col.getList().size());
116: assertEquals(PolyImpl1.class.getName(), col.getList().get(0)
117: .getClass().getName());
118: assertEquals("test1", ((PolyImpl1) col.getList().get(0))
119: .getValue());
120: assertEquals(PolyImpl2.class.getName(), col.getList().get(1)
121: .getClass().getName());
122: assertEquals("test2", ((PolyImpl2) col.getList().get(1))
123: .getValue());
124:
125: tr.commit();
126: }
127:
128: public void testArr() {
129: CompassSession session = openSession();
130: CompassTransaction tr = session.beginTransaction();
131:
132: Long id = new Long(1);
133: Arr arr = new Arr();
134: arr.setId(id);
135:
136: PolyImpl1 impl1 = new PolyImpl1();
137: impl1.setId(new Long(1));
138: impl1.setValue("test1");
139:
140: PolyImpl2 impl2 = new PolyImpl2();
141: impl2.setId(new Long(2));
142: impl2.setValue("test2");
143:
144: arr.setPi(new PolyInterface[] { impl1, impl2 });
145: session.save(arr);
146:
147: arr = (Arr) session.load(Arr.class, id);
148: assertEquals(1, arr.getId().longValue());
149: assertEquals(2, arr.getPi().length);
150: assertEquals(PolyImpl1.class.getName(), arr.getPi()[0]
151: .getClass().getName());
152: assertEquals("test1", arr.getPi()[0].getValue());
153: assertEquals(PolyImpl2.class.getName(), arr.getPi()[1]
154: .getClass().getName());
155: assertEquals("test2", arr.getPi()[1].getValue());
156:
157: tr.commit();
158: }
159:
160: public void testPolyClass() {
161: CompassSession session = openSession();
162: CompassTransaction tr = session.beginTransaction();
163:
164: PolyImpl1 impl1 = new PolyImpl1();
165: impl1.setId(new Long(1));
166: impl1.setValue("test1");
167: session.save("poly2", impl1);
168:
169: PolyImpl2 impl2 = new PolyImpl2();
170: impl2.setId(new Long(2));
171: impl2.setValue("test2");
172: session.save("poly2", impl2);
173:
174: impl1 = (PolyImpl1) session.load("poly2", new Long(1));
175: assertEquals("test1", impl1.getValue());
176:
177: // this will fail on a class cast if we did not have the poly-class
178: impl1 = (PolyImpl1) session.load("poly2", new Long(2));
179: assertEquals("test2", impl1.getValue());
180:
181: tr.commit();
182: }
183: }
|