/* * Copyright (c) 2001 SOFTWARE AG, All Rights Reserved. */ import java.io.StringReader; import java.util.Iterator; import java.util.List; import junit.framework.TestCase; import junit.framework.TestSuite; import org.jdom.Attribute; import org.jdom.Document; import org.jdom.Element; import org.jdom.Namespace; import org.jdom.input.SAXBuilder; /** ** Test a JDom problem. ** ** @author Stefan Liebig **/ public class JDomResponseTest extends TestCase { /** ** Constructs a test case. ** ** @param name name of the test case ** ** @see junit.framework.TestCase **/ public JDomResponseTest(String name) { super(name); } /** ** This method creates the local fixtures for a single test case of the Tamino API. ** ** @exception Exception ** ** @see junit.framework.TestCase#setUp() **/ protected void setUp() throws Exception { jdomBuilder = new SAXBuilder(); // Create the internal ino namespace instance. inoNamespace = Namespace.getNamespace( "ino", "http://namespaces.softwareag.com/tamino/response2" ); } /** ** This method destroys the local fixture for a single test cases of the Tamino API. ** ** @exception Exception ** ** @see junit.framework.TestCase **/ protected void tearDown() throws Exception { } /** ** Test all jdom stuff. **/ public void testJdomStuff() throws Exception { out("testJdomStuff"); Document document = jdomBuilder.build( new StringReader( RESPONSE ) ); // Analyze the message state Element rootElement = document.getRootElement(); // Obtain the list of message elements. List messageList = rootElement.getChildren( "message", inoNamespace ); Iterator messageListIterator = messageList.iterator(); // Iterate thru the list of messages. while ( messageListIterator.hasNext() ) { out( "Next message tag" ); Element message = (Element)messageListIterator.next(); // Obtain the messages returnValue. List attributes = message.getAttributes(); out( "AttributesList:" + attributes ); for ( int i = 0; i < attributes.size(); i++ ) { Attribute attribute = (Attribute) attributes.get( i ); out( "Attribute.name:" + attribute.getName() ); out( "Attribute.value:" + attribute.getValue() ); out( "Attribute.getNS:" + attribute.getNamespace() ); } String returnValue = message.getAttributeValue( "returnvalue", inoNamespace ); out( "ReturnValue:" + returnValue ); // Obtain the list of message lists within the message. List messageLineList = message.getChildren( "messageline", inoNamespace ); // Go thru the list of message lines and retrieve for each the subject and text content. if ( messageLineList.size() > 0 ) { int index = 0; Iterator messageLineIter = messageLineList.iterator(); while ( messageLineIter.hasNext() ) { Element messageLine = (Element)messageLineIter.next(); String text = messageLine.getText(); out( "text:" + text ); } } // Obtain the message text if given. Element messageText = message.getChild( "text", inoNamespace ); if ( messageText != null ) { String text = messageText.getText(); out( "Text:" + text ); } } } private void out( String string ) { System.out.println( string ); } /** ** Start precisely this test suite with the commandline interface. ** ** @pre true ** @post true ** ** @param args commandline options (none are evaluated) **/ public static void main( String[] args ) { junit.textui.TestRunner.run( new TestSuite( JDomResponseTest.class ) ); } // A RESPONSE private final static String RESPONSE = "" + "" + " " + " testUpdate_sli[@ino:id=1]" + " " + " " + " " + " XQL Request processing" + " " + " " + " " + // This line causes that the next ino:returnvalue will not be read " " + // This line works // " " + " DOC: -1" + " " + " " + " " + " " + " XQL Request processed" + " " + " " + ""; // parser private SAXBuilder jdomBuilder = null; // The JDOM ino Namespace attribute. private Namespace inoNamespace = null; }