You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by be...@apache.org on 2007/10/26 21:04:30 UTC

svn commit: r588750 - in /labs/vysper/src: main/java/org/apache/vysper/charset/ main/java/org/apache/vysper/mina/codec/ main/java/org/apache/vysper/xmpp/xmldecoder/ test/java/org/apache/vysper/mina/codec/ test/java/org/apache/vysper/xmpp/xmldecoder/

Author: berndf
Date: Fri Oct 26 12:04:28 2007
New Revision: 588750

URL: http://svn.apache.org/viewvc?rev=588750&view=rev
Log:
[vysper] make xml streaming parser more robust, adding tests

Added:
    labs/vysper/src/main/java/org/apache/vysper/charset/
    labs/vysper/src/main/java/org/apache/vysper/charset/CharsetUtil.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/xmldecoder/DecodingException.java
    labs/vysper/src/test/java/org/apache/vysper/xmpp/xmldecoder/XMLRawToFragementConverterConvertErrorTestCase.java
      - copied, changed from r586960, labs/vysper/src/test/java/org/apache/vysper/xmpp/xmldecoder/XMLRawToFragementConverterConvertTestCase.java
Modified:
    labs/vysper/src/main/java/org/apache/vysper/mina/codec/StanzaWriterProtocolEncoder.java
    labs/vysper/src/main/java/org/apache/vysper/mina/codec/XMLStreamTokenizer.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/xmldecoder/ParticleDecoder.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/xmldecoder/XMLParticle.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/xmldecoder/XMLRawToFragementConverter.java
    labs/vysper/src/test/java/org/apache/vysper/mina/codec/XMLStreamTokenizerTestCase.java
    labs/vysper/src/test/java/org/apache/vysper/xmpp/xmldecoder/XMLRawToFragementConverterBalancedTestCase.java
    labs/vysper/src/test/java/org/apache/vysper/xmpp/xmldecoder/XMLRawToFragementConverterConvertTestCase.java

Added: labs/vysper/src/main/java/org/apache/vysper/charset/CharsetUtil.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/charset/CharsetUtil.java?rev=588750&view=auto
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/charset/CharsetUtil.java (added)
+++ labs/vysper/src/main/java/org/apache/vysper/charset/CharsetUtil.java Fri Oct 26 12:04:28 2007
@@ -0,0 +1,29 @@
+/***********************************************************************
+ * Copyright (c) 2006-2007 The Apache Software Foundation.             *
+ * All rights reserved.                                                *
+ * ------------------------------------------------------------------- *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you *
+ * may not use this file except in compliance with the License. You    *
+ * may obtain a copy of the License at:                                *
+ *                                                                     *
+ *     http://www.apache.org/licenses/LICENSE-2.0                      *
+ *                                                                     *
+ * Unless required by applicable law or agreed to in writing, software *
+ * distributed under the License is distributed on an "AS IS" BASIS,   *
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or     *
+ * implied.  See the License for the specific language governing       *
+ * permissions and limitations under the License.                      *
+ ***********************************************************************/
+package org.apache.vysper.charset;
+
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetDecoder;
+import java.nio.charset.CharsetEncoder;
+
+/**
+ * utility class for charsets
+ */
+public class CharsetUtil {
+    public static final CharsetDecoder UTF8_DECODER = Charset.forName("UTF-8").newDecoder();
+    public static final CharsetEncoder UTF8_ENCODER = Charset.forName("UTF-8").newEncoder();
+}

Modified: labs/vysper/src/main/java/org/apache/vysper/mina/codec/StanzaWriterProtocolEncoder.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/mina/codec/StanzaWriterProtocolEncoder.java?rev=588750&r1=588749&r2=588750&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/mina/codec/StanzaWriterProtocolEncoder.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/mina/codec/StanzaWriterProtocolEncoder.java Fri Oct 26 12:04:28 2007
@@ -20,18 +20,18 @@
 import org.apache.mina.common.IoSession;
 import org.apache.mina.filter.codec.ProtocolEncoder;
 import org.apache.mina.filter.codec.ProtocolEncoderOutput;
+import org.apache.vysper.charset.CharsetUtil;
 import org.apache.vysper.xmpp.stanza.Stanza;
 import org.apache.vysper.xmpp.writer.StanzaWriter;
 import org.apache.vysper.xmpp.xmlfragment.Renderer;
 
-import java.nio.charset.Charset;
 import java.nio.charset.CharsetEncoder;
 
 /**
  * connects MINA low level protocol and session stanza writer
  */
 public class StanzaWriterProtocolEncoder implements ProtocolEncoder, StanzaWriter {
-    private static final CharsetEncoder UTF8_CHARSET_ENCODER = Charset.forName("UTF-8").newEncoder();
+    
 
     public void encode(IoSession ioSession, Object o, ProtocolEncoderOutput protocolEncoderOutput) throws Exception {
         if (!(o instanceof Stanza)) {
@@ -44,9 +44,9 @@
 
         boolean isFirst = true;
         ByteBuffer byteBuffer = ByteBuffer.allocate(16).setAutoExpand(true);
-        byteBuffer.putString(renderer.getOpeningElement(), UTF8_CHARSET_ENCODER);
-        byteBuffer.putString(renderer.getElementContent(), UTF8_CHARSET_ENCODER);
-        if (isFirst) byteBuffer.putString(renderer.getClosingElement(), UTF8_CHARSET_ENCODER);
+        byteBuffer.putString(renderer.getOpeningElement(), getSessionEncoder());
+        byteBuffer.putString(renderer.getElementContent(), getSessionEncoder());
+        if (isFirst) byteBuffer.putString(renderer.getClosingElement(), getSessionEncoder());
 
         byteBuffer.flip();
         protocolEncoderOutput.write(byteBuffer);
@@ -62,6 +62,10 @@
 
     public void close() {
 
+    }
+    
+    public CharsetEncoder getSessionEncoder() {
+        return CharsetUtil.UTF8_ENCODER; // todo get session dependent encoder
     }
     
 }

Modified: labs/vysper/src/main/java/org/apache/vysper/mina/codec/XMLStreamTokenizer.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/mina/codec/XMLStreamTokenizer.java?rev=588750&r1=588749&r2=588750&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/mina/codec/XMLStreamTokenizer.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/mina/codec/XMLStreamTokenizer.java Fri Oct 26 12:04:28 2007
@@ -26,6 +26,7 @@
 import org.apache.vysper.xmpp.xmlfragment.XMLElement;
 import org.apache.vysper.xmpp.xmlfragment.XMLFragment;
 import org.apache.vysper.xmpp.xmlfragment.XMLText;
+import org.apache.vysper.charset.CharsetUtil;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -40,7 +41,7 @@
 
     public boolean doDecode(IoSession ioSession, ByteBuffer byteBuffer, ProtocolDecoderOutput protocolDecoderOutput) throws Exception {
 
-        XMLParticle xmlParticle = ParticleDecoder.decodeParticle(byteBuffer);
+        XMLParticle xmlParticle = ParticleDecoder.decodeParticle(byteBuffer, CharsetUtil.UTF8_DECODER);
 
         if (xmlParticle != null) {
             // new particle is completed 

Added: labs/vysper/src/main/java/org/apache/vysper/xmpp/xmldecoder/DecodingException.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/xmldecoder/DecodingException.java?rev=588750&view=auto
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/xmldecoder/DecodingException.java (added)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/xmldecoder/DecodingException.java Fri Oct 26 12:04:28 2007
@@ -0,0 +1,37 @@
+/***********************************************************************
+ * Copyright (c) 2006-2007 The Apache Software Foundation.             *
+ * All rights reserved.                                                *
+ * ------------------------------------------------------------------- *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you *
+ * may not use this file except in compliance with the License. You    *
+ * may obtain a copy of the License at:                                *
+ *                                                                     *
+ *     http://www.apache.org/licenses/LICENSE-2.0                      *
+ *                                                                     *
+ * Unless required by applicable law or agreed to in writing, software *
+ * distributed under the License is distributed on an "AS IS" BASIS,   *
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or     *
+ * implied.  See the License for the specific language governing       *
+ * permissions and limitations under the License.                      *
+ ***********************************************************************/
+package org.apache.vysper.xmpp.xmldecoder;
+
+/**
+ */
+public class DecodingException extends Exception {
+    public DecodingException() {
+        super();
+    }
+
+    public DecodingException(String s) {
+        super(s);
+    }
+
+    public DecodingException(String s, Throwable throwable) {
+        super(s, throwable);
+    }
+
+    public DecodingException(Throwable throwable) {
+        super(throwable);
+    }
+}

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/xmldecoder/ParticleDecoder.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/xmldecoder/ParticleDecoder.java?rev=588750&r1=588749&r2=588750&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/xmldecoder/ParticleDecoder.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/xmldecoder/ParticleDecoder.java Fri Oct 26 12:04:28 2007
@@ -18,32 +18,38 @@
 
 import org.apache.mina.common.ByteBuffer;
 
-import java.nio.charset.Charset;
+import java.nio.charset.CharsetDecoder;
 
 /**
+ * partitions the incoming byte stream in particles of XML. either those enclosed by '<' and '>', or the text inbetween.
  */
 public class ParticleDecoder {
     /**
      * split in String, either in those parts enclosed by brackets or those who are not
      * @param byteBuffer
-     * @return
+     * @param charsetDecoder
+     * @return the new particle or NULL, if the buffer was exhausted before the particle was completed
      * @throws Exception
      */
-    public static XMLParticle decodeParticle(ByteBuffer byteBuffer) throws Exception {
+    public static XMLParticle decodeParticle(ByteBuffer byteBuffer, CharsetDecoder charsetDecoder) throws Exception {
         int startPosition = byteBuffer.position();
         
-        // skip all leading zeros
-        int leadingGarbage = 0;
-        while (byteBuffer.remaining() > 0) {
-            byte b = byteBuffer.get();
-            if (b == 0) {
-                leadingGarbage++;
-            } else break;
-        }
-        startPosition += leadingGarbage;
-        byteBuffer.position(startPosition);
+        // OUTDATED
+        // TODO this section is outdated. remove, if ISE-error is never thrown at the end
+            // skip all leading zeros
+            int leadingGarbage = 0;
+            while (byteBuffer.remaining() > 0) {
+                byte b = byteBuffer.get();
+                if (b == 0) {
+                    leadingGarbage++;
+                } else break;
+            }
+            startPosition += leadingGarbage;
+            byteBuffer.position(startPosition);
+            if (leadingGarbage > 0) throw new IllegalStateException("garbage detected!");
+        // OUTDATED
 
-        //String DEBUG_VORDERBAND = byteBuffer.duplicate().getString(Charset.forName("UTF-8").newDecoder());
+        //String DEBUG_VORDERBAND = byteBuffer.duplicate().getString(charsetDecoder);
 
         if (!byteBuffer.hasRemaining()) return null;
         
@@ -73,9 +79,9 @@
                     byteBuffer.position(endPosition);
                     byteBuffer.limit(limit);
                 }
-                //String DEBUG_HINTERBAND = stanzaBuffer.duplicate().getString(Charset.forName("UTF-8").newDecoder());
-                //String DEBUG_REMAINS = byteBuffer.duplicate().getString(Charset.forName("UTF-8").newDecoder());
-                return new XMLParticle(stanzaBuffer.getString(Charset.forName("UTF-8").newDecoder()));
+                //String DEBUG_HINTERBAND = stanzaBuffer.duplicate().getString(charsetDecoder);
+                //String DEBUG_REMAINS = byteBuffer.duplicate().getString(charsetDecoder);
+                return new XMLParticle(stanzaBuffer.getString(charsetDecoder));
             }
         }
         // no complete stanza found. prepare next read

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/xmldecoder/XMLParticle.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/xmldecoder/XMLParticle.java?rev=588750&r1=588749&r2=588750&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/xmldecoder/XMLParticle.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/xmldecoder/XMLParticle.java Fri Oct 26 12:04:28 2007
@@ -20,6 +20,7 @@
 import java.util.regex.Pattern;
 
 /**
+ * holds a particle of XML, either representing an start or end element, or an elements body, or other text nodes.
  */
 public class XMLParticle {
     
@@ -88,28 +89,24 @@
         return content;
     }
 
-    public String getContentWithoutElement() {
+    public String getContentWithoutElement() throws DecodingException {
         String elementNameLocal = getElementName();
         int i = content.indexOf(elementNameLocal);
         if (i < 0) return null;
         return content.substring(i + elementNameLocal.length());
     }
 
-    public String getElementName() {
+    public String getElementName() throws DecodingException {
         if (elementName != null) return elementName;
         if (isClosingOnlyElement()) {
             Matcher matcher = PATTERN_NAME_FROM_CLOSINGONLY_ELEMENT.matcher(content);
-            if (!matcher.matches()) {
-                // TODO handle syntax error
-            }
+            if (!matcher.matches()) throw new DecodingException("closing element name could not be determined by parser for " + content);
             elementName = matcher.group(1);
             return elementName;
         } else if (isOpeningElement()) {
             Matcher matcher = PATTERN_NAME_FROM_OPENING_ELEMENT.matcher(content);
-            if (!matcher.matches() || matcher.groupCount() < 2) {
-                System.out.println(content);
-                // TODO handle syntax error
-            }
+            boolean matches = matcher.matches();
+            if (!matches) throw new DecodingException("opening element name could not be determined by parser for " + content);
             elementName = matcher.group(1); // do caching
             return elementName;
         }

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/xmldecoder/XMLRawToFragementConverter.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/xmldecoder/XMLRawToFragementConverter.java?rev=588750&r1=588749&r2=588750&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/xmldecoder/XMLRawToFragementConverter.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/xmldecoder/XMLRawToFragementConverter.java Fri Oct 26 12:04:28 2007
@@ -26,19 +26,25 @@
 import java.util.List;
 import java.util.Stack;
 
+import com.sun.jndi.ldap.Ber;
+
 /**
+ * determines, if a sequence of XMLParticles is balanced, and if it is, converts them to XMLFragments (which are understood
+ * by core XMPP engine.
  */
 public class XMLRawToFragementConverter {
     private static final String CHAR_ATTR_EQUALS = "=";
     private static final String CHAR_ATTR_SINGLEQUOTE = "'";
     private static final String CHAR_ATTR_DOUBLEQUOTE = "\"";
 
+    /**
+     * tracks, where we are when parsing start elements and their attributes (which are composed of key/value pairs)
+     */
     enum AttributeParseState {
         BEFORE_KEY, IN_KEY, BEFORE_EQUALS, BEFORE_VALUE, IN_VALUE
     }
     
-    
-    public boolean isBalanced(List<XMLParticle> particles) {
+    public boolean isBalanced(List<XMLParticle> particles) throws DecodingException {
 
         Stack<XMLParticle> openedElements = new Stack<XMLParticle>();
         
@@ -63,13 +69,13 @@
         return openedElements.isEmpty() || startOfStream;
     }
     
-    public XMLFragment convert(List<XMLParticle> particles) {
+    public XMLFragment convert(List<XMLParticle> particles) throws DecodingException {
         List<XMLFragment> xmlFragmentList = convert(particles, null);
-        if (xmlFragmentList.size() != 1) return null; // TODO syntax error
+        if (xmlFragmentList.size() != 1) throw new DecodingException("converter only allows for one top xml element, all others must be inner elements");
         return xmlFragmentList.get(0);
     }
     
-    protected List<XMLFragment> convert(List<XMLParticle> particles, String stopElementName) {
+    protected List<XMLFragment> convert(List<XMLParticle> particles, String stopElementName) throws DecodingException {
         boolean createStanza = stopElementName == null;
 
         List<XMLFragment> fragments = new ArrayList<XMLFragment>();
@@ -91,7 +97,7 @@
                 boolean properlyClosedTopLevel = (stopElementName == null && particles.isEmpty());
                 boolean properlyClosedNested = closingElementName.equals(stopElementName);
                 if (properlyClosedTopLevel || properlyClosedNested) return fragments;
-                else throw new IllegalStateException("closing xml elements mismatch");
+                else throw new DecodingException("closing xml elements mismatch. expected '" + stopElementName + "' but found '"  + closingElementName + "'");
             } else {
                 fragments.add(new XMLText(particle.getContent()));
             } 
@@ -107,20 +113,20 @@
         return fragments;
     }
 
-    private String getElementNameForClosingOnly(XMLParticle particle) {
+    private String getElementNameForClosingOnly(XMLParticle particle) throws DecodingException {
         if (!particle.isClosingOnlyElement()) throw new IllegalArgumentException();
 
         String elementText = particle.getContent();
         String coreText = elementText.substring(2, elementText.length() - 1);
+        if (coreText.startsWith(" ")) throw new DecodingException("closing element name must follow immediately after '</' in " + coreText);
         return coreText.trim();
     }
 
-    private XMLElement getElementForOpening(XMLParticle particle, boolean createStanza) {
+    private XMLElement getElementForOpening(XMLParticle particle, boolean createStanza) throws DecodingException {
         if (!particle.isOpeningElement()) throw new IllegalArgumentException();
 
         String elementName = particle.getElementName();
-        if (elementName == null) return null; // TODO syntax error. throw exception?
-        
+        if (elementName == null) throw new DecodingException("element name could not be determined for " + particle.getContent());
 
         String content = particle.getContentWithoutElement();
         List<Attribute> attributes = parseAttributes(content);
@@ -138,7 +144,7 @@
         else return new XMLElement(elementName, namespace, attributes, innerFragments);
     }
 
-    private List<Attribute> parseAttributes(String content) {
+    private List<Attribute> parseAttributes(String content) throws DecodingException {
 
         List<Attribute> attributes = new ArrayList<Attribute>();
 
@@ -148,7 +154,9 @@
 
         Stack<Character> stack = new Stack<Character>();
         for (int i = chars.length - 1; i >= 0; i--) {
-             stack.push(chars[i]);
+            char aChar = chars[i];
+            if (aChar == '<') throw new DecodingException("illegal opening brace inside xml element: " + content);
+            stack.push(aChar);
         }
         
         String key = null;
@@ -169,7 +177,7 @@
                     else if (character == '=') {
                         currentState = AttributeParseState.BEFORE_VALUE;
                         stack.pop();
-                    } else return null; // TODO syntax error
+                    } else throw new DecodingException("'=' expected after attribute name " + key);
                     break;
                 case BEFORE_KEY:
                     if (Character.isWhitespace(character)) stack.pop();

Modified: labs/vysper/src/test/java/org/apache/vysper/mina/codec/XMLStreamTokenizerTestCase.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/test/java/org/apache/vysper/mina/codec/XMLStreamTokenizerTestCase.java?rev=588750&r1=588749&r2=588750&view=diff
==============================================================================
--- labs/vysper/src/test/java/org/apache/vysper/mina/codec/XMLStreamTokenizerTestCase.java (original)
+++ labs/vysper/src/test/java/org/apache/vysper/mina/codec/XMLStreamTokenizerTestCase.java Fri Oct 26 12:04:28 2007
@@ -19,10 +19,10 @@
 import junit.framework.TestCase;
 import org.apache.mina.common.ByteBuffer;
 import org.apache.mina.filter.codec.ProtocolDecoderOutput;
+import org.apache.vysper.charset.CharsetUtil;
 import org.apache.vysper.xmpp.xmlfragment.XMLFragment;
 import org.apache.vysper.xmpp.xmlfragment.XMLText;
 
-import java.nio.charset.Charset;
 import java.nio.charset.CharsetEncoder;
 import java.util.ArrayList;
 import java.util.Iterator;
@@ -31,7 +31,7 @@
 /**
  */
 public class XMLStreamTokenizerTestCase extends TestCase {
-    private static final CharsetEncoder CHARSET_ENCODER_UTF8 = Charset.forName("UTF-8").newEncoder();
+    private static final CharsetEncoder CHARSET_ENCODER_UTF8 = CharsetUtil.UTF8_ENCODER;
 
     public void testDecoderSimple() throws Exception {
         XMLStreamTokenizer decoder = new XMLStreamTokenizer();

Modified: labs/vysper/src/test/java/org/apache/vysper/xmpp/xmldecoder/XMLRawToFragementConverterBalancedTestCase.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/test/java/org/apache/vysper/xmpp/xmldecoder/XMLRawToFragementConverterBalancedTestCase.java?rev=588750&r1=588749&r2=588750&view=diff
==============================================================================
--- labs/vysper/src/test/java/org/apache/vysper/xmpp/xmldecoder/XMLRawToFragementConverterBalancedTestCase.java (original)
+++ labs/vysper/src/test/java/org/apache/vysper/xmpp/xmldecoder/XMLRawToFragementConverterBalancedTestCase.java Fri Oct 26 12:04:28 2007
@@ -25,7 +25,7 @@
  */
 public class XMLRawToFragementConverterBalancedTestCase extends TestCase {
 
-    public void testSimple1() {
+    public void testSimple1() throws DecodingException {
         List<XMLParticle> particles = new ArrayList<XMLParticle>();
 
         particles.add(new XMLParticle("<balanced>"));
@@ -33,7 +33,7 @@
         
         assertTrue(new XMLRawToFragementConverter().isBalanced(particles));
     }
-    public void testSimple2() {
+    public void testSimple2() throws DecodingException {
         List<XMLParticle> particles = new ArrayList<XMLParticle>();
 
         particles.add(new XMLParticle("<balanced>"));
@@ -42,28 +42,28 @@
         
         assertTrue(new XMLRawToFragementConverter().isBalanced(particles));
     }
-    public void testSimple3() {
+    public void testSimple3() throws DecodingException {
         List<XMLParticle> particles = new ArrayList<XMLParticle>();
 
         particles.add(new XMLParticle("<balanced/>"));
         
         assertTrue(new XMLRawToFragementConverter().isBalanced(particles));
     }
-    public void testSimple4() {
+    public void testSimple4() throws DecodingException {
         List<XMLParticle> particles = new ArrayList<XMLParticle>();
 
         particles.add(new XMLParticle("balanced"));
         
         assertTrue(new XMLRawToFragementConverter().isBalanced(particles));
     }
-    public void testSimple5() {
+    public void testSimple5() throws DecodingException {
         List<XMLParticle> particles = new ArrayList<XMLParticle>();
 
         particles.add(new XMLParticle("<unbalanced>"));
         
         assertFalse(new XMLRawToFragementConverter().isBalanced(particles));
     }
-    public void testSimple6() {
+    public void testSimple6() throws DecodingException {
         List<XMLParticle> particles = new ArrayList<XMLParticle>();
 
         particles.add(new XMLParticle("<unbalanced>"));
@@ -71,7 +71,7 @@
         
         assertFalse(new XMLRawToFragementConverter().isBalanced(particles));
     }
-    public void testSimple7() {
+    public void testSimple7() throws DecodingException {
         List<XMLParticle> particles = new ArrayList<XMLParticle>();
 
         particles.add(new XMLParticle("<balanced>"));
@@ -82,7 +82,7 @@
     }
 
 
-    public void testNested1() {
+    public void testNested1() throws DecodingException {
         List<XMLParticle> particles = new ArrayList<XMLParticle>();
 
         particles.add(new XMLParticle("<balanced>"));
@@ -91,7 +91,7 @@
         
         assertTrue(new XMLRawToFragementConverter().isBalanced(particles));
     }
-    public void testNested2() {
+    public void testNested2() throws DecodingException {
         List<XMLParticle> particles = new ArrayList<XMLParticle>();
 
         particles.add(new XMLParticle("<balanced>"));
@@ -100,7 +100,7 @@
         
         assertTrue(new XMLRawToFragementConverter().isBalanced(particles));
     }
-    public void testNested3() {
+    public void testNested3() throws DecodingException {
         List<XMLParticle> particles = new ArrayList<XMLParticle>();
 
         particles.add(new XMLParticle("<balanced>"));
@@ -110,7 +110,7 @@
         
         assertTrue(new XMLRawToFragementConverter().isBalanced(particles));
     }
-    public void testNested4() {
+    public void testNested4() throws DecodingException {
         List<XMLParticle> particles = new ArrayList<XMLParticle>();
 
         particles.add(new XMLParticle("<unbalanced>"));

Copied: labs/vysper/src/test/java/org/apache/vysper/xmpp/xmldecoder/XMLRawToFragementConverterConvertErrorTestCase.java (from r586960, labs/vysper/src/test/java/org/apache/vysper/xmpp/xmldecoder/XMLRawToFragementConverterConvertTestCase.java)
URL: http://svn.apache.org/viewvc/labs/vysper/src/test/java/org/apache/vysper/xmpp/xmldecoder/XMLRawToFragementConverterConvertErrorTestCase.java?p2=labs/vysper/src/test/java/org/apache/vysper/xmpp/xmldecoder/XMLRawToFragementConverterConvertErrorTestCase.java&p1=labs/vysper/src/test/java/org/apache/vysper/xmpp/xmldecoder/XMLRawToFragementConverterConvertTestCase.java&r1=586960&r2=588750&rev=588750&view=diff
==============================================================================
--- labs/vysper/src/test/java/org/apache/vysper/xmpp/xmldecoder/XMLRawToFragementConverterConvertTestCase.java (original)
+++ labs/vysper/src/test/java/org/apache/vysper/xmpp/xmldecoder/XMLRawToFragementConverterConvertErrorTestCase.java Fri Oct 26 12:04:28 2007
@@ -17,16 +17,13 @@
 package org.apache.vysper.xmpp.xmldecoder;
 
 import junit.framework.TestCase;
-import org.apache.vysper.xmpp.xmlfragment.XMLElement;
-import org.apache.vysper.xmpp.xmlfragment.XMLFragment;
-import org.apache.vysper.xmpp.xmlfragment.Attribute;
 
 import java.util.ArrayList;
 import java.util.List;
 
 /**
  */
-public class XMLRawToFragementConverterConvertTestCase extends TestCase {
+public class XMLRawToFragementConverterConvertErrorTestCase extends TestCase {
     private XMLRawToFragementConverter xmlRawToFragementConverter;
 
     protected void setUp() throws Exception {
@@ -34,71 +31,150 @@
         xmlRawToFragementConverter = new XMLRawToFragementConverter();
     }
 
-    public void testElementSimple() {
+    public void testTwoToplevels() throws DecodingException {
         List<XMLParticle> particles = new ArrayList<XMLParticle>();
 
-        particles.add(new XMLParticle("<balanced/>"));
+        particles.add(new XMLParticle("<balanced>"));
+        particles.add(new XMLParticle("</balanced>"));
+        particles.add(new XMLParticle("<illegal-top-level>"));
+        particles.add(new XMLParticle("</illegal-top-level>"));
 
-        XMLElement xmlElement = (XMLElement) xmlRawToFragementConverter.convert(particles);
-        assertNotNull(xmlElement);
-        assertEquals("balanced", xmlElement.getName());
+        try {
+            xmlRawToFragementConverter.convert(particles);
+            fail("to consecutive elements are not allowed on top level");
+        } catch (DecodingException e) {
+            // test succeded
+        }
     }
     
-    public void testElementSimpleAttributes() {
+    public void testTwoToplevels2() throws DecodingException {
         List<XMLParticle> particles = new ArrayList<XMLParticle>();
 
-        particles.add(new XMLParticle("<balanced attr1=\"1\" attr2='2' ns:at_tr3=\"\" />"));
+        particles.add(new XMLParticle("<balanced>"));
+        particles.add(new XMLParticle("<inner />"));
+        particles.add(new XMLParticle("</balanced>"));
+        particles.add(new XMLParticle("<illegal-top-level>"));
+        particles.add(new XMLParticle("</illegal-top-level>"));
 
-        XMLElement xmlElement = (XMLElement) xmlRawToFragementConverter.convert(particles);
-        assertNotNull(xmlElement);
-        assertEquals("balanced", xmlElement.getName());
-        List<Attribute> attributes = xmlElement.getAttributes();
-        assertEquals(3, attributes.size());
-        assertEquals("attr1", attributes.get(0).getName());
-        assertEquals("1", attributes.get(0).getValue());
-        assertEquals("attr2", attributes.get(1).getName());
-        assertEquals("2", attributes.get(1).getValue());
-        assertEquals("ns:at_tr3", attributes.get(2).getName());
-        assertEquals("", attributes.get(2).getValue());
+        try {
+            xmlRawToFragementConverter.convert(particles);
+            fail("to consecutive elements are not allowed on top level");
+        } catch (DecodingException e) {
+            // test succeded
+        }
     }
-    public void testElementSimpleAttributesMoreWhitespace() {
+    
+    public void testIllegalOpening() throws DecodingException {
         List<XMLParticle> particles = new ArrayList<XMLParticle>();
 
-        particles.add(new XMLParticle("<balanced attr1 = \"1\"   attr2=' 2 ' ns:at_tr3= \" \" />"));
+        particles.add(new XMLParticle("<start <illegal start>> "));
+        try {
+            xmlRawToFragementConverter.convert(particles);
+            fail("opening not allowed inside element");
+        } catch (DecodingException e) {
+            // test succeded
+        }
+    }
+    
+    public void testIllegalOpeningAfterAttr() throws DecodingException {
+        List<XMLParticle> particles = new ArrayList<XMLParticle>();
 
-        XMLElement xmlElement = (XMLElement) xmlRawToFragementConverter.convert(particles);
-        assertNotNull(xmlElement);
-        assertEquals("balanced", xmlElement.getName());
-        List<Attribute> attributes = xmlElement.getAttributes();
-        assertEquals(3, attributes.size());
-        assertEquals("attr1", attributes.get(0).getName());
-        assertEquals("1", attributes.get(0).getValue());
-        assertEquals("attr2", attributes.get(1).getName());
-        assertEquals(" 2 ", attributes.get(1).getValue());
-        assertEquals("ns:at_tr3", attributes.get(2).getName());
-        assertEquals(" ", attributes.get(2).getValue());
+        particles.add(new XMLParticle("<start attr='val' <illegal start>>"));
+        try {
+            xmlRawToFragementConverter.convert(particles);
+            fail("opening not allowed inside element");
+        } catch (DecodingException e) {
+            // test succeded
+        }
     }
     
-    public void testSimple2() {
+    public void testLateOpeningElementName() throws DecodingException {
         List<XMLParticle> particles = new ArrayList<XMLParticle>();
 
-        particles.add(new XMLParticle("<balanced>"));
-        particles.add(new XMLParticle("</balanced>"));
+        particles.add(new XMLParticle("< latename attr='val'>"));
+        try {
+            xmlRawToFragementConverter.convert(particles);
+            fail("opening xml element name to follow immediately after '<' ");
+        } catch (DecodingException e) {
+            // test succeded
+        }
+    }
+    
+    public void testWrongClosingElementName() throws DecodingException {
+        List<XMLParticle> particles = new ArrayList<XMLParticle>();
 
-        XMLElement xmlElement = (XMLElement) xmlRawToFragementConverter.convert(particles);
-        assertNotNull(xmlElement);
+        particles.add(new XMLParticle("<name attr='val'>"));
+        particles.add(new XMLParticle("</nameother >"));
+        try {
+            xmlRawToFragementConverter.convert(particles);
+            fail("closing xml element name to follow immediately after '</'");
+        } catch (DecodingException e) {
+            // test succeded
+        }
     }
     
-    public void testNested1() {
+    public void testWrongClosingElementWhitespace() throws DecodingException {
         List<XMLParticle> particles = new ArrayList<XMLParticle>();
 
-        particles.add(new XMLParticle("<balanced>"));
-        particles.add(new XMLParticle("<inner>"));
-        particles.add(new XMLParticle("</inner>"));
-        particles.add(new XMLParticle("</balanced>"));
-        
-        XMLElement xmlElement = (XMLElement) xmlRawToFragementConverter.convert(particles);
-        assertEquals(1, ((XMLElement)xmlElement).getInnerElements().size());
+        particles.add(new XMLParticle("<name attr='val'>"));
+        particles.add(new XMLParticle("</ name >"));
+        try {
+            xmlRawToFragementConverter.convert(particles);
+            fail("closing xml element name to follow immediately after '</'");
+        } catch (DecodingException e) {
+            // test succeded
+        }
+    }
+    
+    public void testWrongClosingElementWhitespace2() throws DecodingException {
+        List<XMLParticle> particles = new ArrayList<XMLParticle>();
+
+        particles.add(new XMLParticle("<name attr='val'>"));
+        particles.add(new XMLParticle("< /name >"));
+        try {
+            xmlRawToFragementConverter.convert(particles);
+            fail("closing xml element must start with '</'");
+        } catch (DecodingException e) {
+            // test succeded
+        }
+    }
+
+    public void testStartElementEmpty() throws DecodingException {
+        List<XMLParticle> particles = new ArrayList<XMLParticle>();
+
+        XMLParticle particle = new XMLParticle("< />");
+        String name = particle.getElementName();
+        particles.add(particle);
+        try {
+            xmlRawToFragementConverter.convert(particles);
+            fail("closing xml element must not be empty");
+        } catch (DecodingException e) {
+            // test succeded
+        }
+    }
+
+    public void testWrongOpeningClosingElementWhitespace2() throws DecodingException {
+        List<XMLParticle> particles = new ArrayList<XMLParticle>();
+
+        particles.add(new XMLParticle("<name attr='val' / >"));
+        try {
+            xmlRawToFragementConverter.convert(particles);
+            fail("opening+closing xml element must end with '/>'");
+        } catch (DecodingException e) {
+            // test succeded
+        }
+    }
+
+    public void testAttributeKeyIsNotFollowedByEquals() throws DecodingException {
+        List<XMLParticle> particles = new ArrayList<XMLParticle>();
+
+        particles.add(new XMLParticle("<name attr 'val' />"));
+        try {
+            xmlRawToFragementConverter.convert(particles);
+            fail("attribute and value must be separated by '='");
+        } catch (DecodingException e) {
+            // test succeded
+        }
     }
     
 }

Modified: labs/vysper/src/test/java/org/apache/vysper/xmpp/xmldecoder/XMLRawToFragementConverterConvertTestCase.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/test/java/org/apache/vysper/xmpp/xmldecoder/XMLRawToFragementConverterConvertTestCase.java?rev=588750&r1=588749&r2=588750&view=diff
==============================================================================
--- labs/vysper/src/test/java/org/apache/vysper/xmpp/xmldecoder/XMLRawToFragementConverterConvertTestCase.java (original)
+++ labs/vysper/src/test/java/org/apache/vysper/xmpp/xmldecoder/XMLRawToFragementConverterConvertTestCase.java Fri Oct 26 12:04:28 2007
@@ -34,7 +34,7 @@
         xmlRawToFragementConverter = new XMLRawToFragementConverter();
     }
 
-    public void testElementSimple() {
+    public void testElementSimple() throws DecodingException {
         List<XMLParticle> particles = new ArrayList<XMLParticle>();
 
         particles.add(new XMLParticle("<balanced/>"));
@@ -44,7 +44,7 @@
         assertEquals("balanced", xmlElement.getName());
     }
     
-    public void testElementSimpleAttributes() {
+    public void testElementSimpleAttributes() throws DecodingException {
         List<XMLParticle> particles = new ArrayList<XMLParticle>();
 
         particles.add(new XMLParticle("<balanced attr1=\"1\" attr2='2' ns:at_tr3=\"\" />"));
@@ -61,7 +61,7 @@
         assertEquals("ns:at_tr3", attributes.get(2).getName());
         assertEquals("", attributes.get(2).getValue());
     }
-    public void testElementSimpleAttributesMoreWhitespace() {
+    public void testElementSimpleAttributesMoreWhitespace() throws DecodingException {
         List<XMLParticle> particles = new ArrayList<XMLParticle>();
 
         particles.add(new XMLParticle("<balanced attr1 = \"1\"   attr2=' 2 ' ns:at_tr3= \" \" />"));
@@ -79,7 +79,7 @@
         assertEquals(" ", attributes.get(2).getValue());
     }
     
-    public void testSimple2() {
+    public void testSimple2() throws DecodingException {
         List<XMLParticle> particles = new ArrayList<XMLParticle>();
 
         particles.add(new XMLParticle("<balanced>"));
@@ -89,7 +89,7 @@
         assertNotNull(xmlElement);
     }
     
-    public void testNested1() {
+    public void testNested1() throws DecodingException {
         List<XMLParticle> particles = new ArrayList<XMLParticle>();
 
         particles.add(new XMLParticle("<balanced>"));



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org