KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > mapper > ImplicitCollectionMapperTest


1 package com.thoughtworks.xstream.mapper;
2
3 import com.thoughtworks.acceptance.objects.Hardware;
4 import com.thoughtworks.acceptance.objects.OpenSourceSoftware;
5 import com.thoughtworks.acceptance.objects.SampleLists;
6 import com.thoughtworks.acceptance.objects.Software;
7 import junit.framework.TestCase;
8
9 public class ImplicitCollectionMapperTest extends TestCase {
10
11     private ImplicitCollectionMapper implicitCollections = new ImplicitCollectionMapper(new DefaultMapper(null));
12
13     public void testAllowsFieldsToBeMarkedAsImplicitCollectionsToBeAdded() {
14         implicitCollections.add(SampleLists.class, "good", Object JavaDoc.class);
15         assertNotNull(implicitCollections.getImplicitCollectionDefForFieldName(SampleLists.class, "good"));
16         assertEquals("good", implicitCollections.getFieldNameForItemTypeAndName(SampleLists.class, Object JavaDoc.class, null));
17     }
18
19     public void testDoesNotMarkFieldsAsImplicitCollectionByDefault() {
20         assertNull(implicitCollections.getImplicitCollectionDefForFieldName(SampleLists.class, "good"));
21         assertEquals(null, implicitCollections.getFieldNameForItemTypeAndName(SampleLists.class, Object JavaDoc.class, null));
22     }
23
24     public void testAllowsFieldsToBeMarkedBasedOnItemType() {
25         implicitCollections.add(SampleLists.class, "good", Software.class);
26         implicitCollections.add(SampleLists.class, "bad", Hardware.class);
27         assertNotNull(implicitCollections.getImplicitCollectionDefForFieldName(SampleLists.class, "bad"));
28         assertNotNull(implicitCollections.getImplicitCollectionDefForFieldName(SampleLists.class, "good"));
29         assertEquals("good", implicitCollections.getFieldNameForItemTypeAndName(SampleLists.class, Software.class, null));
30         assertEquals("bad", implicitCollections.getFieldNameForItemTypeAndName(SampleLists.class, Hardware.class, null));
31     }
32
33     public void testIncludesSubClassesWhenCheckingItemType() {
34         implicitCollections.add(SampleLists.class, "good", Software.class);
35         assertEquals("good", implicitCollections.getFieldNameForItemTypeAndName(SampleLists.class, OpenSourceSoftware.class, null));
36         assertEquals(null, implicitCollections.getFieldNameForItemTypeAndName(SampleLists.class, Hardware.class, null));
37     }
38
39     public void testAllowsFieldsToBeMarkedAsNamedImplicitCollectionsToBeAdded() {
40         implicitCollections.add(SampleLists.class, "good", "good-item", Object JavaDoc.class);
41         implicitCollections.add(SampleLists.class, "bad", Object JavaDoc.class);
42         Mapper.ImplicitCollectionMapping mappingGood = implicitCollections.getImplicitCollectionDefForFieldName(SampleLists.class, "good");
43         assertNotNull(mappingGood);
44         assertEquals("good-item", mappingGood.getItemFieldName());
45         assertEquals(Object JavaDoc.class, mappingGood.getItemType());
46         assertEquals("good", mappingGood.getFieldName());
47
48         Mapper.ImplicitCollectionMapping mappingBad = implicitCollections.getImplicitCollectionDefForFieldName(SampleLists.class, "bad");
49         assertNotNull(mappingBad);
50         assertNull(mappingBad.getItemFieldName());
51
52         assertEquals("good", implicitCollections.getFieldNameForItemTypeAndName(SampleLists.class, Object JavaDoc.class, "good-item"));
53         assertEquals("bad", implicitCollections.getFieldNameForItemTypeAndName(SampleLists.class, Object JavaDoc.class, null));
54     }
55
56     public void testAllowsFieldsToBeMarkedBasedOnItemFieldName() {
57         implicitCollections.add(SampleLists.class, "good", "good-item", Object JavaDoc.class);
58         implicitCollections.add(SampleLists.class, "bad", "bad-item", Object JavaDoc.class);
59         Mapper.ImplicitCollectionMapping mappingGood = implicitCollections.getImplicitCollectionDefForFieldName(SampleLists.class, "good");
60         assertNotNull(mappingGood);
61         assertEquals("good-item", mappingGood.getItemFieldName());
62         assertEquals(Object JavaDoc.class, mappingGood.getItemType());
63         assertEquals("good", mappingGood.getFieldName());
64
65         Mapper.ImplicitCollectionMapping mappingBad = implicitCollections.getImplicitCollectionDefForFieldName(SampleLists.class, "bad");
66         assertNotNull(mappingBad);
67         assertEquals("bad-item", mappingBad.getItemFieldName());
68         assertEquals(Object JavaDoc.class, mappingBad.getItemType());
69         assertEquals("bad", mappingBad.getFieldName());
70
71         assertEquals("good", implicitCollections.getFieldNameForItemTypeAndName(SampleLists.class, Object JavaDoc.class, "good-item"));
72         assertEquals("bad", implicitCollections.getFieldNameForItemTypeAndName(SampleLists.class, Object JavaDoc.class, "bad-item"));
73     }
74
75     public void testIncludesSubClassesWhenCheckingItemTypeForNamedImplicitCollections() {
76         implicitCollections.add(SampleLists.class, "good", "good-item", Software.class);
77         assertEquals("good", implicitCollections.getFieldNameForItemTypeAndName(SampleLists.class, OpenSourceSoftware.class, "good-item"));
78         assertEquals(null, implicitCollections.getFieldNameForItemTypeAndName(SampleLists.class, Hardware.class, null));
79     }
80
81     public void testGetItemTypeForItemFieldName() {
82         implicitCollections.add(SampleLists.class, "good", "good-item", Software.class);
83         implicitCollections.add(SampleLists.class, "bad", "bad-item", Software.class);
84
85         assertEquals(Software.class, implicitCollections.getItemTypeForItemFieldName(SampleLists.class, "good-item"));
86     }
87 }
88
Popular Tags