01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: * @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")
35: .openConnection();
36: Class[] classes = { Foo.class, String.class, };
37: ContentHandler handler = new ContentHandlerImpl();
38: ((ContentHandlerImpl) handler).setContent(new Foo());
39: Object content = handler.getContent(conn, classes);
40: assertEquals("Foo", ((Foo) content).getFoo());
41:
42: ((ContentHandlerImpl) handler).setContent(new FooSub());
43: content = handler.getContent(conn, classes);
44: assertEquals("FooSub", ((Foo) content).getFoo());
45:
46: Class[] classes2 = { FooSub.class, String.class, };
47: ((ContentHandlerImpl) handler).setContent(new Foo());
48: content = handler.getContent(conn, classes2);
49: assertNull(content);
50: }
51: }
52:
53: class ContentHandlerImpl extends ContentHandler {
54:
55: private Object content;
56:
57: @Override
58: public Object getContent(URLConnection uConn) throws IOException {
59: return content;
60: }
61:
62: public void setContent(Object content) {
63: this .content = content;
64: }
65: }
66:
67: class Foo {
68: public String getFoo() {
69: return "Foo";
70: }
71: }
72:
73: class FooSub extends Foo {
74: public String getFoo() {
75: return "FooSub";
76: }
77: }
|