|
2 | 2 |
|
3 | 3 | import static org.junit.Assert.assertEquals; |
4 | 4 |
|
5 | | -import java.io.UnsupportedEncodingException; |
6 | 5 | import java.text.ParseException; |
7 | 6 | import java.text.SimpleDateFormat; |
8 | 7 | import java.time.LocalDateTime; |
|
19 | 18 | public class StringConversionTest { |
20 | 19 |
|
21 | 20 | @Test |
22 | | - public void whenConvertedToInt_thenCorrect() { |
23 | | - assertEquals(Integer.parseInt("1"), 1); |
| 21 | + public void whenConvedToInt_thenCorrect() { |
| 22 | + String beforeConvStr = "1"; |
| 23 | + int afterConvInt = 1; |
| 24 | + |
| 25 | + assertEquals(Integer.parseInt(beforeConvStr), afterConvInt); |
24 | 26 | } |
25 | 27 |
|
26 | 28 | @Test |
27 | | - public void whenConvertedToInteger_thenCorrect() { |
28 | | - assertEquals(Integer.valueOf("12").equals(12), true); |
| 29 | + public void whenConvedToInteger_thenCorrect() { |
| 30 | + String beforeConvStr = "12"; |
| 31 | + Integer afterConvInteger = 12; |
| 32 | + |
| 33 | + assertEquals(Integer.valueOf(beforeConvStr).equals(afterConvInteger), true); |
29 | 34 | } |
30 | 35 |
|
31 | 36 | @Test |
32 | | - public void whenConvertedTolong_thenCorrect() { |
33 | | - assertEquals(Long.parseLong("12345"), 12345); |
| 37 | + public void whenConvedTolong_thenCorrect() { |
| 38 | + String beforeConvStr = "12345"; |
| 39 | + long afterConvLongPrimitive = 12345; |
| 40 | + |
| 41 | + assertEquals(Long.parseLong(beforeConvStr), afterConvLongPrimitive); |
34 | 42 | } |
35 | 43 |
|
36 | 44 | @Test |
37 | | - public void whenConvertedToLong_thenCorrect() { |
38 | | - assertEquals(Long.valueOf("14567").equals(14567L), true); |
| 45 | + public void whenConvedToLong_thenCorrect() { |
| 46 | + String beforeConvStr = "14567"; |
| 47 | + Long afterConvLong = 14567l; |
| 48 | + |
| 49 | + assertEquals(Long.valueOf(beforeConvStr).equals(afterConvLong), true); |
39 | 50 | } |
40 | 51 |
|
41 | 52 | @Test |
42 | | - public void whenConvertedTodouble_thenCorrect() { |
43 | | - assertEquals(Double.parseDouble("1.4"), 1.4, 0.0); |
| 53 | + public void whenConvedTodouble_thenCorrect() { |
| 54 | + String beforeConvStr = "1.4"; |
| 55 | + double afterConvDoublePrimitive = 1.4; |
| 56 | + |
| 57 | + assertEquals(Double.parseDouble(beforeConvStr), afterConvDoublePrimitive, 0.0); |
44 | 58 | } |
45 | 59 |
|
46 | 60 | @Test |
47 | | - public void whenConvertedToDouble_thenCorrect() { |
48 | | - assertEquals(Double.valueOf("145.67").equals(145.67d), true); |
| 61 | + public void whenConvedToDouble_thenCorrect() { |
| 62 | + String beforeConvStr = "145.67"; |
| 63 | + double afterConvDouble = 145.67d; |
| 64 | + |
| 65 | + assertEquals(Double.valueOf(beforeConvStr).equals(afterConvDouble), true); |
49 | 66 | } |
50 | 67 |
|
51 | 68 | @Test |
52 | | - public void whenConvertedToByteArray_thenCorrect() throws UnsupportedEncodingException { |
53 | | - byte[] byteArray1 = new byte[] { 'a', 'b', 'c' }; |
54 | | - String string = new String(byteArray1, "UTF-8"); |
| 69 | + public void whenConvedToByteArr_thenCorrect() { |
| 70 | + String beforeConvStr = "abc"; |
| 71 | + byte[] afterConvByteArr = new byte[] { 'a', 'b', 'c' }; |
55 | 72 |
|
56 | | - assertEquals(Arrays.equals(string.getBytes(), byteArray1), true); |
| 73 | + assertEquals(Arrays.equals(beforeConvStr.getBytes(), afterConvByteArr), true); |
57 | 74 | } |
58 | 75 |
|
59 | 76 | @Test |
60 | | - public void whenConvertedToboolean_thenCorrect() { |
61 | | - assertEquals(Boolean.parseBoolean("true"), true); |
| 77 | + public void whenConvedToboolean_thenCorrect() { |
| 78 | + String beforeConvStr = "true"; |
| 79 | + boolean afterConvBooleanPrimitive = true; |
| 80 | + |
| 81 | + assertEquals(Boolean.parseBoolean(beforeConvStr), afterConvBooleanPrimitive); |
62 | 82 | } |
63 | 83 |
|
64 | 84 | @Test |
65 | | - public void whenConvertedToBoolean_thenCorrect() { |
66 | | - assertEquals(Boolean.valueOf("true"), true); |
| 85 | + public void whenConvedToBoolean_thenCorrect() { |
| 86 | + String beforeConvStr = "true"; |
| 87 | + Boolean afterConvBoolean = true; |
| 88 | + |
| 89 | + assertEquals(Boolean.valueOf(beforeConvStr), afterConvBoolean); |
67 | 90 | } |
68 | 91 |
|
69 | 92 | @Test |
70 | | - public void whenConvertedToCharArray_thenCorrect() { |
71 | | - String str = "hello"; |
72 | | - char[] charArray = { 'h', 'e', 'l', 'l', 'o' }; |
| 93 | + public void whenConvedToCharArr_thenCorrect() { |
| 94 | + String beforeConvStr = "hello"; |
| 95 | + char[] afterConvCharArr = { 'h', 'e', 'l', 'l', 'o' }; |
73 | 96 |
|
74 | | - assertEquals(Arrays.equals(charArray, str.toCharArray()), true); |
| 97 | + assertEquals(Arrays.equals(beforeConvStr.toCharArray(), afterConvCharArr), true); |
75 | 98 | } |
76 | 99 |
|
77 | 100 | @Test |
78 | | - public void whenConvertedToDate_thenCorrect() throws ParseException { |
79 | | - String str = "15/10/2013"; |
| 101 | + public void whenConvedToDate_thenCorrect() throws ParseException { |
| 102 | + String beforeConvStr = "15/10/2013"; |
| 103 | + int afterConvCalendarDay = 15; |
| 104 | + int afterConvCalendarMonth = 9; |
| 105 | + int afterConvCalendarYear = 2013; |
80 | 106 | SimpleDateFormat formatter = new SimpleDateFormat("dd/M/yyyy"); |
81 | | - Date date1 = formatter.parse(str); |
82 | | - Calendar calendar = new GregorianCalendar(2013, 9, 15); |
83 | | - Date date2 = calendar.getTime(); |
| 107 | + Date afterConvDate = formatter.parse(beforeConvStr); |
| 108 | + Calendar calendar = new GregorianCalendar(); |
| 109 | + calendar.setTime(afterConvDate); |
84 | 110 |
|
85 | | - assertEquals(date1.compareTo(date2), 0); |
| 111 | + assertEquals(calendar.get(Calendar.DAY_OF_MONTH), afterConvCalendarDay); |
| 112 | + assertEquals(calendar.get(Calendar.MONTH), afterConvCalendarMonth); |
| 113 | + assertEquals(calendar.get(Calendar.YEAR), afterConvCalendarYear); |
86 | 114 | } |
87 | 115 |
|
88 | 116 | @Test |
89 | | - public void whenConvertedToLocalDateTime_thenCorrect() throws ParseException { |
| 117 | + public void whenConvedToLocalDateTime_thenCorrect() { |
90 | 118 | String str = "2007-12-03T10:15:30"; |
91 | | - LocalDateTime localDateTime = new UseLocalDateTime().getLocalDateTimeUsingParseMethod(str); |
92 | | - |
93 | | - assertEquals(localDateTime.getDayOfMonth(), 3); |
94 | | - assertEquals(localDateTime.getMonth(), Month.DECEMBER); |
95 | | - assertEquals(localDateTime.getYear(), 2007); |
| 119 | + int afterConvCalendarDay = 03; |
| 120 | + Month afterConvCalendarMonth = Month.DECEMBER; |
| 121 | + int afterConvCalendarYear = 2007; |
| 122 | + LocalDateTime afterConvDate = new UseLocalDateTime().getLocalDateTimeUsingParseMethod(str); |
| 123 | + |
| 124 | + assertEquals(afterConvDate.getDayOfMonth(), afterConvCalendarDay); |
| 125 | + assertEquals(afterConvDate.getMonth(), afterConvCalendarMonth); |
| 126 | + assertEquals(afterConvDate.getYear(), afterConvCalendarYear); |
96 | 127 | } |
97 | 128 | } |
0 commit comments