From: <kal...@pr...> - 2004-01-31 00:00:57
|
Update of /cvsroot/tm4j/tm4j/src/org/tm4j/topicmap/test In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3465/test Added Files: XTMRoundTripTest.java Log Message: Implemented improved roundtripping (RFE 636513]. Writer now optionally writes stub topics as references only. --- NEW FILE: XTMRoundTripTest.java --- /* * The TM4J Software License * * * Copyright (c) 2000-2004 The TM4J Project. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by * The TM4J Project (http://sourceforge.net/projects/tm4j) * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "TM4J" and "The TM4J Project" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact ka...@te.... * * 5. Products derived from this software may not be called "TM4J", * nor may "TM4J" appear in their name, without prior written * permission of the TM4J Project. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE TM4J PROJECT OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * */ /* *$Header: /cvsroot/tm4j/tm4j/src/org/tm4j/topicmap/test/XTMRoundTripTest.java,v 1.1 2004/01/30 23:59:20 kal_ahmed Exp $ */ package org.tm4j.topicmap.test; import java.io.File; import java.io.FileOutputStream; import java.util.HashSet; import java.util.Set; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.apache.xml.serialize.OutputFormat; import org.apache.xml.serialize.Serializer; import org.apache.xml.serialize.XMLSerializer; import org.tm4j.net.Locator; import org.tm4j.test.ConfigurableBackendTest; import org.tm4j.topicmap.TopicMap; import org.tm4j.topicmap.TopicMapProvider; import org.tm4j.topicmap.utils.TopicMapWalker; import org.tm4j.topicmap.utils.XTMWriter; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; /** * * @author Kal Ahmed (ka...@te...) */ public class XTMRoundTripTest extends ConfigurableBackendTest { /** * @param _name */ public XTMRoundTripTest(String _name) { super(_name); } private void writeMap(TopicMap tm, String fileName) { try { FileOutputStream out = new FileOutputStream(fileName); XTMWriter writer = new XTMWriter(); writer.setProperty(XTMWriter.OPTION_WRITE_STUB_TOPICS, "no"); OutputFormat of = new OutputFormat(); of.setIndenting(true); of.setIndent(2); Serializer ser = new XMLSerializer(of); TopicMapWalker walker = new TopicMapWalker(); ser.setOutputByteStream(out); writer.setContentHandler(ser.asContentHandler()); walker.setHandler(writer); walker.walk(tm); } catch (Exception ex) { ex.printStackTrace(); fail("Unexpected exception: " + ex.toString()); } } public void testRoundTripMap() { try { TopicMap tm = this.getTopicMap("roundtrip_test"); writeMap(tm, "roundtrip.xtm"); TopicMapProvider proivder = this.getProvider(); File tmFile = new File("roundtrip.xtm"); Locator loc = provider.getLocatorFactory().createLocator("URI", tmFile.toURL().toString()); cleanOutTopicMap(loc, provider); SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setValidating(false); spf.setNamespaceAware(true); SAXParser p = spf.newSAXParser(); TestHandler h = new TestHandler(); p.parse(tmFile, h); Locator ref1 = tm.getBaseLocator().resolveRelative("refmap.xtm#ref1"); String ref2 = "http://www.tm4j.org/psi/test1"; assertTrue("Expected SubjectIndicator '" + ref1.getAddress() + "' to be round-tripped.", h.address.contains(ref1.getAddress())); assertTrue("Expected external topic reference '" + ref2 + "' to be round-tripped.", h.si.contains(ref2)); assertEquals("Unexpected topic element count.", 2, h.topicCount); } catch (Exception ex) { ex.printStackTrace(); fail("Unexpected exception: " + ex.toString()); } } class TestHandler extends DefaultHandler { private Set si; private Set address; private boolean inTopicRef = false; private int topicCount; private String XLINK = "http://www.w3.org/1999/xlink"; public TestHandler() { si = new HashSet(); address = new HashSet(); topicCount = 0; } public void startElement( String nsuri, String lname, String qname, Attributes atts) throws SAXException { if (lname.equals("topic")) { topicCount++; return; } if (inTopicRef) { if (lname.equals("subjectIndicatorRef")) { String ref = atts.getValue(XLINK, "href"); si.add(atts.getValue(XLINK, "href")); } else if (lname.equals("topicRef")) { String ref = atts.getValue(XLINK, "href"); address.add(atts.getValue(XLINK, "href")); } } else if (lname.equals("instanceOf") || lname.equals("scope") || lname.equals("parameters") || lname.equals("member")) { inTopicRef = true; } } public void endElement(String lname, String nsuri, String qname) { if (lname.equals("instanceOf") || lname.equals("scope") || lname.equals("parameters") || lname.equals("member")) { inTopicRef = false; } } } } /* * $Log: XTMRoundTripTest.java,v $ * Revision 1.1 2004/01/30 23:59:20 kal_ahmed * Implemented improved roundtripping (RFE 636513]. Writer now optionally writes stub topics as references only. * */ |