rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/CompareStringsTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 747 vmtest/src/test/java/org/apidesign/bck2brwsr/tck/CompareStringsTest.java@ae352b763959
child 926 e5fe6bfca579
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
     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 int highByteLenght() {
    51         byte[] arr= { 77,97,110,105,102,101,115,116,45,86,101,114,115,105,111,110 };
    52         return new String(arr, 0).length();
    53     }
    54     
    55     @Compare public String highByte() {
    56         byte[] arr= { 77,97,110,105,102,101,115,116,45,86,101,114,115,105,111,110 };
    57         StringBuilder sb = new StringBuilder();
    58         sb.append("pref:");
    59         sb.append(new String(arr, 0));
    60         return sb.toString();
    61     }
    62     
    63     @Compare public static Object compareURLs() throws MalformedURLException {
    64         return new URL("http://apidesign.org:8080/wiki/").toExternalForm().toString();
    65     }
    66     
    67     @Compare public String deleteLastTwoCharacters() {
    68         StringBuilder sb = new StringBuilder();
    69         sb.append("453.0");
    70         if (sb.toString().endsWith(".0")) {
    71             final int l = sb.length();
    72             sb.delete(l - 2, l);
    73         }
    74         return sb.toString().toString();
    75     }
    76     
    77     @Compare public String nameOfStringClass() throws Exception {
    78         return Class.forName("java.lang.String").getName();
    79     }
    80     @Compare public String nameOfArrayClass() throws Exception {
    81         return Class.forName("org.apidesign.bck2brwsr.tck.CompareHashTest").getName();
    82     }
    83     
    84     @Compare public String lowerHello() {
    85         return "HeLlO".toLowerCase();
    86     }
    87     
    88     @Compare public String lowerA() {
    89         return String.valueOf(Character.toLowerCase('A')).toString();
    90     }
    91     @Compare public String upperHello() {
    92         return "hello".toUpperCase();
    93     }
    94     
    95     @Compare public String upperA() {
    96         return String.valueOf(Character.toUpperCase('a')).toString();
    97     }
    98     
    99     @Compare public boolean matchRegExp() throws Exception {
   100         return "58038503".matches("\\d*");
   101     }
   102 
   103     @Compare public boolean doesNotMatchRegExp() throws Exception {
   104         return "58038503GH".matches("\\d*");
   105     }
   106 
   107     @Compare public boolean doesNotMatchRegExpFully() throws Exception {
   108         return "Hello".matches("Hell");
   109     }
   110     
   111     @Compare public String emptyCharArray() {
   112         char[] arr = new char[10];
   113         return new String(arr);
   114     }
   115     
   116     @Compare public String variousCharacterTests() throws Exception {
   117         StringBuilder sb = new StringBuilder();
   118         
   119         sb.append(Character.isUpperCase('a'));
   120         sb.append(Character.isUpperCase('A'));
   121         sb.append(Character.isLowerCase('a'));
   122         sb.append(Character.isLowerCase('A'));
   123         
   124         sb.append(Character.isLetter('A'));
   125         sb.append(Character.isLetterOrDigit('9'));
   126         sb.append(Character.isLetterOrDigit('A'));
   127         sb.append(Character.isLetter('0'));
   128         
   129         return sb.toString().toString();
   130     }
   131         
   132     @Compare
   133     public String nullFieldInitialized() {
   134         NullField nf = new NullField();
   135         return ("" + nf.name).toString();
   136     }
   137     @Compare
   138     public String toUTFString() throws UnsupportedEncodingException {
   139         byte[] arr = {
   140             (byte) -59, (byte) -67, (byte) 108, (byte) 117, (byte) -59, (byte) -91,
   141             (byte) 111, (byte) 117, (byte) -60, (byte) -115, (byte) 107, (byte) -61,
   142             (byte) -67, (byte) 32, (byte) 107, (byte) -59, (byte) -81, (byte) -59,
   143             (byte) -120
   144         };
   145         return new String(arr, "utf-8");
   146     }
   147 
   148     @Compare
   149     public int stringToBytesLenght() throws UnsupportedEncodingException {
   150         return "\u017dlu\u0165ou\u010dk\u00fd k\u016f\u0148".getBytes("utf8").length;
   151     }
   152 
   153     @Factory
   154     public static Object[] create() {
   155         return VMTest.create(CompareStringsTest.class);
   156     }
   157 
   158     private static final class NullField {
   159 
   160         String name;
   161     }
   162 }