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.managedid;
018:
019: import org.compass.core.CompassSession;
020: import org.compass.core.CompassTransaction;
021: import org.compass.core.Resource;
022: import org.compass.core.test.AbstractTestCase;
023:
024: /**
025: * @author kimchy
026: */
027: public class ManagedIdTests extends AbstractTestCase {
028:
029: protected String[] getMappings() {
030: return new String[] { "managedid/ManagedId.cpm.xml" };
031: }
032:
033: public void testAutoDifferentMetaData() {
034: CompassSession session = openSession();
035: CompassTransaction tr = session.beginTransaction();
036:
037: Long id = new Long(1);
038: A a = new A();
039: a.setId(id);
040: a.setValue1("value1");
041: a.setValue2("value2");
042:
043: session.save(a);
044:
045: Resource r = session.getResource(A.class, id);
046: String val = r.getValue("$/a/id");
047: assertNotNull(val);
048: assertEquals("1", val);
049: val = r.getValue("$/a/value1");
050: assertNull(val);
051: val = r.getValue("$/a/value2");
052: assertNull(val);
053:
054: tr.commit();
055: }
056:
057: public void testAutoSameMetaData() {
058: CompassSession session = openSession();
059: CompassTransaction tr = session.beginTransaction();
060:
061: Long id = new Long(1);
062: B b = new B();
063: b.setId(id);
064: b.setValue1("value1");
065: b.setValue2("value2");
066:
067: session.save(b);
068:
069: Resource r = session.getResource(B.class, id);
070: String val = r.getValue("$/b/id");
071: assertNotNull(val);
072: assertEquals("1", val);
073: val = r.getValue("$/b/value1");
074: assertNotNull(val);
075: assertEquals("value1", val);
076: val = r.getValue("$/b/value2");
077: assertNotNull(val);
078: assertEquals("value2", val);
079:
080: tr.commit();
081: }
082:
083: public void testAutoMultipleMetaDataWithDifferent() {
084: CompassSession session = openSession();
085: CompassTransaction tr = session.beginTransaction();
086:
087: Long id = new Long(1);
088: C c = new C();
089: c.setId(id);
090: c.setValue1("value1");
091: c.setValue2("value2");
092:
093: session.save(c);
094:
095: Resource r = session.getResource(C.class, id);
096: String val = r.getValue("$/c/id");
097: assertNotNull(val);
098: assertEquals("1", val);
099: val = r.getValue("$/c/value1");
100: assertNull(val);
101: val = r.getValue("$/c/value2");
102: assertNull(val);
103:
104: tr.commit();
105: }
106:
107: public void testAutoConstnat() {
108: CompassSession session = openSession();
109: CompassTransaction tr = session.beginTransaction();
110:
111: Long id = new Long(1);
112: Constant c = new Constant();
113: c.setId(id);
114: c.setValue1("value1");
115: c.setValue2("value2");
116:
117: session.save(c);
118:
119: Resource r = session.getResource(Constant.class, id);
120: String val = r.getValue("$/constant/id");
121: assertNotNull(val);
122: assertEquals("1", val);
123: val = r.getValue("$/constant/value1");
124: assertNotNull(val);
125: val = r.getValue("$/constant/value2");
126: assertNull(val);
127:
128: tr.commit();
129: }
130:
131: public void testAutoWithComponents() {
132: CompassSession session = openSession();
133: CompassTransaction tr = session.beginTransaction();
134:
135: ChildComponent c = new ChildComponent();
136: c.setValue1("value1");
137: c.setValue2("value2");
138:
139: Long id = new Long(1);
140: ParentComponent parentComponent = new ParentComponent();
141:
142: parentComponent.setId(id);
143: parentComponent.setValue1("value1");
144: parentComponent.setValue2("value2");
145: parentComponent.setChildComponent(c);
146:
147: session.save(parentComponent);
148:
149: Resource r = session.getResource(ParentComponent.class, id);
150: String val = r.getValue("$/parentComp/id");
151: assertNotNull(val);
152: assertEquals("1", val);
153: val = r.getValue("$/parentComp/value1");
154: assertNotNull(val);
155: val = r.getValue("$/parentComp/value2");
156: assertNotNull(val);
157: val = r.getValue("$/parentComp/childComponent/value1");
158: assertNotNull(val);
159: val = r.getValue("$/parentComp/childComponent/value2");
160: assertNotNull(val);
161:
162: tr.commit();
163: }
164:
165: public void testAutoWitRreferences() {
166: CompassSession session = openSession();
167: CompassTransaction tr = session.beginTransaction();
168:
169: Long id = new Long(1);
170: ChildReference cr = new ChildReference();
171: cr.setId(id);
172: cr.setValue1("value1");
173: cr.setValue2("value2");
174:
175: session.save(cr);
176:
177: ParentReference pr = new ParentReference();
178: pr.setId(id);
179: pr.setValue1("value1");
180: pr.setValue2("value2");
181: pr.setChildReference(cr);
182:
183: session.save(pr);
184:
185: Resource r = session.getResource(ParentReference.class, id);
186: String val = r.getValue("$/parentRef/id");
187: assertNotNull(val);
188: assertEquals("1", val);
189: val = r.getValue("$/parentRef/value1");
190: assertNull(val);
191: val = r.getValue("$/parentRef/value2");
192: assertNull(val);
193: val = r.getValue("$/parentRef/childReference/id");
194: assertNotNull(val);
195: assertEquals("1", val);
196:
197: tr.commit();
198: }
199:
200: public void testNoManagedId() {
201: CompassSession session = openSession();
202: CompassTransaction tr = session.beginTransaction();
203:
204: Long id = new Long(1);
205: D d = new D();
206: d.setId(id);
207: d.setValue("value1");
208:
209: session.save(d);
210:
211: d = (D) session.load(D.class, new Long(1));
212: assertNotNull(d);
213: assertEquals(new Long(1), d.getId());
214: assertNull(d.getValue());
215:
216: tr.commit();
217: }
218: }
|