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: MathBaseTest.java,v 92b6a7c39d7f 2010/08/11 20:23:17 max $ */
018
019 package net.sourceforge.jeuclid.test;
020
021 import java.io.IOException;
022
023 import javax.xml.parsers.DocumentBuilder;
024 import javax.xml.parsers.ParserConfigurationException;
025
026 import net.sourceforge.jeuclid.DOMBuilder;
027 import net.sourceforge.jeuclid.MathMLParserSupport;
028
029 import org.apache.commons.logging.Log;
030 import org.apache.commons.logging.LogFactory;
031 import org.junit.Assert;
032 import org.junit.Test;
033 import org.w3c.dom.Document;
034 import org.xml.sax.InputSource;
035 import org.xml.sax.SAXException;
036
037 /**
038 * A JUnit Test case for MathBase.
039 *
040 * @version $Revision: 92b6a7c39d7f $
041 */
042 public class MathBaseTest {
043
044 /**
045 * Logger for this class
046 */
047 private static final Log LOGGER = LogFactory.getLog(MathBaseTest.class);
048
049 /**
050 * Helper class to load included examples.
051 * @param name Name of the example
052 * @return the loaded Document
053 * @throws ParserConfigurationException if anything goes wrong.
054 * @throws SAXException if anything goes wrong.
055 * @throws IOException if anything goes wrong.
056 */
057 public static Document loadDocument(final String name)
058 throws ParserConfigurationException, SAXException, IOException {
059 final DocumentBuilder parser = MathMLParserSupport
060 .createDocumentBuilder();
061 Document document = null;
062 MathBaseTest.LOGGER.info("reading:" + name);
063 final InputSource source = new InputSource(MathBaseTest.class
064 .getResourceAsStream("/" + name));
065 document = parser.parse(source);
066 System.out.println(name + " loaded");
067 return document;
068 }
069
070 /**
071 * Tests the examples at resources/test/exampleX.mml.
072 *
073 * @throws Exception
074 * if an error occurs.
075 */
076 @Test
077 public void testEmbeddedExamples() throws Exception {
078 for (int example = 1; example <= 7; example++) {
079 final String exName = "example" + example + ".mml";
080 final Document document = MathBaseTest.loadDocument(exName);
081 DOMBuilder.getInstance().createJeuclidDom(document);
082 }
083 }
084
085 /**
086 * Tests ODF Reader.
087 *
088 * @throws Exception
089 * if an error occurs.
090 */
091 @Test
092 public void testOdf() throws Exception {
093 Assert.assertNotNull(MathMLParserSupport
094 .parseInputStreamODF(MathBaseTest.class
095 .getResourceAsStream("/" + "example.odf")));
096 }
097
098 /**
099 * Tests the new Parser API.
100 *
101 * @throws Exception
102 * if an error occurs.
103 */
104 @Test
105 public void testNewParser() throws Exception {
106 // Assert.assertNotNull(MathBaseFactory.getMathBaseFactory()
107 // .createMathBase(
108 // new StreamSource(MathBaseTest.class
109 // .getResourceAsStream("/" + "example.odf")),
110 // LayoutContextImpl.getDefaultLayoutContext()));
111 // Assert.assertNotNull(MathBaseFactory.getMathBaseFactory()
112 // .createMathBase(
113 // new StreamSource(MathBaseTest.class
114 // .getResourceAsStream("/" + "example1.mml")),
115 // LayoutContextImpl.getDefaultLayoutContext()));
116 }
117
118 }