001    /*
002    // $Id: //open/util/resgen/src/org/eigenbase/xom/XomTest.java#1 $
003    // Package org.eigenbase.xom is an XML Object Mapper.
004    // Copyright (C) 2008-2008 The Eigenbase Project
005    // Copyright (C) 2008-2008 Disruptive Tech
006    // Copyright (C) 2008-2008 LucidEra, Inc.
007    //
008    // This library is free software; you can redistribute it and/or modify it
009    // under the terms of the GNU Lesser General Public License as published by the
010    // Free Software Foundation; either version 2 of the License, or (at your
011    // option) any later version approved by The Eigenbase Project.
012    //
013    // This library is distributed in the hope that it will be useful,
014    // but WITHOUT ANY WARRANTY; without even the implied warranty of
015    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016    // GNU Lesser General Public License for more details.
017    //
018    // You should have received a copy of the GNU Lesser General Public License
019    // along with this library; if not, write to the Free Software
020    // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
021    */
022    package org.eigenbase.xom;
023    
024    import junit.framework.TestCase;
025    
026    /**
027     * Unit tests for XOM module.
028     *
029     * @author jhyde
030     * @version $Id: //open/util/resgen/src/org/eigenbase/xom/XomTest.java#1 $
031     * @since Jun 6, 2008
032     */
033    public class XomTest extends TestCase {
034        public void testFoo() throws XOMException {
035            final Parser xmlParser = XOMUtil.createDefaultParser();
036            xmlParser.setKeepPositions(true);
037            final String lineSep = System.getProperty("line.separator");
038            final String xml = "<Model" + lineSep
039                + "  name=\"meta\"" + lineSep
040                + "  dtdName=\"meta.dtd\"" + lineSep
041                + "  className=\"MetaDef\"" + lineSep
042                + "  packageName=\"org.eigenbase.xom\"" + lineSep
043                + "  root=\"Model\"" + lineSep
044                + "  version=\"1.0\"" + lineSep
045                + ">" + lineSep
046                + "  <!-- a comment" + lineSep
047                + "       spread across multiple lines -->" + lineSep
048                + "<Doc>" + lineSep
049                + "  This model is the XOM Meta Model.  It is the specification of the model used" + lineSep
050                + "  to define new XML-based models.  It is also an instance of itself." + lineSep
051                + "" + lineSep
052                + "</Doc>" + lineSep
053                + "" + lineSep
054                + "<Element type=\"Model\">" + lineSep
055                + "    <Doc>" + lineSep
056                + "       Contains a \"single\" apostrope '." + lineSep
057                + "    </Doc>" + lineSep
058                + "" + lineSep
059                + "    <Attribute name=\"name\" required=\"true\"/>" + lineSep
060                + "    <Attribute name=\"dtdName\"/>" + lineSep
061                + "</Element>" + lineSep
062                + "</Model>";
063            DOMWrapper def = xmlParser.parse(xml);
064            assertNotNull(def);
065            final MetaDef.Model model = new MetaDef.Model(def);
066            assertNotNull(model);
067    
068            Location location = model.getLocation();
069            assertEquals("Model", model.getName());
070            assertEquals(1, location.getStartLine());
071            assertEquals(1, location.getStartColumn());
072            assertEquals(25, location.getEndLine());
073            assertEquals(9, location.getEndColumn());
074    
075            // Model only has one child, Element. Doc is Cdata, so becomes an
076            // attribute.
077            NodeDef[] children = model.getChildren();
078            assertEquals(1, children.length);
079            final NodeDef element = children[0];
080            assertEquals("Element", element.getName());
081            location = element.getLocation();
082            assertEquals(17, location.getStartLine());
083            assertEquals(1, location.getStartColumn());
084            assertEquals(24, location.getEndLine());
085            assertEquals(11, location.getEndColumn());
086    
087            children = element.getChildren();
088            assertEquals(4, children.length);
089            NodeDef attribute = children[1];
090            assertEquals("Attribute", attribute.getName());
091            location = attribute.getLocation();
092            assertEquals(23, location.getStartLine());
093            assertEquals(5, location.getStartColumn());
094            assertEquals(23, location.getEndLine());
095            assertEquals(32, location.getEndColumn());
096        }
097    }
098    
099    // End XomTest.java