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 
18 package org.apache.harmony.luni.tests.java.net;
19 
20 import java.io.IOException;
21 import java.net.ContentHandler;
22 import java.net.URL;
23 import java.net.URLConnection;
24 
25 import junit.framework.TestCase;
26 
27 public class ContentHandlerTest extends TestCase {
28 
29     /**
30      * java.net.ContentHandler#getContent(java.net.URLConnection,
31      *        java.lang.Class[])
32      */
test_getContent()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 
52 class ContentHandlerImpl extends ContentHandler {
53 
54     private Object content;
55 
56     @Override
getContent(URLConnection uConn)57     public Object getContent(URLConnection uConn) throws IOException {
58         return content;
59     }
60 
setContent(Object content)61     public void setContent(Object content) {
62         this.content = content;
63     }
64 }
65 
66 class Foo {
getFoo()67     public String getFoo() {
68         return "Foo";
69     }
70 }
71 
72 class FooSub extends Foo {
getFoo()73     public String getFoo() {
74         return "FooSub";
75     }
76 }
77