01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */package org.apache.cxf.jaxb;
19:
20: import javax.xml.bind.JAXBElement;
21: import javax.xml.namespace.QName;
22:
23: import org.apache.commons.lang.builder.ToStringBuilder;
24:
25: import org.junit.Assert;
26: import org.junit.Test;
27:
28: public class JAXBElementToStringStyleTest {
29:
30: class Holder {
31: String name = "HolderName";
32: Object obj;
33:
34: Holder(Object o) {
35: this .obj = o;
36: }
37: }
38:
39: JAXBElement<String> nel = new JAXBElement<String>(new QName("ab",
40: "bv"), String.class, "SomeText");
41:
42: Holder h = new Holder(nel);
43:
44: @Test
45: public void testToStringDefault() throws Exception {
46:
47: String ts = ToStringBuilder.reflectionToString(h);
48:
49: validateHolderString(ts);
50:
51: // JAXBElement contents not present
52: Assert.assertTrue("has no value", ts.indexOf("value") == -1);
53: Assert.assertTrue("has no bv", ts.indexOf("bv") == -1);
54:
55: }
56:
57: @Test
58: public void testToStringMultiLineStyle() throws Exception {
59: String ts = ToStringBuilder.reflectionToString(h,
60: JAXBToStringStyle.MULTI_LINE_STYLE);
61:
62: validateHolderString(ts);
63: validateElementString(ts);
64: }
65:
66: @Test
67: public void testToStringSimpleStyle() throws Exception {
68: String ts = ToStringBuilder.reflectionToString(h,
69: JAXBToStringStyle.SIMPLE_STYLE);
70:
71: // field names are missing
72: Assert.assertTrue("has no obj field", ts.indexOf("obj") == -1);
73: Assert.assertTrue("has HolderName",
74: ts.indexOf("HolderName") != -1);
75: Assert.assertTrue("has SomeText", ts.indexOf("SomeText") != -1);
76: }
77:
78: private void validateHolderString(String ts) {
79: Assert.assertTrue("has HolderName",
80: ts.indexOf("HolderName") != -1);
81: Assert.assertTrue("has JAXBElement",
82: ts.indexOf("JAXBElement") != -1);
83: Assert.assertTrue("has obj", ts.indexOf("obj") != -1);
84: }
85:
86: private void validateElementString(String ts) {
87: Assert.assertTrue("has value", ts.indexOf("value") != -1);
88: Assert.assertTrue("has scope", ts.indexOf("scope") != -1);
89: Assert.assertTrue("has bv", ts.indexOf("bv") != -1);
90:
91: }
92:
93: }
|