Skip to content

Commit

Permalink
Added a not-so-unit test for reading elemental data from XML using th…
Browse files Browse the repository at this point in the history
…e reader and the underlying SAX handler

Signed-off-by: Rajarshi Guha <rajarshi.guha@gmail.com>
  • Loading branch information
egonw authored and rajarshi committed Jan 17, 2011
1 parent cfa5c93 commit 0c565e4
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
Expand Up @@ -23,6 +23,8 @@
*/
package org.openscience.cdk.tools.periodictable;

import org.openscience.cdk.annotations.TestClass;
import org.openscience.cdk.annotations.TestMethod;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.tools.ILoggingTool;
import org.openscience.cdk.tools.LoggingToolFactory;
Expand Down Expand Up @@ -51,6 +53,7 @@
* @cdk.module core
* @cdk.githash
*/
@TestClass("org.openscience.cdk.tools.periodictable.ElementPTReaderTest")
class ElementPTHandler extends DefaultHandler
{
private final int SCALAR_UNSET = 0;
Expand All @@ -74,6 +77,7 @@ class ElementPTHandler extends DefaultHandler
public String currentElement;
public String dictRef;

@TestMethod("testReading")
public ElementPTHandler()
{
}
Expand All @@ -83,20 +87,23 @@ public ElementPTHandler()
*
* @return A Vector object with all isotopes
*/
@TestMethod("testReading")
public List<PeriodicTableElement> getElements()
{
return elements;
}

// SAX Parser methods

@TestMethod("testReading")
public void startDocument()
{
elements = new ArrayList<PeriodicTableElement>();
scalarType = SCALAR_UNSET;
elementType = null;
}

@TestMethod("testReading")
public void endElement(String uri, String local, String raw) {
logger.debug("end element: ", raw);
if ("elementType".equals(local))
Expand Down Expand Up @@ -145,6 +152,7 @@ public void endElement(String uri, String local, String raw) {
currentChars = "";
}

@TestMethod("testReading")
public void startElement(String uri, String local,
String raw, Attributes atts)
{
Expand Down Expand Up @@ -198,6 +206,7 @@ public void startElement(String uri, String local,

}

@TestMethod("testReading")
public void characters(char chars[], int start, int length)
{
logger.debug("character data");
Expand Down
Expand Up @@ -27,6 +27,8 @@
*/
package org.openscience.cdk.tools.periodictable;

import org.openscience.cdk.annotations.TestClass;
import org.openscience.cdk.annotations.TestMethod;
import org.openscience.cdk.tools.ILoggingTool;
import org.openscience.cdk.tools.LoggingToolFactory;
import org.xml.sax.InputSource;
Expand All @@ -50,6 +52,7 @@
* @cdk.module core
* @cdk.githash
*/
@TestClass("org.openscience.cdk.tools.periodictable.ElementPTReaderTest")
class ElementPTReader {

private XMLReader parser;
Expand All @@ -63,6 +66,7 @@ class ElementPTReader {
*
* @param input Reader with the XML source
*/
@TestMethod("testReading")
public ElementPTReader(Reader input) {
this.init();
this.input = input;
Expand Down Expand Up @@ -122,6 +126,7 @@ private void init() {
* @return a Vector of Isotope's. Returns an empty vector is some reading error
* occured.
*/
@TestMethod("testReading")
public List<org.openscience.cdk.tools.periodictable.PeriodicTableElement> readElements() {
List<PeriodicTableElement> elements = new ArrayList<org.openscience.cdk.tools.periodictable.PeriodicTableElement>();
try {
Expand Down
2 changes: 2 additions & 0 deletions src/test/org/openscience/cdk/modulesuites/McoreTests.java
Expand Up @@ -52,6 +52,7 @@
import org.openscience.cdk.tools.SystemOutLoggingToolTest;
import org.openscience.cdk.tools.manipulator.BondManipulatorTest;
import org.openscience.cdk.tools.periodictable.ElementPTFactoryTest;
import org.openscience.cdk.tools.periodictable.ElementPTReaderTest;
import org.openscience.cdk.tools.periodictable.PeriodicTableElementTest;
import org.openscience.cdk.tools.periodictable.PeriodicTableTest;

Expand Down Expand Up @@ -85,6 +86,7 @@
PeriodicTableElementTest.class,
PeriodicTableTest.class,
ElementPTFactoryTest.class,
ElementPTReaderTest.class,

// the CDK atom typer
CDKAtomTypeMatcherTest.class,
Expand Down
@@ -0,0 +1,63 @@
/* Copyright (C) 2011 Egon Willighagen <egonw@users.sourceforge.net>
*
* Contact: cdk-devel@lists.sourceforge.net
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.openscience.cdk.tools.periodictable;

import java.io.StringReader;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;

/**
* @cdk.module test-core
*/
public class ElementPTReaderTest {

@Test
public void testReading() {
String string =
"<list xmlns=\"http://www.xml-cml.org/schema\"" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
" xsi:schemaLocation=\"http://www.xml-cml.org/schema ../../io/cml/data/cml23.xsd\"" +
" xmlns:cdk=\"http://cdk.sf.net/dict/cdk/\"" +
" xmlns:cvs=\"https://www.cvshome.org/\"" +
" id=\"chemicalElement\" title=\"Properties of the elements\">" +
" <metadataList>" +
" <metadata name=\"cvs:last-change-by\" content=\"$Author$\"/>" +
" <metadata name=\"cvs:date\" content=\"$Date$\"/>" +
" <metadata name=\"cvs:revision\" content=\"$Revision$\"/>" +
" </metadataList>" +
" <elementType id=\"R\">" +
" <label dictRef=\"cas:id\"></label>" +
" <scalar dataType=\"xsd:String\" dictRef=\"cdk:name\">Pseudoatom</scalar>" +
" <scalar dataType=\"xsd:Integer\" dictRef=\"cdk:atomicNumber\">0</scalar>" +
" </elementType>" +
"</list>";
ElementPTReader reader = new ElementPTReader(
new StringReader(string)
);
List<PeriodicTableElement> elements = reader.readElements();
Assert.assertNotNull(elements);
Assert.assertEquals(1, elements.size());
PeriodicTableElement element = elements.get(0);
Assert.assertNotNull(element);
Assert.assertEquals("Pseudoatom", element.getName());
}

}

0 comments on commit 0c565e4

Please sign in to comment.