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.component;
018:
019: import java.util.ArrayList;
020: import java.util.HashSet;
021:
022: import org.compass.annotations.test.AbstractAnnotationsTestCase;
023: import org.compass.core.CompassHits;
024: import org.compass.core.CompassQuery;
025: import org.compass.core.CompassQueryBuilder;
026: import org.compass.core.CompassSession;
027: import org.compass.core.CompassTransaction;
028: import org.compass.core.Resource;
029: import org.compass.core.config.CompassConfiguration;
030:
031: /**
032: * @author kimchy
033: */
034: public class ComponentTests extends AbstractAnnotationsTestCase {
035:
036: protected void addExtraConf(CompassConfiguration conf) {
037: conf.addClass(A.class).addClass(B.class).addClass(User.class)
038: .addClass(Community.class);
039: }
040:
041: public void testSimpleComponent() {
042: CompassSession session = openSession();
043: CompassTransaction tr = session.beginTransaction();
044:
045: A a = new A();
046: a.id = 1;
047: a.value = "avalue";
048:
049: B b = new B();
050: b.value = "bvalue";
051: a.b = b;
052:
053: ArrayList<B> bValues = new ArrayList<B>();
054: bValues.add(new B("bvalue1"));
055: bValues.add(new B("bvalue2"));
056: a.bValues = bValues;
057:
058: HashSet<B> bValuesSet = new HashSet<B>();
059: bValuesSet.add(new B("bvalueset1"));
060: bValuesSet.add(new B("bvalueset2"));
061: a.bValuesSet = bValuesSet;
062:
063: session.save(a);
064:
065: a = (A) session.load(A.class, 1);
066: assertEquals("avalue", a.value);
067: assertEquals("bvalue", a.b.value);
068: assertEquals("bvalue1", a.bValues.get(0).value);
069: assertEquals("bvalue2", a.bValues.get(1).value);
070: assertEquals(2, a.bValuesSet.size());
071:
072: CompassHits hits = session.find("bvalue");
073: assertEquals(1, hits.length());
074: a = (A) hits.data(0);
075: assertEquals("avalue", a.value);
076: assertEquals("bvalue", a.b.value);
077:
078: hits = session.find("bValue:bvalueset1");
079: assertEquals(1, hits.length());
080: a = (A) hits.data(0);
081: assertEquals("avalue", a.value);
082: assertEquals("bvalue", a.b.value);
083:
084: Resource resource = hits.resource(0);
085: assertEquals(5, resource.getProperties("bValue").length);
086:
087: hits = session.find("bValue:bvalueset2");
088: assertEquals(1, hits.length());
089: a = (A) hits.data(0);
090: assertEquals("avalue", a.value);
091: assertEquals("bvalue", a.b.value);
092:
093: hits = session.find("bvalue1");
094: assertEquals(1, hits.length());
095:
096: // this only works because B value is defined with ManageIdIndex.UN_TOKENIZED
097: CompassQuery query = session.queryBuilder().term(
098: "A.bValues.value", "bvalue1");
099: hits = query.hits();
100: assertEquals(1, hits.length());
101:
102: query = session.queryBuilder().term("A.bValues.value",
103: "bvalue1");
104: hits = query.setAliases(new String[] { "A" }).hits();
105: assertEquals(1, hits.length());
106:
107: tr.commit();
108: session.close();
109: }
110:
111: public void testUserCommunities() {
112: User u = new User();
113: u.setUserName("ploppy");
114: u.setCommunities(new HashSet<Community>());
115: Community testCommunity = new Community("testCommunity");
116: Community root = new Community("root");
117: u.getCommunities().add(root);
118: u.getCommunities().add(testCommunity);
119:
120: CompassSession session = openSession();
121: CompassTransaction tr = session.beginTransaction();
122: session.save(u);
123:
124: CompassQueryBuilder builder = session.queryBuilder();
125: CompassQueryBuilder.CompassBooleanQueryBuilder booleanQuery = builder
126: .bool();
127: booleanQuery.addShould(builder.term("userName", "ploppy"));
128: CompassQueryBuilder.CompassBooleanQueryBuilder communityQuery = builder
129: .bool();
130: communityQuery.addShould(builder.term("User.communities.name",
131: "root"));
132: booleanQuery.addMust(communityQuery.toQuery());
133: CompassQuery compassQuery = booleanQuery.toQuery().setAliases(
134: new String[] { "User" });
135: CompassHits hits = compassQuery.hits();
136: assertEquals(1, hits.length());
137:
138: booleanQuery = builder.bool();
139: booleanQuery.addShould(builder.term("userName", "ploppy"));
140: communityQuery = builder.bool();
141: communityQuery.addShould(builder.term("User.communities.name",
142: session.analyzerHelper().analyzeSingle("testCommunity")
143: .getTermText()));
144: booleanQuery.addMust(communityQuery.toQuery());
145: compassQuery = booleanQuery.toQuery().setAliases(
146: new String[] { "User" });
147: hits = compassQuery.hits();
148: assertEquals(1, hits.length());
149:
150: tr.commit();
151: session.close();
152: }
153:
154: }
|