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 java.io.IOException;
21import java.net.ContentHandler;
22import java.net.URL;
23import java.net.URLConnection;
24
25import junit.framework.TestCase;
26
27public class ContentHandlerTest extends TestCase {
28
29    /**
30     * @tests java.net.ContentHandler#getContent(java.net.URLConnection,
31     *java.lang.Class[])
32     */
33    public void test_getContent() throws IOException {
34        URLConnection conn = new URL("http://www.apache.org").openConnection();
35        Class[] classes = { Foo.class, String.class, };
36        ContentHandler handler = new ContentHandlerImpl();
37        ((ContentHandlerImpl) handler).setContent(new Foo());
38        Object content = handler.getContent(conn, classes);
39        assertEquals("Foo", ((Foo) content).getFoo());
40
41        ((ContentHandlerImpl) handler).setContent(new FooSub());
42        content = handler.getContent(conn, classes);
43        assertEquals("FooSub", ((Foo) content).getFoo());
44
45        Class[] classes2 = { FooSub.class, String.class, };
46        ((ContentHandlerImpl) handler).setContent(new Foo());
47        content = handler.getContent(conn, classes2);
48        assertNull(content);
49    }
50}
51
52class ContentHandlerImpl extends ContentHandler {
53
54    private Object content;
55
56    @Override
57    public Object getContent(URLConnection uConn) throws IOException {
58        return content;
59    }
60
61    public void setContent(Object content) {
62        this.content = content;
63    }
64}
65
66class Foo {
67    public String getFoo() {
68        return "Foo";
69    }
70}
71
72class FooSub extends Foo {
73    public String getFoo() {
74        return "FooSub";
75    }
76}
77