UserController.java revision 687:e7736286abe1
181634Sbrian/*
281634Sbrian * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
381634Sbrian * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
481634Sbrian *
581634Sbrian * This code is free software; you can redistribute it and/or modify it
681634Sbrian * under the terms of the GNU General Public License version 2 only, as
781634Sbrian * published by the Free Software Foundation.
881634Sbrian *
981634Sbrian * This code is distributed in the hope that it will be useful, but WITHOUT
1081634Sbrian * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1181634Sbrian * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1281634Sbrian * version 2 for more details (a copy is included in the LICENSE file that
1381634Sbrian * accompanied this code).
1481634Sbrian *
1581634Sbrian * You should have received a copy of the GNU General Public License version
1681634Sbrian * 2 along with this work; if not, write to the Free Software Foundation,
1781634Sbrian * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1881634Sbrian *
1981634Sbrian * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2081634Sbrian * or visit www.oracle.com if you need additional information or have any
2181634Sbrian * questions.
2281634Sbrian */
2381634Sbrianpackage test.auctionportal;
2481634Sbrian
2581634Sbrianimport static com.sun.org.apache.xerces.internal.jaxp.JAXPConstants.JAXP_SCHEMA_LANGUAGE;
2681634Sbrianimport static org.testng.Assert.assertFalse;
2781634Sbrianimport java.io.FileOutputStream;
2881634Sbrianimport java.nio.file.Files;
2981634Sbrianimport java.nio.file.Paths;
3081634Sbrianimport java.nio.file.StandardCopyOption;
3196732Sbrianimport static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
3296732Sbrianimport javax.xml.parsers.DocumentBuilder;
3396732Sbrianimport javax.xml.parsers.DocumentBuilderFactory;
3496732Sbrianimport jaxp.library.JAXPFileBaseTest;
3581634Sbrianimport static jaxp.library.JAXPTestUtilities.USER_DIR;
3681634Sbrianimport static jaxp.library.JAXPTestUtilities.compareDocumentWithGold;
3781634Sbrianimport static org.testng.Assert.assertEquals;
3881634Sbrianimport static org.testng.Assert.assertTrue;
3981634Sbrianimport org.testng.annotations.Test;
4081634Sbrianimport org.w3c.dom.Attr;
4181634Sbrianimport org.w3c.dom.Document;
4281634Sbrianimport org.w3c.dom.Element;
4381634Sbrianimport org.w3c.dom.NodeList;
4481634Sbrianimport org.w3c.dom.Text;
4581634Sbrianimport org.w3c.dom.bootstrap.DOMImplementationRegistry;
4681634Sbrianimport org.w3c.dom.ls.DOMImplementationLS;
4781634Sbrianimport org.w3c.dom.ls.LSParser;
4881634Sbrianimport org.w3c.dom.ls.LSSerializer;
4981634Sbrianimport static test.auctionportal.HiBidConstants.GOLDEN_DIR;
5081634Sbrianimport static test.auctionportal.HiBidConstants.PORTAL_ACCOUNT_NS;
5181634Sbrianimport static test.auctionportal.HiBidConstants.XML_DIR;
5281634Sbrian
5381634Sbrian/**
5481634Sbrian * This is the user controller class for the Auction portal HiBid.com.
5581634Sbrian */
5681634Sbrianpublic class UserController extends JAXPFileBaseTest {
5781634Sbrian    /**
5881634Sbrian     * Checking when creating an XML document using DOM Level 2 validating
5981634Sbrian     * it without having a schema source or a schema location It must throw a
6081634Sbrian     * sax parse exception.
6181634Sbrian     *
6281634Sbrian     * @throws Exception If any errors occur.
6381634Sbrian     */
6481634Sbrian    @Test
6581634Sbrian    public void testCreateNewUser() throws Exception {
6681634Sbrian        String resultFile = USER_DIR + "accountInfoOut.xml";
6781634Sbrian        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
6881634Sbrian        dbf.setNamespaceAware(true);
6981634Sbrian        dbf.setValidating(true);
7081634Sbrian
7181634Sbrian        DocumentBuilder docBuilder = dbf.newDocumentBuilder();
7281634Sbrian        MyErrorHandler eh = new MyErrorHandler();
7381634Sbrian        docBuilder.setErrorHandler(eh);
7481634Sbrian
7581634Sbrian        Document document = docBuilder.newDocument();
7681634Sbrian
7781634Sbrian        Element account = document.createElementNS(PORTAL_ACCOUNT_NS, "acc:Account");
7881634Sbrian        Attr accountID = document.createAttributeNS(PORTAL_ACCOUNT_NS, "acc:accountID");
7981634Sbrian        account.setAttributeNode(accountID);
8081634Sbrian
8181634Sbrian        account.appendChild(document.createElement("FirstName"));
8281634Sbrian        account.appendChild(document.createElementNS(PORTAL_ACCOUNT_NS, "acc:LastName"));
8381634Sbrian        account.appendChild(document.createElement("UserID"));
8481634Sbrian
8581634Sbrian        DOMImplementationLS impl
8681634Sbrian                = (DOMImplementationLS) DOMImplementationRegistry
8781634Sbrian                        .newInstance().getDOMImplementation("LS");
8881634Sbrian        LSSerializer writer = impl.createLSSerializer();
8981634Sbrian        LSParser builder = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
9081634Sbrian        try(FileOutputStream output = new FileOutputStream(resultFile)) {
9181634Sbrian            MyDOMOutput domOutput = new MyDOMOutput();
9281634Sbrian            domOutput.setByteStream(output);
9381634Sbrian            writer.write(account, domOutput);
9481634Sbrian            docBuilder.parse(resultFile);
9581634Sbrian        }
9681634Sbrian        assertTrue(eh.isAnyError());
9781634Sbrian    }
9881634Sbrian
9981634Sbrian    /**
10081634Sbrian     * Checking conflicting namespaces and use renameNode and normalizeDocument.
10181634Sbrian     * @see <a href="content/accountInfo.xml">accountInfo.xml</a>
10281634Sbrian     *
10381634Sbrian     * @throws Exception If any errors occur.
10481634Sbrian     */
10581634Sbrian    @Test
10681634Sbrian    public void testAddUser() throws Exception {
10781634Sbrian        String resultFile = USER_DIR + "accountRole.out";
10881634Sbrian        String xmlFile = XML_DIR + "accountInfo.xml";
10981634Sbrian
11081634Sbrian        // Copy schema for outputfile
11181634Sbrian        Files.copy(Paths.get(XML_DIR, "accountInfo.xsd"),
11281634Sbrian                Paths.get(USER_DIR, "accountInfo.xsd"),
11381634Sbrian                StandardCopyOption.REPLACE_EXISTING);
11481634Sbrian        MyErrorHandler eh = new MyErrorHandler();
11581634Sbrian        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
11681634Sbrian
11781634Sbrian        dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
11881634Sbrian        dbf.setNamespaceAware(true);
11981634Sbrian        dbf.setValidating(true);
12081634Sbrian
12181634Sbrian        DocumentBuilder docBuilder = dbf.newDocumentBuilder();
12281634Sbrian        docBuilder.setErrorHandler(eh);
12381634Sbrian
12481634Sbrian        Document document = docBuilder.parse(xmlFile);
12581634Sbrian        Element sell = (Element) document.getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "Sell").item(0);
12681634Sbrian        Element role = (Element) sell.getParentNode();
12781634Sbrian
12881634Sbrian        Element buy = (Element) document.renameNode(sell, PORTAL_ACCOUNT_NS, "acc:Buy");
12981634Sbrian        role.appendChild(buy);
13081634Sbrian
13181634Sbrian        DOMImplementationLS impl
13281634Sbrian                = (DOMImplementationLS) DOMImplementationRegistry
13381634Sbrian                        .newInstance().getDOMImplementation("LS");
13481634Sbrian        LSSerializer writer = impl.createLSSerializer();
13581634Sbrian
13681634Sbrian
13781634Sbrian        try(FileOutputStream output = new FileOutputStream(resultFile)) {
13881634Sbrian            MyDOMOutput mydomoutput = new MyDOMOutput();
13981634Sbrian            mydomoutput.setByteStream(output);
14081634Sbrian            writer.write(document, mydomoutput);
14181634Sbrian        }
14281634Sbrian
14381634Sbrian        docBuilder.parse(resultFile);
14481634Sbrian        assertFalse(eh.isAnyError());
145115303Speter    }
14681634Sbrian
14781634Sbrian    /**
14881634Sbrian     * Checking Text content in XML file.
14981634Sbrian     * @see <a href="content/accountInfo.xml">accountInfo.xml</a>
15081634Sbrian     *
15181634Sbrian     * @throws Exception If any errors occur.
152115303Speter     */
15381634Sbrian    @Test(groups = {"readLocalFiles"})
154115303Speter    public void testMoreUserInfo() throws Exception {
15581634Sbrian        String xmlFile = XML_DIR + "accountInfo.xml";
15681634Sbrian        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
15781634Sbrian
15881634Sbrian        dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
15981634Sbrian        dbf.setNamespaceAware(true);
16081634Sbrian        dbf.setValidating(true);
16181634Sbrian
16281634Sbrian        DocumentBuilder docBuilder = dbf.newDocumentBuilder();
16381634Sbrian        MyErrorHandler eh = new MyErrorHandler();
16481634Sbrian        docBuilder.setErrorHandler(eh);
16581634Sbrian
166102558Sbrian        Document document = docBuilder.parse(xmlFile);
167102558Sbrian        Element account = (Element)document
168102558Sbrian                .getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "Account").item(0);
169102558Sbrian        String textContent = account.getTextContent();
170102558Sbrian        assertTrue(textContent.trim().regionMatches(0, "Rachel", 0, 6));
171102558Sbrian        assertEquals(textContent, "RachelGreen744");
17281634Sbrian
17381634Sbrian        Attr accountID = account.getAttributeNodeNS(PORTAL_ACCOUNT_NS, "accountID");
17481634Sbrian        assertTrue(accountID.getTextContent().trim().equals("1"));
17581634Sbrian
17681634Sbrian        assertFalse(eh.isAnyError());
17781634Sbrian    }
178102558Sbrian
17981634Sbrian    /**
18081634Sbrian     * This will check if adoptNode works will adoptNode from
18181634Sbrian     * @see <a href="content/userInfo.xml">userInfo.xml</a>
18281634Sbrian     * @see <a href="content/accountInfo.xml">accountInfo.xml</a>. This is
18381634Sbrian     * adopting a node from the XML file which is validated by a DTD and
18481634Sbrian     * into an XML file which is validated by the schema This covers Row 5
18581634Sbrian     * for the table
18681634Sbrian     * http://javaweb.sfbay/~jsuttor/JSR206/jsr-206-html/ch03s05.html. Filed
18781634Sbrian     * bug 4893745 because there was a difference in behavior.
18881634Sbrian     *
18981739Sbrian     * @throws Exception If any errors occur.
19081739Sbrian     */
19181739Sbrian    @Test
19281739Sbrian    public void testCreateUserAccount() throws Exception {
19381739Sbrian        String userXmlFile = XML_DIR + "userInfo.xml";
19481739Sbrian        String accountXmlFile = XML_DIR + "accountInfo.xml";
19581634Sbrian        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
19681634Sbrian        dbf.setNamespaceAware(true);
19781634Sbrian        dbf.setValidating(true);
19881634Sbrian
19981634Sbrian        DocumentBuilder docBuilder = dbf.newDocumentBuilder();
20081634Sbrian        MyErrorHandler eh = new MyErrorHandler();
20181634Sbrian        docBuilder.setErrorHandler(eh);
20281634Sbrian
20381634Sbrian        Document document = docBuilder.parse(userXmlFile);
20481634Sbrian        Element user = (Element) document.getElementsByTagName("FirstName").item(0);
20581634Sbrian        // Set schema after parsing userInfo.xml. Otherwise it will conflict
20681634Sbrian        // with DTD validation.
20781634Sbrian        dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
20881634Sbrian        DocumentBuilder docBuilder1 = dbf.newDocumentBuilder();
20981634Sbrian        docBuilder1.setErrorHandler(eh);
21081634Sbrian        Document accDocument = docBuilder1.parse(accountXmlFile);
21181634Sbrian
21281634Sbrian        Element firstName = (Element) accDocument
21381634Sbrian                .getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "FirstName").item(0);
21481634Sbrian        Element adoptedAccount = (Element) accDocument.adoptNode(user);
21581634Sbrian
21681634Sbrian        Element parent = (Element) firstName.getParentNode();
21781634Sbrian        parent.replaceChild(adoptedAccount, firstName);
21881634Sbrian
21981634Sbrian        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
22081634Sbrian        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
22181634Sbrian        LSSerializer writer = impl.createLSSerializer();
22281634Sbrian
22381634Sbrian        MyDOMOutput mydomoutput = new MyDOMOutput();
22481634Sbrian        mydomoutput.setByteStream(System.out);
22581634Sbrian
22681634Sbrian        writer.write(document, mydomoutput);
22781634Sbrian        writer.write(accDocument, mydomoutput);
22881634Sbrian
22981634Sbrian        assertFalse(eh.isAnyError());
23081634Sbrian    }
23181634Sbrian
23281634Sbrian    /**
23381634Sbrian     * Checking for Row 8 from the schema table when setting the schemaSource
23481634Sbrian     * without the schemaLanguage must report an error.
23581634Sbrian     *
23681634Sbrian     * @throws Exception If any errors occur.
23781634Sbrian     */
23881634Sbrian    @Test(expectedExceptions = IllegalArgumentException.class)
23981634Sbrian    public void testUserError() throws Exception {
24081634Sbrian        String xmlFile = XML_DIR + "userInfo.xml";
24181634Sbrian        String schema = "http://java.sun.com/xml/jaxp/properties/schemaSource";
24281634Sbrian        String schemaValue = "http://dummy.com/dummy.xsd";
24381634Sbrian
24481634Sbrian        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
24581634Sbrian        dbf.setNamespaceAware(true);
24681634Sbrian        dbf.setValidating(true);
24781634Sbrian        dbf.setAttribute(schema, schemaValue);
24881634Sbrian
24981634Sbrian        DocumentBuilder docBuilder = dbf.newDocumentBuilder();
25081634Sbrian        MyErrorHandler eh = new MyErrorHandler();
25181634Sbrian        docBuilder.setErrorHandler(eh);
25281634Sbrian        docBuilder.parse(xmlFile);
25381634Sbrian        assertFalse(eh.isAnyError());
25481634Sbrian    }
25581634Sbrian
25681634Sbrian    /**
25781634Sbrian     * Checking for namespace normalization.
25881634Sbrian     * @see <a href="content/screenName.xml">screenName.xml</a> has prefix of
25981634Sbrian     * userName is bound to "http://hibid.com/user" namespace normalization
26081634Sbrian     * will create a namespace of prefix us and attach userEmail.
26181634Sbrian     *
26281634Sbrian     * @throws Exception If any errors occur.
26381634Sbrian     */
26481634Sbrian    @Test
26581634Sbrian    public void testCheckScreenNameExists() throws Exception {
26681634Sbrian        String resultFile = USER_DIR + "screenName.out";
26781634Sbrian        String xmlFile = XML_DIR + "screenName.xml";
26881634Sbrian        String goldFile = GOLDEN_DIR + "screenNameGold.xml";
26981634Sbrian
27081634Sbrian        String nsTagName = "http://hibid.com/screenName";
27181634Sbrian        String userNs = "http://hibid.com/user";
27281634Sbrian
27381634Sbrian        try (FileOutputStream output = new FileOutputStream(resultFile)) {
27481634Sbrian            DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
27581634Sbrian            DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
27681634Sbrian            LSSerializer writer = impl.createLSSerializer();
27781634Sbrian            LSParser builder = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
27881634Sbrian            Document document = builder.parseURI(xmlFile);
27981634Sbrian            NodeList nl = document.getElementsByTagNameNS(nsTagName, "screen-name");
28081634Sbrian            assertEquals(nl.getLength(), 1);
28181634Sbrian            Element screenName = (Element)nl.item(0);
28281634Sbrian            Element userEmail = document.createElementNS(userNs, "userEmail");
28381634Sbrian            assertTrue(userEmail.isDefaultNamespace(userNs));
28481634Sbrian
28581634Sbrian            Text email = document.createTextNode("myid@hibid.com");
28681634Sbrian            userEmail.appendChild(email);
28781634Sbrian            screenName.appendChild(userEmail);
28881634Sbrian            document.normalizeDocument();
28981634Sbrian
29081634Sbrian            MyDOMOutput domoutput = new MyDOMOutput();
29181634Sbrian            domoutput.setByteStream(output);
29281634Sbrian            writer.write(document, domoutput);
29381634Sbrian        }
29481634Sbrian        assertTrue(compareDocumentWithGold(goldFile, resultFile));
29581634Sbrian    }
29681634Sbrian}
29781634Sbrian