Skip to content

Update error message generation and fix a code smell #534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@
public abstract class GeneralInsertMappingVisitor<R> implements ColumnMappingVisitor<R> {
@Override
public final R visit(SelectMapping mapping) {
throw new UnsupportedOperationException(Messages.getString("ERROR.31", "1")); //$NON-NLS-1$ //$NON-NLS-2$
throw new UnsupportedOperationException(Messages.getInternalErrorString(1));
}

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

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

@Override
public final R visit(ColumnToColumnMapping columnMapping) {
throw new UnsupportedOperationException(Messages.getString("ERROR.31", "4")); //$NON-NLS-1$ //$NON-NLS-2$
throw new UnsupportedOperationException(Messages.getInternalErrorString(4));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@
public abstract class InsertMappingVisitor<R> implements ColumnMappingVisitor<R> {
@Override
public final <T> R visit(ValueMapping<T> mapping) {
throw new UnsupportedOperationException(Messages.getString("ERROR.31", "5")); //$NON-NLS-1$ //$NON-NLS-2$
throw new UnsupportedOperationException(Messages.getInternalErrorString(5));
}

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

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

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

@Override
public final R visit(ColumnToColumnMapping columnMapping) {
throw new UnsupportedOperationException(Messages.getString("ERROR.31", "9")); //$NON-NLS-1$ //$NON-NLS-2$
throw new UnsupportedOperationException(Messages.getInternalErrorString(9));
}
}
26 changes: 8 additions & 18 deletions src/main/java/org/mybatis/dynamic/sql/util/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,28 @@
package org.mybatis.dynamic.sql.util;

import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

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

private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
.getBundle(BUNDLE_NAME);
private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);

private Messages() {}

public static String getString(String key) {
try {
return RESOURCE_BUNDLE.getString(key);
} catch (MissingResourceException e) {
return '!' + key + '!';
}
return RESOURCE_BUNDLE.getString(key);
}

public static String getString(String key, String p1) {
try {
return MessageFormat.format(RESOURCE_BUNDLE.getString(key), p1);
} catch (MissingResourceException e) {
return '!' + key + '!';
}
return MessageFormat.format(getString(key), p1);
}

public static String getString(String key, String p1, String p2, String p3) {
try {
return MessageFormat.format(RESOURCE_BUNDLE.getString(key), p1, p2, p3);
} catch (MissingResourceException e) {
return '!' + key + '!';
}
return MessageFormat.format(getString(key), p1, p2, p3);
}

public static String getInternalErrorString(int internalErrorNumber) {
return MessageFormat.format(getString("INTERNAL.ERROR"), internalErrorNumber); //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
public abstract class MultiRowInsertMappingVisitor<R> extends InsertMappingVisitor<R> {
@Override
public final R visit(PropertyWhenPresentMapping mapping) {
throw new UnsupportedOperationException(Messages.getString("ERROR.31", "12")); //$NON-NLS-1$ //$NON-NLS-2$
throw new UnsupportedOperationException(Messages.getInternalErrorString(12));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
public abstract class UpdateMappingVisitor<R> implements ColumnMappingVisitor<R> {
@Override
public final R visit(PropertyMapping mapping) {
throw new UnsupportedOperationException(Messages.getString("ERROR.31", "10")); //$NON-NLS-1$ //$NON-NLS-2$
throw new UnsupportedOperationException(Messages.getInternalErrorString(10));
}

@Override
public final R visit(PropertyWhenPresentMapping mapping) {
throw new UnsupportedOperationException(Messages.getString("ERROR.31", "11")); //$NON-NLS-1$ //$NON-NLS-2$
throw new UnsupportedOperationException(Messages.getInternalErrorString(11));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ ERROR.27=You must specify a "from" clause before any other clauses in a select s
ERROR.28=You must specify a select statement in a sub query
ERROR.29=Insert Select Statements Must Contain an "into" phrase
ERROR.30=The parameters for insertMultipleWithGeneratedKeys must contain exactly one parameter of type String
ERROR.31=Internal Error {0}
INTERNAL.ERROR=Internal Error {0}
17 changes: 4 additions & 13 deletions src/test/java/org/mybatis/dynamic/sql/InvalidSQLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.mybatis.dynamic.sql;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mybatis.dynamic.sql.SqlBuilder.insert;
import static org.mybatis.dynamic.sql.SqlBuilder.insertInto;
Expand All @@ -24,6 +23,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.MissingResourceException;

import org.junit.jupiter.api.Test;
import org.mybatis.dynamic.sql.exception.InvalidSqlException;
Expand Down Expand Up @@ -221,18 +221,9 @@ void testInvalidUpdateStatementWhenAllOptionalsAreDropped() {
}

@Test
void testMissingMessage1() {
assertThat(Messages.getString("MISSING_MESSAGE")).isEqualTo("!MISSING_MESSAGE!");
}

@Test
void testMissingMessage2() {
assertThat(Messages.getString("MISSING_MESSAGE", "s1")).isEqualTo("!MISSING_MESSAGE!");
}

@Test
void testMissingMessage3() {
assertThat(Messages.getString("MISSING_MESSAGE", "s1", "s2", "s3")).isEqualTo("!MISSING_MESSAGE!");
void testMissingMessage() {
assertThatExceptionOfType(MissingResourceException.class)
.isThrownBy(() -> Messages.getString("MISSING_MESSAGE"));
}

static class TestRow {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ void testThatGeneralInsertVisitorErrorsForColumnToColumnMapping() {
GeneralInsertVisitor tv = new GeneralInsertVisitor();
ColumnToColumnMapping mapping = ColumnToColumnMapping.of(table.id, table.description);

assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
.withMessage("Internal Error 4");
}

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

assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
.withMessage("Internal Error 1");
}

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

assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
.withMessage("Internal Error 2");
}

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

assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
.withMessage("Internal Error 3");
}

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

assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
.withMessage("Internal Error 9");
}

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

assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
.withMessage("Internal Error 8");
}

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

assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
.withMessage("Internal Error 5");
}

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

assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
.withMessage("Internal Error 6");
}

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

assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
.withMessage("Internal Error 7");
}

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

assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
.withMessage("Internal Error 9");
}

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

assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
.withMessage("Internal Error 8");
}

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

assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
.withMessage("Internal Error 5");
}

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

assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
.withMessage("Internal Error 7");
}

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

assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
.withMessage("Internal Error 12");
}

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

assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
.withMessage("Internal Error 10");
}

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

assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
.withMessage("Internal Error 11");
}

private static class TestTable extends SqlTable {
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy