001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.uihandler.interactive;
043:
044: import java.io.ByteArrayInputStream;
045: import java.io.ByteArrayOutputStream;
046: import java.io.IOException;
047: import java.io.InputStream;
048: import java.io.OutputStream;
049: import java.net.URL;
050: import java.net.URLConnection;
051: import java.net.URLStreamHandler;
052: import java.net.URLStreamHandlerFactory;
053: import java.util.HashMap;
054: import java.util.Map;
055: import junit.framework.Assert;
056:
057: /**
058: *
059: * @author Jaroslav Tulach
060: */
061: public class MemoryURL extends URLStreamHandler {
062:
063: static {
064: class F implements URLStreamHandlerFactory {
065: public URLStreamHandler createURLStreamHandler(
066: String protocol) {
067: if (protocol.startsWith("memory")) {
068: return new MemoryURL();
069: }
070: return null;
071: }
072: }
073: F f = new F();
074: URL.setURLStreamHandlerFactory(f);
075: }
076:
077: private static Map<String, InputStream> contents = new HashMap<String, InputStream>();
078: private static Map<String, MC> outputs = new HashMap<String, MC>();
079:
080: public static void registerURL(String u, String content) {
081: contents.put(u, new ByteArrayInputStream(content.getBytes()));
082: }
083:
084: public static void registerURL(String u, InputStream content) {
085: contents.put(u, content);
086: }
087:
088: public static byte[] getOutputForURL(String u) {
089: MC out = outputs.get(u);
090: Assert.assertNotNull("No output for " + u, out);
091: return out.out.toByteArray();
092: }
093:
094: public static String getRequestParameter(String u, String param) {
095: MC out = outputs.get(u);
096: Assert.assertNotNull("No output for " + u, out);
097: return out.params.get(param.toLowerCase());
098: }
099:
100: protected URLConnection openConnection(URL u) throws IOException {
101: return new MC(u);
102: }
103:
104: private static final class MC extends URLConnection {
105: private InputStream values;
106: private ByteArrayOutputStream out;
107: private Map<String, String> params;
108:
109: public MC(URL u) {
110: super (u);
111: outputs.put(u.toExternalForm(), this );
112: params = new HashMap<String, String>();
113: }
114:
115: public void connect() throws IOException {
116: if (values != null) {
117: return;
118: }
119: values = contents.remove(url.toExternalForm());
120: if (values == null) {
121: throw new IOException("No such content: " + url);
122: }
123: }
124:
125: public InputStream getInputStream() throws IOException {
126: connect();
127: return values;
128: }
129:
130: public OutputStream getOutputStream() throws IOException {
131: if (out == null) {
132: out = new ByteArrayOutputStream();
133: }
134: return out;
135: }
136:
137: public void setRequestProperty(String key, String value) {
138: super.setRequestProperty(key, value);
139: params.put(key.toLowerCase(), value);
140: }
141:
142: }
143: }
|