Source Code Cross Referenced for OmitFieldsTest.java in  » XML » xstream-1.3 » com » thoughtworks » acceptance » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » XML » xstream 1.3 » com.thoughtworks.acceptance 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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 20. June 2005 by Joe Walnes
011:         */
012:        package com.thoughtworks.acceptance;
013:
014:        import com.thoughtworks.acceptance.objects.StandardObject;
015:        import com.thoughtworks.xstream.XStream;
016:        import com.thoughtworks.xstream.mapper.Mapper;
017:        import com.thoughtworks.xstream.mapper.MapperWrapper;
018:
019:        public class OmitFieldsTest extends AbstractAcceptanceTest {
020:
021:            public static class Thing extends StandardObject {
022:                transient String alwaysIgnore;
023:                String sometimesIgnore;
024:                String neverIgnore;
025:            }
026:
027:            public void testTransientFieldsAreOmittedByDefault() {
028:                Thing in = new Thing();
029:                in.alwaysIgnore = "a";
030:                in.sometimesIgnore = "b";
031:                in.neverIgnore = "c";
032:
033:                String expectedXml = "" + "<thing>\n"
034:                        + "  <sometimesIgnore>b</sometimesIgnore>\n"
035:                        + "  <neverIgnore>c</neverIgnore>\n" + "</thing>";
036:
037:                xstream.alias("thing", Thing.class);
038:
039:                String actualXml = xstream.toXML(in);
040:                assertEquals(expectedXml, actualXml);
041:
042:                Thing out = (Thing) xstream.fromXML(actualXml);
043:                assertEquals(null, out.alwaysIgnore);
044:                assertEquals("b", out.sometimesIgnore);
045:                assertEquals("c", out.neverIgnore);
046:            }
047:
048:            public void testAdditionalFieldsCanBeExplicitlyOmittedThroughFacade() {
049:                Thing in = new Thing();
050:                in.alwaysIgnore = "a";
051:                in.sometimesIgnore = "b";
052:                in.neverIgnore = "c";
053:
054:                String expectedXml = "" // 
055:                        + "<thing>\n" // 
056:                        + "  <neverIgnore>c</neverIgnore>\n" // 
057:                        + "</thing>";
058:
059:                xstream.alias("thing", Thing.class);
060:                xstream.omitField(Thing.class, "sometimesIgnore");
061:
062:                String actualXml = xstream.toXML(in);
063:                assertEquals(expectedXml, actualXml);
064:
065:                Thing out = (Thing) xstream.fromXML(actualXml);
066:                assertEquals(null, out.alwaysIgnore);
067:                assertEquals(null, out.sometimesIgnore);
068:                assertEquals("c", out.neverIgnore);
069:            }
070:
071:            public static class DerivedThing extends Thing {
072:                String derived;
073:            }
074:
075:            public void testInheritedFieldsCanBeExplicitlyOmittedThroughFacade() {
076:                DerivedThing in = new DerivedThing();
077:                in.alwaysIgnore = "a";
078:                in.sometimesIgnore = "b";
079:                in.neverIgnore = "c";
080:                in.derived = "d";
081:
082:                String expectedXml = "" + "<thing>\n"
083:                        + "  <neverIgnore>c</neverIgnore>\n"
084:                        + "  <derived>d</derived>\n" + "</thing>";
085:
086:                xstream.alias("thing", DerivedThing.class);
087:                xstream.omitField(Thing.class, "sometimesIgnore");
088:
089:                String actualXml = xstream.toXML(in);
090:                assertEquals(expectedXml, actualXml);
091:
092:                DerivedThing out = (DerivedThing) xstream.fromXML(actualXml);
093:                assertEquals(null, out.alwaysIgnore);
094:                assertEquals(null, out.sometimesIgnore);
095:                assertEquals("c", out.neverIgnore);
096:                assertEquals("d", out.derived);
097:            }
098:
099:            public static class AnotherThing extends StandardObject {
100:                String stuff;
101:                String cheese;
102:                String myStuff;
103:                String myCheese;
104:            }
105:
106:            public void testFieldsCanBeIgnoredUsingCustomStrategy() {
107:                AnotherThing in = new AnotherThing();
108:                in.stuff = "a";
109:                in.cheese = "b";
110:                in.myStuff = "c";
111:                in.myCheese = "d";
112:
113:                String expectedXml = "" + "<thing>\n" + "  <stuff>a</stuff>\n"
114:                        + "  <cheese>b</cheese>\n" + "</thing>";
115:
116:                class OmitFieldsWithMyPrefixMapper extends MapperWrapper {
117:                    public OmitFieldsWithMyPrefixMapper(Mapper wrapped) {
118:                        super (wrapped);
119:                    }
120:
121:                    public boolean shouldSerializeMember(Class definedIn,
122:                            String fieldName) {
123:                        return !fieldName.startsWith("my");
124:                    }
125:                }
126:
127:                xstream = new XStream() {
128:                    protected MapperWrapper wrapMapper(MapperWrapper next) {
129:                        return new OmitFieldsWithMyPrefixMapper(next);
130:                    }
131:                };
132:
133:                xstream.alias("thing", AnotherThing.class);
134:
135:                String actualXml = xstream.toXML(in);
136:                assertEquals(expectedXml, actualXml);
137:
138:                AnotherThing out = (AnotherThing) xstream.fromXML(actualXml);
139:                assertEquals("a", out.stuff);
140:                assertEquals("b", out.cheese);
141:                assertEquals(null, out.myStuff);
142:                assertEquals(null, out.myCheese);
143:            }
144:
145:            public void testDeletedElementCanBeOmitted() {
146:                String expectedXml = ""
147:                        + "<thing>\n"
148:                        + "  <meanwhileDeletedIgnore>c</meanwhileDeletedIgnore>\n"
149:                        + "  <sometimesIgnore>b</sometimesIgnore>\n"
150:                        + "  <neverIgnore>c</neverIgnore>\n" + "</thing>";
151:
152:                xstream.alias("thing", Thing.class);
153:                xstream.omitField(Thing.class, "meanwhileDeletedIgnore");
154:
155:                Thing out = (Thing) xstream.fromXML(expectedXml);
156:                assertEquals("b", out.sometimesIgnore);
157:                assertEquals("c", out.neverIgnore);
158:            }
159:
160:            public void testDeletedAttributeCanBeOmitted() {
161:                String expectedXml = ""
162:                        + "<thing meanwhileDeletedIgnore='c'>\n"
163:                        + "  <sometimesIgnore>b</sometimesIgnore>\n"
164:                        + "  <neverIgnore>c</neverIgnore>\n" + "</thing>";
165:
166:                xstream.alias("thing", Thing.class);
167:                xstream.omitField(Thing.class, "meanwhileDeletedIgnore");
168:
169:                Thing out = (Thing) xstream.fromXML(expectedXml);
170:                assertEquals("b", out.sometimesIgnore);
171:                assertEquals("c", out.neverIgnore);
172:            }
173:
174:            public void testAttributeCanBeOmitted() {
175:                String expectedXml = "<thing neverIgnore=\"c\"/>";
176:
177:                xstream.alias("thing", Thing.class);
178:                xstream.useAttributeFor(String.class);
179:                xstream.omitField(Thing.class, "sometimesIgnore");
180:
181:                Thing in = new Thing();
182:                in.alwaysIgnore = "a";
183:                in.sometimesIgnore = "b";
184:                in.neverIgnore = "c";
185:                assertEquals(expectedXml, xstream.toXML(in));
186:
187:                Thing out = (Thing) xstream.fromXML(expectedXml);
188:                assertNull(out.sometimesIgnore);
189:                assertEquals("c", out.neverIgnore);
190:            }
191:
192:            static class ThingAgain extends Thing {
193:                String sometimesIgnore;
194:
195:                void setHidden(String s) {
196:                    super .sometimesIgnore = s;
197:                }
198:            }
199:
200:            // TODO: XSTR-457
201:            public void todoTestAnOmittedFieldMakesADefinedInAttributeSuperfluous() {
202:                ThingAgain in = new ThingAgain();
203:                in.alwaysIgnore = "a";
204:                in.setHidden("b");
205:                in.neverIgnore = "c";
206:                in.sometimesIgnore = "d";
207:
208:                xstream.alias("thing", ThingAgain.class);
209:                xstream.omitField(ThingAgain.class, "sometimesIgnore");
210:
211:                String expectedXml = "" + "<thing>\n"
212:                        + "  <sometimesIgnore>b</sometimesIgnore>\n"
213:                        + "  <neverIgnore>c</neverIgnore>\n" + "</thing>";
214:
215:                String actualXml = xstream.toXML(in);
216:                assertEquals(expectedXml, actualXml);
217:
218:                ThingAgain out = (ThingAgain) xstream.fromXML(expectedXml);
219:                assertNull(out.sometimesIgnore);
220:                out.alwaysIgnore = "a";
221:                out.sometimesIgnore = "d";
222:                assertEquals(in, out);
223:            }
224:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.