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