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