Skip to content

Commit 20aff6c

Browse files
committed
Update error message generation and fix a code smell
1 parent 35687cf commit 20aff6c

File tree

8 files changed

+57
-60
lines changed

8 files changed

+57
-60
lines changed

src/main/java/org/mybatis/dynamic/sql/util/GeneralInsertMappingVisitor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@
1818
public abstract class GeneralInsertMappingVisitor<R> implements ColumnMappingVisitor<R> {
1919
@Override
2020
public final R visit(SelectMapping mapping) {
21-
throw new UnsupportedOperationException(Messages.getString("ERROR.31", "1")); //$NON-NLS-1$ //$NON-NLS-2$
21+
throw new UnsupportedOperationException(Messages.getInternalErrorString(1));
2222
}
2323

2424
@Override
2525
public final R visit(PropertyMapping mapping) {
26-
throw new UnsupportedOperationException(Messages.getString("ERROR.31", "2")); //$NON-NLS-1$ //$NON-NLS-2$
26+
throw new UnsupportedOperationException(Messages.getInternalErrorString(2));
2727
}
2828

2929
@Override
3030
public final R visit(PropertyWhenPresentMapping mapping) {
31-
throw new UnsupportedOperationException(Messages.getString("ERROR.31", "3")); //$NON-NLS-1$ //$NON-NLS-2$
31+
throw new UnsupportedOperationException(Messages.getInternalErrorString(3));
3232
}
3333

3434
@Override
3535
public final R visit(ColumnToColumnMapping columnMapping) {
36-
throw new UnsupportedOperationException(Messages.getString("ERROR.31", "4")); //$NON-NLS-1$ //$NON-NLS-2$
36+
throw new UnsupportedOperationException(Messages.getInternalErrorString(4));
3737
}
3838
}

src/main/java/org/mybatis/dynamic/sql/util/InsertMappingVisitor.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,26 @@
1818
public abstract class InsertMappingVisitor<R> implements ColumnMappingVisitor<R> {
1919
@Override
2020
public final <T> R visit(ValueMapping<T> mapping) {
21-
throw new UnsupportedOperationException(Messages.getString("ERROR.31", "5")); //$NON-NLS-1$ //$NON-NLS-2$
21+
throw new UnsupportedOperationException(Messages.getInternalErrorString(5));
2222
}
2323

2424
@Override
2525
public final <T> R visit(ValueOrNullMapping<T> mapping) {
26-
throw new UnsupportedOperationException(Messages.getString("ERROR.31", "6")); //$NON-NLS-1$ //$NON-NLS-2$
26+
throw new UnsupportedOperationException(Messages.getInternalErrorString(6));
2727
}
2828

2929
@Override
3030
public final <T> R visit(ValueWhenPresentMapping<T> mapping) {
31-
throw new UnsupportedOperationException(Messages.getString("ERROR.31", "7")); //$NON-NLS-1$ //$NON-NLS-2$
31+
throw new UnsupportedOperationException(Messages.getInternalErrorString(7));
3232
}
3333

3434
@Override
3535
public final R visit(SelectMapping mapping) {
36-
throw new UnsupportedOperationException(Messages.getString("ERROR.31", "8")); //$NON-NLS-1$ //$NON-NLS-2$
36+
throw new UnsupportedOperationException(Messages.getInternalErrorString(8));
3737
}
3838

3939
@Override
4040
public final R visit(ColumnToColumnMapping columnMapping) {
41-
throw new UnsupportedOperationException(Messages.getString("ERROR.31", "9")); //$NON-NLS-1$ //$NON-NLS-2$
41+
throw new UnsupportedOperationException(Messages.getInternalErrorString(9));
4242
}
4343
}

src/main/java/org/mybatis/dynamic/sql/util/Messages.java

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,28 @@
1616
package org.mybatis.dynamic.sql.util;
1717

1818
import java.text.MessageFormat;
19-
import java.util.MissingResourceException;
2019
import java.util.ResourceBundle;
2120

2221
public class Messages {
2322
private static final String BUNDLE_NAME = "org.mybatis.dynamic.sql.util.messages"; //$NON-NLS-1$
2423

25-
private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
26-
.getBundle(BUNDLE_NAME);
24+
private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);
2725

2826
private Messages() {}
2927

3028
public static String getString(String key) {
31-
try {
32-
return RESOURCE_BUNDLE.getString(key);
33-
} catch (MissingResourceException e) {
34-
return '!' + key + '!';
35-
}
29+
return RESOURCE_BUNDLE.getString(key);
3630
}
3731

3832
public static String getString(String key, String p1) {
39-
try {
40-
return MessageFormat.format(RESOURCE_BUNDLE.getString(key), p1);
41-
} catch (MissingResourceException e) {
42-
return '!' + key + '!';
43-
}
33+
return MessageFormat.format(getString(key), p1);
4434
}
4535

4636
public static String getString(String key, String p1, String p2, String p3) {
47-
try {
48-
return MessageFormat.format(RESOURCE_BUNDLE.getString(key), p1, p2, p3);
49-
} catch (MissingResourceException e) {
50-
return '!' + key + '!';
51-
}
37+
return MessageFormat.format(getString(key), p1, p2, p3);
38+
}
39+
40+
public static String getInternalErrorString(int internalErrorNumber) {
41+
return MessageFormat.format(getString("INTERNAL.ERROR"), internalErrorNumber); //$NON-NLS-1$
5242
}
5343
}

src/main/java/org/mybatis/dynamic/sql/util/MultiRowInsertMappingVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
public abstract class MultiRowInsertMappingVisitor<R> extends InsertMappingVisitor<R> {
1919
@Override
2020
public final R visit(PropertyWhenPresentMapping mapping) {
21-
throw new UnsupportedOperationException(Messages.getString("ERROR.31", "12")); //$NON-NLS-1$ //$NON-NLS-2$
21+
throw new UnsupportedOperationException(Messages.getInternalErrorString(12));
2222
}
2323
}

src/main/java/org/mybatis/dynamic/sql/util/UpdateMappingVisitor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
public abstract class UpdateMappingVisitor<R> implements ColumnMappingVisitor<R> {
1919
@Override
2020
public final R visit(PropertyMapping mapping) {
21-
throw new UnsupportedOperationException(Messages.getString("ERROR.31", "10")); //$NON-NLS-1$ //$NON-NLS-2$
21+
throw new UnsupportedOperationException(Messages.getInternalErrorString(10));
2222
}
2323

2424
@Override
2525
public final R visit(PropertyWhenPresentMapping mapping) {
26-
throw new UnsupportedOperationException(Messages.getString("ERROR.31", "11")); //$NON-NLS-1$ //$NON-NLS-2$
26+
throw new UnsupportedOperationException(Messages.getInternalErrorString(11));
2727
}
2828
}

src/main/resources/org/mybatis/dynamic/sql/util/messages.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ ERROR.27=You must specify a "from" clause before any other clauses in a select s
4646
ERROR.28=You must specify a select statement in a sub query
4747
ERROR.29=Insert Select Statements Must Contain an "into" phrase
4848
ERROR.30=The parameters for insertMultipleWithGeneratedKeys must contain exactly one parameter of type String
49-
ERROR.31=Internal Error {0}
49+
INTERNAL.ERROR=Internal Error {0}

src/test/java/org/mybatis/dynamic/sql/InvalidSQLTest.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.mybatis.dynamic.sql;
1717

18-
import static org.assertj.core.api.Assertions.assertThat;
1918
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2019
import static org.mybatis.dynamic.sql.SqlBuilder.insert;
2120
import static org.mybatis.dynamic.sql.SqlBuilder.insertInto;
@@ -24,6 +23,7 @@
2423
import java.util.ArrayList;
2524
import java.util.Collections;
2625
import java.util.List;
26+
import java.util.MissingResourceException;
2727

2828
import org.junit.jupiter.api.Test;
2929
import org.mybatis.dynamic.sql.exception.InvalidSqlException;
@@ -221,18 +221,9 @@ void testInvalidUpdateStatementWhenAllOptionalsAreDropped() {
221221
}
222222

223223
@Test
224-
void testMissingMessage1() {
225-
assertThat(Messages.getString("MISSING_MESSAGE")).isEqualTo("!MISSING_MESSAGE!");
226-
}
227-
228-
@Test
229-
void testMissingMessage2() {
230-
assertThat(Messages.getString("MISSING_MESSAGE", "s1")).isEqualTo("!MISSING_MESSAGE!");
231-
}
232-
233-
@Test
234-
void testMissingMessage3() {
235-
assertThat(Messages.getString("MISSING_MESSAGE", "s1", "s2", "s3")).isEqualTo("!MISSING_MESSAGE!");
224+
void testMissingMessage() {
225+
assertThatExceptionOfType(MissingResourceException.class)
226+
.isThrownBy(() -> Messages.getString("MISSING_MESSAGE"));
236227
}
237228

238229
static class TestRow {

src/test/java/org/mybatis/dynamic/sql/util/ColumnMappingVisitorTest.java

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ void testThatGeneralInsertVisitorErrorsForColumnToColumnMapping() {
3030
GeneralInsertVisitor tv = new GeneralInsertVisitor();
3131
ColumnToColumnMapping mapping = ColumnToColumnMapping.of(table.id, table.description);
3232

33-
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
33+
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
34+
.withMessage("Internal Error 4");
3435
}
3536

3637
@Test
@@ -39,7 +40,8 @@ void testThatGeneralInsertVisitorErrorsForSelectMapping() {
3940
GeneralInsertVisitor tv = new GeneralInsertVisitor();
4041
SelectMapping mapping = SelectMapping.of(table.id, SqlBuilder.select(table.id).from(table));
4142

42-
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
43+
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
44+
.withMessage("Internal Error 1");
4345
}
4446

4547
@Test
@@ -48,7 +50,8 @@ void testThatGeneralInsertVisitorErrorsForPropertyMapping() {
4850
GeneralInsertVisitor tv = new GeneralInsertVisitor();
4951
PropertyMapping mapping = PropertyMapping.of(table.id, "id");
5052

51-
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
53+
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
54+
.withMessage("Internal Error 2");
5255
}
5356

5457
@Test
@@ -57,7 +60,8 @@ void testThatGeneralInsertVisitorErrorsForPropertyWhenPresentMapping() {
5760
GeneralInsertVisitor tv = new GeneralInsertVisitor();
5861
PropertyWhenPresentMapping mapping = PropertyWhenPresentMapping.of(table.id, "id", () -> 3);
5962

60-
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
63+
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
64+
.withMessage("Internal Error 3");
6165
}
6266

6367
@Test
@@ -66,7 +70,8 @@ void testThatInsertVisitorErrorsForColumnToColumnMapping() {
6670
InsertVisitor tv = new InsertVisitor();
6771
ColumnToColumnMapping mapping = ColumnToColumnMapping.of(table.id, table.description);
6872

69-
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
73+
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
74+
.withMessage("Internal Error 9");
7075
}
7176

7277
@Test
@@ -75,7 +80,8 @@ void testThatInsertVisitorErrorsForSelectMapping() {
7580
InsertVisitor tv = new InsertVisitor();
7681
SelectMapping mapping = SelectMapping.of(table.id, SqlBuilder.select(table.id).from(table));
7782

78-
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
83+
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
84+
.withMessage("Internal Error 8");
7985
}
8086

8187
@Test
@@ -84,7 +90,8 @@ void testThatInsertVisitorErrorsForValueMapping() {
8490
InsertVisitor tv = new InsertVisitor();
8591
ValueMapping<Integer> mapping = ValueMapping.of(table.id, () -> 3);
8692

87-
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
93+
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
94+
.withMessage("Internal Error 5");
8895
}
8996

9097
@Test
@@ -93,7 +100,8 @@ void testThatInsertVisitorErrorsForValueOrNullMapping() {
93100
InsertVisitor tv = new InsertVisitor();
94101
ValueOrNullMapping<Integer> mapping = ValueOrNullMapping.of(table.id, () -> 3);
95102

96-
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
103+
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
104+
.withMessage("Internal Error 6");
97105
}
98106

99107
@Test
@@ -102,7 +110,8 @@ void testThatInsertVisitorErrorsForValueWhenPresentMapping() {
102110
InsertVisitor tv = new InsertVisitor();
103111
ValueWhenPresentMapping<Integer> mapping = ValueWhenPresentMapping.of(table.id, () -> 3);
104112

105-
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
113+
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
114+
.withMessage("Internal Error 7");
106115
}
107116

108117
@Test
@@ -111,7 +120,8 @@ void testThatMultiRowInsertVisitorErrorsForColumnToColumnMapping() {
111120
MultiRowInsertVisitor tv = new MultiRowInsertVisitor();
112121
ColumnToColumnMapping mapping = ColumnToColumnMapping.of(table.id, table.description);
113122

114-
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
123+
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
124+
.withMessage("Internal Error 9");
115125
}
116126

117127
@Test
@@ -120,7 +130,8 @@ void testThatMultiRowInsertVisitorErrorsForSelectMapping() {
120130
MultiRowInsertVisitor tv = new MultiRowInsertVisitor();
121131
SelectMapping mapping = SelectMapping.of(table.id, SqlBuilder.select(table.id).from(table));
122132

123-
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
133+
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
134+
.withMessage("Internal Error 8");
124135
}
125136

126137
@Test
@@ -129,7 +140,8 @@ void testThatMultiRowInsertVisitorErrorsForValueMapping() {
129140
MultiRowInsertVisitor tv = new MultiRowInsertVisitor();
130141
ValueMapping<Integer> mapping = ValueMapping.of(table.id, () -> 3);
131142

132-
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
143+
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
144+
.withMessage("Internal Error 5");
133145
}
134146

135147
@Test
@@ -138,7 +150,8 @@ void testThatMultiRowInsertVisitorErrorsForValueWhenPresentMapping() {
138150
MultiRowInsertVisitor tv = new MultiRowInsertVisitor();
139151
ValueWhenPresentMapping<Integer> mapping = ValueWhenPresentMapping.of(table.id, () -> 3);
140152

141-
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
153+
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
154+
.withMessage("Internal Error 7");
142155
}
143156

144157
@Test
@@ -147,7 +160,8 @@ void testThatMultiRowInsertVisitorErrorsForPropertyWhenPresentMapping() {
147160
MultiRowInsertVisitor tv = new MultiRowInsertVisitor();
148161
PropertyWhenPresentMapping mapping = PropertyWhenPresentMapping.of(table.id, "id", () -> 3);
149162

150-
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
163+
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
164+
.withMessage("Internal Error 12");
151165
}
152166

153167
@Test
@@ -156,7 +170,8 @@ void testThatUpdateVisitorErrorsForPropertyMapping() {
156170
UpdateVisitor tv = new UpdateVisitor();
157171
PropertyMapping mapping = PropertyMapping.of(table.id, "id");
158172

159-
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
173+
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
174+
.withMessage("Internal Error 10");
160175
}
161176

162177
@Test
@@ -165,7 +180,8 @@ void testThatUpdateVisitorErrorsForPropertyWhenPresentMapping() {
165180
UpdateVisitor tv = new UpdateVisitor();
166181
PropertyWhenPresentMapping mapping = PropertyWhenPresentMapping.of(table.id, "id", () -> 3);
167182

168-
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
183+
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
184+
.withMessage("Internal Error 11");
169185
}
170186

171187
private static class TestTable extends SqlTable {

0 commit comments

Comments
 (0)