1 /* 2 * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package test.auctionportal; 24 25 import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; 26 import static jaxp.library.JAXPTestUtilities.USER_DIR; 27 import static jaxp.library.JAXPTestUtilities.compareDocumentWithGold; 28 import static org.testng.Assert.assertEquals; 29 import static org.testng.Assert.assertFalse; 30 import static org.testng.Assert.assertTrue; 31 import static test.auctionportal.HiBidConstants.GOLDEN_DIR; 32 import static test.auctionportal.HiBidConstants.JAXP_SCHEMA_LANGUAGE; 33 import static test.auctionportal.HiBidConstants.PORTAL_ACCOUNT_NS; 34 import static test.auctionportal.HiBidConstants.XML_DIR; 35 36 import java.io.FileOutputStream; 37 import java.nio.file.Files; 38 import java.nio.file.Paths; 39 import java.nio.file.StandardCopyOption; 40 41 import javax.xml.parsers.DocumentBuilder; 42 import javax.xml.parsers.DocumentBuilderFactory; 43 44 import org.testng.annotations.Listeners; 45 import org.testng.annotations.Test; 46 import org.w3c.dom.Attr; 47 import org.w3c.dom.Document; 48 import org.w3c.dom.Element; 49 import org.w3c.dom.NodeList; 50 import org.w3c.dom.Text; 51 import org.w3c.dom.bootstrap.DOMImplementationRegistry; 52 import org.w3c.dom.ls.DOMImplementationLS; 53 import org.w3c.dom.ls.LSParser; 54 import org.w3c.dom.ls.LSSerializer; 55 56 /** 57 * This is the user controller class for the Auction portal HiBid.com. 58 */ 59 /* 60 * @test 61 * @library /javax/xml/jaxp/libs 62 * @run testng/othervm -DrunSecMngr=true test.auctionportal.UserController 63 * @run testng/othervm test.auctionportal.UserController 64 */ 65 @Listeners({jaxp.library.FilePolicy.class}) 66 public class UserController { 67 /** 68 * Checking when creating an XML document using DOM Level 2 validating 69 * it without having a schema source or a schema location It must throw a 70 * sax parse exception. 71 * 72 * @throws Exception If any errors occur. 73 */ 74 @Test 75 public void testCreateNewUser() throws Exception { 76 String resultFile = USER_DIR + "accountInfoOut.xml"; 77 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 78 dbf.setNamespaceAware(true); 79 dbf.setValidating(true); 80 81 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 82 MyErrorHandler eh = new MyErrorHandler(); 83 docBuilder.setErrorHandler(eh); 84 85 Document document = docBuilder.newDocument(); 86 87 Element account = document.createElementNS(PORTAL_ACCOUNT_NS, "acc:Account"); 88 Attr accountID = document.createAttributeNS(PORTAL_ACCOUNT_NS, "acc:accountID"); 89 account.setAttributeNode(accountID); 90 91 account.appendChild(document.createElement("FirstName")); 92 account.appendChild(document.createElementNS(PORTAL_ACCOUNT_NS, "acc:LastName")); 93 account.appendChild(document.createElement("UserID")); 94 95 DOMImplementationLS impl 96 = (DOMImplementationLS) DOMImplementationRegistry 97 .newInstance().getDOMImplementation("LS"); 98 LSSerializer writer = impl.createLSSerializer(); 99 LSParser builder = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); 100 try(FileOutputStream output = new FileOutputStream(resultFile)) { 101 MyDOMOutput domOutput = new MyDOMOutput(); 102 domOutput.setByteStream(output); 103 writer.write(account, domOutput); 104 docBuilder.parse(resultFile); 105 } 106 assertTrue(eh.isAnyError()); 107 } 108 109 /** 110 * Checking conflicting namespaces and use renameNode and normalizeDocument. 111 * @see <a href="content/accountInfo.xml">accountInfo.xml</a> 112 * 113 * @throws Exception If any errors occur. 114 */ 115 @Test 116 public void testAddUser() throws Exception { 117 String resultFile = USER_DIR + "accountRole.out"; 118 String xmlFile = XML_DIR + "accountInfo.xml"; 119 120 // Copy schema for outputfile 121 Files.copy(Paths.get(XML_DIR, "accountInfo.xsd"), 122 Paths.get(USER_DIR, "accountInfo.xsd"), 123 StandardCopyOption.REPLACE_EXISTING); 124 MyErrorHandler eh = new MyErrorHandler(); 125 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 126 127 dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI); 128 dbf.setNamespaceAware(true); 129 dbf.setValidating(true); 130 131 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 132 docBuilder.setErrorHandler(eh); 133 134 Document document = docBuilder.parse(xmlFile); 135 Element sell = (Element) document.getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "Sell").item(0); 136 Element role = (Element) sell.getParentNode(); 137 138 Element buy = (Element) document.renameNode(sell, PORTAL_ACCOUNT_NS, "acc:Buy"); 139 role.appendChild(buy); 140 141 DOMImplementationLS impl 142 = (DOMImplementationLS) DOMImplementationRegistry 143 .newInstance().getDOMImplementation("LS"); 144 LSSerializer writer = impl.createLSSerializer(); 145 146 147 try(FileOutputStream output = new FileOutputStream(resultFile)) { 148 MyDOMOutput mydomoutput = new MyDOMOutput(); 149 mydomoutput.setByteStream(output); 150 writer.write(document, mydomoutput); 151 } 152 153 docBuilder.parse(resultFile); 154 assertFalse(eh.isAnyError()); 155 } 156 157 /** 158 * Checking Text content in XML file. 159 * @see <a href="content/accountInfo.xml">accountInfo.xml</a> 160 * 161 * @throws Exception If any errors occur. 162 */ 163 @Test 164 public void testMoreUserInfo() throws Exception { 165 String xmlFile = XML_DIR + "accountInfo.xml"; 166 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 167 168 dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI); 169 dbf.setNamespaceAware(true); 170 dbf.setValidating(true); 171 172 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 173 MyErrorHandler eh = new MyErrorHandler(); 174 docBuilder.setErrorHandler(eh); 175 176 Document document = docBuilder.parse(xmlFile); 177 Element account = (Element)document 178 .getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "Account").item(0); 179 String textContent = account.getTextContent(); 180 assertTrue(textContent.trim().regionMatches(0, "Rachel", 0, 6)); 181 assertEquals(textContent, "RachelGreen744"); 182 183 Attr accountID = account.getAttributeNodeNS(PORTAL_ACCOUNT_NS, "accountID"); 184 assertTrue(accountID.getTextContent().trim().equals("1")); 185 186 assertFalse(eh.isAnyError()); 187 } 188 189 /** 190 * This will check if adoptNode works will adoptNode from 191 * @see <a href="content/userInfo.xml">userInfo.xml</a> 192 * @see <a href="content/accountInfo.xml">accountInfo.xml</a>. This is 193 * adopting a node from the XML file which is validated by a DTD and 194 * into an XML file which is validated by the schema This covers Row 5 195 * for the table 196 * http://javaweb.sfbay/~jsuttor/JSR206/jsr-206-html/ch03s05.html. Filed 197 * bug 4893745 because there was a difference in behavior. 198 * 199 * @throws Exception If any errors occur. 200 */ 201 @Test 202 public void testCreateUserAccount() throws Exception { 203 String userXmlFile = XML_DIR + "userInfo.xml"; 204 String accountXmlFile = XML_DIR + "accountInfo.xml"; 205 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 206 dbf.setNamespaceAware(true); 207 dbf.setValidating(true); 208 209 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 210 MyErrorHandler eh = new MyErrorHandler(); 211 docBuilder.setErrorHandler(eh); 212 213 Document document = docBuilder.parse(userXmlFile); 214 Element user = (Element) document.getElementsByTagName("FirstName").item(0); 215 // Set schema after parsing userInfo.xml. Otherwise it will conflict 216 // with DTD validation. 217 dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI); 218 DocumentBuilder docBuilder1 = dbf.newDocumentBuilder(); 219 docBuilder1.setErrorHandler(eh); 220 Document accDocument = docBuilder1.parse(accountXmlFile); 221 222 Element firstName = (Element) accDocument 223 .getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "FirstName").item(0); 224 Element adoptedAccount = (Element) accDocument.adoptNode(user); 225 226 Element parent = (Element) firstName.getParentNode(); 227 parent.replaceChild(adoptedAccount, firstName); 228 229 DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); 230 DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); 231 LSSerializer writer = impl.createLSSerializer(); 232 233 MyDOMOutput mydomoutput = new MyDOMOutput(); 234 mydomoutput.setByteStream(System.out); 235 236 writer.write(document, mydomoutput); 237 writer.write(accDocument, mydomoutput); 238 239 assertFalse(eh.isAnyError()); 240 } 241 242 /** 243 * Checking for Row 8 from the schema table when setting the schemaSource 244 * without the schemaLanguage must report an error. 245 * 246 * @throws Exception If any errors occur. 247 */ 248 @Test(expectedExceptions = IllegalArgumentException.class) 249 public void testUserError() throws Exception { 250 String xmlFile = XML_DIR + "userInfo.xml"; 251 String schema = "http://java.sun.com/xml/jaxp/properties/schemaSource"; 252 String schemaValue = "http://dummy.com/dummy.xsd"; 253 254 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 255 dbf.setNamespaceAware(true); 256 dbf.setValidating(true); 257 dbf.setAttribute(schema, schemaValue); 258 259 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 260 MyErrorHandler eh = new MyErrorHandler(); 261 docBuilder.setErrorHandler(eh); 262 docBuilder.parse(xmlFile); 263 assertFalse(eh.isAnyError()); 264 } 265 266 /** 267 * Checking for namespace normalization. 268 * @see <a href="content/screenName.xml">screenName.xml</a> has prefix of 269 * userName is bound to "http://hibid.com/user" namespace normalization 270 * will create a namespace of prefix us and attach userEmail. 271 * 272 * @throws Exception If any errors occur. 273 */ 274 @Test 275 public void testCheckScreenNameExists() throws Exception { 276 String resultFile = USER_DIR + "screenName.out"; 277 String xmlFile = XML_DIR + "screenName.xml"; 278 String goldFile = GOLDEN_DIR + "screenNameGold.xml"; 279 280 String nsTagName = "http://hibid.com/screenName"; 281 String userNs = "http://hibid.com/user"; 282 283 try (FileOutputStream output = new FileOutputStream(resultFile)) { 284 DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); 285 DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); 286 LSSerializer writer = impl.createLSSerializer(); 287 LSParser builder = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); 288 Document document = builder.parseURI(xmlFile); 289 NodeList nl = document.getElementsByTagNameNS(nsTagName, "screen-name"); 290 assertEquals(nl.getLength(), 1); 291 Element screenName = (Element)nl.item(0); 292 Element userEmail = document.createElementNS(userNs, "userEmail"); 293 assertTrue(userEmail.isDefaultNamespace(userNs)); 294 295 Text email = document.createTextNode("myid@hibid.com"); 296 userEmail.appendChild(email); 297 screenName.appendChild(userEmail); 298 document.normalizeDocument(); 299 300 MyDOMOutput domoutput = new MyDOMOutput(); 301 domoutput.setByteStream(output); 302 writer.write(document, domoutput); 303 } 304 assertTrue(compareDocumentWithGold(goldFile, resultFile)); 305 } 306 } 307 308