vmtest/src/test/java/org/apidesign/bck2brwsr/tck/CompareStringsTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 30 Jan 2013 09:32:05 +0100
branchemul
changeset 608 6e9328ca3462
parent 501 dc07c9001184
child 670 3026d9c844f0
child 693 92b628f99997
permissions -rw-r--r--
new String(byte[], 'utf-8') does necessary conversion
     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.tck;
    19 
    20 import java.io.UnsupportedEncodingException;
    21 import java.net.MalformedURLException;
    22 import java.net.URL;
    23 import org.apidesign.bck2brwsr.vmtest.Compare;
    24 import org.apidesign.bck2brwsr.vmtest.VMTest;
    25 import org.testng.annotations.Factory;
    26 
    27 /**
    28  *
    29  * @author Jaroslav Tulach <jtulach@netbeans.org>
    30  */
    31 public class CompareStringsTest {
    32     @Compare public String firstChar() {
    33         return "" + ("Hello".toCharArray()[0]);
    34     }
    35     
    36     @Compare public String classCast() {
    37         Object o = firstChar();
    38         return String.class.cast(o);
    39     }
    40 
    41     @Compare public String classCastThrown() {
    42         Object o = null;
    43         return String.class.cast(o);
    44     }
    45     
    46     @Compare public boolean equalToNull() {
    47         return "Ahoj".equals(null);
    48     }
    49     
    50     @Compare public static Object compareURLs() throws MalformedURLException {
    51         return new URL("http://apidesign.org:8080/wiki/").toExternalForm().toString();
    52     }
    53     
    54     @Compare public String deleteLastTwoCharacters() {
    55         StringBuilder sb = new StringBuilder();
    56         sb.append("453.0");
    57         if (sb.toString().endsWith(".0")) {
    58             final int l = sb.length();
    59             sb.delete(l - 2, l);
    60         }
    61         return sb.toString().toString();
    62     }
    63     
    64     @Compare public String nameOfStringClass() throws Exception {
    65         return Class.forName("java.lang.String").getName();
    66     }
    67     @Compare public String nameOfArrayClass() throws Exception {
    68         return Class.forName("org.apidesign.bck2brwsr.tck.CompareHashTest").getName();
    69     }
    70     
    71     @Compare public String lowerHello() {
    72         return "HeLlO".toLowerCase();
    73     }
    74     
    75     @Compare public String lowerA() {
    76         return String.valueOf(Character.toLowerCase('A')).toString();
    77     }
    78     @Compare public String upperHello() {
    79         return "hello".toUpperCase();
    80     }
    81     
    82     @Compare public String upperA() {
    83         return String.valueOf(Character.toUpperCase('a')).toString();
    84     }
    85     
    86     @Compare public boolean matchRegExp() throws Exception {
    87         return "58038503".matches("\\d*");
    88     }
    89 
    90     @Compare public boolean doesNotMatchRegExp() throws Exception {
    91         return "58038503GH".matches("\\d*");
    92     }
    93 
    94     @Compare public boolean doesNotMatchRegExpFully() throws Exception {
    95         return "Hello".matches("Hell");
    96     }
    97     
    98     @Compare public String emptyCharArray() {
    99         char[] arr = new char[10];
   100         return new String(arr);
   101     }
   102     
   103     @Compare public String variousCharacterTests() throws Exception {
   104         StringBuilder sb = new StringBuilder();
   105         
   106         sb.append(Character.isUpperCase('a'));
   107         sb.append(Character.isUpperCase('A'));
   108         sb.append(Character.isLowerCase('a'));
   109         sb.append(Character.isLowerCase('A'));
   110         
   111         sb.append(Character.isLetter('A'));
   112         sb.append(Character.isLetterOrDigit('9'));
   113         sb.append(Character.isLetterOrDigit('A'));
   114         sb.append(Character.isLetter('0'));
   115         
   116         return sb.toString().toString();
   117     }
   118         
   119     @Compare
   120     public String nullFieldInitialized() {
   121         NullField nf = new NullField();
   122         return ("" + nf.name).toString();
   123     }
   124     @Compare
   125     public String toUTFString() throws UnsupportedEncodingException {
   126         byte[] arr = {
   127             (byte) -59, (byte) -67, (byte) 108, (byte) 117, (byte) -59, (byte) -91,
   128             (byte) 111, (byte) 117, (byte) -60, (byte) -115, (byte) 107, (byte) -61,
   129             (byte) -67, (byte) 32, (byte) 107, (byte) -59, (byte) -81, (byte) -59,
   130             (byte) -120
   131         };
   132         return new String(arr, "utf-8");
   133     }
   134 
   135     @Compare
   136     public int stringToBytesLenght() throws UnsupportedEncodingException {
   137         return "Žluťoučký kůň".getBytes("utf8").length;
   138     }
   139 
   140     @Factory
   141     public static Object[] create() {
   142         return VMTest.create(CompareStringsTest.class);
   143     }
   144 
   145     private static final class NullField {
   146 
   147         String name;
   148     }
   149 }