aboutsummaryrefslogtreecommitdiffstats
blob: 4516832995028c2599fbe27c4d74320631079ef8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*******************************************************************************
 * Copyright (c) 1998, 2012 Oracle and/or its affiliates. All rights reserved.
 * This program and the accompanying materials are made available under the 
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 
 * which accompanies this distribution. 
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at 
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *     Denise Smith - 2.4 - May 2012
 ******************************************************************************/  
package org.eclipse.persistence.testing.jaxb.json.emptyroot;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.namespace.QName;
import javax.xml.transform.stream.StreamSource;

import org.eclipse.persistence.jaxb.BeanValidationMode;
import org.eclipse.persistence.jaxb.JAXBContext;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
import org.eclipse.persistence.jaxb.MarshallerProperties;
import org.eclipse.persistence.jaxb.UnmarshallerProperties;
import org.eclipse.persistence.oxm.XMLConstants;
import org.eclipse.persistence.testing.oxm.OXTestCase;

public class EmptyNullMarshalUnmarshalTestCases extends OXTestCase{

    private static final String EMPTY_JSON = "{}";    
	private JAXBContext ctx;
	private JAXBContext ctxNoRoot;
    
	public EmptyNullMarshalUnmarshalTestCases(String name) throws Exception {
		super(name);		
	}

	public void setUp() throws Exception{
		super.setUp();
		Map<String, Object> props = new HashMap<String, Object>();
		props.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
        props.put(JAXBContextProperties.BEAN_VALIDATION_MODE, BeanValidationMode.NONE);

		ctx = (JAXBContext) JAXBContextFactory.createContext(new Class[]{Root.class}, props);
		ctxNoRoot = (JAXBContext) JAXBContextFactory.createContext(new Class[]{RootNoXmlRootElement.class}, props);
	}

	public void testJAXBElementNullValueIncludeRootTrue() throws Exception{
		StringWriter sw = new StringWriter();
		JAXBElement obj = new JAXBElement<Object>(new QName(XMLConstants.EMPTY_STRING),Object.class, null );
		Marshaller m = ctx.createMarshaller();
		m.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true);
		m.marshal(obj, sw);
		compareStrings("testJAXBElementNullValueIncludeRootTrue", EMPTY_JSON, sw.getBuffer().toString());
	}

	public void testJAXBElementNullValueIncludeRootFalse() throws Exception{
		StringWriter sw = new StringWriter();
		JAXBElement obj = new JAXBElement<Object>(new QName(XMLConstants.EMPTY_STRING),Object.class, null );

		Marshaller m = ctx.createMarshaller();
		m.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
		m.marshal(obj, sw);
		compareStrings("testJAXBElementNullValueIncludeRootFalse", EMPTY_JSON, sw.getBuffer().toString());
	}

	public void testJAXBElementEmptyValueIncludeRootTrue() throws Exception{
		StringWriter sw = new StringWriter();
		JAXBElement obj = new JAXBElement<Object>(new QName(XMLConstants.EMPTY_STRING),Object.class, new Root() );
		Marshaller m = ctx.createMarshaller();
		m.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true);
		m.marshal(obj, sw);
		String expectedString = "{\"type\":\"root\"}";
		compareStrings("testJAXBElementEmptyValueIncludeRootTrue", expectedString, sw.getBuffer().toString());
	}

	public void testJAXBElementEmptyValueIncludeRootFalse() throws Exception{
		StringWriter sw = new StringWriter();
		JAXBElement obj = new JAXBElement<Object>(new QName(XMLConstants.EMPTY_STRING),Object.class, new Root() );

		Marshaller m = ctx.createMarshaller();
		m.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
		m.marshal(obj, sw);
		String expectedString = "{\"type\":\"root\"}";
		compareStrings("testJAXBElementEmptyValueIncludeRootFalse", expectedString, sw.getBuffer().toString());
	}

	public void testJAXBElementEmptyValueNoRootIncludeRootTrue() throws Exception{
		StringWriter sw = new StringWriter();
		JAXBElement obj = new JAXBElement<Object>(new QName(XMLConstants.EMPTY_STRING),Object.class, new RootNoXmlRootElement() );
		Marshaller m = ctxNoRoot.createMarshaller();
		m.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true);
		m.marshal(obj, sw);
		String expectedString = "{\"type\":\"rootNoXmlRootElement\"}";
		compareStrings("testJAXBElementEmptyValueNoRootIncludeRootTrue", expectedString, sw.getBuffer().toString());
	}

	public void testJAXBElementEmptyValueNoRootIncludeRootFalse() throws Exception{
		StringWriter sw = new StringWriter();
		JAXBElement obj = new JAXBElement<Object>(new QName(XMLConstants.EMPTY_STRING),Object.class, new RootNoXmlRootElement() );

		Marshaller m = ctxNoRoot.createMarshaller();
		m.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
		m.marshal(obj, sw);
		String expectedString = "{\"type\":\"rootNoXmlRootElement\"}";
		compareStrings("testJAXBElementEmptyValueNoRootIncludeRootFalse", expectedString, sw.getBuffer().toString());
	}

	public void testEmptyIncludeRootTrue() throws Exception{
		StringWriter sw = new StringWriter();
		Object obj = new Root();
		Marshaller m = ctx.createMarshaller();
		m.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true);
		m.marshal(obj, sw);
		String expected = "{\"root\":{}}";
		compareStrings("testEmptyIncludeRootTrue", expected, sw.getBuffer().toString());
	}

	public void testEmptyIncludeRootFalse() throws Exception{
		StringWriter sw = new StringWriter();
		Object obj = new Root();
		Marshaller m = ctx.createMarshaller();
		m.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
		m.marshal(obj, sw);
		compareStrings("testEmptyIncludeRootFalse", EMPTY_JSON, sw.getBuffer().toString());
	}

	public void testEmptyNoRootIncludeRootTrue() throws Exception{
		StringWriter sw = new StringWriter();
		Object obj = new RootNoXmlRootElement();
		Marshaller m = ctxNoRoot.createMarshaller();
		m.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true);
		m.marshal(obj, sw);
		compareStrings("testEmptyNoRootIncludeRootTrue", EMPTY_JSON, sw.getBuffer().toString());
	}

	public void testEmptyNoRootIncludeRootFalse() throws Exception{
		StringWriter sw = new StringWriter();
		Object obj = new RootNoXmlRootElement();
		Marshaller m = ctxNoRoot.createMarshaller();
		m.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
		m.marshal(obj, sw);
		compareStrings("testEmptyNoRootIncludeRootFalse", EMPTY_JSON, sw.getBuffer().toString());
	}
	
	public void testListJAXBElementIncludeRootTrue() throws Exception{
		StringWriter sw = new StringWriter();		
		JAXBElement obj = new JAXBElement<Object>(new QName(XMLConstants.EMPTY_STRING),Object.class, new Root() );
		JAXBElement obj2 = new JAXBElement<Object>(new QName(XMLConstants.EMPTY_STRING),Object.class, new Root() );
		List theList = new ArrayList();
		theList.add(obj);
		theList.add(obj2);
		Marshaller m = ctx.createMarshaller();
		m.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true);
		m.marshal(theList, sw);
		String expectedString = "[{\"type\":\"root\"},{\"type\":\"root\"}]";
		compareStrings("testJAXBElementEmptyValueIncludeRootTrue", expectedString, sw.getBuffer().toString());
	}
	
	public void testListJAXBElementIncludeRootFalse() throws Exception{
		StringWriter sw = new StringWriter();		
		JAXBElement obj = new JAXBElement<Object>(new QName(XMLConstants.EMPTY_STRING),Object.class, new Root() );
		JAXBElement obj2 = new JAXBElement<Object>(new QName(XMLConstants.EMPTY_STRING),Object.class, new Root() );
		List theList = new ArrayList();
		theList.add(obj);
		theList.add(obj2);
		
		Marshaller m = ctx.createMarshaller();
		m.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
		m.marshal(theList, sw);
		String expectedString = "[{\"type\":\"root\"},{\"type\":\"root\"}]";
		compareStrings("testJAXBElementEmptyValueIncludeRootFalse", expectedString, sw.getBuffer().toString());
	}
	
	public void testUnmarshalIncludeRootTrue() throws Exception{
		Unmarshaller unmarshaller = ctx.createUnmarshaller();
		unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, true);
		StringReader reader = new StringReader("{}");
		Object o = unmarshaller.unmarshal(reader);
		assertNull(o);
	}
	
	public void testUnmarshalIncludeRootFalse() throws Exception{
		Unmarshaller unmarshaller = ctx.createUnmarshaller();
		unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
		StringReader reader = new StringReader("{}");
		Object o = unmarshaller.unmarshal(reader);
		assertNull(o);	
	}
	
	public void testUnmarshalClassIncludeRootTrue() throws Exception{
		Unmarshaller unmarshaller = ctx.createUnmarshaller();
		unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, true);
		StringReader reader = new StringReader("{}");
		Object o = unmarshaller.unmarshal(new StreamSource(reader), Root.class);
		assertTrue (o instanceof JAXBElement);
		JAXBElement controlObj = new JAXBElement(new QName(XMLConstants.EMPTY_STRING),Root.class, null );
		compareJAXBElementObjects(controlObj, (JAXBElement)o);
	}

	public void testUnmarshalClassIncludeRootFalse() throws Exception{
		Unmarshaller unmarshaller = ctx.createUnmarshaller();
		unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
		StringReader reader = new StringReader("{}");
		Object o = unmarshaller.unmarshal(new StreamSource(reader), Root.class);

		assertTrue (o instanceof JAXBElement);
		JAXBElement controlObj = new JAXBElement(new QName(XMLConstants.EMPTY_STRING),Root.class, new Root() );
		compareJAXBElementObjects(controlObj, (JAXBElement)o);
	}


	public void testUnmarshalNoRootTypeAttrIncludeRootFalse() throws Exception{
		Unmarshaller unmarshaller = ctxNoRoot.createUnmarshaller();
		unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
		StringReader reader = new StringReader("{\"type\":\"rootNoXmlRootElement\"}");
		StringReader reader2 = new StringReader("{\"type\":\"rootNoXmlRootElement\"}");
		Object o = unmarshaller.unmarshal(new StreamSource(reader), RootNoXmlRootElement.class);
		assertTrue (o instanceof JAXBElement);
		JAXBElement controlObj = new JAXBElement(new QName(XMLConstants.EMPTY_STRING),RootNoXmlRootElement.class, new RootNoXmlRootElement() );
		compareJAXBElementObjects(controlObj, (JAXBElement)o);		
		
		o = unmarshaller.unmarshal(new StreamSource(reader2));
		assertTrue (o instanceof JAXBElement);
		controlObj = new JAXBElement(new QName(XMLConstants.EMPTY_STRING), Object.class, new RootNoXmlRootElement() );
		compareJAXBElementObjects(controlObj, (JAXBElement)o);
	}
	
	public void testUnmarshalTypeAttrIncludeRootFalse() throws Exception{
		Unmarshaller unmarshaller = ctx.createUnmarshaller();
		unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
		StringReader reader = new StringReader("{\"type\":\"root\"}");
		StringReader reader2 = new StringReader("{\"type\":\"root\"}");
		Object o = unmarshaller.unmarshal(new StreamSource(reader), Root.class);

		assertTrue (o instanceof JAXBElement);
		JAXBElement controlObj = new JAXBElement(new QName(XMLConstants.EMPTY_STRING),Root.class, new Root() );
		compareJAXBElementObjects(controlObj, (JAXBElement)o);
		
		Object o2 = unmarshaller.unmarshal(new StreamSource(reader2));
		assertTrue (o instanceof JAXBElement);
		controlObj = new JAXBElement(new QName(XMLConstants.EMPTY_STRING),Root.class, new Root() );
		compareJAXBElementObjects(controlObj, (JAXBElement)o);

	}

	
	public void testUnmarshalEmptyListIncludeRootTrue() throws Exception{
		Unmarshaller unmarshaller = ctx.createUnmarshaller();
		unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, true);
		StringReader reader = new StringReader("[]");
		Object o = unmarshaller.unmarshal(new StreamSource(reader));
		assertEquals(new ArrayList(), o);
	}
	
	public void testUnmarshalEmptyListIncludeRootFalse() throws Exception{
		Unmarshaller unmarshaller = ctx.createUnmarshaller();
		unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
		StringReader reader = new StringReader("[]");
		Object o = unmarshaller.unmarshal(new StreamSource(reader));
		assertEquals(new ArrayList(), o);
	}
	
	public void testUnmarshalEmptyListObjectIncludeRootTrue() throws Exception{
		Unmarshaller unmarshaller = ctx.createUnmarshaller();
		unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, true);
		StringReader reader = new StringReader("[{}]");
		Object o = unmarshaller.unmarshal(new StreamSource(reader));
		
		assertTrue (o instanceof List);
		assertEquals(1, ((List)o).size());
		assertNull(((List)o).get(0));

		//JAXBElement controlObj = new JAXBElement(new QName(XMLConstants.EMPTY_STRING),Object.class, null );
		//compareJAXBElementObjects(controlObj, (JAXBElement)((List)o).get(0));		
		//fail();
	}
	
	public void testUnmarshalEmptyListObjectIncludeRootFalse() throws Exception{
		Unmarshaller unmarshaller = ctx.createUnmarshaller();
		unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
		StringReader reader = new StringReader("[{}]");
		Object o = unmarshaller.unmarshal(new StreamSource(reader));
		
		assertTrue (o instanceof List);
		assertEquals(1, ((List)o).size());
		assertNull(((List)o).get(0));
		//JAXBElement controlObj = new JAXBElement(new QName(XMLConstants.EMPTY_STRING),Object.class, null );
		//compareJAXBElementObjects(controlObj, (JAXBElement)((List)o).get(0));		
	}
	
	public void testUnmarshalEmptyListObjectClassIncludeRootTrue() throws Exception{
		Unmarshaller unmarshaller = ctx.createUnmarshaller();
		unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, true);
		StringReader reader = new StringReader("[{}]");
		JAXBElement o = unmarshaller.unmarshal(new StreamSource(reader), Root.class);
		//assertTrue (o instanceof JAXBElement);
		//JAXBElement controlObj = new JAXBElement(new QName(XMLConstants.EMPTY_STRING),Root.class, new Root() );
		//compareJAXBElementObjects(controlObj, (JAXBElement)o);
		
		assertTrue(o.getDeclaredType().equals(Root.class));
		assertTrue(o.getName().equals(new QName("")));
		
		Object value = o.getValue();
		assertTrue (value instanceof List);
		assertEquals(1, ((List)value).size());
		
		
		JAXBElement controlObj = new JAXBElement(new QName(XMLConstants.EMPTY_STRING),Root.class, null );
		compareJAXBElementObjects(controlObj, (JAXBElement)((List)value).get(0));	
	}
	
	public void testUnmarshalEmptyListObjectClassIncludeRootFalse() throws Exception{
		Unmarshaller unmarshaller = ctx.createUnmarshaller();
		unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
		StringReader reader = new StringReader("[{}]");
		JAXBElement o = unmarshaller.unmarshal(new StreamSource(reader),Root.class);
		
		assertTrue(o.getDeclaredType().equals(Root.class));
		assertTrue(o.getName().equals(new QName("")));
		
		Object value = o.getValue();
		assertTrue (value instanceof List);
		assertEquals(1, ((List)value).size());

		assertTrue(((List)value).get(0) instanceof Root);
		assertNull(((Root)((List)value).get(0)).something);
	}
	
	protected void compareStrings(String test, String expectedString, String testString) {
	    log(test);
	    log("Expected (With All Whitespace Removed):");
	    log(expectedString);
	    log("\nActual (With All Whitespace Removed):");
	    testString = testString.replaceAll("[ \b\t\n\r]", "");
	    log(testString);
	    assertEquals(expectedString, testString);
	}
}