Skip to content

Commit

Permalink
Review Sonarcloud findings
Browse files Browse the repository at this point in the history
  • Loading branch information
spannm committed Apr 14, 2024
1 parent e877fa4 commit d6517df
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ public Database open() throws IOException {
/**
* Creates a new Database using the configured information.
*/
@SuppressWarnings("java:S2095") // suppress sonarcloud warning regarding try-with-resources
public Database create() throws IOException {
Database db = DatabaseImpl.create(_fileFormat, _mdbFile, _channel, _autoSync, _charset, _timeZone);
if (_dbProps != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public final Object validate(Column col, Object val) throws IOException {

@Override
public String toString() {
StringBuilder sb = new StringBuilder('{');
StringBuilder sb = new StringBuilder("{");
if (_delegate instanceof InternalColumnValidator) {
((InternalColumnValidator) _delegate).appendToString(sb);
} else if (_delegate != SimpleColumnValidator.INSTANCE) {
Expand Down
54 changes: 27 additions & 27 deletions src/main/java/io/github/spannm/jackcess/impl/PropertyMapImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,36 @@
* @author James Ahlborn
*/
public class PropertyMapImpl implements PropertyMap {
@SuppressWarnings("serial")
private static final Map<String, PropDef> DEFAULT_TYPES = new HashMap<>() {{
put(ACCESS_VERSION_PROP, new PropDef(DataType.TEXT, false));
put(TITLE_PROP, new PropDef(DataType.TEXT, false));
put(AUTHOR_PROP, new PropDef(DataType.TEXT, false));
put(COMPANY_PROP, new PropDef(DataType.TEXT, false));

put(DEFAULT_VALUE_PROP, new PropDef(DataType.MEMO, true));
put(REQUIRED_PROP, new PropDef(DataType.BOOLEAN, true));
put(ALLOW_ZERO_LEN_PROP, new PropDef(DataType.BOOLEAN, true));
put(DECIMAL_PLACES_PROP, new PropDef(DataType.BYTE, true));
put(FORMAT_PROP, new PropDef(DataType.TEXT, true));
put(INPUT_MASK_PROP, new PropDef(DataType.TEXT, true));
put(CAPTION_PROP, new PropDef(DataType.MEMO, false));
put(VALIDATION_RULE_PROP, new PropDef(DataType.TEXT, true));
put(VALIDATION_TEXT_PROP, new PropDef(DataType.TEXT, true));
put(GUID_PROP, new PropDef(DataType.BINARY, true));
put(DESCRIPTION_PROP, new PropDef(DataType.MEMO, false));
put(RESULT_TYPE_PROP, new PropDef(DataType.BYTE, true));
put(EXPRESSION_PROP, new PropDef(DataType.MEMO, true));
put(DISPLAY_CONTROL_PROP, new PropDef(DataType.INT, false));
put(TEXT_FORMAT_PROP, new PropDef(DataType.BYTE, false));
put(IME_MODE_PROP, new PropDef(DataType.BYTE, false));
put(IME_SENTENCE_MODE_PROP, new PropDef(DataType.BYTE, false));
}};
private static final Map<String, PropDef> DEFAULT_TYPES = new HashMap<>();
static {
DEFAULT_TYPES.putAll(Map.of(
ACCESS_VERSION_PROP, new PropDef(DataType.TEXT, false),
TITLE_PROP, new PropDef(DataType.TEXT, false),
AUTHOR_PROP, new PropDef(DataType.TEXT, false),
COMPANY_PROP, new PropDef(DataType.TEXT, false),
DEFAULT_VALUE_PROP, new PropDef(DataType.MEMO, true),
REQUIRED_PROP, new PropDef(DataType.BOOLEAN, true),
ALLOW_ZERO_LEN_PROP, new PropDef(DataType.BOOLEAN, true),
DECIMAL_PLACES_PROP, new PropDef(DataType.BYTE, true),
FORMAT_PROP, new PropDef(DataType.TEXT, true),
INPUT_MASK_PROP, new PropDef(DataType.TEXT, true)));
DEFAULT_TYPES.putAll(Map.of(
CAPTION_PROP, new PropDef(DataType.MEMO, false),
VALIDATION_RULE_PROP, new PropDef(DataType.TEXT, true),
VALIDATION_TEXT_PROP, new PropDef(DataType.TEXT, true),
GUID_PROP, new PropDef(DataType.BINARY, true),
DESCRIPTION_PROP, new PropDef(DataType.MEMO, false),
RESULT_TYPE_PROP, new PropDef(DataType.BYTE, true),
EXPRESSION_PROP, new PropDef(DataType.MEMO, true),
DISPLAY_CONTROL_PROP, new PropDef(DataType.INT, false),
TEXT_FORMAT_PROP, new PropDef(DataType.BYTE, false)));
DEFAULT_TYPES.put(IME_MODE_PROP, new PropDef(DataType.BYTE, false));
DEFAULT_TYPES.put(IME_SENTENCE_MODE_PROP, new PropDef(DataType.BYTE, false));
}

private final String _mapName;
private final short _mapType;
private final Map<String, Property> _props =
new LinkedHashMap<>();
private final Map<String, Property> _props = new LinkedHashMap<>();
private final PropertyMaps _owner;

public PropertyMapImpl(String name, short type, PropertyMaps owner) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,18 +161,15 @@ private void validate() throws IOException {
_secondaryCols = getColumns(_secondaryTable, _relationship.getToColumns());

if (_primaryCols == null || _primaryCols.isEmpty() || _secondaryCols == null || _secondaryCols.isEmpty()) {
throw new IllegalArgumentException(withErrorContext(
"Missing columns in relationship"));
throw new IllegalArgumentException(withErrorContext("Missing columns in relationship"));
}

if (_primaryCols.size() != _secondaryCols.size()) {
throw new IllegalArgumentException(withErrorContext(
"Must have same number of columns on each side of relationship"));
throw new IllegalArgumentException(withErrorContext("Must have same number of columns on each side of relationship"));
}

for (ColumnImpl pcol : _primaryCols) {

if (pcol.getType() != pcol.getType()) {
for (int i = 0; i < _primaryCols.size(); i++) {
if (_primaryCols.get(i).getType() != _secondaryCols.get(i).getType()) {
throw new IllegalArgumentException(withErrorContext(
"Matched columns must have the same data type"));
}
Expand Down
15 changes: 6 additions & 9 deletions src/main/java/io/github/spannm/jackcess/impl/RowIdImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,13 @@ public Type getType() {

@Override
public int compareTo(RowId other) {
return compareTo((RowIdImpl) other);
}

public int compareTo(RowIdImpl other) {
int compare = getType().compareTo(other.getType());
if (compare == 0) {
compare = Integer.compare(getPageNumber(), other.getPageNumber());
}
RowIdImpl o = (RowIdImpl) other;
int compare = getType().compareTo(o.getType());
if (compare == 0) {
compare = Integer.compare(getRowNumber(), other.getRowNumber());
compare = Integer.compare(getPageNumber(), o.getPageNumber());
if (compare == 0) {
compare = Integer.compare(getRowNumber(), o.getRowNumber());
}
}
return compare;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ protected Value eval3(EvalContext ctx, Value param1, Value param2, Value param3)
}

// we have to construct incrementally to handle out of range values
LocalDate ld = LocalDate.of(year, 1, 1).plusMonths(month - 1)
.plusDays(day - 1);
LocalDate ld = LocalDate.of(year, 1, 1).plusMonths(month - 1).plusDays(day - 1);

return ValueSupport.toValue(ld);
}
Expand Down
32 changes: 18 additions & 14 deletions src/test/java/io/github/spannm/jackcess/impl/FKEnforcerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.github.spannm.jackcess.test.AbstractBaseTest;
import io.github.spannm.jackcess.test.TestDb;
import io.github.spannm.jackcess.test.source.TestDbSource;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.params.ParameterizedTest;

import java.io.IOException;
Expand Down Expand Up @@ -65,20 +66,23 @@ void testEnforceForeignKeys(TestDb testDb) throws Exception {
Table t2 = db.getTable("Table2");
Table t3 = db.getTable("Table3");

assertTrue(assertThrows(IOException.class, () ->
t1.addRow(20, 0, 20, "some data", 20)).getMessage().contains("Table1[otherfk2]"));

assertTrue(assertThrows(IOException.class, () -> {
Cursor c = CursorBuilder.createCursor(t2);
c.moveToNextRow();
c.updateCurrentRow(30, "foo30");
}).getMessage().contains("Table2[id]"));

assertTrue(assertThrows(IOException.class, () -> {
Cursor c = CursorBuilder.createCursor(t3);
c.moveToNextRow();
c.deleteCurrentRow();
}).getMessage().contains("Table3[id]"));
Map<Executable, String> tests = Map.of(
() -> t1.addRow(20, 0, 20, "some data", 20), "Table1[otherfk2]",
() -> {
Cursor c = CursorBuilder.createCursor(t2);
c.moveToNextRow();
c.updateCurrentRow(30, "foo30");
}, "Table2[id]",
() -> {
Cursor c = CursorBuilder.createCursor(t3);
c.moveToNextRow();
c.deleteCurrentRow();
}, "Table3[id]");
tests.entrySet().forEach(e -> {
IOException ex = assertThrows(IOException.class, e.getKey());
assertTrue(ex.getMessage().contains(e.getValue()));

});

t1.addRow(21, null, null, "null fks", null);

Expand Down

0 comments on commit d6517df

Please sign in to comment.