1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package test.java.lang.StringBuffer; 25 26 import java.util.Iterator; 27 import java.util.Set; 28 import java.util.TreeSet; 29 import org.testng.Assert; 30 import org.testng.annotations.Test; 31 32 /** 33 * @test 34 * @bug 8137326 35 * @summary Test to verify the Comparable implementation for the StringBuffer class. 36 * @run testng Comparison 37 */ 38 public class Comparison { 39 static char SEP = ':'; 40 41 static String[][] books = { 42 {"Biography", "Steve Jobs"}, 43 {"Biography", "Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future"}, 44 {"Law", "Law 101: Everything You Need to Know About American Law, Fourth Edition"}, 45 {"Law", "The Tools of Argument: How the Best Lawyers Think, Argue, and Win"}, 46 {"History", "The History Book (Big Ideas Simply Explained)"}, 47 {"History", "A People's History of the United States"}, 48 }; 49 50 /** 51 * Verifies the Comparable implementation by comparing with two TreeSet that 52 * contain either StringBuffer or String. 53 */ 54 @Test compareWithString()55 public void compareWithString() { 56 Set<StringBuffer> sbSet = constructSBSet(); 57 Set<String> sSet = constructStringSet(); 58 Iterator<StringBuffer> iSB = sbSet.iterator(); 59 Iterator<String> iS = sSet.iterator(); 60 while (iSB.hasNext()) { 61 String temp1 = iSB.next().toString(); 62 System.out.println(temp1); 63 String temp2 = iS.next(); 64 System.out.println(temp2); 65 66 Assert.assertTrue(temp1.equals(temp2), "Comparing item by item"); 67 } 68 69 } 70 71 /** 72 * Compares between StringBuffers 73 */ 74 @Test testCompare()75 public void testCompare() { 76 StringBuffer sb1 = generateTestBuffer(65, 70, 97, 102); 77 StringBuffer sb2 = generateTestBuffer(65, 70, 97, 102); 78 StringBuffer sb3 = generateTestBuffer(65, 71, 97, 103); 79 80 System.out.println(sb1.toString()); 81 System.out.println(sb2.toString()); 82 System.out.println(sb3.toString()); 83 Assert.assertTrue(sb1.compareTo(sb2) == 0, "Compare sb1 and sb2"); 84 Assert.assertFalse(sb1.compareTo(sb3) == 0, "Compare sb1 and sb3"); 85 } 86 87 /** 88 * Verifies that the comparison is from index 0 to length() - 1 of the two 89 * character sequences. 90 */ 91 @Test testModifiedSequence()92 public void testModifiedSequence() { 93 StringBuffer sb1 = generateTestBuffer(65, 70, 97, 102); 94 StringBuffer sb2 = generateTestBuffer(65, 70, 98, 103); 95 96 // contain different character sequences 97 Assert.assertFalse(sb1.compareTo(sb2) == 0, "Compare the sequences before truncation"); 98 99 // the first 5 characters however are the same 100 sb1.setLength(5); 101 sb2.setLength(5); 102 103 System.out.println(sb1.toString()); 104 System.out.println(sb2.toString()); 105 106 Assert.assertTrue(sb1.compareTo(sb2) == 0, "Compare sb1 and sb2"); 107 Assert.assertTrue(sb1.toString().compareTo(sb2.toString()) == 0, "Compare strings of sb1 and sb2"); 108 } 109 constructStringSet()110 private Set<String> constructStringSet() { 111 Set<String> sSet = new TreeSet<>(); 112 for (String[] book : books) { 113 sSet.add(book[0] + SEP + book[1]); 114 } 115 return sSet; 116 } 117 constructSBSet()118 private Set<StringBuffer> constructSBSet() { 119 Set<StringBuffer> sbSet = new TreeSet<>(); 120 for (String[] book : books) { 121 sbSet.add(new StringBuffer(book[0]).append(SEP).append(book[1])); 122 } 123 return sbSet; 124 } 125 generateTestBuffer(int from1, int to1, int from2, int to2)126 private static StringBuffer generateTestBuffer(int from1, int to1, 127 int from2, int to2) { 128 StringBuffer aBuffer = new StringBuffer(50); 129 130 for (int i = from1; i < to1; i++) { 131 aBuffer.append((char)i); 132 } 133 for (int i = from2; i < to2; i++) { 134 aBuffer.append((char)i); 135 } 136 return aBuffer; 137 } 138 } 139