001: /*
002: * Copyright (C) 2005 Joe Walnes.
003: * Copyright (C) 2006, 2007 XStream Committers.
004: * All rights reserved.
005: *
006: * The software in this package is published under the terms of the BSD
007: * style license a copy of which has been included with this distribution in
008: * the LICENSE.txt file.
009: *
010: * Created on 16. February 2005 by Joe Walnes
011: */
012: package com.thoughtworks.xstream.mapper;
013:
014: import com.thoughtworks.acceptance.objects.Hardware;
015: import com.thoughtworks.acceptance.objects.OpenSourceSoftware;
016: import com.thoughtworks.acceptance.objects.SampleLists;
017: import com.thoughtworks.acceptance.objects.Software;
018:
019: import junit.framework.TestCase;
020:
021: public class ImplicitCollectionMapperTest extends TestCase {
022:
023: private ImplicitCollectionMapper implicitCollections = new ImplicitCollectionMapper(
024: new DefaultMapper(null));
025:
026: public void testAllowsFieldsToBeMarkedAsImplicitCollectionsToBeAdded() {
027: implicitCollections
028: .add(SampleLists.class, "good", Object.class);
029: assertNotNull(implicitCollections
030: .getImplicitCollectionDefForFieldName(
031: SampleLists.class, "good"));
032: assertEquals("good", implicitCollections
033: .getFieldNameForItemTypeAndName(SampleLists.class,
034: Object.class, null));
035: }
036:
037: public void testDoesNotMarkFieldsAsImplicitCollectionByDefault() {
038: assertNull(implicitCollections
039: .getImplicitCollectionDefForFieldName(
040: SampleLists.class, "good"));
041: assertEquals(null, implicitCollections
042: .getFieldNameForItemTypeAndName(SampleLists.class,
043: Object.class, null));
044: }
045:
046: public void testAllowsFieldsToBeMarkedBasedOnItemType() {
047: implicitCollections.add(SampleLists.class, "good",
048: Software.class);
049: implicitCollections.add(SampleLists.class, "bad",
050: Hardware.class);
051: assertNotNull(implicitCollections
052: .getImplicitCollectionDefForFieldName(
053: SampleLists.class, "bad"));
054: assertNotNull(implicitCollections
055: .getImplicitCollectionDefForFieldName(
056: SampleLists.class, "good"));
057: assertEquals("good", implicitCollections
058: .getFieldNameForItemTypeAndName(SampleLists.class,
059: Software.class, null));
060: assertEquals("bad", implicitCollections
061: .getFieldNameForItemTypeAndName(SampleLists.class,
062: Hardware.class, null));
063: }
064:
065: public void testIncludesSubClassesWhenCheckingItemType() {
066: implicitCollections.add(SampleLists.class, "good",
067: Software.class);
068: assertEquals("good", implicitCollections
069: .getFieldNameForItemTypeAndName(SampleLists.class,
070: OpenSourceSoftware.class, null));
071: assertEquals(null, implicitCollections
072: .getFieldNameForItemTypeAndName(SampleLists.class,
073: Hardware.class, null));
074: }
075:
076: public void testAllowsFieldsToBeMarkedAsNamedImplicitCollectionsToBeAdded() {
077: implicitCollections.add(SampleLists.class, "good", "good-item",
078: Object.class);
079: implicitCollections.add(SampleLists.class, "bad", Object.class);
080: Mapper.ImplicitCollectionMapping mappingGood = implicitCollections
081: .getImplicitCollectionDefForFieldName(
082: SampleLists.class, "good");
083: assertNotNull(mappingGood);
084: assertEquals("good-item", mappingGood.getItemFieldName());
085: assertEquals(Object.class, mappingGood.getItemType());
086: assertEquals("good", mappingGood.getFieldName());
087:
088: Mapper.ImplicitCollectionMapping mappingBad = implicitCollections
089: .getImplicitCollectionDefForFieldName(
090: SampleLists.class, "bad");
091: assertNotNull(mappingBad);
092: assertNull(mappingBad.getItemFieldName());
093:
094: assertEquals("good", implicitCollections
095: .getFieldNameForItemTypeAndName(SampleLists.class,
096: Object.class, "good-item"));
097: assertEquals("bad", implicitCollections
098: .getFieldNameForItemTypeAndName(SampleLists.class,
099: Object.class, null));
100: }
101:
102: public void testAllowsFieldsToBeMarkedBasedOnItemFieldName() {
103: implicitCollections.add(SampleLists.class, "good", "good-item",
104: Object.class);
105: implicitCollections.add(SampleLists.class, "bad", "bad-item",
106: Object.class);
107: Mapper.ImplicitCollectionMapping mappingGood = implicitCollections
108: .getImplicitCollectionDefForFieldName(
109: SampleLists.class, "good");
110: assertNotNull(mappingGood);
111: assertEquals("good-item", mappingGood.getItemFieldName());
112: assertEquals(Object.class, mappingGood.getItemType());
113: assertEquals("good", mappingGood.getFieldName());
114:
115: Mapper.ImplicitCollectionMapping mappingBad = implicitCollections
116: .getImplicitCollectionDefForFieldName(
117: SampleLists.class, "bad");
118: assertNotNull(mappingBad);
119: assertEquals("bad-item", mappingBad.getItemFieldName());
120: assertEquals(Object.class, mappingBad.getItemType());
121: assertEquals("bad", mappingBad.getFieldName());
122:
123: assertEquals("good", implicitCollections
124: .getFieldNameForItemTypeAndName(SampleLists.class,
125: Object.class, "good-item"));
126: assertEquals("bad", implicitCollections
127: .getFieldNameForItemTypeAndName(SampleLists.class,
128: Object.class, "bad-item"));
129: }
130:
131: public void testIncludesSubClassesWhenCheckingItemTypeForNamedImplicitCollections() {
132: implicitCollections.add(SampleLists.class, "good", "good-item",
133: Software.class);
134: assertEquals("good", implicitCollections
135: .getFieldNameForItemTypeAndName(SampleLists.class,
136: OpenSourceSoftware.class, "good-item"));
137: assertEquals(null, implicitCollections
138: .getFieldNameForItemTypeAndName(SampleLists.class,
139: Hardware.class, null));
140: }
141:
142: public void testGetItemTypeForItemFieldName() {
143: implicitCollections.add(SampleLists.class, "good", "good-item",
144: Software.class);
145: implicitCollections.add(SampleLists.class, "bad", "bad-item",
146: Software.class);
147:
148: assertEquals(Software.class, implicitCollections
149: .getItemTypeForItemFieldName(SampleLists.class,
150: "good-item"));
151: }
152: }
|