jdk/test/java/net/URI/Test.java
changeset 15272 b0055428d835
parent 10422 83581a2cf49d
child 23010 6dadb192ad81
equal deleted inserted replaced
15271:570508dfed48 15272:b0055428d835
    22  */
    22  */
    23 
    23 
    24 /* @test
    24 /* @test
    25  * @summary Unit test for java.net.URI
    25  * @summary Unit test for java.net.URI
    26  * @bug 4464135 4505046 4503239 4438319 4991359 4866303 7023363 7041800
    26  * @bug 4464135 4505046 4503239 4438319 4991359 4866303 7023363 7041800
       
    27  *      7171415
    27  * @author Mark Reinhold
    28  * @author Mark Reinhold
    28  */
    29  */
    29 
    30 
    30 import java.io.ByteArrayInputStream;
    31 import java.io.ByteArrayInputStream;
    31 import java.io.ByteArrayOutputStream;
    32 import java.io.ByteArrayOutputStream;
  1335             .s("s").p("/%00").h("a")
  1336             .s("s").p("/%00").h("a")
  1336             .ta("s://a/%00").z();
  1337             .ta("s://a/%00").z();
  1337     }
  1338     }
  1338 
  1339 
  1339 
  1340 
  1340     static void eq0(Comparable u, Comparable v) throws URISyntaxException {
  1341     static void eq0(URI u, URI v) throws URISyntaxException {
  1341         testCount++;
  1342         testCount++;
  1342         if (!u.equals(v))
  1343         if (!u.equals(v))
  1343             throw new RuntimeException("Not equal: " + u + " " + v);
  1344             throw new RuntimeException("Not equal: " + u + " " + v);
  1344         int uh = u.hashCode();
  1345         int uh = u.hashCode();
  1345         int vh = v.hashCode();
  1346         int vh = v.hashCode();
  1350         out.println();
  1351         out.println();
  1351         out.println(u + " == " + v
  1352         out.println(u + " == " + v
  1352                     + "  [" + Integer.toHexString(uh) + "]");
  1353                     + "  [" + Integer.toHexString(uh) + "]");
  1353     }
  1354     }
  1354 
  1355 
  1355     static void cmp0(Comparable u, Comparable v, boolean same)
  1356     static void cmp0(URI u, URI v, boolean same)
  1356         throws URISyntaxException
  1357         throws URISyntaxException
  1357     {
  1358     {
  1358         int c = u.compareTo(v);
  1359         int c = u.compareTo(v);
  1359         if ((c == 0) != same)
  1360         if ((c == 0) != same)
  1360             throw new RuntimeException("Comparison inconsistent: " + u + " " + v
  1361             throw new RuntimeException("Comparison inconsistent: " + u + " " + v
  1361                                        + " " + c);
  1362                                        + " " + c);
  1362     }
  1363     }
  1363 
  1364 
  1364     static void eq(Comparable u, Comparable v) throws URISyntaxException {
  1365     static void eq(URI u, URI v) throws URISyntaxException {
  1365         eq0(u, v);
  1366         eq0(u, v);
  1366         cmp0(u, v, true);
  1367         cmp0(u, v, true);
  1367     }
  1368     }
  1368 
  1369 
  1369     static void eqeq(Comparable u, Comparable v) {
  1370     static void eqeq(URI u, URI v) {
  1370         testCount++;
  1371         testCount++;
  1371         if (u != v)
  1372         if (u != v)
  1372             throw new RuntimeException("Not ==: " + u + " " + v);
  1373             throw new RuntimeException("Not ==: " + u + " " + v);
  1373     }
  1374     }
  1374 
  1375 
  1375     static void ne0(Comparable u, Comparable v) throws URISyntaxException {
  1376     static void ne0(URI u, URI v) throws URISyntaxException {
  1376         testCount++;
  1377         testCount++;
  1377         if (u.equals(v))
  1378         if (u.equals(v))
  1378             throw new RuntimeException("Equal: " + u + " " + v);
  1379             throw new RuntimeException("Equal: " + u + " " + v);
  1379         out.println();
  1380         out.println();
  1380         out.println(u + " != " + v
  1381         out.println(u + " != " + v
  1381                     + "  [" + Integer.toHexString(u.hashCode())
  1382                     + "  [" + Integer.toHexString(u.hashCode())
  1382                     + " " + Integer.toHexString(v.hashCode())
  1383                     + " " + Integer.toHexString(v.hashCode())
  1383                     + "]");
  1384                     + "]");
  1384     }
  1385     }
  1385 
  1386 
  1386     static void ne(Comparable u, Comparable v) throws URISyntaxException {
  1387     static void ne(URI u, URI v) throws URISyntaxException {
  1387         ne0(u, v);
  1388         ne0(u, v);
  1388         cmp0(u, v, false);
  1389         cmp0(u, v, false);
  1389     }
  1390     }
  1390 
  1391 
  1391     static void lt(Comparable u, Comparable v) throws URISyntaxException {
  1392     static void lt(URI u, URI v) throws URISyntaxException {
  1392         ne0(u, v);
  1393         ne0(u, v);
  1393         int c = u.compareTo(v);
  1394         int c = u.compareTo(v);
  1394         if (c >= 0) {
  1395         if (c >= 0) {
  1395             show((URI)u);
  1396             show(u);
  1396             show((URI)v);
  1397             show(v);
  1397             throw new RuntimeException("Not less than: " + u + " " + v
  1398             throw new RuntimeException("Not less than: " + u + " " + v
  1398                                        + " " + c);
  1399                                        + " " + c);
  1399         }
  1400         }
  1400         out.println(u + " < " + v);
  1401         out.println(u + " < " + v);
  1401     }
  1402     }
  1402 
  1403 
  1403     static void lt(String s, String t) throws URISyntaxException {
  1404     static void lt(String s, String t) throws URISyntaxException {
  1404         lt(new URI(s), new URI(t));
  1405         lt(new URI(s), new URI(t));
  1405     }
  1406     }
  1406 
  1407 
  1407     static void gt(Comparable u, Comparable v) throws URISyntaxException {
  1408    static void gt(URI u, URI v) throws URISyntaxException {
  1408         lt(v, u);
  1409         lt(v, u);
  1409     }
  1410     }
  1410 
  1411 
  1411     static void eqHashComp() throws URISyntaxException {
  1412     static void eqHashComp() throws URISyntaxException {
  1412 
  1413 
  1428         gt(s, new URI("http://jag:CafeBabe@java.sun.com:94/b/c/d?q#f"));
  1429         gt(s, new URI("http://jag:CafeBabe@java.sun.com:94/b/c/d?q#f"));
  1429         lt(s, new URI("http://jag:cafebabe@java.sun.com:94/b/c/d?r#f"));
  1430         lt(s, new URI("http://jag:cafebabe@java.sun.com:94/b/c/d?r#f"));
  1430         lt(s, new URI("http://jag:cafebabe@java.sun.com:94/b/c/d?q#g"));
  1431         lt(s, new URI("http://jag:cafebabe@java.sun.com:94/b/c/d?q#g"));
  1431         eq(new URI("http://host/a%00bcd"), new URI("http://host/a%00bcd"));
  1432         eq(new URI("http://host/a%00bcd"), new URI("http://host/a%00bcd"));
  1432         ne(new URI("http://host/a%00bcd"), new URI("http://host/aZ00bcd"));
  1433         ne(new URI("http://host/a%00bcd"), new URI("http://host/aZ00bcd"));
       
  1434         eq0(new URI("http://host/abc%e2def%C3ghi"),
       
  1435             new URI("http://host/abc%E2def%c3ghi"));
  1433 
  1436 
  1434         lt("p", "s:p");
  1437         lt("p", "s:p");
  1435         lt("s:p", "T:p");
  1438         lt("s:p", "T:p");
  1436         lt("S:p", "t:p");
  1439         lt("S:p", "t:p");
  1437         lt("s:/p", "s:p");
  1440         lt("s:/p", "s:p");
  1463 
  1466 
  1464         ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
  1467         ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
  1465         ObjectInputStream oi = new ObjectInputStream(bi);
  1468         ObjectInputStream oi = new ObjectInputStream(bi);
  1466         try {
  1469         try {
  1467             Object o = oi.readObject();
  1470             Object o = oi.readObject();
  1468             eq(u, (Comparable)o);
  1471             eq(u, (URI)o);
  1469         } catch (ClassNotFoundException x) {
  1472         } catch (ClassNotFoundException x) {
  1470             x.printStackTrace();
  1473             x.printStackTrace();
  1471             throw new RuntimeException(x.toString());
  1474             throw new RuntimeException(x.toString());
  1472         }
  1475         }
  1473 
  1476