Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JAX-WS: JAXB: backup with parent namespace test #30566

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2024 IBM Corporation and others.
* Copyright (c) 2019, 2025 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -24,6 +24,7 @@
@RunWith(Suite.class)
@SuiteClasses({
LibertyJAXBTest.class,
LibertyJAXBSpecTest.class,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copyright needs to be

2019, 2025

ThirdPartyJAXBFromJDKTest.class,
ThirdPartyJAXBFromAppTest.class,
JAXBToolsTest.class
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*******************************************************************************
* Copyright (c) 2025 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package com.ibm.ws.jaxb.fat;

import static org.junit.Assert.assertTrue;

import java.io.BufferedReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.ibm.websphere.simplicity.ShrinkHelper;

import componenttest.annotation.Server;
import componenttest.annotation.SkipForRepeat;
import componenttest.custom.junit.runner.FATRunner;
import componenttest.topology.impl.LibertyServer;
import componenttest.topology.utils.HttpUtils;

@RunWith(FATRunner.class)
public class LibertyJAXBSpecTest {

private static final String APP_NAME = "jaxbSpecApp";

@Server("jaxbspec_fat")
public static LibertyServer server;

private static final int CONN_TIMEOUT = 5;

@BeforeClass
public static void setUp() throws Exception {
ShrinkHelper.defaultDropinApp(server, APP_NAME, "jaxb.web", "jaxb.xmlnsform.unqualified", "jaxb.xmlnsform.qualified", "jaxb.xmlnsform.servlet");
}

@AfterClass
public static void tearDown() throws Exception {
server.stopServer();
}

@After
public void afterTest() throws Exception {
server.stopServer();
}

/*
* Test com.sun.xml.bind.backupWithParentNamespace which is introduced to JAXB 2.3.9
* Skipping EE9 and EE10 because they don't use this JAXB version.
* This property doesn't work for them.
*/
@Test
@SkipForRepeat({ SkipForRepeat.EE9_FEATURES, SkipForRepeat.EE10_FEATURES })
public void testBackupWithParentNamespaceTrue_Servlet() throws Exception {
Map<String, String> map = server.getJvmOptionsAsMap();
map.put("-Dcom.sun.xml.bind.backupWithParentNamespace", "true");
server.setJvmOptions(map);
server.startServer();
server.waitForStringInLog("CWWKZ0001I:.*" + APP_NAME);

String testResultString = runTestServlet();
if (testResultString != null) {
assertTrue("When backupWithParentNamespace property is applied, an element with a prefix from a XmlNsForm.QUALIFIED package should not be null",
testResultString.contains("qpwp:value")); // qpwp: Qualified Person With Prefix
assertTrue("When backupWithParentNamespace property is applied, an element without a prefix from a XmlNsForm.QUALIFIED package should not be null",
testResultString.contains("qpnp:value")); // qpnp: Qualified Person No Prefix
assertTrue("When backupWithParentNamespace property is applied, an element with a prefix from a XmlNsForm.UNQUALIFIED package should be null",
testResultString.contains("upwp:null")); // upwp: Unqualified Person With Prefix
assertTrue("When backupWithParentNamespace property is applied, an element without a prefix from a XmlNsForm.UNQUALIFIED package should not be null",
testResultString.contains("upnp:value")); // upnp: Unqualified Person No Prefix
}
}

/*
* Tests JAXB specs to see if ElementFormDefault setting is honored
*/
@Test
public void testBackupWithParentNamespaceFalse() throws Exception {
Map<String, String> map = server.getJvmOptionsAsMap();
map.clear();
server.setJvmOptions(map);
server.startServer();
server.waitForStringInLog("CWWKZ0001I:.*" + APP_NAME);

String testResultString = runTestServlet();
if (testResultString != null) {
assertTrue("Violation of JAXB spec, an element with a prefix from a XmlNsForm.QUALIFIED package should not be null",
testResultString.contains("qpwp:value")); // qpwp: Qualified Person With Prefix
assertTrue("Violation of JAXB spec, an element without a prefix from a XmlNsForm.QUALIFIED package should be null",
testResultString.contains("qpnp:null")); // qpnp: Qualified Person No Prefix
assertTrue("Violation of JAXB spec, an element with a prefix from a XmlNsForm.UNQUALIFIED package should be null",
testResultString.contains("upwp:null")); // upwp: Unqualified Person With Prefix
assertTrue("Violation of JAXB spec, an element without a prefix from a XmlNsForm.UNQUALIFIED package should not be null",
testResultString.contains("upnp:value")); // upnp: Unqualified Person No Prefix
}
}

/*
* Runs the test servlet and return all results in a string with specific keys
*/
private String runTestServlet() throws Exception {
server.copyFileToLibertyServerRoot("Person.xml"); //Read the XML file to unmarshall

String servletUrl = new StringBuilder("http://").append(server.getHostname())
.append(":")
.append(server.getHttpDefaultPort())
.append("/")
.append(APP_NAME)
.append("/JaxbSpecTest")
.toString(); // Build servlet url
HttpURLConnection con = HttpUtils.getHttpConnection(new URL(servletUrl), HttpURLConnection.HTTP_OK, CONN_TIMEOUT);
BufferedReader br = HttpUtils.getConnectionStream(con);
String result = br.readLine();
if (result != null && result.contains("XML not found")) {
assertTrue("Person.xml file is not found", false);
result = null;
}
return result;
}
}
4 changes: 4 additions & 0 deletions dev/com.ibm.ws.jaxb_fat/publish/files/Person.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<ns:Person xmlns:ns="http://a">
<ns:firstNameWithPrefix>John</ns:firstNameWithPrefix>
<LastNameWithOutPrefix>Doe</LastNameWithOutPrefix>
</ns:Person>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bootstrap.include=../testports.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<server>
<featureManager>
<feature>servlet-3.1</feature>
<feature>componenttest-1.0</feature>
<feature>jaxb-2.2</feature>
</featureManager>

<include location="../fatTestPorts.xml"/>
</server>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*******************************************************************************
* Copyright (c) 2025 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/

package jaxb.xmlnsform.qualified;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Person", namespace = "http://a")
@XmlAccessorType(XmlAccessType.FIELD)
public class Person {
@XmlElement
private String firstNameWithPrefix;

@XmlElement
private String LastNameWithOutPrefix;

public String getFirstNameWithPrefix() {
return firstNameWithPrefix;
}

public void setFirstNameWithPrefix(String firstNameWithPrefix) {
this.firstNameWithPrefix = firstNameWithPrefix;
}

public String getLastNameWithOutPrefix() {
return LastNameWithOutPrefix;
}

public void setLastNameWithOutPrefix(String lastNameWithOutPrefix) {
LastNameWithOutPrefix = lastNameWithOutPrefix;
}

@Override
public String toString() {
return "Person{nameWithPrefix='" + firstNameWithPrefix + "', nameWithOutPrefix=" + LastNameWithOutPrefix + '}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*******************************************************************************
* Copyright (c) 2025 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/

@javax.xml.bind.annotation.XmlSchema(namespace = "http://a", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package jaxb.xmlnsform.qualified;
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*******************************************************************************
* Copyright (c) 2025 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/

package jaxb.xmlnsform.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

/**
*
*/
@WebServlet("/JaxbSpecTest")
public class JaxbSpecTestServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doWorker(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doWorker(req, resp);
}

private void doWorker(HttpServletRequest req, HttpServletResponse resp) {
PrintWriter writer = null;
try {
writer = resp.getWriter();
File personXml = new File("Person.xml");

if (!personXml.exists()) {
writer.write("XML not found");
return;
}
JAXBContext jaxbContextQualified = JAXBContext.newInstance(jaxb.xmlnsform.qualified.Person.class);
Unmarshaller unmarshallerQualified = jaxbContextQualified.createUnmarshaller();
jaxb.xmlnsform.qualified.Person qualifiedPerson = (jaxb.xmlnsform.qualified.Person) unmarshallerQualified.unmarshal(personXml);
writeNullStatus(writer, "qpwp", qualifiedPerson.getFirstNameWithPrefix()); // qpwp: Qualified Person With Prefix
writeNullStatus(writer, "qpnp", qualifiedPerson.getLastNameWithOutPrefix()); // qpnp: Qualified Person No Prefix

JAXBContext jaxbContextUnQualified = JAXBContext.newInstance(jaxb.xmlnsform.unqualified.Person.class);
Unmarshaller unmarshallerUnQualified = jaxbContextUnQualified.createUnmarshaller();
jaxb.xmlnsform.unqualified.Person unQualifiedPerson = (jaxb.xmlnsform.unqualified.Person) unmarshallerUnQualified.unmarshal(personXml);
writeNullStatus(writer, "upwp", unQualifiedPerson.getFirstNameWithPrefix()); // upwp: Unqualified Person With Prefix
writeNullStatus(writer, "upnp", unQualifiedPerson.getLastNameWithOutPrefix()); // upnp: Unqualified Person No Prefix

} catch (Exception e) {
e.printStackTrace();
}
}

/**
*
* @param key key to mark which assertion it's related to
* @param value value to test if it's null
* @return
*/
private void writeNullStatus(PrintWriter writer, String key, String value) {
writer.write(key + (value == null ? ":null |" : ":value |"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*******************************************************************************
* Copyright (c) 2025 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/

package jaxb.xmlnsform.unqualified;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Person", namespace = "http://a")
@XmlAccessorType(XmlAccessType.FIELD)
public class Person {
@XmlElement
private String firstNameWithPrefix;

@XmlElement
private String LastNameWithOutPrefix;

public String getFirstNameWithPrefix() {
return firstNameWithPrefix;
}

public void setFirstNameWithPrefix(String firstNameWithPrefix) {
this.firstNameWithPrefix = firstNameWithPrefix;
}

public String getLastNameWithOutPrefix() {
return LastNameWithOutPrefix;
}

public void setLastNameWithOutPrefix(String lastNameWithOutPrefix) {
LastNameWithOutPrefix = lastNameWithOutPrefix;
}

@Override
public String toString() {
return "Person{nameWithPrefix='" + firstNameWithPrefix + "', nameWithOutPrefix=" + LastNameWithOutPrefix + '}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*******************************************************************************
* Copyright (c) 2025 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/

@javax.xml.bind.annotation.XmlSchema(namespace = "http://a")
package jaxb.xmlnsform.unqualified;