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.xml;
018:
019: import java.io.InputStreamReader;
020: import java.io.Reader;
021:
022: import org.compass.core.CompassHits;
023: import org.compass.core.CompassSession;
024: import org.compass.core.CompassTransaction;
025: import org.compass.core.Resource;
026: import org.compass.core.test.AbstractTestCase;
027: import org.compass.core.xml.AliasedXmlObject;
028: import org.compass.core.xml.XmlObject;
029:
030: /**
031: * @author kimchy
032: */
033: public abstract class AbstractXmlObjectTests extends AbstractTestCase {
034:
035: protected String[] getMappings() {
036: return new String[] { "xml/xml.cpm.xml" };
037: }
038:
039: protected abstract AliasedXmlObject buildAliasedXmlObject(
040: String alias, Reader data) throws Exception;
041:
042: private Reader readData(String path) {
043: path = "org/compass/core/test/xml/" + path + ".xml";
044: return new InputStreamReader(Thread.currentThread()
045: .getContextClassLoader().getResourceAsStream(path));
046: }
047:
048: public void testData1() throws Exception {
049: CompassSession session = openSession();
050: CompassTransaction tr = session.beginTransaction();
051:
052: AliasedXmlObject xmlObject = buildAliasedXmlObject("data1",
053: readData("data1"));
054: session.save(xmlObject);
055:
056: assertNull(session.get("data1", "1"));
057:
058: Resource resource = session.loadResource("data1", "1");
059: assertEquals("1", resource.getValue("$/data1/id"));
060: assertEquals(2, resource.getProperties("eleText").length);
061: assertEquals(2, resource.getProperties("value").length);
062:
063: resource = session.loadResource("data1", xmlObject);
064: assertEquals("1", resource.getValue("$/data1/id"));
065: assertEquals(2, resource.getProperties("eleText").length);
066: assertEquals(2, resource.getProperties("value").length);
067:
068: CompassHits hits = session.find("data11");
069: assertEquals(1, hits.length());
070: hits = session.find("data11attr");
071: assertEquals(1, hits.length());
072:
073: tr.commit();
074: session.close();
075: }
076:
077: public void testData2() throws Exception {
078: CompassSession session = openSession();
079: CompassTransaction tr = session.beginTransaction();
080:
081: AliasedXmlObject xmlObject = buildAliasedXmlObject("data2",
082: readData("data2"));
083: session.save(xmlObject);
084:
085: Resource resource = session.loadResource("data2", "1");
086: assertEquals("1", resource.getValue("$/data2/id"));
087: assertEquals(2, resource.getProperties("eleText").length);
088: assertEquals(2, resource.getProperties("value").length);
089:
090: resource = session.getResource("data2", "2");
091: assertNull(resource);
092:
093: CompassHits hits = session.find("data11");
094: assertEquals(1, hits.length());
095: hits = session.find("data11attr");
096: assertEquals(1, hits.length());
097:
098: tr.commit();
099: session.close();
100: }
101:
102: public void testData3() throws Exception {
103: CompassSession session = openSession();
104: CompassTransaction tr = session.beginTransaction();
105:
106: AliasedXmlObject xmlObject = buildAliasedXmlObject("data3",
107: readData("data3"));
108: session.save(xmlObject);
109:
110: Resource resource = session.loadResource("data3", "1");
111: assertEquals("1", resource.getValue("$/data3/id"));
112: assertEquals(2, resource.getProperties("eleText").length);
113: assertEquals(2, resource.getProperties("value").length);
114:
115: resource = session.getResource("data3", "2");
116: assertEquals("2", resource.getValue("$/data3/id"));
117: assertEquals(2, resource.getProperties("eleText").length);
118: assertEquals(2, resource.getProperties("value").length);
119:
120: CompassHits hits = session.find("data11");
121: assertEquals(1, hits.length());
122: hits = session.find("data11attr");
123: assertEquals(1, hits.length());
124: hits = session.find("data21attr");
125: assertEquals(1, hits.length());
126:
127: session.delete(xmlObject);
128: hits = session.find("data11attr");
129: assertEquals(0, hits.length());
130: hits = session.find("data21attr");
131: assertEquals(0, hits.length());
132:
133: tr.commit();
134: session.close();
135: }
136:
137: public void innerTestData4XmlContent() throws Exception {
138: CompassSession session = openSession();
139: CompassTransaction tr = session.beginTransaction();
140:
141: AliasedXmlObject xmlObject = buildAliasedXmlObject("data4",
142: readData("data4"));
143: session.save(xmlObject);
144:
145: Resource resource = session.loadResource("data4", "1");
146: assertEquals("1", resource.getValue("$/data4/id"));
147: assertEquals(2, resource.getProperties("eleText").length);
148: assertEquals(2, resource.getProperties("value").length);
149: assertNotNull(resource.getValue("content"));
150:
151: resource = session.getResource("data4", "2");
152: assertEquals("2", resource.getValue("$/data4/id"));
153: assertEquals(2, resource.getProperties("eleText").length);
154: assertEquals(2, resource.getProperties("value").length);
155: assertNotNull(resource.getValue("content"));
156:
157: xmlObject = (AliasedXmlObject) session.get("data4", "1");
158: assertNotNull(xmlObject);
159: XmlObject[] ids = xmlObject.selectPath("/data/id/@value");
160: assertEquals(1, ids.length);
161: assertEquals("1", ids[0].getValue());
162: xmlObject = (AliasedXmlObject) session.get("data4", "2");
163: assertNotNull(xmlObject);
164: ids = xmlObject.selectPath("/data/id/@value");
165: assertEquals(1, ids.length);
166: assertEquals("2", ids[0].getValue());
167:
168: tr.commit();
169: session.close();
170: }
171: }
|