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
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");
|
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 @Listeners({jaxp.library.FilePolicy.class})
60 public class UserController {
61 /**
62 * Checking when creating an XML document using DOM Level 2 validating
63 * it without having a schema source or a schema location It must throw a
64 * sax parse exception.
65 *
66 * @throws Exception If any errors occur.
67 */
68 @Test
69 public void testCreateNewUser() throws Exception {
70 String resultFile = USER_DIR + "accountInfoOut.xml";
71 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
72 dbf.setNamespaceAware(true);
73 dbf.setValidating(true);
74
75 DocumentBuilder docBuilder = dbf.newDocumentBuilder();
76 MyErrorHandler eh = new MyErrorHandler();
77 docBuilder.setErrorHandler(eh);
78
79 Document document = docBuilder.newDocument();
80
137 .newInstance().getDOMImplementation("LS");
138 LSSerializer writer = impl.createLSSerializer();
139
140
141 try(FileOutputStream output = new FileOutputStream(resultFile)) {
142 MyDOMOutput mydomoutput = new MyDOMOutput();
143 mydomoutput.setByteStream(output);
144 writer.write(document, mydomoutput);
145 }
146
147 docBuilder.parse(resultFile);
148 assertFalse(eh.isAnyError());
149 }
150
151 /**
152 * Checking Text content in XML file.
153 * @see <a href="content/accountInfo.xml">accountInfo.xml</a>
154 *
155 * @throws Exception If any errors occur.
156 */
157 @Test
158 public void testMoreUserInfo() throws Exception {
159 String xmlFile = XML_DIR + "accountInfo.xml";
160 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
161
162 dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
163 dbf.setNamespaceAware(true);
164 dbf.setValidating(true);
165
166 DocumentBuilder docBuilder = dbf.newDocumentBuilder();
167 MyErrorHandler eh = new MyErrorHandler();
168 docBuilder.setErrorHandler(eh);
169
170 Document document = docBuilder.parse(xmlFile);
171 Element account = (Element)document
172 .getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "Account").item(0);
173 String textContent = account.getTextContent();
174 assertTrue(textContent.trim().regionMatches(0, "Rachel", 0, 6));
175 assertEquals(textContent, "RachelGreen744");
176
177 Attr accountID = account.getAttributeNodeNS(PORTAL_ACCOUNT_NS, "accountID");
|