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.samechild;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import org.compass.core.CompassHits;
024: import org.compass.core.CompassQuery;
025: import org.compass.core.CompassSession;
026: import org.compass.core.CompassTransaction;
027: import org.compass.core.cache.first.DefaultFirstLevelCache;
028: import org.compass.core.config.CompassEnvironment;
029: import org.compass.core.config.CompassSettings;
030: import org.compass.core.test.AbstractTestCase;
031:
032: /**
033: * @author kimchy
034: */
035: public class SameChildTests extends AbstractTestCase {
036:
037: protected String[] getMappings() {
038: return new String[] { "component/samechild/mapping.cpm.xml" };
039: }
040:
041: protected void addSettings(CompassSettings settings) {
042: settings.setSetting(CompassEnvironment.Cache.FirstLevel.TYPE,
043: DefaultFirstLevelCache.class.getName());
044: }
045:
046: public void testSameChild() throws Exception {
047: Parent p1 = createParent(1);
048: Parent p2 = createParent(2);
049:
050: ArrayList allChildren = new ArrayList();
051: allChildren.add(createChild(1));
052: allChildren.add(createChild(3));
053: allChildren.add(createChild(2));
054:
055: p1.children = allChildren.subList(0, 2);
056: p2.children = allChildren.subList(1, 3);
057:
058: // initial test
059: assertIdsMatchNames("All original children", allChildren);
060: assertIdsMatchNames("Original p1 children", p1.children);
061: assertIdsMatchNames("Original p2 children", p2.children);
062:
063: CompassSession session = openSession();
064: CompassTransaction tr = session.beginTransaction();
065:
066: for (Iterator it = allChildren.iterator(); it.hasNext();) {
067: session.save(it.next());
068: }
069: session.save(p1);
070: session.save(p2);
071:
072: tr.commit();
073: session.close();
074:
075: session = openSession();
076: tr = session.beginTransaction();
077:
078: p1 = (Parent) session.load(Parent.class, "1");
079: p2 = (Parent) session.load(Parent.class, "2");
080:
081: List children = new ArrayList();
082: CompassQuery query = session.queryBuilder().matchAll()
083: .setAliases(new String[] { "child" });
084: CompassHits hits = query.hits();
085: for (int i = 0, n = hits.length(); i < n; i++) {
086: children.add(hits.data(i));
087: }
088:
089: tr.commit();
090: session.close();
091:
092: assertIdsMatchNames("All fetched children", children);
093: assertIdsMatchNames("Fetched p1 children", p1.children);
094: assertIdsMatchNames("Fetched p2 children", p2.children);
095: }
096:
097: private void assertIdsMatchNames(String header, List children) {
098: // System.out.println(header);
099: for (Iterator it = children.iterator(); it.hasNext();) {
100: Child c = (Child) it.next();
101: assertEquals("child" + c.id, c.name);
102: }
103: }
104:
105: private Child createChild(int i) {
106: Child c = new Child();
107: c.id = i;
108: c.name = "child" + i;
109: return c;
110: }
111:
112: private Parent createParent(int i) {
113: Parent p = new Parent();
114: p.id = i;
115: p.name = "parent" + i;
116: return p;
117: }
118:
119: }
|