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.nounmarshall.reference;
018:
019: import java.util.ArrayList;
020:
021: import org.compass.core.CompassSession;
022: import org.compass.core.CompassTransaction;
023: import org.compass.core.Resource;
024: import org.compass.core.mapping.ResourceMapping;
025: import org.compass.core.mapping.ResourcePropertyMapping;
026: import org.compass.core.spi.InternalCompass;
027: import org.compass.core.test.AbstractTestCase;
028:
029: /**
030: * @author kimchy
031: */
032: public class ABTests extends AbstractTestCase {
033:
034: protected String[] getMappings() {
035: return new String[] { "nounmarshall/reference/AB.cpm.xml" };
036: }
037:
038: public void testMappings() {
039: ResourceMapping aMapping = ((InternalCompass) getCompass())
040: .getMapping().getRootMappingByAlias("a");
041: ResourcePropertyMapping[] aPropertyMappings = aMapping
042: .getResourcePropertyMappings();
043: assertEquals(4, aPropertyMappings.length);
044: assertNotNull(aMapping.getResourcePropertyMapping("value"));
045: assertNotNull(aMapping.getResourcePropertyMapping("id"));
046: }
047:
048: public void testBSingleValue() throws Exception {
049: CompassSession session = openSession();
050: CompassTransaction tr = session.beginTransaction();
051:
052: A a = new A();
053: a.id = new Long(1);
054: a.value = "value";
055: B b = new B();
056: b.id = new Long(1);
057: b.value = "bvalue";
058: a.b = b;
059: session.save(b);
060: session.save(a);
061:
062: Resource resource = session.loadResource(A.class, new Long(1));
063: assertNotNull(resource);
064: assertEquals(5, resource.getProperties().length);
065: assertEquals("a", resource.getAlias());
066: assertEquals(1, resource.getProperties("value").length);
067: assertEquals("1", resource.getValue("$/a/b/id"));
068:
069: a = (A) session.load(A.class, new Long(1));
070: assertEquals(1, a.id.longValue());
071: assertNull(a.value);
072: assertNull(a.b);
073:
074: tr.commit();
075: session.close();
076: }
077:
078: public void testBTwoValues() throws Exception {
079: CompassSession session = openSession();
080: CompassTransaction tr = session.beginTransaction();
081:
082: A a = new A();
083: a.id = new Long(1);
084: a.value = "value";
085: B b = new B();
086: b.id = new Long(1);
087: b.value = "bvalue";
088: b.value2 = "bvalue2";
089: a.b = b;
090: session.save(a);
091:
092: Resource resource = session.loadResource(A.class, new Long(1));
093: assertNotNull(resource);
094: assertEquals(5, resource.getProperties().length);
095: assertEquals("a", resource.getAlias());
096: assertEquals(1, resource.getProperties("value").length);
097: assertEquals("1", resource.getValue("$/a/b/id"));
098:
099: a = (A) session.load(A.class, new Long(1));
100: assertEquals(1, a.id.longValue());
101: assertNull(a.value);
102: assertNull(a.b);
103:
104: tr.commit();
105: session.close();
106: }
107:
108: public void testBCollection() throws Exception {
109: CompassSession session = openSession();
110: CompassTransaction tr = session.beginTransaction();
111:
112: A a = new A();
113: a.id = new Long(1);
114: a.value = "value";
115: a.bs = new ArrayList();
116: B b = new B();
117: b.id = new Long(1);
118: b.value = "bvalue11";
119: b.value2 = "bvalue12";
120: session.save(b);
121: a.bs.add(b);
122: b = new B();
123: b.id = new Long(2);
124: b.value = "bvalue21";
125: b.value = "bvalue22";
126: session.save(b);
127: a.bs.add(b);
128: session.save(a);
129:
130: Resource resource = session.loadResource(A.class, new Long(1));
131: assertNotNull(resource);
132: assertEquals(6, resource.getProperties().length);
133: assertEquals("a", resource.getAlias());
134: assertEquals(1, resource.getProperties("value").length);
135: assertEquals(2, resource.getProperties("$/a/bs/id").length);
136:
137: tr.commit();
138: session.close();
139: }
140: }
|