1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 */
17
18package org.apache.harmony.luni.tests.java.net;
19
20import dalvik.annotation.TestTargetClass;
21import dalvik.annotation.TestTargets;
22import dalvik.annotation.TestLevel;
23import dalvik.annotation.TestTargetNew;
24
25import java.io.IOException;
26import java.net.ContentHandler;
27import java.net.URL;
28import java.net.URLConnection;
29
30import junit.framework.TestCase;
31
32@TestTargetClass(ContentHandler.class)
33public class ContentHandlerTest extends TestCase {
34
35    /**
36     * @tests java.net.ContentHandler#getContent(java.net.URLConnection,
37     *        java.lang.Class[])
38     */
39    @TestTargetNew(
40        level = TestLevel.COMPLETE,
41        notes = "",
42        method = "getContent",
43        args = {java.net.URLConnection.class, java.lang.Class[].class}
44    )
45    public void test_getContent() throws IOException {
46        URLConnection conn = new URL("http://www.apache.org").openConnection();
47        Class[] classes = { Foo.class, String.class, };
48        ContentHandler handler = new ContentHandlerImpl();
49        ((ContentHandlerImpl) handler).setContent(new Foo());
50        Object content = handler.getContent(conn, classes);
51        assertEquals("Foo", ((Foo) content).getFoo());
52
53        ((ContentHandlerImpl) handler).setContent(new FooSub());
54        content = handler.getContent(conn, classes);
55        assertEquals("FooSub", ((Foo) content).getFoo());
56
57        Class[] classes2 = { FooSub.class, String.class, };
58        ((ContentHandlerImpl) handler).setContent(new Foo());
59        content = handler.getContent(conn, classes2);
60        assertNull(content);
61
62    }
63
64    @TestTargetNew(
65        level = TestLevel.COMPLETE,
66        notes = "",
67        method = "getContent",
68        args = {java.net.URLConnection.class}
69    )
70    public void test_getContentLURLConnection() throws IOException {
71        URLConnection conn = new URL("http://www.apache.org").openConnection();
72        Class[] classes = { Foo.class, String.class, };
73        ContentHandler handler = new ContentHandlerImpl();
74        ((ContentHandlerImpl) handler).setContent(new Foo());
75        Object content = handler.getContent(conn);
76        assertEquals("Foo", ((Foo) content).getFoo());
77    }
78
79    @TestTargetNew(
80        level = TestLevel.COMPLETE,
81        notes = "",
82        method = "ContentHandler",
83        args = {}
84    )
85    public void test_Constructor() {
86        ContentHandlerImpl ch = new ContentHandlerImpl();
87        ch.setContent(new Object());
88    }
89}
90
91class ContentHandlerImpl extends ContentHandler {
92
93    private Object content;
94
95    @Override
96    public Object getContent(URLConnection uConn) throws IOException {
97        return content;
98    }
99
100    public void setContent(Object content) {
101        this.content = content;
102    }
103}
104
105class Foo {
106    public String getFoo() {
107        return "Foo";
108    }
109}
110
111class FooSub extends Foo {
112    public String getFoo() {
113        return "FooSub";
114    }
115}
116