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.annotations.test.inheritance;
018:
019: import org.compass.annotations.test.AbstractAnnotationsTestCase;
020: import org.compass.core.CompassHits;
021: import org.compass.core.CompassSession;
022: import org.compass.core.CompassTransaction;
023: import org.compass.core.Resource;
024: import org.compass.core.config.CompassConfiguration;
025: import org.compass.core.mapping.ResourceMapping;
026: import org.compass.core.spi.InternalCompass;
027:
028: /**
029: * @author kimchy
030: */
031: public class InheritanceTests extends AbstractAnnotationsTestCase {
032:
033: protected void addExtraConf(CompassConfiguration conf) {
034: conf.addClass(A.class);
035: conf.addClass(B.class);
036: conf.addClass(C.class);
037: }
038:
039: public void testExtendedAliases() {
040: ResourceMapping resourceMapping = ((InternalCompass) getCompass())
041: .getMapping().getMappingByAlias("B");
042: assertEquals(1, resourceMapping.getExtendedAliases().length);
043: assertEquals("A", resourceMapping.getExtendedAliases()[0]);
044:
045: resourceMapping = ((InternalCompass) getCompass()).getMapping()
046: .getMappingByAlias("A");
047: assertEquals(0, resourceMapping.getExtendedAliases().length);
048: }
049:
050: public void testExtendingAliases() {
051: ResourceMapping resourceMapping = ((InternalCompass) getCompass())
052: .getMapping().getMappingByAlias("A");
053: assertEquals(1, resourceMapping.getExtendingAliases().length);
054: assertEquals("B", resourceMapping.getExtendingAliases()[0]);
055:
056: resourceMapping = ((InternalCompass) getCompass()).getMapping()
057: .getMappingByAlias("B");
058: assertEquals(0, resourceMapping.getExtendingAliases().length);
059: }
060:
061: public void testOverride() throws Exception {
062: CompassSession session = openSession();
063: CompassTransaction tr = session.beginTransaction();
064:
065: B b = new B();
066: b.setId(1);
067: b.setValue1("value1");
068: b.setValue2("value2");
069: session.save(b);
070:
071: b = (B) session.load(B.class, 1);
072: assertEquals("value1", b.getValue1());
073: assertEquals("value2", b.getValue2());
074:
075: Resource resource = session.loadResource(B.class, 1);
076: // 5 properties, one for the alias, and one for the poly class
077: assertEquals(8, resource.getProperties().length);
078: assertNull(resource.getValue("value1"));
079: assertNotNull(resource.getValue("value1e"));
080: assertNotNull(resource.getValue("value2"));
081: assertNotNull(resource.getValue("value2e"));
082: assertEquals(resource.getValue("abase"), "abasevalue");
083:
084: tr.commit();
085: session.close();
086: }
087:
088: public void testComponentRefAliasInheritance() {
089: CompassSession session = openSession();
090: CompassTransaction tr = session.beginTransaction();
091:
092: B b = new B();
093: b.setId(1);
094: b.setValue1("value1");
095: b.setValue2("value2");
096: b.setValue3("value3");
097:
098: C c = new C();
099: c.id = 1;
100: c.a = b;
101: session.save(c);
102: c = (C) session.load(C.class, 1);
103: assertTrue(c.a instanceof B);
104: assertEquals("value1", c.a.getValue1());
105: assertEquals("value2", c.a.getValue2());
106: assertEquals("value3", ((B) c.a).getValue3());
107:
108: Resource resource = session.loadResource(C.class, 1);
109: assertNull(resource.getValue("value1"));
110: assertNotNull(resource.getValue("value1e"));
111: assertNotNull(resource.getValue("value2"));
112: assertNotNull(resource.getValue("value2e"));
113: assertEquals(resource.getValue("abase"), "abasevalue");
114:
115: tr.commit();
116: session.close();
117: }
118:
119: public void testPolyAliasQuery() {
120: CompassSession session = openSession();
121: CompassTransaction tr = session.beginTransaction();
122:
123: B b = new B();
124: b.setId(1);
125: b.setValue1("value1");
126: b.setValue2("value2");
127: session.save(b);
128:
129: CompassHits hits = session.queryBuilder().alias("B").hits();
130: assertEquals(1, hits.length());
131: hits = session.queryBuilder().polyAlias("A").hits();
132: assertEquals(1, hits.length());
133: hits = session.queryBuilder().polyAlias("B").hits();
134: assertEquals(1, hits.length());
135: hits = session.queryBuilder().alias("A").hits();
136: assertEquals(0, hits.length());
137:
138: tr.commit();
139: session.close();
140: }
141: }
|