Skip to content

Commit

Permalink
#113 junit5
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jul 13, 2022
1 parent db0cd34 commit b028d38
Show file tree
Hide file tree
Showing 21 changed files with 127 additions and 163 deletions.
41 changes: 25 additions & 16 deletions pom.xml
Expand Up @@ -33,7 +33,7 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
<parent>
<groupId>com.jcabi</groupId>
<artifactId>jcabi</artifactId>
<version>1.24</version>
<version>1.33.0</version>
</parent>
<artifactId>jcabi-dynamo</artifactId>
<version>1.0-SNAPSHOT</version>
Expand All @@ -56,69 +56,79 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
<distributionManagement>
<site>
<id>dynamo.jcabi.com</id>
<url>http://dynamo.jcabi.com/</url>
<url>https://dynamo.jcabi.com/</url>
</site>
</distributionManagement>
<dependencies>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-aspects</artifactId>
<version>0.22.5</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-log</artifactId>
<version>0.21.0</version>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-immutable</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-core</artifactId>
<version>1.11.57</version>
<version>1.12.259</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>1.11.57</version>
<version>1.12.259</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.9.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-jdbc</artifactId>
<version>0.17.8</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.193</version>
<version>2.1.214</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>2.0.0-alpha5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down Expand Up @@ -213,7 +223,6 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
<plugin>
<groupId>com.qulice</groupId>
<artifactId>qulice-maven-plugin</artifactId>
<version>0.16.2</version>
<configuration>
<excludes combine.children="append">
<exclude>findbugs:.*</exclude>
Expand Down
23 changes: 14 additions & 9 deletions src/main/java/com/jcabi/dynamo/Credentials.java
Expand Up @@ -29,11 +29,12 @@
*/
package com.jcabi.dynamo;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.RegionUtils;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.jcabi.aspects.Immutable;
import com.jcabi.aspects.Loggable;
import lombok.EqualsAndHashCode;
Expand Down Expand Up @@ -119,11 +120,14 @@ public AmazonDynamoDB aws() {
String.format("Failed to find region '%s'", this.region)
);
}
final AmazonDynamoDB aws = new AmazonDynamoDBClient(
new BasicAWSCredentials(this.key, this.secret)
);
aws.setRegion(reg);
return aws;
return AmazonDynamoDBClientBuilder.standard()
.withRegion(reg.getName())
.withCredentials(
new AWSStaticCredentialsProvider(
new BasicAWSCredentials(this.key, this.secret)
)
)
.build();
}
}

Expand Down Expand Up @@ -166,9 +170,9 @@ public AmazonDynamoDB aws() {
String.format("Failed to detect region '%s'", this.region)
);
}
final AmazonDynamoDB aws = new AmazonDynamoDBClient();
aws.setRegion(reg);
return aws;
return AmazonDynamoDBClientBuilder.standard()
.withRegion(reg.getName())
.build();
}
}

Expand Down Expand Up @@ -209,6 +213,7 @@ public String toString() {
return String.format("%s at %s", this.origin, this.endpoint);
}
@Override
@SuppressWarnings("deprecation")
public AmazonDynamoDB aws() {
final AmazonDynamoDB aws = this.origin.aws();
aws.setEndpoint(this.endpoint);
Expand Down
83 changes: 22 additions & 61 deletions src/main/java/com/jcabi/dynamo/mock/H2Data.java
Expand Up @@ -46,7 +46,6 @@
import com.jcabi.jdbc.Outcome;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
Expand All @@ -57,13 +56,13 @@
import java.util.LinkedList;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.sql.DataSource;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.apache.commons.codec.binary.Base32;
import org.h2.Driver;
import org.h2.jdbcx.JdbcDataSource;

/**
* Mock data in H2 database.
Expand All @@ -89,8 +88,7 @@ public final class H2Data implements MkData {
@Override
public Iterable<Attributes> handle(final ResultSet rset,
final Statement stmt) throws SQLException {
final Collection<Attributes> items =
new LinkedList<Attributes>();
final Collection<Attributes> items = new LinkedList<>();
while (rset.next()) {
items.add(this.fetch(rset));
}
Expand Down Expand Up @@ -124,12 +122,7 @@ private Attributes fetch(final ResultSet rset) throws SQLException {
* Where clause.
*/
private static final Function<String, String> WHERE =
new Function<String, String>() {
@Override
public String apply(final String key) {
return String.format("%s = ?", key);
}
};
key -> String.format("%s = ?", key);

/**
* Select WHERE.
Expand Down Expand Up @@ -169,23 +162,13 @@ public String apply(final Map.Entry<String, Condition> cnd) {
* Create primary key.
*/
private static final Function<String, String> CREATE_KEY =
new Function<String, String>() {
@Override
public String apply(final String key) {
return String.format("%s VARCHAR PRIMARY KEY", key);
}
};
key -> String.format("%s VARCHAR PRIMARY KEY", key);

/**
* Create attr.
*/
private static final Function<String, String> CREATE_ATTR =
new Function<String, String>() {
@Override
public String apply(final String key) {
return String.format("%s CLOB", key);
}
};
key -> String.format("%s CLOB", key);

/**
* WHERE clauses are joined with this.
Expand Down Expand Up @@ -219,39 +202,15 @@ public H2Data(final File file) {
@Override
public Iterable<String> keys(final String table) throws IOException {
try {
return Iterables.transform(
new JdbcSession(this.connection())
// @checkstyle LineLength (1 line)
.sql("SELECT SQL FROM INFORMATION_SCHEMA.CONSTRAINTS WHERE TABLE_NAME = ?")
.set(H2Data.encodeTableName(table))
.select(
new ListOutcome<String>(
new ListOutcome.Mapping<String>() {
@Override
public String map(final ResultSet rset)
throws SQLException {
return rset.getString(1);
}
}
)
),
new Function<String, String>() {
@Override
public String apply(final String input) {
final Matcher matcher = Pattern.compile(
"PRIMARY KEY\\((.*)\\)"
).matcher(input);
if (!matcher.find()) {
throw new IllegalStateException(
String.format(
"something is wrong here: \"%s\"", input
)
);
}
return matcher.group(1).toLowerCase(Locale.ENGLISH);
}
}
);
return new JdbcSession(this.connection())
// @checkstyle LineLength (1 line)
.sql("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME = ?")
.set(H2Data.encodeTableName(table))
.select(
new ListOutcome<>(
rset -> rset.getString(1).toLowerCase(Locale.ENGLISH)
)
);
} catch (final SQLException ex) {
throw new IOException(ex);
}
Expand All @@ -275,7 +234,7 @@ public Iterable<Attributes> iterate(final String table,
for (final Condition cond : conds.values()) {
if (cond.getAttributeValueList().size() != 1) {
throw new UnsupportedOperationException(
"at the moment only one value of condition is supported"
"At the moment only one value of condition is supported"
);
}
final AttributeValue val = cond.getAttributeValueList().get(0);
Expand Down Expand Up @@ -374,7 +333,7 @@ public H2Data with(final String table, final String[] keys,
final String... attrs) throws IOException {
if (keys.length == 0) {
throw new IllegalArgumentException(
String.format("empty list of keys for %s table", table)
String.format("Empty list of keys for %s table", table)
);
}
final StringBuilder sql = new StringBuilder("CREATE TABLE ")
Expand Down Expand Up @@ -403,8 +362,10 @@ public H2Data with(final String table, final String[] keys,
* @return Data source for JDBC
* @throws SQLException If fails
*/
private Connection connection() throws SQLException {
return new Driver().connect(this.jdbc, new Properties());
private DataSource connection() throws SQLException {
final JdbcDataSource src = new JdbcDataSource();
src.setURL(this.jdbc);
return src;
}

/**
Expand All @@ -419,7 +380,7 @@ private static String value(final AttributeValue attr) {
}
if (val == null) {
throw new IllegalArgumentException(
"we support only N and S at the moment"
"We support only N and S at the moment"
);
}
return val;
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/com/jcabi/dynamo/AttributeUpdatesTest.java
Expand Up @@ -35,8 +35,8 @@
import org.apache.commons.lang3.StringUtils;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

/**
Expand Down Expand Up @@ -244,7 +244,7 @@ public void putThrowsException() {
passed = true;
}
if (!passed) {
Assert.fail("#put should not be supported");
Assertions.fail("#put should not be supported");
}
}

Expand All @@ -261,7 +261,7 @@ public void putAllThrowsException() {
passed = true;
}
if (!passed) {
Assert.fail("#putAll should not be supported.");
Assertions.fail("#putAll should not be supported.");
}
}

Expand All @@ -278,7 +278,7 @@ public void removeThrowsException() {
passed = true;
}
if (!passed) {
Assert.fail("#remove should not be supported.");
Assertions.fail("#remove should not be supported.");
}
}

Expand All @@ -295,7 +295,7 @@ public void clearThrowsException() {
passed = true;
}
if (!passed) {
Assert.fail("#clear should not be supported.");
Assertions.fail("#clear should not be supported.");
}
}
}
2 changes: 1 addition & 1 deletion src/test/java/com/jcabi/dynamo/AttributesTest.java
Expand Up @@ -36,7 +36,7 @@
import java.util.Map;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.jupiter.api.Test;

/**
* Test case for {@link Attributes}.
Expand Down

0 comments on commit b028d38

Please sign in to comment.