001: /* Licensed to the Apache Software Foundation (ASF) under one or more
002: * contributor license agreements. See the NOTICE file distributed with
003: * this work for additional information regarding copyright ownership.
004: * The ASF licenses this file to You under the Apache License, Version 2.0
005: * (the "License"); you may not use this file except in compliance with
006: * the License. You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.harmony.luni.tests.java.net;
018:
019: import java.io.IOException;
020: import java.net.CacheRequest;
021: import java.net.CacheResponse;
022: import java.net.NetPermission;
023: import java.net.ResponseCache;
024: import java.net.URI;
025: import java.net.URLConnection;
026: import java.security.Permission;
027: import java.util.Map;
028:
029: import junit.framework.TestCase;
030:
031: public class ResponseCacheTest extends TestCase {
032:
033: /**
034: * @tests java.net.ResponseCache#getDefault()
035: */
036: public void test_GetDefault() throws Exception {
037: assertNull(ResponseCache.getDefault());
038: }
039:
040: /**
041: * @tests java.net.ResponseCache#setDefault(ResponseCache)
042: */
043: public void test_SetDefaultLjava_net_ResponseCache_Normal()
044: throws Exception {
045: ResponseCache rc1 = new MockResponseCache();
046: ResponseCache rc2 = new MockResponseCache();
047: ResponseCache.setDefault(rc1);
048: assertSame(ResponseCache.getDefault(), rc1);
049: ResponseCache.setDefault(rc2);
050: assertSame(ResponseCache.getDefault(), rc2);
051: ResponseCache.setDefault(null);
052: assertNull(ResponseCache.getDefault());
053: }
054:
055: /**
056: * @tests java.net.ResponseCache#getDefault()
057: */
058: public void test_GetDefault_Security() {
059: SecurityManager old = System.getSecurityManager();
060: try {
061: System.setSecurityManager(new MockSM());
062: } catch (SecurityException e) {
063: System.err.println("No setSecurityManager permission.");
064: System.err
065: .println("test_setDefaultLjava_net_ResponseCache_NoPermission is not tested");
066: return;
067: }
068: try {
069: ResponseCache.getDefault();
070: fail("should throw SecurityException");
071: } catch (SecurityException e) {
072: // expected
073: } finally {
074: System.setSecurityManager(old);
075: }
076: }
077:
078: /**
079: * @tests java.net.ResponseCache#setDefault(ResponseCache)
080: */
081: public void test_setDefaultLjava_net_ResponseCache_NoPermission() {
082: ResponseCache rc = new MockResponseCache();
083: SecurityManager old = System.getSecurityManager();
084: try {
085: System.setSecurityManager(new MockSM());
086: } catch (SecurityException e) {
087: System.err.println("No setSecurityManager permission.");
088: System.err
089: .println("test_setDefaultLjava_net_ResponseCache_NoPermission is not tested");
090: return;
091: }
092: try {
093: ResponseCache.setDefault(rc);
094: fail("should throw SecurityException");
095: } catch (SecurityException e) {
096: // expected
097: } finally {
098: System.setSecurityManager(old);
099: }
100: }
101:
102: /*
103: * MockResponseCache for testSetDefault(ResponseCache)
104: */
105: class MockResponseCache extends ResponseCache {
106:
107: public CacheResponse get(URI arg0, String arg1, Map arg2)
108: throws IOException {
109: return null;
110: }
111:
112: public CacheRequest put(URI arg0, URLConnection arg1)
113: throws IOException {
114: return null;
115: }
116: }
117:
118: /*
119: * MockSecurityMaanger. It denies NetPermission("getResponseCache") and
120: * NetPermission("setResponseCache").
121: */
122: class MockSM extends SecurityManager {
123: public void checkPermission(Permission permission) {
124: if (permission instanceof NetPermission) {
125: if ("setResponseCache".equals(permission.getName())) {
126: throw new SecurityException();
127: }
128: }
129:
130: if (permission instanceof NetPermission) {
131: if ("getResponseCache".equals(permission.getName())) {
132: throw new SecurityException();
133: }
134: }
135:
136: if (permission instanceof RuntimePermission) {
137: if ("setSecurityManager".equals(permission.getName())) {
138: return;
139: }
140: }
141: }
142: }
143: }
|