Skip to content

Feature/refactoring #122

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 6 commits into from
Dec 27, 2020
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 @@ -152,6 +152,7 @@ private void populateCoverageAssets() {
Path f = Paths.get(assetDir.toString() + File.separator + entry.getName().substring(ASSETS_PATH.length()));
copyStreamToFile(jar.getInputStream(entry), f);
}
jar.close();
} else {
// class loaded from file system (IDE or during test/build)
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@

import oracle.jdbc.OracleTypes;

@SuppressWarnings("StringBufferReplaceableByString")
public class RealtimeReporterDao {
private static final Logger logger = Logger.getLogger(RealtimeReporterDao.class.getName());
private static final int FIRST_VERSION_WITH_REALTIME_REPORTER = 3001004;
Expand Down Expand Up @@ -246,7 +245,6 @@ private RealtimeReporterEvent convert(final String itemType, final String text)
}
}

@SuppressWarnings("DuplicatedCode")
private RealtimeReporterEvent convertToPreRunEvent(final Document doc) {
final PreRunEvent event = new PreRunEvent();
final Node totalNumberOfTestsNode = xmlTools.getNode(doc, "/event/totalNumberOfTests");
Expand Down Expand Up @@ -327,7 +325,6 @@ private RealtimeReporterEvent convertToPostTestEvent(final Document doc) {
return event;
}

@SuppressWarnings("DuplicatedCode")
private void populate(final Suite suite, final Node node) {
if (node instanceof Element) {
suite.setId(xmlTools.getAttributeValue(node, "id"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
import org.utplsql.sqldev.model.runner.RealtimeReporterEvent;

public interface RealtimeReporterEventConsumer {
public abstract void process(final RealtimeReporterEvent event);
void process(final RealtimeReporterEvent event);
}
142 changes: 64 additions & 78 deletions sqldev/src/main/java/org/utplsql/sqldev/dal/UtplsqlDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
package org.utplsql.sqldev.dal;

import java.net.URL;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -38,13 +36,12 @@

public class UtplsqlDao {
public static final String UTPLSQL_PACKAGE_NAME = "UT";
public static final int NOT_INSTALLED = 0;
public static final int FIRST_VERSION_WITH_INTERNAL_ANNOTATION_API = 3000004;
public static final int FIRST_VERSION_WITH_ANNOTATION_API = 3001003;
public static final int FIRST_VERSION_WITHOUT_INTERNAL_API = 3001008;
public static final int FIRST_VERSION_WITH_HAS_SUITES_API = 3001008;
public static final int FETCH_ROWS = 100;
private JdbcTemplate jdbcTemplate;
private final JdbcTemplate jdbcTemplate;
// cache fields
private Boolean cachedDbaViewAccessible;
private String cachedUtplsqlSchema;
Expand Down Expand Up @@ -90,7 +87,7 @@ public int normalizedUtPlsqlVersionNumber() {
final String minor = m.group();
m.find();
final String bugfix = m.group();
return Integer.valueOf(major) * 1000000 + Integer.valueOf(minor) * 1000 + Integer.valueOf(bugfix);
return Integer.parseInt(major) * 1000000 + Integer.parseInt(minor) * 1000 + Integer.parseInt(bugfix);
}

/**
Expand All @@ -104,13 +101,10 @@ public String getUtPlsqlVersion() {
sb.append("END;");
final String sql = sb.toString();
try {
cachedUtPlsqlVersion = jdbcTemplate.execute(sql, new CallableStatementCallback<String>() {
@Override
public String doInCallableStatement(final CallableStatement cs) throws SQLException {
cs.registerOutParameter(1, Types.VARCHAR);
cs.execute();
return cs.getString(1);
}
cachedUtPlsqlVersion = jdbcTemplate.execute(sql, (CallableStatementCallback<String>) cs -> {
cs.registerOutParameter(1, Types.VARCHAR);
cs.execute();
return cs.getString(1);
});
} catch (DataAccessException e) {
// ignore error
Expand Down Expand Up @@ -141,7 +135,7 @@ public boolean isDbaViewAccessible() {
cachedDbaViewAccessible = false;
}
}
return cachedDbaViewAccessible.booleanValue();
return cachedDbaViewAccessible;
}

public String getDbaView(String viewName) {
Expand Down Expand Up @@ -178,8 +172,7 @@ public String getUtplsqlSchema() {
sb.append("'");
final String sql = sb.toString();
try {
final String schema = jdbcTemplate.queryForObject(sql, String.class);
cachedUtplsqlSchema = schema;
cachedUtplsqlSchema = jdbcTemplate.queryForObject(sql, String.class);
} catch (EmptyResultDataAccessException e) {
cachedUtplsqlSchema = null;
}
Expand Down Expand Up @@ -229,18 +222,16 @@ public boolean containsUtplsqlTest(final String owner, final String objectName,
sb.append(" ? := l_return;\n");
sb.append("END;");
final String sql = sb.toString();
return jdbcTemplate.execute(sql, new CallableStatementCallback<Boolean>() {
@Override
public Boolean doInCallableStatement(final CallableStatement cs) throws SQLException {
cs.setString(1, owner);
cs.setString(2, objectName);
cs.setString(3, subobjectName);
cs.registerOutParameter(4, Types.VARCHAR);
cs.execute();
final String ret = cs.getString(4);
return "1".equals(ret);
}
final Boolean ret = jdbcTemplate.execute(sql, (CallableStatementCallback<Boolean>) cs -> {
cs.setString(1, owner);
cs.setString(2, objectName);
cs.setString(3, subobjectName);
cs.registerOutParameter(4, Types.VARCHAR);
cs.execute();
final String ret1 = cs.getString(4);
return "1".equals(ret1);
});
return ret != null && ret;
} else if (normalizedUtPlsqlVersionNumber() >= FIRST_VERSION_WITH_ANNOTATION_API) {
// using API available since 3.1.3, can handle nulls in objectName and subobjectName
StringBuilder sb = new StringBuilder();
Expand All @@ -251,7 +242,7 @@ public Boolean doInCallableStatement(final CallableStatement cs) throws SQLExcep
final String sql = sb.toString();
final Object[] binds = new Object[] {owner, objectName, subobjectName, subobjectName};
final Integer found = jdbcTemplate.queryForObject(sql, Integer.class, binds);
return found > 0;
return found != null && found > 0;
} else {
// using internal API (deprecated, not accessible in latest version)
StringBuilder sb = new StringBuilder();
Expand Down Expand Up @@ -282,7 +273,7 @@ public Boolean doInCallableStatement(final CallableStatement cs) throws SQLExcep
final String sql = sb.toString();
final Object[] binds = new Object[] {subobjectName, subobjectName, owner, objectName, objectName};
final Integer found = jdbcTemplate.queryForObject(sql, Integer.class, binds);
return found > 0;
return found != null && found > 0;
}
} catch (EmptyResultDataAccessException e) {
return false;
Expand All @@ -302,17 +293,14 @@ public boolean containsUtplsqlTest(final String owner) {
sb.append(" ? := l_return;\n");
sb.append("END;");
final String sql = sb.toString();
return jdbcTemplate.execute(sql, new CallableStatementCallback<Boolean>() {
@Override
public Boolean doInCallableStatement(final CallableStatement cs)
throws SQLException {
cs.setString(1, owner);
cs.registerOutParameter(2, Types.VARCHAR);
cs.execute();
final String ret = cs.getString(2);
return "1".equals(ret);
}
final Boolean ret = jdbcTemplate.execute(sql, (CallableStatementCallback<Boolean>) cs -> {
cs.setString(1, owner);
cs.registerOutParameter(2, Types.VARCHAR);
cs.execute();
final String ret1 = cs.getString(2);
return "1".equals(ret1);
});
return ret != null && ret;
} else {
return containsUtplsqlTest(owner, null, null);
}
Expand All @@ -330,18 +318,15 @@ public boolean containsUtplsqlTest(final String owner, final String objectName)
sb.append(" ? := l_return;\n");
sb.append("END;");
final String sql = sb.toString();
return jdbcTemplate.execute(sql, new CallableStatementCallback<Boolean>() {
@Override
public Boolean doInCallableStatement(final CallableStatement cs)
throws SQLException {
cs.setString(1, owner);
cs.setString(2, objectName);
cs.registerOutParameter(3, Types.VARCHAR);
cs.execute();
final String ret = cs.getString(3);
return "1".equals(ret);
}
Boolean ret = jdbcTemplate.execute(sql, (CallableStatementCallback<Boolean>) cs -> {
cs.setString(1, owner);
cs.setString(2, objectName);
cs.registerOutParameter(3, Types.VARCHAR);
cs.execute();
final String ret1 = cs.getString(3);
return "1".equals(ret1);
});
return ret != null && ret;
} else {
return containsUtplsqlTest(owner, objectName, null);
}
Expand Down Expand Up @@ -410,7 +395,7 @@ public List<String> units(final String objectType, final String objectName) {
final Object[] binds = new Object[] {objectType, objectName};
return jdbcTemplate.queryForList(sql, String.class, binds);
} else {
return Arrays.asList(objectName);
return Collections.singletonList(objectName);
}
}

Expand Down Expand Up @@ -868,30 +853,34 @@ public String getDbmsOutput(final int bufferSize) {
sb.append(" sys.dbms_output.get_lines(?, ?);\n");
sb.append("END;");
final String sql = sb.toString();
OutputLines ret = null;
OutputLines ret;
do {
ret = jdbcTemplate.execute(sql, new CallableStatementCallback<OutputLines>() {
@Override
public OutputLines doInCallableStatement(final CallableStatement cs) throws SQLException {
cs.registerOutParameter(1, Types.ARRAY, "DBMSOUTPUT_LINESARRAY");
cs.registerOutParameter(2, Types.INTEGER);
cs.setInt(2, bufferSize);
cs.execute();
final OutputLines out = new OutputLines();
ret = jdbcTemplate.execute(sql, (CallableStatementCallback<OutputLines>) cs -> {
cs.registerOutParameter(1, Types.ARRAY, "DBMSOUTPUT_LINESARRAY");
cs.registerOutParameter(2, Types.INTEGER);
cs.setInt(2, bufferSize);
cs.execute();
final OutputLines out = new OutputLines();
try {
Object array = cs.getArray(1).getArray();
out.setLines((String[]) array);
out.setNumlines(cs.getInt(2));
return out;
} catch (NullPointerException e) {
out.setLines(null);
out.setNumlines(0);
}
return out;
});
for (int i = 0; i < ret.getNumlines(); i++) {
final String line = ret.getLines()[i];
if (line != null) {
resultSb.append(ret.getLines()[i]);
if (ret != null && ret.getNumlines() != null) {
for (int i = 0; i < ret.getNumlines(); i++) {
final String line = ret.getLines()[i];
if (line != null) {
resultSb.append(ret.getLines()[i]);
}
resultSb.append(System.lineSeparator());
}
resultSb.append(System.lineSeparator());
}
} while (ret.getNumlines() > 0);
} while (ret != null && ret.getNumlines() != null && ret.getNumlines() > 0);
return resultSb.toString();
}

Expand Down Expand Up @@ -1032,16 +1021,13 @@ public String getSource(final String owner, final String objectType, final Strin
sb.append(" );\n");
sb.append("END;");
final String sql = sb.toString();
return jdbcTemplate.execute(sql, new CallableStatementCallback<String>() {
@Override
public String doInCallableStatement(final CallableStatement cs) throws SQLException {
cs.registerOutParameter(1, Types.CLOB);
cs.setString(2, owner);
cs.setString(3, fixedObjectType);
cs.setString(4, objectName);
cs.execute();
return cs.getString(1);
}
return jdbcTemplate.execute(sql, (CallableStatementCallback<String>) cs -> {
cs.registerOutParameter(1, Types.CLOB);
cs.setString(2, owner);
cs.setString(3, fixedObjectType);
cs.setString(4, objectName);
cs.execute();
return cs.getString(1);
});
}

Expand Down Expand Up @@ -1071,6 +1057,6 @@ public String getObjectType(final String owner, final String objectName) {
sb.append(" WHERE rownum = 1");
final String sql = sb.toString();
final Object[] binds = new Object[] {owner, objectName};
return jdbcTemplate.queryForObject(sql, binds, String.class);
return jdbcTemplate.queryForObject(sql, String.class, binds);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@

import javax.annotation.Nullable;

import org.jetbrains.annotations.NotNull;
import org.springframework.core.style.ToStringStyler;
import org.springframework.core.style.ValueStyler;

public class JsonToStringStyler implements ToStringStyler, ValueStyler{
public static final String INDENT_SPACES = " ";
private int indent = 0;

private static ThreadLocal<JsonToStringStyler> threadLocal = ThreadLocal.withInitial(JsonToStringStyler::new);
private static final ThreadLocal<JsonToStringStyler> threadLocal = ThreadLocal.withInitial(JsonToStringStyler::new);

private void newLine(StringBuilder buffer) {
buffer.append('\n');
Expand Down Expand Up @@ -102,7 +103,7 @@ public static ToStringStyler getInstance() {
}

@Override
public void styleStart(StringBuilder buffer, Object obj) {
public void styleStart(@NotNull StringBuilder buffer, Object obj) {
indent++;
if (!obj.getClass().isArray()) {
buffer.append("{");
Expand All @@ -119,7 +120,7 @@ public void styleStart(StringBuilder buffer, Object obj) {
}

@Override
public void styleEnd(StringBuilder buffer, Object obj) {
public void styleEnd(@NotNull StringBuilder buffer, Object obj) {
indent--;
newLine(buffer);
if (!obj.getClass().isArray()) {
Expand All @@ -130,7 +131,7 @@ public void styleEnd(StringBuilder buffer, Object obj) {
}

@Override
public void styleField(StringBuilder buffer, String fieldName, @Nullable Object value) {
public void styleField(@NotNull StringBuilder buffer, @NotNull String fieldName, @Nullable Object value) {
newLine(buffer);
buffer.append('"');
buffer.append(fieldName);
Expand All @@ -140,7 +141,7 @@ public void styleField(StringBuilder buffer, String fieldName, @Nullable Object
}

@Override
public void styleValue(StringBuilder buffer, Object value) {
public void styleValue(StringBuilder buffer, @Nullable Object value) {
buffer.append(style(value));
}

Expand All @@ -149,6 +150,7 @@ public void styleFieldSeparator(StringBuilder buffer) {
buffer.append(",");
}

@NotNull
@Override
public String style(Object value) {
if (value == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.util.LinkedHashMap;
import java.util.Map;

public class LimitedLinkedHashMap<K extends Object, V extends Object> extends LinkedHashMap<K, V> {
public class LimitedLinkedHashMap<K, V> extends LinkedHashMap<K, V> {
private static final long serialVersionUID = -4184317926729190411L;
private final int maxEntries;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public String nodeToString(final Node node, final String cdataSectionElements) {
public String getAttributeValue(final Node node, final String namedItem) {
String value = null;
if (node instanceof Element) {
final NamedNodeMap attributes = ((Element) node).getAttributes();
final NamedNodeMap attributes = node.getAttributes();
if (attributes != null) {
final Node item = attributes.getNamedItem(namedItem);
if (item != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.utplsql.sqldev.model.JsonToStringStyler;
import org.utplsql.sqldev.model.URLTools;

@SuppressWarnings("unused")
public class Run {
private String reporterId;
private String connectionName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.utplsql.sqldev.model.JsonToStringStyler;
import org.utplsql.sqldev.resources.UtplsqlResources;

@SuppressWarnings("unused")
public class Test extends Item {
private String executableType;
private String ownerName;
Expand Down
Loading