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.component.comp2;
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.test.AbstractTestCase;
025:
026: /**
027: * @author kimchy
028: */
029: public class Comp2Tests extends AbstractTestCase {
030:
031: protected String[] getMappings() {
032: return new String[] { "component/comp2/mapping.cpm.xml" };
033: }
034:
035: public void testWithSpecialPerson() throws Exception {
036: CompassSession session = openSession();
037: CompassTransaction tr = session.beginTransaction();
038:
039: Person person = new SpecialPerson();
040: person.id = 1;
041: person.description = "test person";
042: person.names = new ArrayList();
043: person.names.add("name1");
044: session.save(person);
045:
046: User user = new User();
047: user.id = 1;
048: user.description = "test user";
049: user.identity = person;
050: session.save(user);
051:
052: person = (Person) session.load(SpecialPerson.class,
053: new Integer(1));
054: assertEquals("test person", person.description);
055: assertEquals(1, person.names.size());
056:
057: user = (User) session.load(User.class, "1");
058: assertEquals("test user", user.description);
059: person = user.identity;
060: assertEquals("test person", person.description);
061: assertEquals(1, person.names.size());
062:
063: Resource resource = session.loadResource(User.class,
064: new Integer(1));
065: assertEquals(10, resource.getProperties().length);
066: assertEquals("user", resource.getValue("alias"));
067: assertEquals(User.class.getName(), resource
068: .getValue("$/user/class"));
069: assertEquals("test user", resource.getValue("description2"));
070: assertEquals("1", resource.getValue("$/user/id"));
071: assertEquals(SpecialPerson.class.getName(), resource
072: .getValue("$/user/identity/class"));
073: assertEquals("test person", resource.getValue("description1"));
074: assertEquals("1", resource.getValue("$/user/identity/id"));
075: assertEquals("name1", resource.getValue("person"));
076: assertEquals("1", resource
077: .getValue("$/user/identity/names/colSize"));
078:
079: tr.commit();
080: session.close();
081: }
082:
083: public void testWithPerson() throws Exception {
084: CompassSession session = openSession();
085: CompassTransaction tr = session.beginTransaction();
086:
087: Person person = new Person();
088: person.id = 1;
089: person.description = "test person";
090: person.names = new ArrayList();
091: person.names.add("name1");
092: session.save(person);
093:
094: User user = new User();
095: user.id = 1;
096: user.description = "test user";
097: user.identity = person;
098: session.save(user);
099:
100: person = (Person) session.load(Person.class, new Integer(1));
101: assertEquals("test person", person.description);
102: assertEquals(1, person.names.size());
103:
104: user = (User) session.load(User.class, "1");
105: assertEquals("test user", user.description);
106: person = user.identity;
107: assertEquals("test person", person.description);
108: assertEquals(1, person.names.size());
109:
110: Resource resource = session.loadResource(User.class,
111: new Integer(1));
112: assertEquals(10, resource.getProperties().length);
113: assertEquals("user", resource.getValue("alias"));
114: assertEquals(User.class.getName(), resource
115: .getValue("$/user/class"));
116: assertEquals("test user", resource.getValue("description2"));
117: assertEquals("1", resource.getValue("$/user/id"));
118: assertEquals(Person.class.getName(), resource
119: .getValue("$/user/identity/class"));
120: assertEquals("test person", resource.getValue("description1"));
121: assertEquals("1", resource.getValue("$/user/identity/id"));
122: assertEquals("name1", resource.getValue("person"));
123: assertEquals("1", resource
124: .getValue("$/user/identity/names/colSize"));
125:
126: tr.commit();
127: session.close();
128: }
129: }
|