All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.spardat.xma.boot.test.XMA_URITest Maven / Gradle / Ivy

/*******************************************************************************
 * Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/

/*
 * @(#) $Id: XMA_URITest.java 2084 2007-11-27 14:53:31Z s3460 $
 */
package at.spardat.xma.boot.test;

import java.net.MalformedURLException;
import java.util.Properties;

import at.spardat.xma.boot.transport.XMA_URI;

import junit.framework.Assert;
import junit.framework.TestCase;

public class XMA_URITest extends TestCase {

    public XMA_URITest(String arg0) {
        super(arg0);
  }

    public static void main(String[] args) {
        junit.awtui.TestRunner.run(XMA_URITest.class);
    }

    protected void setUp() throws Exception {
        Properties props = new Properties();
        props.setProperty("boot.transport.urlencoding","CP1252");
        XMA_URI.setProperties(props);
    }

    /*
     * tests XMA_URI(String), toString(), getHTTP_URI(), XMA_URI(URL)
     */
    public void testXMA_URI() throws MalformedURLException {
        String string = "http://localhost:8080/xma_samplespar/Sparlist";
        XMA_URI uri = new XMA_URI(string);
        Assert.assertEquals(string,uri.toString());

        uri = new XMA_URI(uri.getHTTP_URI());
        Assert.assertEquals(string,uri.toString());

        // with parameters
        string = "http://server/app/comp/res?param=a¶2=b";
        uri = new XMA_URI(string);
        Assert.assertEquals(string,uri.toString());

        uri = new XMA_URI(uri.getHTTP_URI());
        Assert.assertEquals(string,uri.toString());


        // additional / and &
        string = "http://server//app//comp//res?param=a&¶2=b&&&p3=aus&";
        String escaped = "http://server/app/comp/res?p3=aus¶m=a¶2=b";
        uri = new XMA_URI(string);
        Assert.assertEquals(escaped,uri.toString());

        uri = new XMA_URI(uri.getHTTP_URI());
        Assert.assertEquals(escaped,uri.toString());



        // special characters in parameters
        string = "http://server/app/comp/res?name=\"i\" /?&v a l u e=?";
        escaped = "http://server/app/comp/res?v+a+l+u+e=%80&name=%22i%22+%2F%3F";
        uri = new XMA_URI(string);
        Assert.assertEquals(escaped,uri.toString());

        uri = new XMA_URI(uri.getHTTP_URI());
        Assert.assertEquals(escaped,uri.toString());


        // escape special characters everywhere
        string = "http://s?rwa.at/ap p//c?mp/r??/re/r?name=\"i\" /?&v a l u e=?";
        escaped = "http://s%F6rwa.at/ap+p/c%F6mp/r%E4%DF/re/r?v+a+l+u+e=%80&name=%22i%22+%2F%3F";
        uri = new XMA_URI(string);
        Assert.assertEquals(escaped,uri.toString());

        uri = new XMA_URI(uri.getHTTP_URI());
        Assert.assertEquals(escaped,uri.toString());


        // escaped / in resource
        string="http://s.a.t/app/comp/res%2Fres";
        uri = new XMA_URI(string);
        Assert.assertEquals(string,uri.toString());

        uri = new XMA_URI(uri.getHTTP_URI());
        Assert.assertEquals(string,uri.toString());
    }

    /*
     * Class under test for void XMA_URI(XMA_URI)
     */
    public void testXMA_URIXMA_URI() throws MalformedURLException {
        // minimal uri
        String string = "http://server";
        XMA_URI uri = new XMA_URI(string);
        uri = new XMA_URI(uri);
        Assert.assertEquals(string,uri.toString());


        // escape special characters everywhere
        string = "http://s?rwa.at/ap p//c?mp/r??/re/r?name=\"i\" /?&v a l u e=?";
        String ecaped = "http://s%F6rwa.at/ap+p/c%F6mp/r%E4%DF/re/r?v+a+l+u+e=%80&name=%22i%22+%2F%3F";
        uri = new XMA_URI(string);
        uri = new XMA_URI(uri);
        Assert.assertEquals(ecaped,uri.toString());
    }

    public void testGetApplicationURI() throws MalformedURLException {
        // escape special characters everywhere
        String string = "http://s?rwa.at/ap p//c?mp/r??/re/r?name=\"i\" /?&v a l u e=?";
        String ecaped = "http://s%F6rwa.at/ap+p";
        XMA_URI uri = new XMA_URI(string);
        Assert.assertEquals(ecaped,uri.getApplicationURI().toString());
    }

    public void testGetHTTP_AppUri() throws MalformedURLException {
        // simple
        String string = "http://server.at/app/comp/res/re/r?name=value";
        String resource = "index.html" ;
        String ecaped = "http://server.at/app/index.html";
        XMA_URI uri = new XMA_URI(string);
        Assert.assertEquals(ecaped,uri.getHTTP_AppUri(resource).toString());

        // more
        string = "http://server.at/app/comp/res/re/r?name=value";
        resource = "myservlet/index.html" ;
        ecaped = "http://server.at/app/myservlet/index.html";
        uri = new XMA_URI(string);
        Assert.assertEquals(ecaped,uri.getHTTP_AppUri(resource).toString());

        // escape special characters everywhere
        string = "http://s?rwa.at/ap p//c?mp/r??/re/r?name=\"i\" /?&v a l u e=?";
        resource = "my??rflet/up/index html" ;
        ecaped = "http://s%F6rwa.at/ap+p/my%DF%F6rflet/up/index+html";
        uri = new XMA_URI(string);
        Assert.assertEquals(ecaped,uri.getHTTP_AppUri(resource).toString());
    }

    public void testGetDEFAULT_PROTOCOL() {
        Assert.assertEquals("http",XMA_URI.getDEFAULT_PROTOCOL());
    }

    public void testGetApplication() throws MalformedURLException {
        // simple
        String string = "http://server.at/app/comp/res/re/r?name=value";
        XMA_URI uri = new XMA_URI(string);
        Assert.assertEquals("app",uri.getApplication());

        // special characters
        string = "http://server.at/a pp?/comp/res/re/r?name=value";
        uri = new XMA_URI(string);
        Assert.assertEquals("a pp?",uri.getApplication());
    }

    public void testGetComponent() throws MalformedURLException {
        // simple
        String string = "http://server.at/app/comp/res/re/r?name=value";
        XMA_URI uri = new XMA_URI(string);
        Assert.assertEquals("comp",uri.getComponent());

        // special characters
        string = "http://server.at/app/c?mp ?/res/re/r?name=value";
        uri = new XMA_URI(string);
        Assert.assertEquals("c?mp ?",uri.getComponent());

        // setter
        uri.setComponent("jaja");
        Assert.assertEquals("jaja",uri.getComponent());
        uri.setComponent("ja n?");
        Assert.assertEquals("ja n?",uri.getComponent());
    }

    public void testGetEnvironment() throws MalformedURLException {
        String string = "http://server/app/comp/res/re/r?name=value";
        XMA_URI uri = new XMA_URI(string);
        Assert.assertEquals(null,uri.getEnvironment()); // not implmented -> allways null
    }

    public void testGetInstallation() throws MalformedURLException {
        String string = "http://server/app/comp/res/re/r?name=value";
        XMA_URI uri = new XMA_URI(string);
        Assert.assertEquals(null,uri.getInstallation()); // not implmented -> allways null
    }

    public void testGetPort_() throws MalformedURLException {
        // default
        String string = "http://server/app/comp/res/re/r?name=value";
        XMA_URI uri = new XMA_URI(string);
        Assert.assertEquals(-1,uri.getPort_());

        // defined
        string = "http://server:8080/app/comp/res/re/r?name=value";
        uri = new XMA_URI(string);
        Assert.assertEquals(8080,uri.getPort_());

        // setter
        uri.setPort(7001);
        Assert.assertEquals(7001,uri.getPort_());
    }

    public void testGetProtocol_() throws MalformedURLException {
        // http
        String string = "http://server/app/comp/res/re/r?name=value";
        XMA_URI uri = new XMA_URI(string);
        Assert.assertEquals("http",uri.getProtocol_());
        Assert.assertEquals(-1,uri.getPort_());

        // https
        string = "https://server/app/comp/res/re/r?name=value";
        uri = new XMA_URI(string);
        Assert.assertEquals("https",uri.getProtocol_());
        Assert.assertEquals(-1,uri.getPort_());

        // setter
        uri.setProtocol("http");
        Assert.assertEquals("http",uri.getProtocol_());
    }

    public void testGetResource() throws MalformedURLException {
        XMA_URI uri = new XMA_URI("http://server.lan.at/app/comp");

        // simple
        String resource ="hallo";
        uri.setResource(resource);
        Assert.assertEquals(resource,uri.getResource());

        // more
        resource ="hallo/du/da";
        uri.setResource(resource);
        Assert.assertEquals(resource,uri.getResource());

        // escape
        resource ="h?ll?";
        uri.setResource(resource);
        Assert.assertEquals(resource,uri.getResource());

        // more + escape
        resource ="h?llo/du%2Fda?";
        uri.setResource(resource);
        Assert.assertEquals(resource,uri.getResource());

        // dupplicate and trailing / disappears
        resource ="hallo//du/da/";
        String expected="hallo/du/da";
        uri.setResource(resource);
        Assert.assertEquals(expected,uri.getResource());

    }

    public void testGetServer() throws MalformedURLException {
        XMA_URI uri = new XMA_URI("http://server.lan.at/app/comp");
        Assert.assertEquals("server.lan.at",uri.getServer());

        uri = new XMA_URI("http://s?r ver.an.at/app/comp");
        Assert.assertEquals("s?r ver.an.at",uri.getServer());
    }

    public void testGetter() throws MalformedURLException {
        // escape special characters everywhere
        String string = "http://s?rwa.at:443/ap p//c?mp/r??/re/r?name=\"i\" /?&v a l u e=?";
        XMA_URI uri = new XMA_URI(string);
        Assert.assertEquals("http",uri.getProtocol_());
        Assert.assertEquals(443,uri.getPort_());
        Assert.assertEquals("s?rwa.at",uri.getServer());
        Assert.assertEquals("ap p",uri.getApplication());
        Assert.assertEquals("c?mp",uri.getComponent());
        Assert.assertEquals("r??/re/r",uri.getResource());

        // escaped
        string = "http://s%F6rwa.at:443/ap+p/c%F6mp/r%E4%DF/re/r?v+a+l+u+e=%80&name=%22i%22+%2F%3F";
        uri = new XMA_URI(string);
        Assert.assertEquals("http",uri.getProtocol_());
        Assert.assertEquals(443,uri.getPort_());
        Assert.assertEquals("s?rwa.at",uri.getServer());
        Assert.assertEquals("ap p",uri.getApplication());
        Assert.assertEquals("c?mp",uri.getComponent());
        Assert.assertEquals("r??/re/r",uri.getResource());
    }

    public void testSetProperties() throws MalformedURLException {
        Properties props = new Properties();
        // utf-8
        props.setProperty("boot.transport.urlencoding","UTF-8");
        XMA_URI.setProperties(props);
        String string = "http://s?rwa.at:443/ap p//c?mp/r??/re/r?name=\"i\" /?&v a l u e=?";
        String escaped = "http://s%C3%B6rwa.at:443/ap+p/c%C3%B6mp/r%C3%A4%C3%9F/re/r?v+a+l+u+e=%E2%82%AC&name=%22i%22+%2F%3F";
        XMA_URI uri = new XMA_URI(string);
        Assert.assertEquals(escaped,uri.toString());

        // default
        props.setProperty("boot.transport.urlencoding","CP1252");
        XMA_URI.setProperties(props);
        string = "http://s?rwa.at:443/ap p//c?mp/r??/re/r?name=\"i\" /?&v a l u e=?";
        escaped = "http://s%F6rwa.at:443/ap+p/c%F6mp/r%E4%DF/re/r?v+a+l+u+e=%80&name=%22i%22+%2F%3F";
        uri = new XMA_URI(string);
        Assert.assertEquals(escaped,uri.toString());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy