/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.jackrabbit.rmi.xml;

import java.io.UnsupportedEncodingException;

import javax.jcr.RepositoryException;

import junit.framework.TestCase;

import org.apache.jackrabbit.rmi.xml.ImportContentHandler;
import org.xml.sax.SAXException;

public class ImportContentHandlerTest extends TestCase {

    public void testImportContentHandler() throws RepositoryException, SAXException {
        // fail test if handler cannot be set up
        DummyImportContentHandler ch = new DummyImportContentHandler();

        // these may throw SAXException
        ch.startDocument();
        ch.startElement(null, "sample", "sample", null);
        ch.endElement(null, "sample", "sample");
        ch.endDocument();

        byte[] xml = ch.getXML();
        assertNotNull("Serialized XML is null", xml);
        assertTrue("Serialized XML is empty", xml.length > 0);

        // for the moment we don't actually care for the concrete contents
    }

    private static class DummyImportContentHandler extends ImportContentHandler {

        private byte[] xml;

        DummyImportContentHandler() throws RepositoryException {
            super();
        }

        protected void importXML(byte[] xml) throws Exception {
            this.xml = xml;
        }

        byte[] getXML() {
            return xml;
        }
    }
}

@@ -845,54 +845,8 @@
 l;%0A%0A
-import java.io.UnsupportedEncodingException;%0A%0A
 impo
@@ -992,28 +992,38 @@
 xml.sax.
-SAXException
+helpers.AttributesImpl
 ;%0A%0Apubli
@@ -1129,32 +1129,8 @@
 ows 
-RepositoryException, SAX
 Exce
@@ -1343,28 +1343,135 @@
 ch.start
-Element(null
+PrefixMapping(%22foo%22, %22http://example.com/ns/foo%22);%0A        ch.startElement(%0A                %22http://example.com/ns/foo%22
 , %22sampl
@@ -1475,24 +1475,28 @@
 mple%22, %22
+foo:
 sample%22,
  null);%0A
@@ -1491,42 +1491,97 @@
 le%22,
- null);%0A        ch.endElement(null
+%0A                new AttributesImpl());%0A        ch.endElement(%22http://example.com/ns/foo%22
 , %22s
@@ -1585,24 +1585,28 @@
  %22sample%22, %22
+foo:
 sample%22);%0A  
@@ -1599,24 +1599,60 @@
 o:sample%22);%0A
+        ch.endPrefixMapping(%22foo%22);%0A
         ch.e
@@ -1678,226 +1678,162 @@
     
-byte%5B%5D
+String
  xml = 
-ch.getXML();%0A        assertNotNull(%22Serialized XML is null%22, xml);%0A        assertTrue(%22Serialized XML is empty%22, xml.length %3E 0);%0A%0A        // for the moment we don't actually care for the concrete contents
+new String(ch.getXML(), %22UTF-8%22);%0A        assertTrue(xml.contains(%0A                %22%3Cfoo:sample xmlns:foo=%5C%22http://example.com/ns/foo%5C%22/%3E%22));
 %0A   

Replace use of Xerces by JAXP to implement SAX DocumentHandler - Improved test case, previous one was throwing an NPE because of the null attributes argument to startElement()