/** 
 * 
 * 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.activemq.ws.notification;

import junit.framework.TestCase;
import org.activemq.ws.xmlbeans.addressing.v2003_03.AttributedURI;
import org.activemq.ws.xmlbeans.addressing.v2003_03.EndpointReferenceType;
import org.activemq.ws.xmlbeans.addressing.v2003_03.ReferencePropertiesType;
import org.activemq.ws.xmlbeans.notification.base.SubscribeDocument;
import org.activemq.ws.xmlbeans.resource.properties.QueryExpressionType;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlAnyURI;

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

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

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

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

        SubscribeDocument doc = (SubscribeDocument) xmlObject;
        SubscribeDocument.Subscribe subscribe = doc.getSubscribe();


        // lets get the endpoint reference
        EndpointReferenceType endpointReference = subscribe.getConsumerReference();
        AttributedURI address = endpointReference.getAddress();
        System.out.println("EndpointReference address is: " + address.getStringValue());

        ReferencePropertiesType referenceProperties = endpointReference.getReferenceProperties();
        System.out.println("Reference properties: " + referenceProperties);


        // lets get the selector
        QueryExpressionType selector = subscribe.getSelector();
        String dialect = selector.getDialect();
        XmlAnyURI dialectURI = selector.xgetDialect();
        String text = selector.xmlText();
        System.out.println("Selector dialect: " + dialect + " of uri: " + dialectURI + " with value: " + text);


        // termination time
        Calendar initialTerminationTime = subscribe.getInitialTerminationTime();
        System.out.println("initialTerminationTime: " + initialTerminationTime);


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

    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;
    }
}
