001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.betwixt.io;
018:
019: import java.io.StringWriter;
020:
021: import org.apache.commons.betwixt.AbstractTestCase;
022: import org.apache.commons.betwixt.AttributeDescriptor;
023: import org.apache.commons.betwixt.ElementDescriptor;
024: import org.apache.commons.betwixt.strategy.ValueSuppressionStrategy;
025:
026: /**
027: * Tests supress element strategy.
028: *
029: * @author <a href='http://jakarta.apache.org/commons'>Jakarta Commons Team</a>
030: * of the <a href='http://www.apache.org'>Apache Software Foundation</a>
031: */
032: public class TestSuppressElement extends AbstractTestCase {
033:
034: public TestSuppressElement(String testName) {
035: super (testName);
036: }
037:
038: public void testSuppressNothing() throws Exception {
039: PersonBean angLee = new NullPersonBean("Ang", "Lee");
040: MovieBean movie = new MovieBean(
041: "Crouching Tiger, Hidden Dragon", 2000, angLee);
042: movie.addActor(new NullPersonBean("Yun-Fat", "Chow"));
043: movie.addActor(new PersonBean("Michelle", "Yeoh"));
044: movie.addActor(new PersonBean("Ziyi", "Zhang"));
045:
046: StringWriter out = new StringWriter();
047: out.write("<?xml version='1.0'?>");
048: BeanWriter writer = new BeanWriter(out);
049: writer.getBindingConfiguration().setMapIDs(false);
050: writer.write(movie);
051:
052: String expected = "<?xml version='1.0'?>" + "<movie>"
053: + " <name>Crouching Tiger, Hidden Dragon</name>"
054: + " <year>2000</year>" + " <director>"
055: + " <forenames>Ang</forenames>"
056: + " <surname>Lee</surname>" + " </director>"
057: + " <actors>" + " <actor>"
058: + " <forenames>Yun-Fat</forenames>"
059: + " <surname>Chow</surname>"
060: + " </actor>" + " <actor>"
061: + " <forenames>Michelle</forenames>"
062: + " <surname>Yeoh</surname>"
063: + " </actor>" + " <actor>"
064: + " <forenames>Ziyi</forenames>"
065: + " <surname>Zhang</surname>"
066: + " </actor>" + " </actors>" + "</movie>";
067:
068: xmlAssertIsomorphicContent(parseString(expected),
069: parseString(out));
070: }
071:
072: public void testSuppressType() throws Exception {
073: PersonBean angLee = new NullPersonBean("Ang", "Lee");
074: MovieBean movie = new MovieBean(
075: "Crouching Tiger, Hidden Dragon", 2000, angLee);
076: movie.addActor(new NullPersonBean("Yun-Fat", "Chow"));
077: movie.addActor(new PersonBean("Michelle", "Yeoh"));
078: movie.addActor(new PersonBean("Ziyi", "Zhang"));
079:
080: StringWriter out = new StringWriter();
081: out.write("<?xml version='1.0'?>");
082: BeanWriter writer = new BeanWriter(out);
083: writer.getBindingConfiguration().setMapIDs(false);
084: writer.getBindingConfiguration().setValueSuppressionStrategy(
085: new ValueSuppressionStrategy() {
086:
087: public boolean suppressAttribute(
088: AttributeDescriptor attributeDescriptor,
089: String value) {
090: return DEFAULT.suppressAttribute(
091: attributeDescriptor, value);
092: }
093:
094: public boolean suppressElement(
095: ElementDescriptor element,
096: String namespaceUrl, String localName,
097: String qualifiedName, Object value) {
098: // suppress NullPersonBean's
099: boolean result = false;
100: if (value instanceof NullPersonBean) {
101: result = true;
102: }
103: return result;
104: }
105: });
106: writer.write(movie);
107:
108: String expected = "<?xml version='1.0'?>" + "<movie>"
109: + " <name>Crouching Tiger, Hidden Dragon</name>"
110: + " <year>2000</year>" + " <actors>"
111: + " <actor>"
112: + " <forenames>Michelle</forenames>"
113: + " <surname>Yeoh</surname>"
114: + " </actor>" + " <actor>"
115: + " <forenames>Ziyi</forenames>"
116: + " <surname>Zhang</surname>"
117: + " </actor>" + " </actors>" + "</movie>";
118:
119: xmlAssertIsomorphicContent(parseString(expected),
120: parseString(out));
121: }
122:
123: public void testSuppressElementName() throws Exception {
124: PersonBean angLee = new NullPersonBean("Ang", "Lee");
125: MovieBean movie = new MovieBean(
126: "Crouching Tiger, Hidden Dragon", 2000, angLee);
127: movie.addActor(new NullPersonBean("Yun-Fat", "Chow"));
128: movie.addActor(new PersonBean("Michelle", "Yeoh"));
129: movie.addActor(new PersonBean("Ziyi", "Zhang"));
130:
131: StringWriter out = new StringWriter();
132: out.write("<?xml version='1.0'?>");
133: BeanWriter writer = new BeanWriter(out);
134: writer.getBindingConfiguration().setMapIDs(false);
135: writer.getBindingConfiguration().setValueSuppressionStrategy(
136: new ValueSuppressionStrategy() {
137:
138: public boolean suppressAttribute(
139: AttributeDescriptor attributeDescriptor,
140: String value) {
141: return DEFAULT.suppressAttribute(
142: attributeDescriptor, value);
143: }
144:
145: public boolean suppressElement(
146: ElementDescriptor element,
147: String namespaceUrl, String localName,
148: String qualifiedName, Object value) {
149: // suppress NullPersonBean's
150: boolean result = false;
151: if ("year".equals(element.getQualifiedName())) {
152: result = true;
153: }
154: return result;
155: }
156: });
157: writer.write(movie);
158:
159: String expected = "<?xml version='1.0'?>" + "<movie>"
160: + " <name>Crouching Tiger, Hidden Dragon</name>"
161: + " <director>"
162: + " <forenames>Ang</forenames>"
163: + " <surname>Lee</surname>" + " </director>"
164: + " <actors>" + " <actor>"
165: + " <forenames>Yun-Fat</forenames>"
166: + " <surname>Chow</surname>"
167: + " </actor>" + " <actor>"
168: + " <forenames>Michelle</forenames>"
169: + " <surname>Yeoh</surname>"
170: + " </actor>" + " <actor>"
171: + " <forenames>Ziyi</forenames>"
172: + " <surname>Zhang</surname>"
173: + " </actor>" + " </actors>" + "</movie>";
174:
175: xmlAssertIsomorphicContent(parseString(expected),
176: parseString(out));
177: }
178:
179: public void testSuppressName() throws Exception {
180: PersonBean angLee = new NullPersonBean("Ang", "Lee");
181: MovieBean movie = new MovieBean(
182: "Crouching Tiger, Hidden Dragon", 2000, angLee);
183: movie.addActor(new NullPersonBean("Yun-Fat", "Chow"));
184: movie.addActor(new PersonBean("Michelle", "Yeoh"));
185: movie.addActor(new PersonBean("Ziyi", "Zhang"));
186:
187: StringWriter out = new StringWriter();
188: out.write("<?xml version='1.0'?>");
189: BeanWriter writer = new BeanWriter(out);
190: writer.getBindingConfiguration().setMapIDs(false);
191: writer.getBindingConfiguration().setValueSuppressionStrategy(
192: new ValueSuppressionStrategy() {
193:
194: public boolean suppressAttribute(
195: AttributeDescriptor attributeDescriptor,
196: String value) {
197: return DEFAULT.suppressAttribute(
198: attributeDescriptor, value);
199: }
200:
201: public boolean suppressElement(
202: ElementDescriptor element,
203: String namespaceUrl, String localName,
204: String qualifiedName, Object value) {
205: // suppress NullPersonBean's
206: boolean result = false;
207: if ("actor".equals(qualifiedName)) {
208: result = true;
209: }
210: return result;
211: }
212: });
213: writer.write(movie);
214:
215: String expected = "<?xml version='1.0'?>" + "<movie>"
216: + " <name>Crouching Tiger, Hidden Dragon</name>"
217: + " <year>2000</year>" + " <director>"
218: + " <forenames>Ang</forenames>"
219: + " <surname>Lee</surname>" + " </director>"
220: + " <actors>" + " </actors>" + "</movie>";
221:
222: xmlAssertIsomorphicContent(parseString(expected),
223: parseString(out));
224: }
225: }
|