1.1 --- a/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/ResourcesTest.java Sun Apr 27 22:40:17 2014 +0200
1.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/ResourcesTest.java Wed Apr 30 15:04:10 2014 +0200
1.3 @@ -17,7 +17,10 @@
1.4 */
1.5 package org.apidesign.bck2brwsr.tck;
1.6
1.7 +import java.io.IOException;
1.8 import java.io.InputStream;
1.9 +import java.net.URL;
1.10 +import java.util.Enumeration;
1.11 import org.apidesign.bck2brwsr.vmtest.Compare;
1.12 import org.apidesign.bck2brwsr.vmtest.VMTest;
1.13 import org.testng.annotations.Factory;
1.14 @@ -27,17 +30,62 @@
1.15 * @author Jaroslav Tulach <jtulach@netbeans.org>
1.16 */
1.17 public class ResourcesTest {
1.18 + @Compare public String allManifests() throws Exception {
1.19 + Enumeration<URL> en = ClassLoader.getSystemResources("META-INF/MANIFEST.MF");
1.20 + assert en.hasMoreElements() : "Should have at least one manifest";
1.21 + String first = readString(en.nextElement().openStream());
1.22 + boolean different = false;
1.23 + int cnt = 1;
1.24 + while (en.hasMoreElements()) {
1.25 + URL url = en.nextElement();
1.26 + String now = readString(url.openStream());
1.27 + if (!first.equals(now)) {
1.28 + different = true;
1.29 + }
1.30 + cnt++;
1.31 + if (cnt > 500) {
1.32 + throw new IllegalStateException(
1.33 + "Giving up. First manifest:\n" + first +
1.34 + "\nLast manifest:\n" + now
1.35 + );
1.36 + }
1.37 + }
1.38 + assert different : "Not all manifests should look like first one:\n" + first;
1.39 + return "" + cnt;
1.40 + }
1.41
1.42 @Compare public String readResourceAsStream() throws Exception {
1.43 InputStream is = getClass().getResourceAsStream("Resources.txt");
1.44 - assert is != null : "The stream for Resources.txt should be found";
1.45 - byte[] b = new byte[30];
1.46 - int len = is.read(b);
1.47 + return readString(is);
1.48 + }
1.49 +
1.50 + @Compare public String readResourceViaConnection() throws Exception {
1.51 + InputStream is = getClass().getResource("Resources.txt").openConnection().getInputStream();
1.52 + return readString(is);
1.53 + }
1.54 +
1.55 + private String readString(InputStream is) throws IOException {
1.56 StringBuilder sb = new StringBuilder();
1.57 - for (int i = 0; i < len; i++) {
1.58 - sb.append((char)b[i]);
1.59 + byte[] b = new byte[512];
1.60 + for (;;) {
1.61 + int len = is.read(b);
1.62 + if (len == -1) {
1.63 + return sb.toString();
1.64 + }
1.65 + for (int i = 0; i < len; i++) {
1.66 + sb.append((char)b[i]);
1.67 + }
1.68 }
1.69 - return sb.toString();
1.70 + }
1.71 +
1.72 + @Compare public String readResourceAsStreamFromClassLoader() throws Exception {
1.73 + InputStream is = getClass().getClassLoader().getResourceAsStream("org/apidesign/bck2brwsr/tck/Resources.txt");
1.74 + return readString(is);
1.75 + }
1.76 +
1.77 + @Compare public String toURIFromURL() throws Exception {
1.78 + URL u = new URL("http://apidesign.org");
1.79 + return u.toURI().toString();
1.80 }
1.81
1.82 @Factory public static Object[] create() {