001: package org.compass.core.test.inheritance;
002:
003: import org.compass.core.CompassSession;
004: import org.compass.core.CompassTransaction;
005: import org.compass.core.mapping.AliasMapping;
006: import org.compass.core.mapping.CompassMapping;
007: import org.compass.core.mapping.ResourcePropertyMapping;
008: import org.compass.core.mapping.osem.ClassMapping;
009: import org.compass.core.mapping.osem.HasRefAliasMapping;
010: import org.compass.core.spi.InternalCompass;
011: import org.compass.core.spi.InternalCompassSession;
012: import org.compass.core.test.AbstractTestCase;
013:
014: public class PolyCollectionTests extends AbstractTestCase {
015:
016: protected String[] getMappings() {
017: return new String[] { "inheritance/PolyCollection.cpm.xml" };
018: }
019:
020: public void testClassBasedRefAliasIdentification() {
021: CompassMapping compassMapping = ((InternalCompass) getCompass())
022: .getMapping();
023: ClassMapping classMapping = (ClassMapping) compassMapping
024: .getMappingByAlias("cComponentNoRefAlias");
025: assertEquals(2, ((HasRefAliasMapping) classMapping
026: .getMapping("a")).getRefAliases().length);
027: }
028:
029: public void testPolyComponentCollection() throws Exception {
030: CompassMapping compassMapping = ((InternalCompass) getCompass())
031: .getMapping();
032: AliasMapping aliasMapping = compassMapping
033: .getAliasMapping("polybase");
034: assertEquals(1, aliasMapping.getExtendingAliases().length);
035: aliasMapping = compassMapping.getAliasMapping("contract");
036: assertEquals(2, aliasMapping.getExtendingAliases().length);
037:
038: InternalCompassSession session = (InternalCompassSession) openSession();
039: CompassTransaction tr = session.beginTransaction();
040:
041: CompassMapping mapping = session.getMapping();
042: ClassMapping bComponent = (ClassMapping) mapping
043: .getRootMappingByAlias("bComponent");
044: ResourcePropertyMapping rpMapping = bComponent
045: .getResourcePropertyMappingByDotPath("a.value");
046: assertNotNull(rpMapping);
047: // this mvalue is shared between polybase and polyextends, so there should not be
048: // an internal id
049: assertEquals("mvalue", rpMapping.getName());
050: assertEquals("mvalue", rpMapping.getPath().getPath());
051: rpMapping = bComponent
052: .getResourcePropertyMappingByDotPath("a.value.mvalue");
053: assertNotNull(rpMapping);
054: assertEquals("bComponent", rpMapping.getRootAlias());
055: assertEquals("mvalue", rpMapping.getName());
056: assertEquals("mvalue", rpMapping.getPath().getPath());
057: rpMapping = bComponent
058: .getResourcePropertyMappingByDotPath("a.extendsValue");
059: assertEquals("bComponent", rpMapping.getRootAlias());
060: assertNotNull(rpMapping);
061: assertEquals("mextendsValue", rpMapping.getName());
062: assertEquals("mextendsValue", rpMapping.getPath().getPath()); // no internal id needed
063: rpMapping = bComponent
064: .getResourcePropertyMappingByDotPath("a.extendsValue.mextendsValue");
065: assertNotNull(rpMapping);
066: assertEquals("bComponent", rpMapping.getRootAlias());
067: assertEquals("mextendsValue", rpMapping.getName());
068: assertEquals("mextendsValue", rpMapping.getPath().getPath());
069:
070: B b = new B();
071: b.id = 1;
072:
073: Long id = new Long(1);
074: ExtendsA extendsA = new ExtendsA();
075: extendsA.setId(id);
076: extendsA.setValue("value");
077: extendsA.setExtendsValue("evalue");
078: extendsA.setD(new D("edvalue"));
079: b.a.add(extendsA);
080:
081: id = new Long(2);
082: BaseA base = new BaseA();
083: base.setId(id);
084: base.setValue("baseValue");
085: base.setD(new D("bdvalue"));
086: b.a.add(base);
087:
088: session.save("bComponent", b);
089:
090: b = (B) session.load("bComponent", new Long(1));
091: assertEquals(2, b.a.size());
092: extendsA = (ExtendsA) b.a.get(0);
093: assertEquals("value", extendsA.getValue());
094: assertEquals("evalue", extendsA.getExtendsValue());
095: assertEquals("edvalue", extendsA.getD().value);
096: base = (BaseA) b.a.get(1);
097: assertEquals("baseValue", base.getValue());
098: assertEquals("bdvalue", base.getD().value);
099:
100: tr.commit();
101: session.close();
102: }
103:
104: public void testPolyReferenceCollection() throws Exception {
105: CompassSession session = openSession();
106: CompassTransaction tr = session.beginTransaction();
107:
108: B b = new B();
109: b.id = 1;
110:
111: Long id = new Long(1);
112: ExtendsA extendsA = new ExtendsA();
113: extendsA.setId(id);
114: extendsA.setValue("value");
115: extendsA.setExtendsValue("evalue");
116: b.a.add(extendsA);
117: session.save("polyextends", extendsA);
118:
119: id = new Long(2);
120: BaseA base = new BaseA();
121: base.setId(id);
122: base.setValue("baseValue");
123: b.a.add(base);
124: session.save("polybase", base);
125:
126: session.save("bReference", b);
127:
128: b = (B) session.load("bReference", new Long(1));
129: assertEquals(2, b.a.size());
130: extendsA = (ExtendsA) b.a.get(0);
131: assertEquals("value", extendsA.getValue());
132: assertEquals("evalue", extendsA.getExtendsValue());
133: base = (BaseA) b.a.get(1);
134: assertEquals("baseValue", base.getValue());
135:
136: tr.commit();
137: session.close();
138: }
139:
140: public void testPolyComponent() throws Exception {
141: CompassSession session = openSession();
142: CompassTransaction tr = session.beginTransaction();
143:
144: C c = new C();
145: c.id = 1;
146:
147: Long id = new Long(1);
148: ExtendsA extendsA = new ExtendsA();
149: extendsA.setId(id);
150: extendsA.setValue("value");
151: extendsA.setExtendsValue("evalue");
152: c.a = extendsA;
153:
154: session.save("cComponent", c);
155:
156: c = (C) session.load("cComponent", new Long(1));
157: extendsA = (ExtendsA) c.a;
158: assertEquals("value", extendsA.getValue());
159: assertEquals("evalue", extendsA.getExtendsValue());
160:
161: c = new C();
162: c.id = 2;
163: id = new Long(2);
164: BaseA base = new BaseA();
165: base.setId(id);
166: base.setValue("baseValue");
167: c.a = base;
168:
169: session.save("cComponent", c);
170:
171: c = (C) session.load("cComponent", new Long(2));
172: assertEquals("baseValue", c.a.getValue());
173:
174: tr.commit();
175: session.close();
176: }
177: }
|