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.nounmarshall.simple;
018:
019: import org.compass.core.CompassSession;
020: import org.compass.core.CompassTransaction;
021: import org.compass.core.Resource;
022: import org.compass.core.mapping.ResourceMapping;
023: import org.compass.core.mapping.ResourcePropertyMapping;
024: import org.compass.core.spi.InternalCompass;
025: import org.compass.core.test.AbstractTestCase;
026:
027: /**
028: * @author kimchy
029: */
030: public class SimpleTests extends AbstractTestCase {
031:
032: protected String[] getMappings() {
033: return new String[] { "nounmarshall/simple/A.cpm.xml" };
034: }
035:
036: public void testMappings() {
037: ResourceMapping aMapping = ((InternalCompass) getCompass())
038: .getMapping().getRootMappingByAlias("a");
039: ResourcePropertyMapping[] aPropertyMappings = aMapping
040: .getResourcePropertyMappings();
041: assertEquals(3, aPropertyMappings.length);
042:
043: aMapping = ((InternalCompass) getCompass()).getMapping()
044: .getRootMappingByAlias("a1");
045: aPropertyMappings = aMapping.getResourcePropertyMappings();
046: // including the internal id
047: assertEquals(4, aPropertyMappings.length);
048: }
049:
050: public void testNoUnmarshall() throws Exception {
051: CompassSession session = openSession();
052: CompassTransaction tr = session.beginTransaction();
053:
054: A a = new A();
055: a.id = 1;
056: a.value = "value";
057: a.value2 = "value2";
058: session.save("a", a);
059:
060: Resource resource = session.loadResource("a", new Long(1));
061: assertNotNull(resource);
062: assertEquals(5, resource.getProperties().length);
063: assertEquals("a", resource.getAlias());
064: assertEquals(2, resource.getProperties("value").length);
065: assertEquals("1", resource.getValue("$/a/id"));
066:
067: // when unmarshalling, only A with its id is set
068: a = (A) session.load("a", new Long(1));
069: assertEquals(1, a.id);
070: assertNull(a.value);
071: assertNull(a.value2);
072:
073: tr.commit();
074: session.close();
075: }
076:
077: public void testNoUnmarshallWithIdHasSameMetaDataAsAnotherProperty()
078: throws Exception {
079: CompassSession session = openSession();
080: CompassTransaction tr = session.beginTransaction();
081:
082: A a = new A();
083: a.id = 1;
084: a.value = "value";
085: a.value2 = "value2";
086: session.save("a1", a);
087:
088: Resource resource = session.loadResource("a1", new Long(1));
089: assertNotNull(resource);
090: assertEquals(6, resource.getProperties().length);
091: assertEquals("a1", resource.getAlias());
092: assertEquals(3, resource.getProperties("value").length);
093: assertEquals("1", resource.getValue("$/a1/id"));
094:
095: // when unmarshalling, only A with its id is set
096: a = (A) session.load("a1", new Long(1));
097: assertEquals(1, a.id);
098: assertNull(a.value);
099: assertNull(a.value2);
100:
101: tr.commit();
102: session.close();
103: }
104: }
|