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 12. April 2005 by Joe Walnes
011: */
012: package com.thoughtworks.xstream.converters.javabean;
013:
014: import junit.framework.TestCase;
015:
016: import java.util.Iterator;
017:
018: public class PropertyDictionaryTest extends TestCase {
019:
020: private PropertyDictionary propertyDictionary;
021:
022: protected void setUp() throws Exception {
023: super .setUp();
024: propertyDictionary = new PropertyDictionary();
025: }
026:
027: /**
028: * Test class: three serializable properties, one with a all capital name,
029: * two others non serializable, one readable, one writable, and another and
030: * a lonely field
031: */
032: class SomeClass {
033: private String a;
034:
035: private String URL;
036:
037: private String c;
038:
039: private String d;
040:
041: private String e;
042:
043: private String f;
044:
045: public String getA() {
046: return a;
047: }
048:
049: public void setA(String a) {
050: this .a = a;
051: }
052:
053: public String getURL() {
054: return URL;
055: }
056:
057: public void setURL(String url) {
058: this .URL = url;
059: }
060:
061: public String getC() {
062: return c;
063: }
064:
065: public void setC(String c) {
066: this .c = c;
067: }
068:
069: public String getD() {
070: return d;
071: }
072:
073: public void setE(String e) {
074: this .e = e;
075: }
076: }
077:
078: public void testListsFieldsInClassInDefinitionOrder() {
079: Iterator properties = propertyDictionary
080: .serializablePropertiesFor(SomeClass.class);
081: assertEquals("URL", ((BeanProperty) properties.next())
082: .getName());
083: assertEquals("a", ((BeanProperty) properties.next()).getName());
084: assertEquals("c", ((BeanProperty) properties.next()).getName());
085: assertFalse("No more fields should be present", properties
086: .hasNext());
087: }
088:
089: /**
090: * Test subclassing and private properties
091: */
092: class SpecialClass extends SomeClass {
093: private String brilliant;
094:
095: public String getBrilliant() {
096: return brilliant;
097: }
098:
099: public void setBrilliant(String brilliant) {
100: this .brilliant = brilliant;
101: }
102:
103: public String getPrivate() {
104: return null;
105: }
106:
107: private void setPrivate(String string) {
108:
109: }
110: }
111:
112: public void testIncludesFieldsInSuperClasses() {
113: Iterator properties = propertyDictionary
114: .serializablePropertiesFor(SpecialClass.class);
115: assertEquals("URL", ((BeanProperty) properties.next())
116: .getName());
117: assertEquals("a", ((BeanProperty) properties.next()).getName());
118: assertEquals("brilliant", ((BeanProperty) properties.next())
119: .getName());
120: assertEquals("c", ((BeanProperty) properties.next()).getName());
121: assertFalse("No more fields should be present", properties
122: .hasNext());
123: }
124: }
|