001package beginner; 002 003import com.sandwich.koan.Koan; 004 005import java.text.MessageFormat; 006 007import static com.sandwich.koan.constant.KoanConstants.__; 008import static com.sandwich.util.Assert.assertEquals; 009import static com.sandwich.util.Assert.fail; 010 011public class AboutStrings { 012 013 @Koan 014 public void implicitStrings() { 015 assertEquals("just a plain ole string".getClass(), __); 016 } 017 018 @Koan 019 public void newString() { 020 // very rarely if ever should Strings be created via new String() in 021 // practice - generally it is redundant, and done repetitively can be slow 022 String string = new String(); 023 String empty = ""; 024 assertEquals(string.equals(empty), __); 025 } 026 027 @Koan 028 public void newStringIsRedundant() { 029 String stringInstance = "zero"; 030 String stringReference = new String(stringInstance); 031 assertEquals(stringInstance.equals(stringReference), __); 032 } 033 034 @Koan 035 public void newStringIsNotIdentical() { 036 String stringInstance = "zero"; 037 String stringReference = new String(stringInstance); 038 assertEquals(stringInstance == stringReference, __); 039 } 040 041 @Koan 042 public void stringIsEmpty() { 043 assertEquals("".isEmpty(), __); 044 assertEquals("one".isEmpty(), __); 045 assertEquals(new String().isEmpty(), __); 046 assertEquals(new String("").isEmpty(), __); 047 assertEquals(new String("one").isEmpty(), __); 048 } 049 050 @Koan 051 public void stringLength() { 052 assertEquals("".length(), __); 053 assertEquals("one".length(), __); 054 assertEquals("the number is one".length(), __); 055 } 056 057 @Koan 058 public void stringTrim() { 059 assertEquals("".trim(), __); 060 assertEquals("one".trim(), "one"); 061 assertEquals(" one more time".trim(), __); 062 assertEquals(" one more time ".trim(), __); 063 assertEquals(" and again\t".trim(), __); 064 assertEquals("\t\t\twhat about now?\t".trim(), __); 065 } 066 067 @Koan 068 public void stringConcatenation() { 069 String one = "one"; 070 String space = " "; 071 String two = "two"; 072 assertEquals(one + space + two, __); 073 assertEquals(space + one + two, __); 074 assertEquals(two + space + one, __); 075 } 076 077 @Koan 078 public void stringUpperCase() { 079 String str = "I am a number one!"; 080 assertEquals(str.toUpperCase(), __); 081 } 082 083 @Koan 084 public void stringLowerCase() { 085 String str = "I AM a number ONE!"; 086 assertEquals(str.toLowerCase(), __); 087 } 088 089 @Koan 090 public void stringCompare() { 091 String str = "I AM a number ONE!"; 092 assertEquals(str.compareTo("I AM a number ONE!") == 0, __); 093 assertEquals(str.compareTo("I am a number one!") == 0, __); 094 assertEquals(str.compareTo("I AM A NUMBER ONE!") == 0, __); 095 } 096 097 @Koan 098 public void stringCompareIgnoreCase() { 099 String str = "I AM a number ONE!"; 100 assertEquals(str.compareToIgnoreCase("I AM a number ONE!") == 0, __); 101 assertEquals(str.compareToIgnoreCase("I am a number one!") == 0, __); 102 assertEquals(str.compareToIgnoreCase("I AM A NUMBER ONE!") == 0, __); 103 } 104 105 @Koan 106 public void stringStartsWith() { 107 assertEquals("".startsWith("one"), __); 108 assertEquals("one".startsWith("one"), __); 109 assertEquals("one is the number".startsWith("one"), __); 110 assertEquals("ONE is the number".startsWith("one"), __); 111 } 112 113 @Koan 114 public void stringEndsWith() { 115 assertEquals("".endsWith("one"), __); 116 assertEquals("one".endsWith("one"), __); 117 assertEquals("the number is one".endsWith("one"), __); 118 assertEquals("the number is two".endsWith("one"), __); 119 assertEquals("the number is One".endsWith("one"), __); 120 } 121 122 @Koan 123 public void stringSubstring() { 124 String str = "I AM a number ONE!"; 125 assertEquals(str.substring(0), __); 126 assertEquals(str.substring(1), __); 127 assertEquals(str.substring(5), __); 128 assertEquals(str.substring(14, 17), __); 129 assertEquals(str.substring(7, str.length()), __); 130 } 131 132 @Koan 133 public void stringContains() { 134 String str = "I AM a number ONE!"; 135 assertEquals(str.contains("one"), __); 136 assertEquals(str.contains("ONE"), __); 137 } 138 139 @Koan 140 public void stringReplace() { 141 String str = "I am a number ONE!"; 142 assertEquals(str.replace("ONE", "TWO"), __); 143 assertEquals(str.replace("I am", "She is"), __); 144 } 145 146 @Koan 147 public void stringBuilderCanActAsAMutableString() { 148 assertEquals(new StringBuilder("one").append(" ").append("two").toString(), __); 149 } 150 151 @Koan 152 public void readableStringFormattingWithStringFormat() { 153 assertEquals(String.format("%s %s %s", "a", "b", "a"), __); 154 } 155 156 @Koan 157 public void extraArgumentsToStringFormatGetIgnored() { 158 assertEquals(String.format("%s %s %s", "a", "b", "c", "d"), __); 159 } 160 161 @Koan 162 public void insufficientArgumentsToStringFormatCausesAnError() { 163 try { 164 String.format("%s %s %s", "a", "b"); 165 fail("No Exception was thrown!"); 166 } catch (Exception e) { 167 assertEquals(e.getClass(), __); 168 assertEquals(e.getMessage(), __); 169 } 170 } 171 172 @Koan 173 public void readableStringFormattingWithMessageFormat() { 174 assertEquals(MessageFormat.format("{0} {1} {0}", "a", "b"), __); 175 } 176 177 @Koan 178 public void extraArgumentsToMessageFormatGetIgnored() { 179 assertEquals(MessageFormat.format("{0} {1} {0}", "a", "b", "c"), __); 180 } 181 182 @Koan 183 public void insufficientArgumentsToMessageFormatDoesNotReplaceTheToken() { 184 assertEquals(MessageFormat.format("{0} {1} {0}", "a"), __); 185 } 186}