Skip to content

Commit 062b83f

Browse files
mansi2392pedja4
authored andcommitted
BAEL-1237 String Formatter (eugenp#2912)
* Example Code For Evaluation Article This is an example code for the evaluation article on "Different Types of Bean Injection in Spring" * Added unit tests * Minor changes to application context * Removed code committed for evaluation article * BAEL-944 Demonstrating the problems with new Url pattern matching in Spring 5 * BAEL-944 Demonstrating the problems with new Url pattern matching in Spring 5 * BAEL-944 Exploring the Spring MVC URL Matching Improvements * BAEL-944 Exploring the Spring MVC URL Matching Improvements * BAEL-944 Exploring the Spring MVC URL Matching Improvements * BAEL-944 Code Formatting and solving build issue * BAEL-944 Resolving build issue due to change in Spring version * BAEL-944 Resolving build issue * BAEL-944 Formatting code * BAEL-944 Moving tests to correct package * BAEL-944 Moving tests to correct package * BAEL-944 Replacing @RequestMapping by @GetMapping * BAEL-944 Remove unnecessary attribute name, "value" in annotations * BAEL-79 Intro to Activiti with Spring * BAEL-79 Intro to Activiti with Spring * BAEL-79 Adding activiti module to the parent modules * BAEL-79 Using latest version * BAEL-79 Update Spring boot version that works with Activiti * BAEL-79 Replace RequestMapping with GetMapping * BAEL-79 Use Java 8 Syntax * BAEL-79 Formatting * BAEL-79 changed module name * BAEL-378 A Guide to Activiti with Java * BAEL-79 Fixed unit tests * BAEL-79 Simplified the process * BAEL-79 Fix test cases * BAEL-1045 Lambda Behave * BAEL-1045 Lambda Behave * BAEL-1045 Lambda Behave * BAEL-1090 Difference between compact and compressed strings in Java 9 * BAEL-1237 String Formatter * BAEL-1237 String Formatter * BAEL-1237 String Formatter * BAEL-1237 String Formatter * BAEL-1237 Guide to java.util.Formatter
1 parent 1b31c3f commit 062b83f

1 file changed

Lines changed: 141 additions & 0 deletions

File tree

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package com.baeldung.string.formatter;
2+
3+
import java.util.Calendar;
4+
import java.util.Formatter;
5+
import java.util.GregorianCalendar;
6+
import static org.junit.Assert.assertEquals;
7+
import static org.junit.Assert.assertFalse;
8+
import static org.junit.Assert.assertTrue;
9+
import org.junit.Test;
10+
11+
public class StringFormatterExampleTests {
12+
13+
@Test
14+
public void givenString_whenFormatSpecifierForCalendar_thenGotExpected() {
15+
//Syntax of Format Specifiers for Date/Time Representation
16+
Calendar c = new GregorianCalendar(2017, 11, 10);
17+
String s = String.format("The date is: %1$tm %1$te,%1$tY", c);
18+
19+
assertEquals("The date is: 12 10,2017", s);
20+
}
21+
22+
@Test
23+
public void givenString_whenGeneralConversion_thenConvertedString() {
24+
//General Conversions
25+
String s = String.format("The correct answer is %s", false);
26+
assertEquals("The correct answer is false", s);
27+
28+
s = String.format("The correct answer is %b", null);
29+
assertEquals("The correct answer is false", s);
30+
31+
s = String.format("The correct answer is %B", true);
32+
assertEquals("The correct answer is TRUE", s);
33+
}
34+
35+
@Test
36+
public void givenString_whenCharConversion_thenConvertedString() {
37+
//Character Conversions
38+
String s = String.format("The correct answer is %c", 'a');
39+
assertEquals("The correct answer is a", s);
40+
41+
s = String.format("The correct answer is %c", null);
42+
assertEquals("The correct answer is null", s);
43+
44+
s = String.format("The correct answer is %C", 'b');
45+
assertEquals("The correct answer is B", s);
46+
47+
s = String.format("The valid unicode character: %c", 0x0400);
48+
assertTrue(Character.isValidCodePoint(0x0400));
49+
assertEquals("The valid unicode character: Ѐ", s);
50+
}
51+
52+
@Test(expected = java.util.IllegalFormatCodePointException.class)
53+
public void givenString_whenIllegalCodePointForConversion_thenError() {
54+
String s = String.format("The valid unicode character: %c", 0x11FFFF);
55+
assertFalse(Character.isValidCodePoint(0x11FFFF));
56+
assertEquals("The valid unicode character: Ā", s);
57+
}
58+
59+
@Test
60+
public void givenString_whenNumericIntegralConversion_thenConvertedString() {
61+
//Numeric Integral Conversions
62+
String s = String.format("The number 25 in decimal = %d", 25);
63+
assertEquals("The number 25 in decimal = 25", s);
64+
65+
s = String.format("The number 25 in octal = %o", 25);
66+
assertEquals("The number 25 in octal = 31", s);
67+
68+
s = String.format("The number 25 in hexadecimal = %x", 25);
69+
assertEquals("The number 25 in hexadecimal = 19", s);
70+
}
71+
72+
@Test
73+
public void givenString_whenNumericFloatingConversion_thenConvertedString() {
74+
//Numeric Floating-point Conversions
75+
String s = String.format("The computerized scientific format of 10000.00 "
76+
+ "= %e", 10000.00);
77+
assertEquals("The computerized scientific format of 10000.00 = 1.000000e+04", s);
78+
79+
s = String.format("The decimal format of 10.019 = %f", 10.019);
80+
assertEquals("The decimal format of 10.019 = 10.019000", s);
81+
}
82+
83+
@Test
84+
public void givenString_whenLineSeparatorConversion_thenConvertedString() {
85+
//Line Separator Conversion
86+
String s = String.format("First Line %nSecond Line");
87+
assertEquals("First Line \n"
88+
+ "Second Line", s);
89+
}
90+
91+
@Test
92+
public void givenString_whenSpecifyFlag_thenGotFormattedString() {
93+
//Without left-justified flag
94+
String s = String.format("Without left justified flag: %5d", 25);
95+
assertEquals("Without left justified flag: 25", s);
96+
97+
//Using left-justified flag
98+
s = String.format("With left justified flag: %-5d", 25);
99+
assertEquals("With left justified flag: 25 ", s);
100+
}
101+
102+
@Test
103+
public void givenString_whenSpecifyPrecision_thenGotExpected() {
104+
105+
//Precision
106+
String s = String.format("Output of 25.09878 with Precision 2: %.2f", 25.09878);
107+
assertEquals("Output of 25.09878 with Precision 2: 25.10", s);
108+
109+
s = String.format("Output of general conversion type with Precision 2: %.2b", true);
110+
assertEquals("Output of general conversion type with Precision 2: tr", s);
111+
}
112+
113+
@Test
114+
public void givenString_whenSpecifyArgumentIndex_thenGotExpected() {
115+
Calendar c = new GregorianCalendar(2017, 11, 10);
116+
//Argument_Index
117+
String s = String.format("The date is: %1$tm %1$te,%1$tY", c);
118+
assertEquals("The date is: 12 10,2017", s);
119+
120+
s = String.format("The date is: %1$tm %<te,%<tY", c);
121+
assertEquals("The date is: 12 10,2017", s);
122+
}
123+
124+
@Test
125+
public void givenAppendable_whenCreateFormatter_thenFormatterWorksOnAppendable() {
126+
//Using String Formatter with Appendable
127+
StringBuilder sb = new StringBuilder();
128+
Formatter formatter = new Formatter(sb);
129+
formatter.format("I am writting to a %1$s Instance.", sb.getClass());
130+
131+
assertEquals("I am writting to a class java.lang.StringBuilder Instance.", sb.toString());
132+
}
133+
134+
@Test
135+
public void givenString_whenNoArguments_thenExpected() {
136+
//Using String Formatter without arguments
137+
String s = String.format("John scored 90%% in Fall semester");
138+
assertEquals("John scored 90% in Fall semester", s);
139+
}
140+
141+
}

0 commit comments

Comments
 (0)