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.inheritance;
018:
019: import java.util.HashSet;
020:
021: import org.compass.core.CompassHits;
022: import org.compass.core.CompassSession;
023: import org.compass.core.CompassTransaction;
024: import org.compass.core.Resource;
025: import org.compass.core.config.CompassEnvironment;
026: import org.compass.core.config.CompassSettings;
027: import org.compass.core.mapping.AliasMapping;
028: import org.compass.core.mapping.CompassMapping;
029: import org.compass.core.spi.InternalCompass;
030: import org.compass.core.test.AbstractTestCase;
031:
032: public class InheritanceTests extends AbstractTestCase {
033:
034: protected String[] getMappings() {
035: return new String[] { "inheritance/Inheritance.cpm.xml" };
036: }
037:
038: protected void addSettings(CompassSettings settings) {
039: settings.setBooleanSetting(
040: CompassEnvironment.All.EXCLUDE_ALIAS, false);
041: }
042:
043: public void testExtendingAliases() {
044: CompassMapping compassMapping = ((InternalCompass) getCompass())
045: .getMapping();
046: AliasMapping aliasMapping = compassMapping
047: .getAliasMapping("base");
048: assertEquals(3, aliasMapping.getExtendingAliases().length);
049: HashSet aliases = new HashSet();
050: aliases.add("override");
051: aliases.add("override1");
052: aliases.add("extends");
053: for (int i = 0; i < aliasMapping.getExtendingAliases().length; i++) {
054: assertTrue(aliases.contains(aliasMapping
055: .getExtendingAliases()[i]));
056: }
057: }
058:
059: public void testPolyQuery() throws Exception {
060: CompassSession session = openSession();
061: CompassTransaction tr = session.beginTransaction();
062:
063: Long id = new Long(1);
064: ExtendsA extendsA = new ExtendsA();
065: extendsA.setId(id);
066: extendsA.setValue("value");
067: extendsA.setExtendsValue("evalue");
068:
069: session.save("extends", extendsA);
070:
071: extendsA = (ExtendsA) session.load("extends", id);
072: assertEquals("value", extendsA.getValue());
073:
074: id = new Long(1);
075: extendsA = new ExtendsA();
076: extendsA.setId(id);
077: extendsA.setValue("value");
078: extendsA.setExtendsValue("evalue");
079:
080: session.save("override", extendsA);
081:
082: extendsA = (ExtendsA) session.load("override", id);
083: assertEquals("value", extendsA.getValue());
084:
085: CompassHits hits = session.queryBuilder().polyAlias("base")
086: .hits();
087: assertEquals(2, hits.length());
088:
089: hits = session.find("base");
090: assertEquals(2, hits.length());
091:
092: tr.commit();
093: session.close();
094: }
095:
096: public void testSimpleExtends() throws Exception {
097: CompassSession session = openSession();
098: CompassTransaction tr = session.beginTransaction();
099:
100: Long id = new Long(1);
101: ExtendsA extendsA = new ExtendsA();
102: extendsA.setId(id);
103: extendsA.setValue("value");
104: extendsA.setExtendsValue("evalue");
105:
106: session.save("extends", extendsA);
107:
108: extendsA = (ExtendsA) session.load("extends", id);
109: assertEquals("value", extendsA.getValue());
110:
111: tr.commit();
112: session.close();
113: }
114:
115: public void testSimpleExtendsWithOverride() throws Exception {
116: CompassSession session = openSession();
117: CompassTransaction tr = session.beginTransaction();
118:
119: Long id = new Long(1);
120: ExtendsA extendsA = new ExtendsA();
121: extendsA.setId(id);
122: extendsA.setValue("value");
123: extendsA.setExtendsValue("evalue");
124:
125: session.save("override", extendsA);
126:
127: extendsA = (ExtendsA) session.load("override", id);
128: assertEquals("value", extendsA.getValue());
129:
130: Resource r = session.loadResource("override", id);
131: assertNull(r.getProperty("mvalue"));
132: assertNotNull(r.getProperty("mvalue1"));
133:
134: tr.commit();
135: session.close();
136: }
137:
138: public void testSimpleExtendsWithNoOverride() throws Exception {
139: CompassSession session = openSession();
140: CompassTransaction tr = session.beginTransaction();
141:
142: Long id = new Long(1);
143: ExtendsA extendsA = new ExtendsA();
144: extendsA.setId(id);
145: extendsA.setValue("value");
146: extendsA.setExtendsValue("evalue");
147:
148: session.save("override1", extendsA);
149:
150: extendsA = (ExtendsA) session.load("override1", id);
151: assertEquals("value", extendsA.getValue());
152:
153: Resource r = session.loadResource("override1", id);
154: assertNotNull(r.getProperty("mvalue"));
155: assertNotNull(r.getProperty("mvalue1"));
156:
157: tr.commit();
158: session.close();
159: }
160: }
|