/** 
 * 
 * Copyright 2004 Protique Ltd
 * 
 * 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.codehaus.activemq.ws.soap;

import junit.framework.TestCase;
import org.apache.xmlbeans.XmlObject;
import org.codehaus.activemq.ws.schema.eventing.SubscribeDocument;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.IOException;
import java.net.URL;

/**
 * @version $Revision: 1.1 $
 */
public class EnvelopeParserTest extends TestCase {
    public void testParseXmlBeansUsingURL() throws Exception {
        SubscribeDocument doc = SubscribeDocument.Factory.parse(getClass().getResource("subscribe.xml"));
        //System.out.println("Parsed: " + doc);
    }

    public void testParseXmlBeansUsingStAX() throws Exception {
        XMLStreamReader in = createXMLStreamReader(getClass().getResource("subscribe.xml"));

        XmlObject xmlObject = XmlObject.Factory.parse(in);
        assertTrue("should be a SubscribeDocument: " + xmlObject.getClass(), xmlObject instanceof SubscribeDocument);

        SubscribeDocument doc = (SubscribeDocument) xmlObject;
        //System.out.println("Parsed: " + doc);
    }

    public void testParseSubscribeRequest() throws Exception {
        XMLStreamReader in = createXMLStreamReader(getClass().getResource("subscribeRequest.xml"));

        EnvelopeParser parser = new EnvelopeParser(in);
        parser.parse();

        XmlObject xmlObject = parser.getBody();
        assertTrue("should be a SubscribeDocument: " + xmlObject.getClass(), xmlObject instanceof SubscribeDocument);

        SubscribeDocument doc = (SubscribeDocument) xmlObject;
        System.out.println("Parsed: " + doc);
    }

    public void testParseUnknownRequest() throws Exception {
        XMLStreamReader in = createXMLStreamReader(getClass().getResource("unknownRequest.xml"));

        EnvelopeParser parser = new EnvelopeParser(in);
        parser.parse();
    }

    protected XMLStreamReader createXMLStreamReader(URL resource) throws XMLStreamException, IOException {
        assertTrue("Found resource", resource != null);

        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        XMLStreamReader in = inputFactory.createXMLStreamReader(resource.openStream());
        return in;
    }
}
