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.strategy;
018:
019: import java.io.StringReader;
020: import java.io.StringWriter;
021:
022: import org.apache.commons.betwixt.AbstractTestCase;
023: import org.apache.commons.betwixt.expression.Context;
024: import org.apache.commons.betwixt.io.BeanReader;
025: import org.apache.commons.betwixt.io.BeanWriter;
026: import org.xml.sax.InputSource;
027:
028: /**
029: */
030: public class TestIdStorageStrategy extends AbstractTestCase {
031:
032: public TestIdStorageStrategy(String testName) {
033: super (testName);
034: }
035:
036: public void testWrite() throws Exception {
037:
038: final Element alpha = new Element("ONE");
039: Element beta = new Element("TWO");
040: ElementsList elements = new ElementsList();
041: elements.addElement(alpha);
042: elements.addElement(beta);
043:
044: IdStoringStrategy storingStrategy = new DefaultIdStoringStrategy() {
045:
046: public String getReferenceFor(Context context, Object bean) {
047: String result = null;
048: if (bean == alpha) {
049: result = "ALPHA";
050: } else {
051: result = super .getReferenceFor(context, bean);
052: }
053: return result;
054: }
055:
056: public void setReference(Context context, Object bean,
057: String id) {
058: if (bean != alpha) {
059: super .setReference(context, bean, id);
060: }
061: }
062: };
063:
064: StringWriter out = new StringWriter();
065: out.write("<?xml version='1.0'?>");
066: BeanWriter writer = new BeanWriter(out);
067: writer.getBindingConfiguration().setIdMappingStrategy(
068: storingStrategy);
069: writer.write(elements);
070:
071: String expected = "<?xml version='1.0'?>"
072: + "<ElementsList id='1'>" + " <elements>"
073: + " <element idref='ALPHA'/>"
074: + " <element id='2'>"
075: + " <value>TWO</value>" + " </element>"
076: + " </elements>" + "</ElementsList>";
077:
078: xmlAssertIsomorphicContent(parseString(expected),
079: parseString(out));
080: }
081:
082: public void testRead() throws Exception {
083:
084: String xml = "<?xml version='1.0'?>" + "<ElementsList id='1'>"
085: + " <elements>" + " <element idref='ALPHA'/>"
086: + " <element id='2'>"
087: + " <value>TWO</value>" + " </element>"
088: + " </elements>" + "</ElementsList>";
089:
090: final Element alpha = new Element("ONE");
091:
092: IdStoringStrategy storingStrategy = new DefaultIdStoringStrategy() {
093:
094: public void setReference(Context context, Object bean,
095: String id) {
096: if (bean != alpha) {
097: super .setReference(context, bean, id);
098: }
099: }
100:
101: public Object getReferenced(Context context, String id) {
102: if ("ALPHA".equals(id)) {
103: return alpha;
104: }
105: return getReferenced(context, id);
106: }
107:
108: };
109:
110: BeanReader reader = new BeanReader();
111: reader.getBindingConfiguration().setIdMappingStrategy(
112: storingStrategy);
113: reader.registerBeanClass(ElementsList.class);
114: ElementsList elements = (ElementsList) reader
115: .parse(new StringReader(xml));
116: assertNotNull(elements);
117: Element one = elements.get(0);
118: assertTrue(one == alpha);
119: Element two = elements.get(1);
120: assertNotNull(two);
121: }
122:
123: public void testWriteWithOptions() throws Exception {
124:
125: final Element alpha = new Element("ONE");
126: Element beta = new Element("TWO");
127: ElementsList elements = new ElementsList();
128: elements.addElement(alpha);
129: elements.addElement(beta);
130:
131: String MAPPING = "<?xml version='1.0'?>" + "<betwixt-config>"
132: + " <class name=\"" + ElementsList.class.getName()
133: + "\">" + " <element name=\"ElementsList\">"
134: + " <option>"
135: + " <name>id-strategy-prefix</name>"
136: + " <value>alice</value>" + " </option>"
137: + " <element name=\"elements\">"
138: + " <element property=\"elements\">"
139: + " <option>"
140: + " <name>id-strategy-prefix</name>"
141: + " <value>bob</value>"
142: + " </option>" + " </element>"
143: + " </element>" + " </element>" + " </class>"
144: + "</betwixt-config>";
145:
146: IdStoringStrategy storingStrategy = new DefaultIdStoringStrategy() {
147:
148: public String getReferenceFor(Context context, Object bean) {
149: String result = null;
150: if (bean instanceof ElementsList) {
151: assertNotNull(context.getOptions());
152: assertEquals("Checking ElementsList option",
153: "alice", context.getOptions().getValue(
154: "id-strategy-prefix"));
155: }
156: if (bean instanceof Element) {
157: assertNotNull(context.getOptions());
158: assertEquals("Checking Element option", "bob",
159: context.getOptions().getValue(
160: "id-strategy-prefix"));
161: }
162: if (bean == alpha) {
163: result = "ALPHA";
164: } else {
165: result = super .getReferenceFor(context, bean);
166: }
167: return result;
168: }
169:
170: public void setReference(Context context, Object bean,
171: String id) {
172: if (bean != alpha) {
173: super .setReference(context, bean, id);
174: }
175: }
176: };
177:
178: StringWriter out = new StringWriter();
179: out.write("<?xml version='1.0'?>");
180: BeanWriter writer = new BeanWriter(out);
181: writer.getBindingConfiguration().setIdMappingStrategy(
182: storingStrategy);
183: writer.getXMLIntrospector().register(
184: new InputSource(new StringReader(MAPPING)));
185: writer.write(elements);
186:
187: String expected = "<?xml version='1.0'?>"
188: + "<ElementsList id='1'>" + " <elements>"
189: + " <Element idref='ALPHA'/>"
190: + " <Element id='2'>"
191: + " <value>TWO</value>" + " </Element>"
192: + " </elements>" + "</ElementsList>";
193:
194: xmlAssertIsomorphicContent(parseString(expected),
195: parseString(out));
196: }
197:
198: public void testWriteWithParentOptions() throws Exception {
199:
200: AlphaBean alpha = new AlphaBean();
201: alpha.setName("apple");
202: BetaBean beta = new BetaBean();
203: beta.setName("banana");
204: alpha.setBetaBean(beta);
205:
206: String MAPPING = "<?xml version='1.0'?>" + "<betwixt-config>"
207: + " <class name=\"" + AlphaBean.class.getName()
208: + "\">" + " <element name=\"alpha\">"
209: + " <element name=\"name\" property=\"name\" />"
210: + " <element property=\"betaBean\">"
211: + " <option>"
212: + " <name>id-strategy-prefix</name>"
213: + " <value>parent</value>"
214: + " </option>" + " </element>"
215: + " </element>" + " </class>" + " <class name=\""
216: + BetaBean.class.getName() + "\">"
217: + " <element name=\"beta\">"
218: + " <element name=\"name\" property=\"name\" />"
219: + " </element>" + " </class>" + "</betwixt-config>";
220:
221: IdStoringStrategy storingStrategy = new DefaultIdStoringStrategy() {
222: public String getReferenceFor(Context context, Object bean) {
223: if (bean instanceof BetaBean) {
224: assertNotNull(context.getOptions());
225: assertEquals("Checking BetaBean option", "parent",
226: context.getOptions().getValue(
227: "id-strategy-prefix"));
228: }
229: return super .getReferenceFor(context, bean);
230: }
231: };
232:
233: StringWriter out = new StringWriter();
234: out.write("<?xml version='1.0'?>");
235: BeanWriter writer = new BeanWriter(out);
236: writer.getBindingConfiguration().setIdMappingStrategy(
237: storingStrategy);
238: writer.getXMLIntrospector().register(
239: new InputSource(new StringReader(MAPPING)));
240: writer.write(alpha);
241:
242: String expected = "<?xml version='1.0'?>" + "<alpha id=\"1\">"
243: + " <name>apple</name>" + " <beta id=\"2\">"
244: + " <name>banana</name>" + " </beta>" + "</alpha>";
245:
246: xmlAssertIsomorphicContent(parseString(expected),
247: parseString(out));
248: }
249:
250: public void testWriteWithTargetOptions() throws Exception {
251:
252: AlphaBean alpha = new AlphaBean();
253: alpha.setName("apple");
254: BetaBean beta = new BetaBean();
255: beta.setName("banana");
256: alpha.setBetaBean(beta);
257:
258: String MAPPING = "<?xml version='1.0'?>" + "<betwixt-config>"
259: + " <class name=\"" + AlphaBean.class.getName()
260: + "\">" + " <element name=\"alpha\">"
261: + " <element name=\"name\" property=\"name\" />"
262: + " <element property=\"betaBean\" />"
263: + " </element>" + " </class>" + " <class name=\""
264: + BetaBean.class.getName() + "\">"
265: + " <element name=\"beta\">" + " <option>"
266: + " <name>id-strategy-prefix</name>"
267: + " <value>target</value>" + " </option>"
268: + " <element name=\"name\" property=\"name\" />"
269: + " </element>" + " </class>" + "</betwixt-config>";
270:
271: IdStoringStrategy storingStrategy = new DefaultIdStoringStrategy() {
272: public String getReferenceFor(Context context, Object bean) {
273: if (bean instanceof BetaBean) {
274: assertNotNull(context.getOptions());
275: assertEquals("Checking BetaBean option", "target",
276: context.getOptions().getValue(
277: "id-strategy-prefix"));
278: }
279: return super .getReferenceFor(context, bean);
280: }
281: };
282:
283: StringWriter out = new StringWriter();
284: out.write("<?xml version='1.0'?>");
285: BeanWriter writer = new BeanWriter(out);
286: writer.getBindingConfiguration().setIdMappingStrategy(
287: storingStrategy);
288: writer.getXMLIntrospector().register(
289: new InputSource(new StringReader(MAPPING)));
290: writer.write(alpha);
291:
292: String expected = "<?xml version='1.0'?>" + "<alpha id=\"1\">"
293: + " <name>apple</name>" + " <beta id=\"2\">"
294: + " <name>banana</name>" + " </beta>" + "</alpha>";
295:
296: xmlAssertIsomorphicContent(parseString(expected),
297: parseString(out));
298: }
299:
300: public void testWriteWithParentAndTargetOptions() throws Exception {
301:
302: AlphaBean alpha = new AlphaBean();
303: alpha.setName("apple");
304: BetaBean beta = new BetaBean();
305: beta.setName("banana");
306: alpha.setBetaBean(beta);
307:
308: String MAPPING = "<?xml version='1.0'?>" + "<betwixt-config>"
309: + " <class name=\""
310: + AlphaBean.class.getName()
311: + "\">"
312: + " <element name=\"alpha\">"
313: + " <element name=\"name\" property=\"name\" />"
314: + " <element property=\"betaBean\">"
315: + " <option>"
316: + " <name>id-strategy-prefix</name>"
317: + " <value>parent</value>"
318: + " </option>"
319: + " </element>"
320: + " </element>"
321: + " </class>"
322: + " <class name=\""
323: + BetaBean.class.getName()
324: + "\">"
325: + " <element name=\"beta\">"
326: + " <option>"
327: + " <name>id-strategy-prefix</name>"
328: + " <value>target</value>"
329: + " </option>"
330: + " <element name=\"name\" property=\"name\" />"
331: + " </element>" + " </class>" + "</betwixt-config>";
332:
333: IdStoringStrategy storingStrategy = new DefaultIdStoringStrategy() {
334: public String getReferenceFor(Context context, Object bean) {
335: if (bean instanceof BetaBean) {
336: assertNotNull(context.getOptions());
337: assertEquals("Checking BetaBean option", "parent",
338: context.getOptions().getValue(
339: "id-strategy-prefix"));
340: }
341: return super .getReferenceFor(context, bean);
342: }
343: };
344:
345: StringWriter out = new StringWriter();
346: out.write("<?xml version='1.0'?>");
347: BeanWriter writer = new BeanWriter(out);
348: writer.getBindingConfiguration().setIdMappingStrategy(
349: storingStrategy);
350: writer.getXMLIntrospector().register(
351: new InputSource(new StringReader(MAPPING)));
352: writer.write(alpha);
353:
354: String expected = "<?xml version='1.0'?>" + "<alpha id=\"1\">"
355: + " <name>apple</name>" + " <beta id=\"2\">"
356: + " <name>banana</name>" + " </beta>" + "</alpha>";
357:
358: xmlAssertIsomorphicContent(parseString(expected),
359: parseString(out));
360: }
361: }
|