rt/emul/brwsrtest/src/test/java/org/apidesign/bck2brwsr/brwsrtest/ResourcesInBrwsrTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 30 Apr 2014 15:04:10 +0200
branchclosure
changeset 1513 ba912ef24b27
parent 1398 rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/ResourcesTest.java@9926996eca2d
parent 1498 rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/ResourcesTest.java@9583bcc50db3
child 1787 ea12a3bb4b33
permissions -rw-r--r--
Merging from default branch and resolving conflicts. mvn install -DskipTests passes OK.
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.brwsrtest;
    19 
    20 import java.io.IOException;
    21 import java.io.InputStream;
    22 import java.net.URL;
    23 import java.util.Enumeration;
    24 import org.apidesign.bck2brwsr.vmtest.Compare;
    25 import org.apidesign.bck2brwsr.vmtest.VMTest;
    26 import org.testng.annotations.Factory;
    27 
    28 /**
    29  *
    30  * @author Jaroslav Tulach <jtulach@netbeans.org>
    31  */
    32 public class ResourcesInBrwsrTest {
    33     
    34     @Compare public String readResourceAsStream() throws Exception {
    35         InputStream is = getClass().getResourceAsStream("Resources.txt");
    36         return readString(is);
    37     }
    38     
    39     @Compare public String readResourceViaConnection() throws Exception {
    40         InputStream is = getClass().getResource("Resources.txt").openConnection().getInputStream();
    41         return readString(is);
    42     }
    43 
    44     private String readString(InputStream is) throws IOException {
    45         StringBuilder sb = new StringBuilder();
    46         byte[] b = new byte[512];
    47         for (;;) { 
    48             int len = is.read(b);
    49             if (len == -1) {
    50                 return sb.toString();
    51             }
    52             for (int i = 0; i < len; i++) {
    53                 sb.append((char)b[i]);
    54             }
    55         }
    56     }
    57 
    58     @Compare public String readResourceAsStreamFromClassLoader() throws Exception {
    59         InputStream is = getClass().getClassLoader().getResourceAsStream("org/apidesign/bck2brwsr/brwsrtest/Resources.txt");
    60         return readString(is);
    61     }
    62     
    63     @Compare public String toURIFromURL() throws Exception {
    64         URL u = new URL("http://apidesign.org");
    65         return u.toURI().toString();
    66     }
    67     
    68     @Factory public static Object[] create() {
    69         return VMTest.create(ResourcesInBrwsrTest.class);
    70     }
    71 }