Skip to content

Commit

Permalink
JAXB Legacy behavior support test is added
Browse files Browse the repository at this point in the history
  • Loading branch information
BerksanAtes committed Jan 15, 2025
1 parent 854ae75 commit d3274f0
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@
*******************************************************************************/
package com.ibm.ws.jaxb.fat;

import org.junit.ClassRule;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

import componenttest.rules.repeater.JakartaEE10Action;
import componenttest.rules.repeater.JakartaEE9Action;
import componenttest.rules.repeater.RepeatTests;

@RunWith(Suite.class)
@SuiteClasses({
LibertyJAXBTest.class,
Expand All @@ -26,8 +31,8 @@
})

public class FATSuite {
// @ClassRule
// public static RepeatTests r = RepeatTests.withoutModification()
// .andWith(new JakartaEE9Action())
// .andWith(new JakartaEE10Action());
@ClassRule
public static RepeatTests r = RepeatTests.withoutModification()
.andWith(new JakartaEE9Action())
.andWith(new JakartaEE10Action());
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@
*******************************************************************************/
package com.ibm.ws.jaxb.fat;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

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

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
Expand All @@ -31,21 +28,24 @@
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 junit.framework.Assert;
import componenttest.topology.utils.HttpUtils;

@RunWith(FATRunner.class)
public class LibertyJAXBSpecTest {

private static final String APP_NAME = "jaxbApp";
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");
ShrinkHelper.defaultDropinApp(server, APP_NAME, "jaxb.web", "jaxb.xmlnsform.unqualified", "jaxb.xmlnsform.qualified", "jaxb.xmlnsform.servlet");
}

@AfterClass
Expand All @@ -58,63 +58,77 @@ 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
public void testBackupWithParentNamespaceTrue() throws Exception {
Map<String, String> map = new HashMap<String, String>();
map.put("com.sun.xml.bind.backupWithParentNamespace", "true");
// server.setJvmOptions(map);
server.addBootstrapProperties(map);
@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();

File personXml = new File(server.pathToAutoFVTTestFiles + "/Person.xml");
if (personXml.exists()) {
System.out.println("Person.xml path: " + personXml.getPath());
} else {
Assert.fail("Person.xml does not exist - " + personXml.getPath());
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
}

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);
System.out.println("~qualifiedPerson: " + qualifiedPerson);
assertNull("1", qualifiedPerson.getFirstNameWithPrefix());
assertNotNull("2", qualifiedPerson.getLastNameWithOutPrefix());

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);
System.out.println("~unQualifiedPerson: " + unQualifiedPerson);
assertNotNull("3", unQualifiedPerson.getFirstNameWithPrefix());
assertNull("4", unQualifiedPerson.getLastNameWithOutPrefix());
}

/*
* Tests JAXB specs to see if ElementFormDefault setting is honored
*/
@Test
public void testBackupWithParentNamespaceFalse() throws Exception {
// server.setJvmOptions(new HashMap<String, String>());
// server.addBootstrapProperties(new HashMap<String, String>());
Map map = (Map) server.getBootstrapProperties().remove("com.sun.xml.bind.backupWithParentNamespace");

Map<String, String> map = server.getJvmOptionsAsMap();
map.clear();
server.setJvmOptions(map);
server.startServer();

File personXml = new File(server.pathToAutoFVTTestFiles + "/Person.xml");
if (personXml.exists()) {
System.out.println("Person.xml path: " + personXml.getPath());
} else {
Assert.fail("Person.xml does not exist - " + personXml.getPath());
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
}
}

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);
System.out.println("~qualifiedPerson: " + qualifiedPerson);
assertNotNull("Violation of JAXB spec, an element with a prefix from a XmlNsForm.QUALIFIED package should not be null", qualifiedPerson.getFirstNameWithPrefix());
assertNull("Violation of JAXB spec, an element with a prefix from a XmlNsForm.QUALIFIED package should not be null", qualifiedPerson.getLastNameWithOutPrefix());

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);
System.out.println("~unQualifiedPerson: " + unQualifiedPerson);
assertNull("Violation of JAXB spec, an element with a prefix from a XmlNsForm.UNQUALIFIED package should be null", unQualifiedPerson.getFirstNameWithPrefix());
assertNotNull("Violation of JAXB spec, an element with a prefix from a XmlNsForm.UNQUALIFIED package should not be null", unQualifiedPerson.getLastNameWithOutPrefix());
/*
* 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,3 @@
# IBM Corporation - initial API and implementation
###############################################################################
bootstrap.include=../testports.properties
# This server is exempt from j2sec because it only tests third-party/JDK usage of JAX-B
# The only Liberty code involved is trivial server start and servlet GET logic
websphere.java.security.exempt=true
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
*
*/
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 |"));
}

}

0 comments on commit d3274f0

Please sign in to comment.