Debian Codegraph @

src/test/java/org/jboss/modules/DataURLStreamHandlerTest.java

raw
1 /*
2 * JBoss, Home of Professional Open Source.
3 * Copyright 2018 Red Hat, Inc., and individual contributors
4 * as indicated by the @author tags.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.jboss.modules;
20
21 import static org.junit.Assert.*;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.net.MalformedURLException;
26 import java.net.URL;
27 import java.net.URLConnection;
28 import java.nio.charset.StandardCharsets;
29
30 import org.junit.Test;
31
32 /**
33 */
34 public class DataURLStreamHandlerTest {
35 public DataURLStreamHandlerTest() {
36 }
37
38 static URL makeUrl(String str) throws MalformedURLException {
39 return new URL(null, str, DataURLStreamHandler.getInstance());
40 }
41
42 static byte[] readAllBytes(URLConnection uc) throws IOException {
43 final byte[] bytes = new byte[uc.getContentLength()];
44 final InputStream inputStream = uc.getInputStream();
45 if (inputStream.read(bytes) < bytes.length) {
46 throw new IOException("incomplete read");
47 }
48 return bytes;
49 }
50
51 static byte[] bytes(int... vals) {
52 final byte[] bytes = new byte[vals.length];
53 for (int i = 0; i < bytes.length; i++) {
54 bytes[i] = (byte) vals[i];
55 }
56 return bytes;
57 }
58
59 @Test
60 public void testEmpty() throws Exception {
61 final URLConnection urlConnection = makeUrl("data:,").openConnection();
62 assertEquals(0, urlConnection.getContentLength());
63 assertEquals("text/plain", urlConnection.getContentType());
64 assertNull(urlConnection.getContentEncoding());
65 assertEquals(-1, urlConnection.getInputStream().read());
66 }
67
68 @Test
69 public void testText() throws Exception {
70 final URLConnection urlConnection = makeUrl("data:,some kinda text").openConnection();
71 assertEquals(15, urlConnection.getContentLength());
72 assertEquals("text/plain", urlConnection.getContentType());
73 assertEquals("some kinda text", new String(readAllBytes(urlConnection), StandardCharsets.US_ASCII));
74 }
75
76 @Test
77 public void testTextUtf8() throws Exception {
78 final URLConnection urlConnection = makeUrl("data:;charset=utf-8,some kïndä tëxt").openConnection();
79 assertEquals(18, urlConnection.getContentLength());
80 assertEquals("text/plain;charset=utf-8", urlConnection.getContentType());
81 assertEquals("some kïndä tëxt", new String(readAllBytes(urlConnection), StandardCharsets.UTF_8));
82 }
83
84 @Test
85 public void testTextUtf8Ascii() throws Exception {
86 final URLConnection urlConnection = makeUrl("data:,some kïndä tëxt").openConnection();
87 assertEquals(15, urlConnection.getContentLength());
88 assertEquals("text/plain", urlConnection.getContentType());
89 assertEquals("some k?nd? t?xt", new String(readAllBytes(urlConnection), StandardCharsets.UTF_8));
90 }
91
92 @Test
93 public void testPlainBinary() throws Exception {
94 final URLConnection urlConnection = makeUrl("data:x-whatever/stuff;charset=utf-8,some kïndä tëxt").openConnection();
95 assertEquals(15, urlConnection.getContentLength());
96 assertEquals("x-whatever/stuff", urlConnection.getContentType());
97 assertEquals("some k�nd� t�xt", new String(readAllBytes(urlConnection), StandardCharsets.UTF_8));
98 }
99
100 @Test
101 public void testRawBinary() throws Exception {
102 final URLConnection urlConnection = makeUrl("data:x-whatever/stuff,%01%02%03%04%05%F0%FF%00").openConnection();
103 assertEquals(8, urlConnection.getContentLength());
104 assertEquals("x-whatever/stuff", urlConnection.getContentType());
105 assertArrayEquals(bytes(0x01, 0x02, 0x03, 0x04, 0x05, 0xf0, 0xff, 0x00), readAllBytes(urlConnection));
106 }
107
108 @Test
109 public void testBase64Binary() throws Exception {
110 final URLConnection urlConnection = makeUrl("data:x-whatever/stuff;base64,dGhpcyBpcyBhIHRlc3QhCg==").openConnection();
111 assertEquals(16, urlConnection.getContentLength());
112 assertEquals("x-whatever/stuff", urlConnection.getContentType());
113 assertArrayEquals("this is a test!\n".getBytes(StandardCharsets.UTF_8), readAllBytes(urlConnection));
114 }
115 }