Search in sources :

Example 1 with ContentHandler

use of java.net.ContentHandler in project robovm by robovm.

the class ContentHandlerFactoryTest method test_createContentHandler.

@SideEffect("This test affects tests that are run after this one." + " The reason are side effects due to caching in URLConnection." + " Maybe this test needs to be run in isolation.")
public void test_createContentHandler() throws IOException {
    TestContentHandlerFactory factory = new TestContentHandlerFactory();
    if (isTestable) {
        assertFalse(isCreateContentHandlerCalled);
        URL url = new URL("http://" + Support_Configuration.SpecialInetTestAddress);
        URLConnection.setContentHandlerFactory(factory);
        URLConnection con = url.openConnection();
        try {
            con.getContent();
            assertTrue(isCreateContentHandlerCalled);
            assertTrue(isGetContentCalled);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        isGetContentCalled = false;
        try {
            con.getContent(new Class[] {});
            assertTrue(isGetContentCalled);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        try {
            con.setContentHandlerFactory(factory);
            fail("java.lang.Error was not thrown.");
        } catch (java.lang.Error e) {
        //expected
        }
        try {
            con.setContentHandlerFactory(null);
            fail("java.lang.Error was not thrown.");
        } catch (java.lang.Error e) {
        //expected
        }
    } else {
        ContentHandler ch = factory.createContentHandler("text/plain");
        URL url;
        try {
            url = new URL("http://" + Support_Configuration.SpecialInetTestAddress);
            assertNotNull(ch.getContent(url.openConnection()));
        } catch (MalformedURLException e) {
            fail("MalformedURLException was thrown: " + e.getMessage());
        } catch (IOException e) {
            fail("IOException was thrown.");
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) URL(java.net.URL) URLConnection(java.net.URLConnection) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ContentHandler(java.net.ContentHandler) SideEffect(dalvik.annotation.SideEffect)

Example 2 with ContentHandler

use of java.net.ContentHandler in project robovm by robovm.

the class FooSub method test_getContent.

/**
     * java.net.ContentHandler#getContent(java.net.URLConnection,
     *        java.lang.Class[])
     */
public void test_getContent() throws IOException {
    URLConnection conn = new URL("http://www.apache.org").openConnection();
    Class[] classes = { Foo.class, String.class };
    ContentHandler handler = new ContentHandlerImpl();
    ((ContentHandlerImpl) handler).setContent(new Foo());
    Object content = handler.getContent(conn, classes);
    assertEquals("Foo", ((Foo) content).getFoo());
    ((ContentHandlerImpl) handler).setContent(new FooSub());
    content = handler.getContent(conn, classes);
    assertEquals("FooSub", ((Foo) content).getFoo());
    Class[] classes2 = { FooSub.class, String.class };
    ((ContentHandlerImpl) handler).setContent(new Foo());
    content = handler.getContent(conn, classes2);
    assertNull(content);
}
Also used : URLConnection(java.net.URLConnection) URL(java.net.URL) ContentHandler(java.net.ContentHandler)

Aggregations

ContentHandler (java.net.ContentHandler)2 URL (java.net.URL)2 URLConnection (java.net.URLConnection)2 SideEffect (dalvik.annotation.SideEffect)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1