11/*
2- * Copyright 2005-2010 the original author or authors.
2+ * Copyright 2005-2012 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
66 * You may obtain a copy of the License at
77 *
8- * http://www.apache.org/licenses/LICENSE-2.0
8+ * http://www.apache.org/licenses/LICENSE-2.0
99 *
1010 * Unless required by applicable law or agreed to in writing, software
1111 * distributed under the License is distributed on an "AS IS" BASIS,
1818
1919import java .io .ByteArrayOutputStream ;
2020import java .io .StringReader ;
21+ import java .util .Iterator ;
2122
2223import org .apache .axiom .om .OMAbstractFactory ;
2324import org .apache .axiom .om .OMDocument ;
3132import org .xml .sax .helpers .XMLReaderFactory ;
3233
3334import static org .custommonkey .xmlunit .XMLAssert .assertXMLEqual ;
35+ import static org .junit .Assert .assertEquals ;
36+ import static org .junit .Assert .assertTrue ;
3437
3538public class AxiomHandlerTest {
3639
@@ -49,6 +52,10 @@ public class AxiomHandlerTest {
4952 private static final String XML_3_ENTITY =
5053 "<predefined-entity-reference><>&"'</predefined-entity-reference>" ;
5154
55+ private static final String XML_4_SNIPPET = "<?xml version='1.0' encoding='UTF-8'?>" + "<child xmlns='namespace1' />" ;
56+
57+ private static final String XML_5_SNIPPET = "<?xml version='1.0' encoding='UTF-8'?>" + "<x:child xmlns:x='namespace1' />" ;
58+
5259 private AxiomHandler handler ;
5360
5461 private OMDocument result ;
@@ -98,6 +105,48 @@ public void testContentHandlerElement() throws Exception {
98105 result .serialize (bos );
99106 assertXMLEqual ("Invalid result" , XML_2_EXPECTED , bos .toString ("UTF-8" ));
100107 }
108+
109+ @ Test
110+ public void testContentHandlerElementWithSamePrefixAndDifferentNamespace () throws Exception {
111+ OMNamespace namespace = factory .createOMNamespace ("namespace1" , "" );
112+ OMElement rootElement = factory .createOMElement ("root" , namespace , result );
113+ handler = new AxiomHandler (rootElement , factory );
114+ xmlReader .setContentHandler (handler );
115+ xmlReader .parse (new InputSource (new StringReader (XML_2_SNIPPET )));
116+ Iterator <?> it = result .getOMDocumentElement ().getChildrenWithLocalName ("child" );
117+ assertTrue (it .hasNext ());
118+ OMElement child = (OMElement ) it .next ();
119+ assertEquals ("" , child .getQName ().getPrefix ());
120+ assertEquals ("namespace2" , child .getQName ().getNamespaceURI ());
121+ }
122+
123+ @ Test
124+ public void testContentHandlerElementWithSameNamespacesAndPrefix () throws Exception {
125+ OMNamespace namespace = factory .createOMNamespace ("namespace1" , "" );
126+ OMElement rootElement = factory .createOMElement ("root" , namespace , result );
127+ handler = new AxiomHandler (rootElement , factory );
128+ xmlReader .setContentHandler (handler );
129+ xmlReader .parse (new InputSource (new StringReader (XML_4_SNIPPET )));
130+ Iterator <?> it = result .getOMDocumentElement ().getChildrenWithLocalName ("child" );
131+ assertTrue (it .hasNext ());
132+ OMElement child = (OMElement ) it .next ();
133+ assertEquals ("" , child .getQName ().getPrefix ());
134+ assertEquals ("namespace1" , child .getQName ().getNamespaceURI ());
135+ }
136+
137+ @ Test
138+ public void testContentHandlerElementWithSameNamespacesAndDifferentPrefix () throws Exception {
139+ OMNamespace namespace = factory .createOMNamespace ("namespace1" , "" );
140+ OMElement rootElement = factory .createOMElement ("root" , namespace , result );
141+ handler = new AxiomHandler (rootElement , factory );
142+ xmlReader .setContentHandler (handler );
143+ xmlReader .parse (new InputSource (new StringReader (XML_5_SNIPPET )));
144+ Iterator <?> it = result .getOMDocumentElement ().getChildrenWithLocalName ("child" );
145+ assertTrue (it .hasNext ());
146+ OMElement child = (OMElement ) it .next ();
147+ assertEquals ("x" , child .getQName ().getPrefix ());
148+ assertEquals ("namespace1" , child .getQName ().getNamespaceURI ());
149+ }
101150
102151 @ Test
103152 public void testContentHandlerPredefinedEntityReference () throws Exception {
@@ -109,4 +158,4 @@ public void testContentHandlerPredefinedEntityReference() throws Exception {
109158 result .serialize (bos );
110159 assertXMLEqual ("Invalid result" , XML_3_ENTITY , bos .toString ("UTF-8" ));
111160 }
112- }
161+ }
0 commit comments