rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/CompareStringsTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 28 Sep 2013 02:43:18 +0200
branchjavac
changeset 1317 66fdb47061aa
parent 1034 28dc692f3b11
child 1336 804f6f982f4e
permissions -rw-r--r--
Some trivial implementations of methods that work with Locale
     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 java.util.Locale;
    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 CompareStringsTest {
    33     @Compare public String firstChar() {
    34         return "" + ("Hello".toCharArray()[0]);
    35     }
    36     
    37     @Compare public String classCast() {
    38         Object o = firstChar();
    39         return String.class.cast(o);
    40     }
    41 
    42     @Compare public String classCastThrown() {
    43         Object o = null;
    44         return String.class.cast(o);
    45     }
    46     
    47     @Compare public boolean equalToNull() {
    48         return "Ahoj".equals(null);
    49     }
    50     
    51     @Compare public int highByteLenght() {
    52         byte[] arr= { 77,97,110,105,102,101,115,116,45,86,101,114,115,105,111,110 };
    53         return new String(arr, 0).length();
    54     }
    55     
    56     @Compare public String highByte() {
    57         byte[] arr= { 77,97,110,105,102,101,115,116,45,86,101,114,115,105,111,110 };
    58         StringBuilder sb = new StringBuilder();
    59         sb.append("pref:");
    60         sb.append(new String(arr, 0));
    61         return sb.toString();
    62     }
    63     
    64     @Compare public static Object compareURLs() throws MalformedURLException {
    65         return new URL("http://apidesign.org:8080/wiki/").toExternalForm().toString();
    66     }
    67     
    68     @Compare public String deleteLastTwoCharacters() {
    69         StringBuilder sb = new StringBuilder();
    70         sb.append("453.0");
    71         if (sb.toString().endsWith(".0")) {
    72             final int l = sb.length();
    73             sb.delete(l - 2, l);
    74         }
    75         return sb.toString().toString();
    76     }
    77     
    78     @Compare public String nameOfStringClass() throws Exception {
    79         return Class.forName("java.lang.String").getName();
    80     }
    81     @Compare public String nameOfArrayClass() throws Exception {
    82         return Class.forName("org.apidesign.bck2brwsr.tck.CompareHashTest").getName();
    83     }
    84     
    85     @Compare public String lowerHello() {
    86         return "HeLlO".toLowerCase();
    87     }
    88     
    89     @Compare public String lowerA() {
    90         return String.valueOf(Character.toLowerCase('A')).toString();
    91     }
    92     @Compare public String upperHello() {
    93         return "hello".toUpperCase();
    94     }
    95     
    96     @Compare public String upperA() {
    97         return String.valueOf(Character.toUpperCase('a')).toString();
    98     }
    99     
   100     @Compare public boolean matchRegExp() throws Exception {
   101         return "58038503".matches("\\d*");
   102     }
   103 
   104     @Compare public boolean doesNotMatchRegExp() throws Exception {
   105         return "58038503GH".matches("\\d*");
   106     }
   107 
   108     @Compare public boolean doesNotMatchRegExpFully() throws Exception {
   109         return "Hello".matches("Hell");
   110     }
   111     
   112     @Compare public String emptyCharArray() {
   113         char[] arr = new char[10];
   114         return new String(arr);
   115     }
   116     
   117     @Compare public String variousCharacterTests() throws Exception {
   118         StringBuilder sb = new StringBuilder();
   119         
   120         sb.append(Character.isUpperCase('a'));
   121         sb.append(Character.isUpperCase('A'));
   122         sb.append(Character.isLowerCase('a'));
   123         sb.append(Character.isLowerCase('A'));
   124         
   125         sb.append(Character.isLetter('A'));
   126         sb.append(Character.isLetterOrDigit('9'));
   127         sb.append(Character.isLetterOrDigit('A'));
   128         sb.append(Character.isLetter('0'));
   129         
   130         return sb.toString().toString();
   131     }
   132         
   133     @Compare
   134     public String nullFieldInitialized() {
   135         NullField nf = new NullField();
   136         return ("" + nf.name).toString();
   137     }
   138     @Compare
   139     public String toUTFString() throws UnsupportedEncodingException {
   140         byte[] arr = {
   141             (byte) -59, (byte) -67, (byte) 108, (byte) 117, (byte) -59, (byte) -91,
   142             (byte) 111, (byte) 117, (byte) -60, (byte) -115, (byte) 107, (byte) -61,
   143             (byte) -67, (byte) 32, (byte) 107, (byte) -59, (byte) -81, (byte) -59,
   144             (byte) -120
   145         };
   146         return new String(arr, "utf-8");
   147     }
   148 
   149     @Compare
   150     public int stringToBytesLenght() throws UnsupportedEncodingException {
   151         return "\u017dlu\u0165ou\u010dk\u00fd k\u016f\u0148".getBytes("utf8").length;
   152     }
   153     
   154     @Compare public String replaceSeq() {
   155         return "Hello World.".replace(".", "!");
   156     }
   157     @Compare public String replaceSeqAll() {
   158         return "Hello World! Hello World.".replace("World", "Jarda");
   159     }
   160     @Compare public String replaceSeqAA() {
   161         String res = "aaa".replace("aa", "b");
   162         assert res.equals("ba") : "Expecting ba: " + res;
   163         return res;
   164     }
   165     
   166     @Compare public String localeUS() {
   167         return Locale.US.toString();
   168     }
   169     
   170     @Compare public String localeFrench() {
   171         return Locale.FRENCH.toString();
   172     }
   173     
   174     
   175     @Compare public String formatSimple() {
   176         return String.format((Locale)null, "Hello %s!", "World");
   177     }
   178 
   179     @Factory
   180     public static Object[] create() {
   181         return VMTest.create(CompareStringsTest.class);
   182     }
   183 
   184     private static final class NullField {
   185 
   186         String name;
   187     }
   188 }