1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 import junit.framework.Assert; 18 import java.lang.reflect.Method; 19 20 /** 21 * more string tests 22 */ 23 public class Main { 24 public static void main(String args[]) throws Exception { 25 String test = "0123456789"; 26 String test1 = new String("0123456789"); // different object 27 String test2 = new String("0123456780"); // different value 28 String offset = new String("xxx0123456789yyy"); 29 String sub = offset.substring(3, 13); 30 Object blah = new Object(); 31 32 Assert.assertTrue(test.equals(test)); 33 Assert.assertTrue(test.equals(test1)); 34 Assert.assertFalse(test.equals(test2)); 35 36 Assert.assertEquals(test.compareTo(test1), 0); 37 Assert.assertTrue(test1.compareTo(test2) > 0); 38 Assert.assertTrue(test2.compareTo(test1) < 0); 39 40 Assert.assertEquals("".compareTo(""), 0); 41 Assert.assertTrue(test.compareTo("") > 0); 42 Assert.assertTrue("".compareTo(test) < 0); 43 44 /* compare string with a nonzero offset, in left/right side */ 45 Assert.assertEquals(test.compareTo(sub), 0); 46 Assert.assertEquals(sub.compareTo(test), 0); 47 Assert.assertTrue(test.equals(sub)); 48 Assert.assertTrue(sub.equals(test)); 49 /* same base, one is a substring */ 50 Assert.assertFalse(offset.equals(sub)); 51 Assert.assertFalse(sub.equals(offset)); 52 /* wrong class */ 53 Assert.assertFalse(test.equals(blah)); 54 55 /* null ptr - throw */ 56 try { 57 test.compareTo(null); 58 Assert.fail("didn't get expected npe"); 59 } catch (NullPointerException npe) { 60 System.out.println("Got expected npe"); 61 } 62 /* null ptr - ok */ 63 Assert.assertFalse(test.equals(null)); 64 65 test = test.substring(1); 66 Assert.assertTrue(test.equals("123456789")); 67 Assert.assertFalse(test.equals(test1)); 68 69 test = test.substring(1); 70 Assert.assertTrue(test.equals("23456789")); 71 72 test = test.substring(1); 73 Assert.assertTrue(test.equals("3456789")); 74 75 test = test.substring(1); 76 Assert.assertTrue(test.equals("456789")); 77 78 test = test.substring(3,5); 79 Assert.assertTrue(test.equals("78")); 80 81 test = "this/is/a/path"; 82 String[] strings = test.split("/"); 83 Assert.assertEquals(4, strings.length); 84 85 Assert.assertEquals("this is a path", test.replaceAll("/", " ")); 86 Assert.assertEquals("this is a path", test.replace("/", " ")); 87 88 Class Strings = Class.forName("com.android.org.bouncycastle.util.Strings"); 89 Method fromUTF8ByteArray = Strings.getDeclaredMethod("fromUTF8ByteArray", byte[].class); 90 String result = (String) fromUTF8ByteArray.invoke(null, new byte[] {'O', 'K'}); 91 System.out.println(result); 92 } 93 } 94