blob: 258ab19914a297dd5a1d023d1d83b6a2b499c4af [file] [log] [blame]
/*
* Copyright (c) 2011, 2021 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 v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
// Contributors:
// Matt MacIvor - 2.3 - initial implementation
package org.eclipse.persistence.testing.jaxb.binder.nullpolicy;
import java.io.StringReader;
import jakarta.xml.bind.Binder;
import jakarta.xml.bind.JAXBContext;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
import org.eclipse.persistence.platform.xml.XMLParser;
import org.eclipse.persistence.platform.xml.XMLPlatform;
import org.eclipse.persistence.platform.xml.XMLPlatformFactory;
import org.eclipse.persistence.testing.jaxb.JAXBXMLComparer;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import junit.framework.TestCase;
public class BinderWithNullPolicyTestCases extends TestCase{
private XMLParser parser;
public BinderWithNullPolicyTestCases() {
XMLPlatform platform = XMLPlatformFactory.getInstance().getXMLPlatform();
parser = platform.newXMLParser();
}
public void testAbsentNode() throws Exception {
String xml = "<employee><!-- Comment 1 --><name>Matt</name><age>32</age><!-- Comment 2 --><address>Kanata</address><gender>MALE</gender></employee>";
String controlSource = "org/eclipse/persistence/testing/jaxb/binder/nullpolicy/absentnode.xml";
Document controlDocument = parser.parse(Thread.currentThread().getContextClassLoader().getResource(controlSource));
JAXBContext ctx = JAXBContextFactory.createContext(new Class[]{EmployeeA.class}, null);
Binder<Node> binder = ctx.createBinder();
EmployeeA emp = (EmployeeA)binder.unmarshal(parser.parse(new StringReader(xml)));
emp.setName(null);
emp.setGender(null);
binder.updateXML(emp);
JAXBXMLComparer comparer = new JAXBXMLComparer();
assertTrue("Marshalled document does not match the control document.", comparer.isNodeEqual(controlDocument, binder.getXMLNode(emp).getOwnerDocument()));
}
public void testXsiNilNullMarshal() throws Exception {
String xml = "<employee><!-- Comment 1 --><name>Matt</name><age>32</age><!-- Comment 2 --><address>Kanata</address><gender>MALE</gender></employee>";
String controlSource = "org/eclipse/persistence/testing/jaxb/binder/nullpolicy/xsinil.xml";
Document controlDocument = parser.parse(Thread.currentThread().getContextClassLoader().getResource(controlSource));
JAXBContext ctx = JAXBContextFactory.createContext(new Class[]{EmployeeB.class}, null);
Binder<Node> binder = ctx.createBinder();
EmployeeB emp = (EmployeeB)binder.unmarshal(parser.parse(new StringReader(xml)));
emp.setName(null);
emp.setGender(null);
binder.updateXML(emp);
JAXBXMLComparer comparer = new JAXBXMLComparer();
assertTrue("Marshalled document does not match the control document.", comparer.isNodeEqual(controlDocument, binder.getXMLNode(emp).getOwnerDocument()));
}
public void testXsiNilUnmarshalMarshalValue() throws Exception {
String xml = "<employee><!-- Comment 1 --><name xmlns:xsi=\"" + javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI + "\" xsi:nil=\"true\">Matt</name><age>32</age><!-- Comment 2 --><address>Kanata</address><gender xmlns:xsi=\"" + javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI + "\" xsi:nil=\"true\">MALE</gender></employee>";
String controlSource = "org/eclipse/persistence/testing/jaxb/binder/nullpolicy/nilwithvalue.xml";
Document controlDocument = parser.parse(Thread.currentThread().getContextClassLoader().getResource(controlSource));
JAXBContext ctx = JAXBContextFactory.createContext(new Class[]{EmployeeB.class}, null);
Binder<Node> binder = ctx.createBinder();
EmployeeB emp = (EmployeeB)binder.unmarshal(parser.parse(new StringReader(xml)));
emp.setName("Matt");
emp.setGender(Gender.MALE);
binder.updateXML(emp);
JAXBXMLComparer comparer = new JAXBXMLComparer();
assertTrue("Marshalled document does not match the control document.", comparer.isNodeEqual(controlDocument, binder.getXMLNode(emp).getOwnerDocument()));
}
public void testEmptyNode() throws Exception {
String xml = "<employee><!-- Comment 1 --><name>Matt</name><age>32</age><!-- Comment 2 --><address>Kanata</address><gender>MALE</gender></employee>";
String controlSource = "org/eclipse/persistence/testing/jaxb/binder/nullpolicy/emptynode.xml";
Document controlDocument = parser.parse(Thread.currentThread().getContextClassLoader().getResource(controlSource));
JAXBContext ctx = JAXBContextFactory.createContext(new Class[]{EmployeeC.class}, null);
Binder<Node> binder = ctx.createBinder();
EmployeeC emp = (EmployeeC)binder.unmarshal(parser.parse(new StringReader(xml)));
emp.setName(null);
emp.setGender(null);
binder.updateXML(emp);
JAXBXMLComparer comparer = new JAXBXMLComparer();
assertTrue("Marshalled document does not match the control document.", comparer.isNodeEqual(controlDocument, binder.getXMLNode(emp).getOwnerDocument()));
}
}