001    /*
002     * Copyright 2002 - 2010 JEuclid, http://jeuclid.sf.net
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    /* $Id: DOMBuilderTest.java,v 92b6a7c39d7f 2010/08/11 20:23:17 max $ */
018    
019    package net.sourceforge.jeuclid.test;
020    
021    import java.io.StringReader;
022    
023    import javax.xml.parsers.DocumentBuilderFactory;
024    
025    import net.sourceforge.jeuclid.DOMBuilder;
026    import net.sourceforge.jeuclid.elements.AbstractJEuclidElement;
027    import net.sourceforge.jeuclid.elements.presentation.token.Mspace;
028    
029    import org.junit.Assert;
030    import org.junit.Test;
031    import org.w3c.dom.Document;
032    import org.w3c.dom.DocumentFragment;
033    import org.w3c.dom.Node;
034    import org.w3c.dom.mathml.MathMLPresentationToken;
035    import org.xml.sax.InputSource;
036    
037    /**
038     * A JUnit test for DOMBuilder; in particular, the sources it can take as input.
039     *
040     * @version $Revision: 92b6a7c39d7f $
041     */
042    public class DOMBuilderTest {
043    
044        private final DocumentBuilderFactory documentBuilderFactory;
045    
046        /**
047         * Initialize test.
048         */
049        public DOMBuilderTest() {
050            this.documentBuilderFactory = DocumentBuilderFactory.newInstance();
051            this.documentBuilderFactory.setNamespaceAware(true);
052            this.documentBuilderFactory.setValidating(false);
053        }
054    
055        /**
056         * Tests if DOM can be constructed manually.
057         *
058         * @throws Exception
059         *             if the test fails.
060         */
061        @Test
062        public void testConstructor() throws Exception {
063            final Document doc = this.parse("<math><mn>1</mn></math>");
064            final Document jdoc = DOMBuilder.getInstance().createJeuclidDom(doc);
065            final Node firstChild = jdoc.getFirstChild();
066            Assert.assertEquals(firstChild.getNodeName(), "math");
067            Assert.assertEquals(firstChild.getFirstChild().getNodeName(), "mn");
068            final DocumentFragment documentFragment = doc.createDocumentFragment();
069            documentFragment.appendChild(doc.createElement(Mspace.ELEMENT));
070            Assert.assertEquals(DOMBuilder.getInstance().createJeuclidDom(
071                    documentFragment).getFirstChild().getNodeName(), "mspace");
072            try {
073                DOMBuilder.getInstance().createJeuclidDom(
074                        doc.getDocumentElement().getFirstChild().getFirstChild());
075                Assert.fail("Expected IllegalArgumentException");
076            } catch (final IllegalArgumentException e) {
077            }
078        }
079    
080        private Document parse(final String text) throws Exception {
081            return this.documentBuilderFactory.newDocumentBuilder().parse(
082                    new InputSource(new StringReader(text)));
083        }
084    
085        /**
086         * Tests for namespace issues with manual element construction in default
087         * namespace.
088         *
089         * @throws Exception
090         *             if the test fails.
091         */
092        @Test
093        public void testConstructionNSDefault() throws Exception {
094            final Document doc = this.parse("<math></math>");
095            final Document jdoc = DOMBuilder.getInstance().createJeuclidDom(doc);
096            final Node mathNode = jdoc.getFirstChild();
097            Assert.assertEquals(mathNode.getNodeName(), "math");
098            Assert.assertEquals(mathNode.getNamespaceURI(),
099                    AbstractJEuclidElement.URI);
100            final MathMLPresentationToken mn = (MathMLPresentationToken) mathNode
101                    .getOwnerDocument().createElement("mn");
102            mn.setTextContent("1");
103            mathNode.appendChild(mn);
104            Assert.assertSame(mn.getOwnerDocument(), mathNode.getOwnerDocument());
105            Assert.assertSame(mn.getOwnerMathElement(), mathNode);
106            Assert.assertEquals(mn.getNamespaceURI(), AbstractJEuclidElement.URI);
107        }
108    
109        /**
110         * Tests for namespace issues with manual element construction in mml
111         * namespace.
112         *
113         * @throws Exception
114         *             if the test fails.
115         */
116        @Test
117        public void testConstructionNSMml() throws Exception {
118            final Document doc = this.parse("<mml:math "
119                    + "xmlns:mml='http://www.w3.org/1998/Math/MathML' "
120                    + "xmlns='http://bla'></mml:math>");
121            final Document jdoc = DOMBuilder.getInstance().createJeuclidDom(doc);
122            final Node mathNode = jdoc.getFirstChild();
123            Assert.assertEquals(mathNode.getNodeName(), "mml:math");
124            Assert.assertEquals(mathNode.getLocalName(), "math");
125            Assert.assertEquals(mathNode.getNamespaceURI(),
126                    AbstractJEuclidElement.URI);
127            final MathMLPresentationToken mn = (MathMLPresentationToken) mathNode
128                    .getOwnerDocument().createElement("mn");
129            mn.setTextContent("1");
130            mathNode.appendChild(mn);
131            Assert.assertSame(mn.getOwnerDocument(), mathNode.getOwnerDocument());
132            Assert.assertSame(mn.getOwnerMathElement(), mathNode);
133            Assert.assertEquals(mn.getNamespaceURI(), AbstractJEuclidElement.URI);
134        }
135    
136    }