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.component.simple1;
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 CollectionNoUnmarshallComonentTests extends
033: AbstractTestCase {
034:
035: protected String[] getMappings() {
036: return new String[] { "nounmarshall/component/simple1/mapping.cpm.xml" };
037: }
038:
039: public void testMappingsA() {
040: ResourceMapping aMapping = ((InternalCompass) getCompass())
041: .getMapping().getRootMappingByAlias("a");
042: ResourcePropertyMapping[] aPropertyMappings = aMapping
043: .getResourcePropertyMappings();
044: assertEquals(8, aPropertyMappings.length);
045: assertNotNull(aMapping.getResourcePropertyMapping("value"));
046: assertNotNull(aMapping.getResourcePropertyMapping("value2"));
047: assertNotNull(aMapping.getResourcePropertyMapping("id"));
048: }
049:
050: public void testBSingleValue() throws Exception {
051: CompassSession session = openSession();
052: CompassTransaction tr = session.beginTransaction();
053:
054: A a = new A();
055: a.id = 1;
056: a.value = "value";
057: B b = new B();
058: b.value = "bvalue";
059: a.b = b;
060: session.save("a", a);
061:
062: Resource resource = session.loadResource("a", new Long(1));
063: assertNotNull(resource);
064: assertEquals(5, resource.getProperties().length);
065: assertEquals("a", resource.getAlias());
066: assertEquals(2, resource.getProperties("value").length);
067:
068: // make sure no unmarshall returns A only with its ids set
069: a = (A) session.load("a", 1);
070: assertEquals(1, a.id);
071: assertNull(a.b);
072:
073: tr.commit();
074: session.close();
075: }
076:
077: public void testBwoValues() throws Exception {
078: CompassSession session = openSession();
079: CompassTransaction tr = session.beginTransaction();
080:
081: A a = new A();
082: a.id = 1;
083: a.value = "value";
084: B b = new B();
085: b.value = "bvalue";
086: b.value2 = "bvalue2";
087: a.b = b;
088: session.save("a", a);
089:
090: Resource resource = session.loadResource("a", new Long(1));
091: assertNotNull(resource);
092: assertEquals(7, resource.getProperties().length);
093: assertEquals("a", resource.getAlias());
094: assertEquals(3, resource.getProperties("value").length);
095:
096: // make sure no unmarshall returns A only with its ids set
097: a = (A) session.load("a", 1);
098: assertEquals(1, a.id);
099: assertNull(a.b);
100:
101: tr.commit();
102: session.close();
103: }
104:
105: public void testBCollection() throws Exception {
106: CompassSession session = openSession();
107: CompassTransaction tr = session.beginTransaction();
108:
109: A a = new A();
110: a.id = 1;
111: a.value = "value";
112: a.bs = new ArrayList<B>();
113: B b = new B();
114: b.value = "bvalue11";
115: b.value2 = "bvalue12";
116: a.bs.add(b);
117: b = new B();
118: b.value = "bvalue21";
119: b.value = "bvalue22";
120: a.bs.add(b);
121: session.save("a", a);
122:
123: Resource resource = session.loadResource("a", 1);
124: assertNotNull(resource);
125: assertEquals(8, resource.getProperties().length);
126: assertEquals("a", resource.getAlias());
127: assertEquals(4, resource.getProperties("value").length);
128:
129: tr.commit();
130: session.close();
131: }
132:
133: public void testBCollectionWithNullValue() throws Exception {
134: CompassSession session = openSession();
135: CompassTransaction tr = session.beginTransaction();
136:
137: A a = new A();
138: a.id = 1;
139: a.value = "value";
140: a.bs = new ArrayList<B>();
141: B b = new B();
142: b.value = null;
143: b.value2 = "bvalue12";
144: a.bs.add(b);
145: b = new B();
146: b.value = "bvalue21";
147: b.value = "bvalue22";
148: a.bs.add(b);
149: session.save("a", a);
150:
151: Resource resource = session.loadResource("a", new Long(1));
152: assertNotNull(resource);
153: assertEquals(7, resource.getProperties().length);
154: assertEquals("a", resource.getAlias());
155: assertEquals(3, resource.getProperties("value").length);
156:
157: tr.commit();
158: session.close();
159: }
160: }
|