Skip to content

Commit

Permalink
Merge pull request schemaspy#666 from npetzall/sonar_time
Browse files Browse the repository at this point in the history
Fixing sonarLint issues
  • Loading branch information
npetzall authored Mar 16, 2020
2 parents 60e4eb5 + ea1d3b3 commit 399a534
Show file tree
Hide file tree
Showing 23 changed files with 129 additions and 171 deletions.
10 changes: 3 additions & 7 deletions src/main/java/org/schemaspy/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -862,9 +862,7 @@ public Pattern getTableInclusions() {
try {
tableInclusions = Pattern.compile(strInclusions);
} catch (PatternSyntaxException badPattern) {
throw new InvalidConfigurationException(badPattern)
.setParamName("-i")
.setParamValue(strInclusions);
throw new InvalidConfigurationException(badPattern, "-i", strInclusions);
}
}

Expand Down Expand Up @@ -896,9 +894,7 @@ public Pattern getTableExclusions() {
try {
tableExclusions = Pattern.compile(strExclusions);
} catch (PatternSyntaxException badPattern) {
throw new InvalidConfigurationException(badPattern)
.setParamName("-I")
.setParamValue(strExclusions);
throw new InvalidConfigurationException(badPattern, "-I", strExclusions);
}
}

Expand Down Expand Up @@ -1383,7 +1379,7 @@ public static Set<String> getBuiltInDatabaseTypes(String loadedFromJar) {
try (JarInputStream jar = new JarInputStream(new FileInputStream(loadedFromJar))){
JarEntry entry;

while ((entry = jar.getNextJarEntry()) != null) {
while ((entry = jar.getNextJarEntry()) != null) { //NOSONAR
Matcher dbTypeMatcher = DBTYPE_PATTERN.matcher(entry.getName());
if (dbTypeMatcher.find()) {
databaseTypes.add(dbTypeMatcher.group(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

public class DegreeOfSeparationValidator implements IValueValidator<Integer> {
@Override
public void validate(String name, Integer value) throws ParameterException {
public void validate(String name, Integer value) {
if (value > 2 || value < 1 ) {
throw new ParameterException("Illegal value for '"+name+"', allowed values are ['1','2']");
}
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/org/schemaspy/cli/PropertyFileDefaultProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@
import com.beust.jcommander.IDefaultProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.FileCopyUtils;

import java.io.*;
import java.io.IOException;
import java.io.StringReader;
import java.lang.invoke.MethodHandles;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.Properties;

Expand All @@ -52,15 +55,14 @@ public PropertyFileDefaultProvider(String propertiesFilename) {
}

private static Properties loadProperties(String path) {
try (Reader reader = new InputStreamReader(new FileInputStream(path), "UTF-8")){
try {
String contents = new String(Files.readAllBytes(Paths.get(path)), StandardCharsets.UTF_8);
Properties properties = new Properties();
String contents = FileCopyUtils.copyToString(reader);
// Replace backslashes with double backslashes to escape windows path separator.
// Example input: schemaspy.o=C:\tools\schemaspy\output
properties.load(new StringReader(contents.replace("\\", "\\\\")));
return properties;
} catch (IOException e) {
LOGGER.error("File not found: {}", path, e);
throw new IllegalArgumentException("Could not find or load properties file: " + path, e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private URL findFile(String file) {
try {
return path.toUri().toURL();
} catch (MalformedURLException e) {
LOGGER.debug("Couldn't convert existing file: {}", path.toString(), e);
LOGGER.debug("Couldn't convert existing file: {}", path, e);
return null;
}
}
Expand All @@ -93,7 +93,7 @@ private URL findClassPath(String resource) {
LOGGER.debug("Couldn't convert url to uri to file: {}", url, e);
return null;
} catch (IOException e) {
LOGGER.error("Unable to create filesystem for url: {}", url.toString(), e);
LOGGER.error("Unable to create filesystem for url: {}", url, e);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ public Properties getDbProperties(String dbType) {
LOGGER.debug(resolutionInfo.getTrace());
return props;
} catch (ResourceNotFoundException rnfe) {
throw new InvalidConfigurationException("Unable to resolve databaseType: " + dbType, rnfe)
.setParamName("-t")
.setParamValue(dbType);
throw new InvalidConfigurationException("Unable to resolve databaseType: " + dbType, rnfe, "-t", dbType);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private void initColumnAutoUpdate(Table table, boolean forceQuotes) {
if (forceQuotes) {
if (!table.isLogical()) {
// don't completely choke just because we couldn't do this....
LOGGER.warn("Failed to determine auto increment status: {}", exc);
LOGGER.warn("Failed to determine auto increment status: {}", exc.getMessage(), exc);
LOGGER.warn("SQL: {}", sql);
}
} else {
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/schemaspy/input/dbms/service/IndexService.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private void initIndexes(Database db, Table table) throws SQLException {
}
} catch (SQLException exc) {
if (!table.isLogical()) {
LOGGER.warn("Unable to extract index info for table '{}' in schema '{}': {}", table.getName(), table.getContainer(), exc);
LOGGER.warn("Unable to extract index info for table '{}' in schema '{}': {}", table.getName(), table.getContainer(), exc.getMessage(), exc);
}
}
}
Expand Down Expand Up @@ -171,12 +171,12 @@ private void processPrimaryKeyResultSet(final Table table, final ResultSet resul
TableColumn tableColumn = table.getColumn(primaryKeyColumn.column);
if (Objects.nonNull(tableColumn)) {
table.setPrimaryColumn(tableColumn);
updateIndex(primaryKeyColumn.pk_name, table, tableColumn);
updateIndex(primaryKeyColumn.name, table, tableColumn);
} else {
LOGGER.error(
"Found PrimaryKey index '{}' with column '{}.{}.{}.{}'" +
", but was unable to find column in table '{}'",
primaryKeyColumn.pk_name,
primaryKeyColumn.name,
primaryKeyColumn.catalog,
primaryKeyColumn.schema,
primaryKeyColumn.table,
Expand Down Expand Up @@ -216,15 +216,15 @@ private class PrimaryKeyColumn {
public final String schema;
public final String table;
public final String column;
public final String pk_name;
public final String name;
public final int seqno;

public PrimaryKeyColumn(ResultSet resultSet) throws SQLException {
this.catalog = resultSet.getString("TABLE_CAT");
this.schema = resultSet.getString("TABLE_SCHEM");
this.table = resultSet.getString("TABLE_NAME");
this.column = resultSet.getString("COLUMN_NAME");;
this.pk_name = resultSet.getString("PK_NAME");
this.column = resultSet.getString("COLUMN_NAME");
this.name = resultSet.getString("PK_NAME");
this.seqno = resultSet.getShort("KEY_SEQ");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private static String getOptionalString(ResultSet rs, String columnName) {
try {
return rs.getString(columnName);
} catch (SQLException sqlException) {
LOGGER.debug("Failed to get value for column '{}'", sqlException);
LOGGER.debug("Failed to get value for column '{}'", sqlException.getMessage(), sqlException);
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

import org.schemaspy.Config;
import org.schemaspy.model.Database;
import org.schemaspy.model.Routine;
import org.schemaspy.model.RoutineParameter;
import org.schemaspy.model.Sequence;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -76,7 +74,7 @@ private static int getOptionalInt(ResultSet rs, String columnName, int defaultIf
try {
return rs.getInt(columnName);
} catch (SQLException sqlException) {
LOGGER.debug("Failed to get value for column '{}'", sqlException);
LOGGER.debug("Failed to get value for column '{}'", sqlException.getMessage(), sqlException);
return defaultIfNotFound;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/schemaspy/input/dbms/xml/SchemaMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private Document parse(File file) {
try {
validate(doc);
} catch (SAXException | IOException exc) {
LOGGER.warn("Failed to validate {}: {}", file, exc);
LOGGER.warn("Failed to validate {}: {}", file, exc.getMessage(), exc);
}

return doc;
Expand Down
17 changes: 7 additions & 10 deletions src/main/java/org/schemaspy/model/ForeignKeyConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@
public class ForeignKeyConstraint implements Comparable<ForeignKeyConstraint> {
private final String name;
private Table parentTable;
private final List<TableColumn> parentColumns = new ArrayList<TableColumn>();
private final List<TableColumn> parentColumns = new ArrayList<>();
private final Table childTable;
private final List<TableColumn> childColumns = new ArrayList<TableColumn>();
private final List<TableColumn> childColumns = new ArrayList<>();
private final int deleteRule;
private final int updateRule;
private final static Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

/**
* Construct a foreign key for the specified child table.
Expand Down Expand Up @@ -366,14 +366,11 @@ public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
if (obj instanceof ForeignKeyConstraint) {
ForeignKeyConstraint other = (ForeignKeyConstraint)obj;
return parentTable == other.parentTable && childTable == other.childTable;
}
if (this.childTable != ((ForeignKeyConstraint)obj).childTable) {
return false;
}

return this.parentTable == ((ForeignKeyConstraint) obj).parentTable;
return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
*/
package org.schemaspy.model;

import java.util.Objects;

/**
* Base class to indicate that there was problem with how SchemaSpy was configured / used.
*
Expand All @@ -30,8 +28,8 @@
*/
public class InvalidConfigurationException extends RuntimeException {
private static final long serialVersionUID = 1L;
private String paramName;
private String paramValue = "";
private final String paramName;
private final String paramValue;

/**
* When a message is sufficient
Expand All @@ -40,6 +38,8 @@ public class InvalidConfigurationException extends RuntimeException {
*/
public InvalidConfigurationException(String msg) {
super(msg);
paramName = null;
paramValue = null;
}

/**
Expand All @@ -51,6 +51,14 @@ public InvalidConfigurationException(String msg) {
*/
public InvalidConfigurationException(String msg, Throwable cause) {
super(msg, cause);
paramName = null;
paramValue = null;
}

public InvalidConfigurationException(String msg, Throwable cause, String paramName, String paramValue) {
super(msg, cause);
this.paramName = paramName;
this.paramValue = paramValue;
}

/**
Expand All @@ -60,22 +68,20 @@ public InvalidConfigurationException(String msg, Throwable cause) {
*/
public InvalidConfigurationException(Throwable cause) {
super(cause);
paramName = null;
paramValue = null;
}

public InvalidConfigurationException setParamName(String paramName) {
public InvalidConfigurationException(Throwable cause, String paramName, String paramValue) {
super(cause);
this.paramName = paramName;
return this;
this.paramValue = paramValue;
}

public String getParamName() {
return paramName;
}

public InvalidConfigurationException setParamValue(String paramValue) {
if (Objects.nonNull(paramValue))
this.paramValue = paramValue;
return this;
}

public String getParamValue() {
return paramValue;
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/schemaspy/model/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class Table implements Comparable<Table> {
private int maxChildren;
private int maxParents;

private final static Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

/**
* Construct a table that knows everything about the database table's metadata
Expand All @@ -75,7 +75,7 @@ public Table(Database db, String catalog, String schema, String name, String com
this.db = db;
this.catalog = catalog;
this.schema = schema;
this.container = schema != null ? schema : catalog != null ? catalog : db.getName();
this.container = Optional.ofNullable(schema).orElse(Optional.ofNullable(catalog).orElse(db.getName()));
this.name = name;
this.fullName = getFullName(db.getName(), catalog, schema, name);
LOGGER.debug("Creating {} {}", getClass().getSimpleName(), fullName);
Expand Down Expand Up @@ -547,18 +547,18 @@ public int getNumNonImpliedParents() {
* @return
*/
public ForeignKeyConstraint removeAForeignKeyConstraint() {
final List<TableColumn> columns = getColumns();
final List<TableColumn> sortedColumns = getColumns();
int numParents = 0;
int numChildren = 0;
// remove either a child or parent, choosing which based on which has the
// least number of foreign key associations (when either gets to zero then
// the table can be pruned)
for (TableColumn column : columns) {
for (TableColumn column : sortedColumns) {
numParents += column.getParents().size();
numChildren += column.getChildren().size();
}

for (TableColumn column : columns) {
for (TableColumn column : sortedColumns) {
ForeignKeyConstraint constraint;
if (numParents <= numChildren)
constraint = column.removeAParentFKConstraint();
Expand Down
24 changes: 10 additions & 14 deletions src/main/java/org/schemaspy/model/TableColumn.java
Original file line number Diff line number Diff line change
Expand Up @@ -413,13 +413,11 @@ public ForeignKeyConstraint getParentConstraint(TableColumn parent) {
* @return the removed {@link ForeignKeyConstraint}
*/
public ForeignKeyConstraint removeAParentFKConstraint() {
for (TableColumn relatedColumn : parents.keySet()) {
ForeignKeyConstraint constraint = parents.remove(relatedColumn);
relatedColumn.removeChild(this);
return constraint;
}

return null;
return parents.entrySet().stream().findFirst().map(entry -> {
parents.remove(entry.getKey());
entry.getKey().removeChild(this);
return entry.getValue();
}).orElse(null);
}

/**
Expand All @@ -428,13 +426,11 @@ public ForeignKeyConstraint removeAParentFKConstraint() {
* @return the removed constraint, or <code>null</code> if none were available to be removed
*/
public ForeignKeyConstraint removeAChildFKConstraint() {
for (TableColumn relatedColumn : children.keySet()) {
ForeignKeyConstraint constraint = children.remove(relatedColumn);
relatedColumn.removeParent(this);
return constraint;
}

return null;
return children.entrySet().stream().findFirst().map(entry -> {
children.remove(entry.getKey());
entry.getKey().removeParent(this);
return entry.getValue();
}).orElse(null);
}

/**
Expand Down
Loading

0 comments on commit 399a534

Please sign in to comment.