27
27
* plugin does not escape unicode character in http request
28
28
* @modules java.base/sun.net.www
29
29
* jdk.httpserver
30
- * @run main ClassnameCharTest
30
+ * @run junit ClassnameCharTest
31
31
*/
32
32
33
33
import java .io .*;
40
40
import com .sun .net .httpserver .*;
41
41
import sun .net .www .ParseUtil ;
42
42
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
+
43
48
public class ClassnameCharTest {
49
+
44
50
private static final Path JAR_PATH = Path .of ("testclasses.jar" );
45
- static HttpServer server ;
51
+ private static HttpServer server ;
46
52
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
48
67
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 );
71
86
}
87
+ return ; // success
72
88
}
73
- fail ("Failed to find " + filename );
74
89
}
75
- } catch (IOException e ) {
76
- unexpected (e );
90
+ contextFail ("Failed to find " + filename );
77
91
}
92
+ } catch (IOException e ) {
93
+ unexpectedFail (e );
78
94
}
79
95
});
80
96
server .start ();
@@ -84,7 +100,6 @@ public void handle(HttpExchange exchange) {
84
100
MyURLClassLoader acl = new MyURLClassLoader (base );
85
101
Class <?> class1 = acl .findClass ("fo o" );
86
102
System .out .println ("class1 = " + class1 );
87
- pass ();
88
103
// can't test the following class unless platform in unicode locale
89
104
// Class class2 = acl.findClass("\u624b\u518c");
90
105
// System.out.println("class2 = "+class2);
@@ -133,7 +148,7 @@ public Class<?> findClass(String name) {
133
148
byte [] b = getBytes (finalURL );
134
149
return defineClass (name , b , 0 , b .length , codesource );
135
150
}
136
- // protocol/host/port mismatch, fail with RuntimeExc
151
+ // protocol/host/port mismatch, fail with RuntimeException
137
152
} catch (Exception underlyingE ) {
138
153
exc = underlyingE ; // Most likely CFE from defineClass
139
154
}
@@ -170,73 +185,19 @@ private static byte[] getBytes(URL url) throws IOException {
170
185
}
171
186
}
172
187
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 ) {
183
189
if (server != null ) {
184
190
server .stop (0 );
185
191
}
186
192
Thread .dumpStack ();
187
- return false ;
188
- }
189
-
190
- static boolean fail (String msg ) {
191
- System .out .println (msg );
192
- return fail ();
193
+ fail (msg );
193
194
}
194
195
195
- static void unexpected (Throwable t ) {
196
- failed ++;
196
+ static void unexpectedFail (Throwable t ) {
197
197
if (server != null ) {
198
198
server .stop (0 );
199
199
}
200
200
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 ("\n Passed = " + passed + " failed = " + failed );
238
- if (failed > 0 ) {
239
- throw new AssertionError ("Some tests failed" );
240
- }
201
+ fail ("Handler threw an unexpected IO Exception" );
241
202
}
242
203
}
0 commit comments