Skip to content

Commit 02c76c7

Browse files
Address review - Convert to JUnit, correct comment typo, remove 'Infra' methods
1 parent 47f62aa commit 02c76c7

File tree

1 file changed

+49
-88
lines changed

1 file changed

+49
-88
lines changed

test/jdk/jdk/internal/loader/URLClassPath/ClassnameCharTest.java

Lines changed: 49 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* plugin does not escape unicode character in http request
2828
* @modules java.base/sun.net.www
2929
* jdk.httpserver
30-
* @run main ClassnameCharTest
30+
* @run junit ClassnameCharTest
3131
*/
3232

3333
import java.io.*;
@@ -40,41 +40,57 @@
4040
import com.sun.net.httpserver.*;
4141
import sun.net.www.ParseUtil;
4242

43+
import org.junit.jupiter.api.BeforeAll;
44+
import org.junit.jupiter.api.Test;
45+
46+
import static org.junit.jupiter.api.Assertions.fail;
47+
4348
public class ClassnameCharTest {
49+
4450
private static final Path JAR_PATH = Path.of("testclasses.jar");
45-
static HttpServer server;
51+
private static HttpServer server;
4652

47-
public static void realMain(String[] args) throws Exception {
53+
// Create the class file and write it to the testable jar
54+
@BeforeAll
55+
static void setup() throws IOException {
56+
var bytes = ClassFile.of().build(ClassDesc.of("fo o"), _ -> {});
57+
try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(JAR_PATH.toFile()))) {
58+
jos.putNextEntry(new JarEntry("fo o.class"));
59+
jos.write(bytes, 0, bytes.length);
60+
jos.closeEntry();
61+
}
62+
}
63+
64+
@Test
65+
void testClassName() throws Exception {
66+
// Build the server and set the context
4867
server = HttpServer.create(new InetSocketAddress(0), 0);
49-
server.createContext("/", new HttpHandler() {
50-
@Override
51-
public void handle(HttpExchange exchange) {
52-
try {
53-
String filename = exchange.getRequestURI().getPath();
54-
System.out.println("getRequestURI = " + exchange.getRequestURI());
55-
System.out.println("filename = " + filename);
56-
try (FileInputStream fis = new FileInputStream(JAR_PATH.toFile());
57-
JarInputStream jis = new JarInputStream(fis)) {
58-
JarEntry entry;
59-
while ((entry = jis.getNextJarEntry()) != null) {
60-
if (filename.endsWith(entry.getName())) {
61-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
62-
byte[] buf = new byte[8092];
63-
int count = 0;
64-
while ((count = jis.read(buf)) != -1)
65-
baos.write(buf, 0, count);
66-
exchange.sendResponseHeaders(200, baos.size());
67-
try (OutputStream os = exchange.getResponseBody()) {
68-
baos.writeTo(os);
69-
}
70-
return;
68+
server.createContext("/", exchange -> {
69+
try {
70+
String filename = exchange.getRequestURI().getPath();
71+
System.out.println("getRequestURI = " + exchange.getRequestURI());
72+
System.out.println("filename = " + filename);
73+
try (FileInputStream fis = new FileInputStream(JAR_PATH.toFile());
74+
JarInputStream jis = new JarInputStream(fis)) {
75+
JarEntry entry;
76+
while ((entry = jis.getNextJarEntry()) != null) {
77+
if (filename.endsWith(entry.getName())) {
78+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
79+
byte[] buf = new byte[8092];
80+
int count = 0;
81+
while ((count = jis.read(buf)) != -1)
82+
baos.write(buf, 0, count);
83+
exchange.sendResponseHeaders(200, baos.size());
84+
try (OutputStream os = exchange.getResponseBody()) {
85+
baos.writeTo(os);
7186
}
87+
return; // success
7288
}
73-
fail("Failed to find " + filename);
7489
}
75-
} catch (IOException e) {
76-
unexpected(e);
90+
contextFail("Failed to find " + filename);
7791
}
92+
} catch (IOException e) {
93+
unexpectedFail(e);
7894
}
7995
});
8096
server.start();
@@ -84,7 +100,6 @@ public void handle(HttpExchange exchange) {
84100
MyURLClassLoader acl = new MyURLClassLoader(base);
85101
Class<?> class1 = acl.findClass("fo o");
86102
System.out.println("class1 = " + class1);
87-
pass();
88103
// can't test the following class unless platform in unicode locale
89104
// Class class2 = acl.findClass("\u624b\u518c");
90105
// System.out.println("class2 = "+class2);
@@ -133,7 +148,7 @@ public Class<?> findClass(String name) {
133148
byte[] b = getBytes(finalURL);
134149
return defineClass(name, b, 0, b.length, codesource);
135150
}
136-
// protocol/host/port mismatch, fail with RuntimeExc
151+
// protocol/host/port mismatch, fail with RuntimeException
137152
} catch (Exception underlyingE) {
138153
exc = underlyingE; // Most likely CFE from defineClass
139154
}
@@ -170,73 +185,19 @@ private static byte[] getBytes(URL url) throws IOException {
170185
}
171186
}
172187

173-
//--------------------- Infrastructure ---------------------------
174-
static volatile int passed = 0, failed = 0;
175-
176-
static boolean pass() {
177-
passed++;
178-
return true;
179-
}
180-
181-
static boolean fail() {
182-
failed++;
188+
static void contextFail(String msg) {
183189
if (server != null) {
184190
server.stop(0);
185191
}
186192
Thread.dumpStack();
187-
return false;
188-
}
189-
190-
static boolean fail(String msg) {
191-
System.out.println(msg);
192-
return fail();
193+
fail(msg);
193194
}
194195

195-
static void unexpected(Throwable t) {
196-
failed++;
196+
static void unexpectedFail(Throwable t) {
197197
if (server != null) {
198198
server.stop(0);
199199
}
200200
t.printStackTrace();
201-
}
202-
203-
static boolean check(boolean cond) {
204-
if (cond) {
205-
pass();
206-
} else {
207-
fail();
208-
}
209-
return cond;
210-
}
211-
212-
static boolean equal(Object x, Object y) {
213-
if (x == null ? y == null : x.equals(y)) {
214-
return pass();
215-
} else {
216-
return fail(x + " not equal to " + y);
217-
}
218-
}
219-
220-
// Create the class file and write it to the testable jar
221-
static void buildJar() throws IOException {
222-
var bytes = ClassFile.of().build(ClassDesc.of("fo o"), _ -> {});
223-
try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(JAR_PATH.toFile()))) {
224-
jos.putNextEntry(new JarEntry("fo o.class"));
225-
jos.write(bytes, 0, bytes.length);
226-
jos.closeEntry();
227-
}
228-
}
229-
230-
public static void main(String[] args) throws Throwable {
231-
try {
232-
buildJar();
233-
realMain(args);
234-
} catch (Throwable t) {
235-
unexpected(t);
236-
}
237-
System.out.println("\nPassed = " + passed + " failed = " + failed);
238-
if (failed > 0) {
239-
throw new AssertionError("Some tests failed");
240-
}
201+
fail("Handler threw an unexpected IO Exception");
241202
}
242203
}

0 commit comments

Comments
 (0)