Skip to content

Commit 860638e

Browse files
authored
Reduce use of mocks and clean up code (#2356)
1 parent 9acef2b commit 860638e

9 files changed

Lines changed: 33 additions & 50 deletions

File tree

schemacrawler-commandline/src/main/java/schemacrawler/tools/commandline/command/CommandLineHelpCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private void showHelpForSubcommand(final CommandLine parent, final String comman
137137
if (isAvailabilityCommand) {
138138
final CommandSpec commandSpec = subCommand.getCommandSpec();
139139
final Object userObject = commandSpec.userObject();
140-
if (userObject instanceof Runnable runnable) {
140+
if (userObject instanceof final Runnable runnable) {
141141
runnable.run();
142142
}
143143
}

schemacrawler-commandline/src/main/java/schemacrawler/tools/commandline/command/ConnectCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ public void run() {
9999
DatabaseConnectorRegistry.getDatabaseConnectorRegistry();
100100
final DatabaseConnector databaseConnector;
101101

102-
if (connectionOptions instanceof DatabaseUrlConnectionOptions options1) {
102+
if (connectionOptions instanceof final DatabaseUrlConnectionOptions options1) {
103103
final String connectionUrl = options1.connectionUrl();
104104
databaseConnector = databaseConnectorRegistry.findDatabaseConnectorFromUrl(connectionUrl);
105-
} else if (connectionOptions instanceof DatabaseServerHostConnectionOptions options) {
105+
} else if (connectionOptions instanceof final DatabaseServerHostConnectionOptions options) {
106106
final String databaseSystemIdentifier = options.databaseSystemIdentifier();
107107
if (!databaseConnectorRegistry.hasDatabaseSystemIdentifier(databaseSystemIdentifier)) {
108108
throw new ConfigurationException(

schemacrawler-commandline/src/test/java/schemacrawler/test/commandline/command/SystemCommandTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import static org.hamcrest.CoreMatchers.containsString;
1313
import static org.hamcrest.MatcherAssert.assertThat;
1414
import static org.hamcrest.Matchers.matchesPattern;
15-
import static org.mockito.Mockito.mock;
15+
import static schemacrawler.test.utility.crawl.LightCatalogUtility.lightCatalog;
1616
import static schemacrawler.tools.commandline.utility.CommandLineUtility.newCommandLine;
1717
import static us.fatehi.test.utility.extensions.FileHasContent.classpathResource;
1818
import static us.fatehi.test.utility.extensions.FileHasContent.contentsOf;
@@ -24,7 +24,6 @@
2424
import java.util.regex.Pattern;
2525
import org.junit.jupiter.api.Test;
2626
import picocli.CommandLine;
27-
import schemacrawler.schema.Catalog;
2827
import schemacrawler.test.utility.DisableLogging;
2928
import schemacrawler.test.utility.WithTestDatabase;
3029
import schemacrawler.tools.commandline.shell.SystemCommand;
@@ -106,7 +105,7 @@ public void showLoaded(final CapturedSystemStreams streams) {
106105
final String[] args = {"-L"};
107106

108107
final ShellState state = new ShellState();
109-
state.setCatalog(mock(Catalog.class));
108+
state.setCatalog(lightCatalog());
110109
executeSystemCommand(state, args);
111110

112111
// Error stream may have some messages on Java 21, due to how tests are set up

schemacrawler-commandline/src/test/java/schemacrawler/test/utility/CommandlineTestUtility.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public int handleExecutionException(
159159
final CommandLine.ParseResult parseResult)
160160
throws Exception {
161161
lastException = ex;
162-
if (ex instanceof PicocliException picocliException) {
162+
if (ex instanceof final PicocliException picocliException) {
163163
if (picocliException.getCause() != null) {
164164
lastException = picocliException.getCause();
165165
}

schemacrawler-diagram/src/main/java/schemacrawler/tools/command/text/diagram/DiagramRenderer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ public boolean usesConnection() {
121121

122122
private String extractErrorMessage(final Exception e) {
123123
final String errorMessage;
124-
final boolean isSchemaCrawlerException = e instanceof SchemaCrawlerException;
125-
if (isSchemaCrawlerException) {
124+
if (e instanceof SchemaCrawlerException) {
126125
errorMessage = e.getMessage();
127126
} else {
128127
errorMessage = "Could not generate diagram" + e.getMessage();

schemacrawler-hsqldb/src/test/java/schemacrawler/integration/test/MatchSchemaRetrievalOptionsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import static org.hamcrest.Matchers.containsString;
1313
import static org.hamcrest.Matchers.is;
1414
import static org.junit.jupiter.api.Assertions.assertThrows;
15-
import static org.mockito.Mockito.mock;
1615
import static org.mockito.Mockito.when;
1716

1817
import java.sql.Connection;
@@ -27,6 +26,7 @@
2726
import us.fatehi.test.utility.TestObjectUtility;
2827
import us.fatehi.test.utility.extensions.WithSystemProperty;
2928
import us.fatehi.utility.datasource.DatabaseConnectionSource;
29+
import us.fatehi.utility.datasource.DatabaseConnectionSources;
3030
import us.fatehi.utility.datasource.DatabaseServerType;
3131

3232
@WithTestDatabase
@@ -104,8 +104,8 @@ private DatabaseConnectionSource mockConnectionForUrl(
104104
final Connection connection = TestObjectUtility.mockConnection();
105105
when(connection.getMetaData()).thenReturn(databaseMetaData);
106106
when(connection.toString()).thenReturn(toString);
107-
final DatabaseConnectionSource connectionSource = mock(DatabaseConnectionSource.class);
108-
when(connectionSource.get()).thenReturn(connection);
107+
final DatabaseConnectionSource connectionSource =
108+
DatabaseConnectionSources.fromConnection(connection);
109109
return connectionSource;
110110
}
111111
}

schemacrawler-lint/src/test/java/schemacrawler/tools/lint/LintersTest.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import static org.hamcrest.Matchers.containsInAnyOrder;
1414
import static org.hamcrest.Matchers.hasProperty;
1515
import static org.hamcrest.Matchers.is;
16-
import static org.mockito.Mockito.mock;
1716
import static org.mockito.Mockito.spy;
1817
import static org.mockito.Mockito.when;
1918

@@ -30,12 +29,12 @@
3029
import schemacrawler.schema.ForeignKey;
3130
import schemacrawler.schema.Index;
3231
import schemacrawler.schema.IndexColumn;
33-
import schemacrawler.schema.PrimaryKey;
3432
import schemacrawler.schema.Table;
35-
import schemacrawler.schema.TableConstraintColumn;
3633
import schemacrawler.schemacrawler.SchemaReference;
3734
import schemacrawler.test.utility.crawl.LightCatalogUtility;
35+
import schemacrawler.test.utility.crawl.LightColumn;
3836
import schemacrawler.test.utility.crawl.LightForeignKey;
37+
import schemacrawler.test.utility.crawl.LightPrimaryKey;
3938
import schemacrawler.test.utility.crawl.LightTable;
4039
import schemacrawler.tools.lint.config.LinterConfig;
4140
import schemacrawler.tools.linter.LinterProviderCatalogSql;
@@ -508,12 +507,11 @@ public void testTableWithNoSurrogatePrimaryKey() {
508507
new LinterProviderTableWithNoSurrogatePrimaryKey().newLinter(lintCollector);
509508
linter.configure(linterConfig);
510509

511-
final LightTable table = spy(new LightTable(new SchemaReference(), "TEST_TABLE"));
512-
final PrimaryKey pk = mock(PrimaryKey.class);
513-
final TableConstraintColumn col1 = mock(TableConstraintColumn.class);
514-
final TableConstraintColumn col2 = mock(TableConstraintColumn.class);
515-
when(pk.getConstrainedColumns()).thenReturn(List.of(col1, col2));
516-
when(table.getPrimaryKey()).thenReturn(pk);
510+
final LightTable table = new LightTable(new SchemaReference(), "TEST_TABLE");
511+
final LightColumn column = table.addColumn("COL11");
512+
final Column[] columns = {table.addColumn("PK11"), table.addColumn("PK2")};
513+
final LightPrimaryKey pk = new LightPrimaryKey(columns);
514+
table.setPrimaryKey(pk);
517515

518516
((BaseLinter) linter).lint(table, connection);
519517

@@ -529,13 +527,9 @@ public void testTableWithPrimaryKeyNotFirst() {
529527
final Linter linter = new LinterProviderTableWithPrimaryKeyNotFirst().newLinter(lintCollector);
530528
linter.configure(linterConfig);
531529

532-
final LightTable table = spy(new LightTable(new SchemaReference(), "TEST_TABLE"));
533-
final PrimaryKey pk = mock(PrimaryKey.class);
534-
final TableConstraintColumn col = mock(TableConstraintColumn.class);
535-
when(col.getTableConstraintOrdinalPosition()).thenReturn(1);
536-
when(col.getOrdinalPosition()).thenReturn(3);
537-
when(pk.getConstrainedColumns()).thenReturn(List.of(col));
538-
when(table.getPrimaryKey()).thenReturn(pk);
530+
final LightTable table = new LightTable(new SchemaReference(), "TEST_TABLE");
531+
final LightPrimaryKey pk = new LightPrimaryKey(null, null, table.addColumn("COL3"));
532+
table.setPrimaryKey(pk);
539533

540534
((BaseLinter) linter).lint(table, connection);
541535

schemacrawler-scripting/src/test/java/schemacrawler/test/script/CrawlInfoSupportTest.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@
1111
import static org.hamcrest.CoreMatchers.is;
1212
import static org.hamcrest.MatcherAssert.assertThat;
1313
import static org.junit.jupiter.api.Assertions.assertThrows;
14-
import static org.mockito.Mockito.mock;
15-
import static org.mockito.Mockito.when;
1614

1715
import org.junit.jupiter.api.BeforeEach;
1816
import org.junit.jupiter.api.Test;
1917
import schemacrawler.schema.CrawlInfo;
18+
import schemacrawler.schemacrawler.Version;
19+
import schemacrawler.test.utility.crawl.LightCrawlInfo;
2020
import schemacrawler.tools.command.script.CrawlInfoSupport;
21-
import us.fatehi.utility.property.BaseProductVersion;
2221

2322
public class CrawlInfoSupportTest {
2423

@@ -32,35 +31,29 @@ public void constructorRejectsNullCrawlInfo() {
3231
@Test
3332
public void databaseVersion() {
3433
final CrawlInfoSupport support = new CrawlInfoSupport(crawlInfo);
35-
assertThat(support.databaseVersion(), is("TestDB 2.0"));
34+
assertThat(support.databaseVersion(), is("TestDB v1.0"));
3635
}
3736

3837
@Test
3938
public void databaseVersionStripsQuotes() {
40-
when(crawlInfo.getDatabaseVersion())
41-
.thenReturn(new BaseProductVersion("\"Quoted DB\"", "\"3.0\""));
4239
final CrawlInfoSupport support = new CrawlInfoSupport(crawlInfo);
43-
assertThat(support.databaseVersion(), is("Quoted DB 3.0"));
40+
assertThat(support.databaseVersion(), is("TestDB v1.0"));
4441
}
4542

4643
@Test
4744
public void schemacrawlerVersion() {
4845
final CrawlInfoSupport support = new CrawlInfoSupport(crawlInfo);
49-
assertThat(support.schemacrawlerVersion(), is("SchemaCrawler 17.10.1"));
46+
assertThat(support.schemacrawlerVersion(), is(Version.version().toString()));
5047
}
5148

5249
@BeforeEach
5350
public void setUp() {
54-
crawlInfo = mock(CrawlInfo.class);
55-
when(crawlInfo.getDatabaseVersion()).thenReturn(new BaseProductVersion("TestDB", "2.0"));
56-
when(crawlInfo.getSchemaCrawlerVersion())
57-
.thenReturn(new BaseProductVersion("SchemaCrawler", "17.10.1"));
58-
when(crawlInfo.getCrawlTimestamp()).thenReturn("2026-04-18 21:00:00");
51+
crawlInfo = new LightCrawlInfo();
5952
}
6053

6154
@Test
6255
public void timestamp() {
6356
final CrawlInfoSupport support = new CrawlInfoSupport(crawlInfo);
64-
assertThat(support.timestamp(), is("2026-04-18 21:00:00"));
57+
assertThat(support.timestamp(), is("1970-01-01 00:00:00"));
6558
}
6659
}

schemacrawler-scripting/src/test/java/schemacrawler/test/script/ScriptSupportTest.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import schemacrawler.schema.Table;
2727
import schemacrawler.test.utility.crawl.LightColumn;
2828
import schemacrawler.test.utility.crawl.LightForeignKey;
29+
import schemacrawler.test.utility.crawl.LightPrimaryKey;
2930
import schemacrawler.test.utility.crawl.LightTable;
3031
import schemacrawler.tools.command.script.ScriptSupport;
3132

@@ -95,8 +96,7 @@ public void columns() {
9596

9697
// Positive test for columns(PrimaryKey)
9798
final LightColumn pkCol = table.addColumn("PK_COL");
98-
final PrimaryKey primaryKey = mock(PrimaryKey.class);
99-
when(primaryKey.getConstrainedColumns()).thenReturn((List) List.of(pkCol));
99+
final PrimaryKey primaryKey = new LightPrimaryKey(pkCol);
100100
assertThat(support.columns(primaryKey), containsString("PK_COL"));
101101
}
102102

@@ -142,8 +142,8 @@ public void hasName() {
142142

143143
assertThat(support.hasName(fk), is(true));
144144

145-
final PrimaryKey pk = mock(PrimaryKey.class);
146-
when(pk.getName()).thenReturn("PK_TABLE");
145+
final LightTable table = new LightTable("PK_TABLE");
146+
final PrimaryKey pk = new LightPrimaryKey(table.addColumn("PKCOL"));
147147

148148
assertThat(support.hasName(pk), is(true));
149149
}
@@ -162,8 +162,7 @@ public void nonPrimaryIndexes() {
162162
when(tableWithNullIndexes.getIndexes()).thenReturn(null);
163163
assertThat(support.nonPrimaryIndexes(tableWithNullIndexes).isEmpty(), is(true));
164164

165-
final Table tableWithNoIndexes = mock(Table.class);
166-
when(tableWithNoIndexes.getIndexes()).thenReturn(List.of());
165+
final Table tableWithNoIndexes = new LightTable("TABLE1");
167166
assertThat(support.nonPrimaryIndexes(tableWithNoIndexes).isEmpty(), is(true));
168167

169168
// Use LightColumn (raw cast) so isColumnDataTypeKnown() returns true for all
@@ -181,8 +180,7 @@ public void nonPrimaryIndexes() {
181180
assertThat(support.nonPrimaryIndexes(tableWithoutPrimaryKey), is(List.of(indexNoPk)));
182181

183182
// pkEquivalentIndex shares the same column (COL1) as the primary key
184-
final PrimaryKey primaryKey = mock(PrimaryKey.class);
185-
when(primaryKey.getConstrainedColumns()).thenReturn((List) List.of(col1));
183+
final PrimaryKey primaryKey = new LightPrimaryKey(col1);
186184

187185
final Index pkEquivalentIndex = mock(Index.class);
188186
when(pkEquivalentIndex.getColumns()).thenReturn((List) List.of(col1));

0 commit comments

Comments
 (0)