Skip to content

Commit

Permalink
Enable compilation failures on warnings for examples (#1277)
Browse files Browse the repository at this point in the history
  • Loading branch information
injectives authored Jul 29, 2022
1 parent b0d2c93 commit 6e8c27f
Show file tree
Hide file tree
Showing 38 changed files with 189 additions and 139 deletions.
36 changes: 23 additions & 13 deletions driver/src/test/java/org/neo4j/driver/util/StdIOCapture.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,34 @@

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
* Utility that can be used to temporarily capture and store process-wide stdout and stderr output.
*/
public class StdIOCapture {
public class StdIOCapture implements AutoCloseable {
private final List<String> stdout = new CopyOnWriteArrayList<>();
private final List<String> stderr = new CopyOnWriteArrayList<>();
private final PrintStream originalStdOut;
private final PrintStream originalStdErr;
private final ByteArrayOutputStream capturedStdOut;
private final ByteArrayOutputStream capturedStdErr;

/** Put this in a try-with-resources block to capture all standard io that happens within the try block */
public AutoCloseable capture() {
final PrintStream originalStdOut = System.out;
final PrintStream originalStdErr = System.err;
final ByteArrayOutputStream capturedStdOut = new ByteArrayOutputStream();
final ByteArrayOutputStream capturedStdErr = new ByteArrayOutputStream();
public static StdIOCapture capture() {
return new StdIOCapture();
}

private StdIOCapture() {
originalStdOut = System.out;
originalStdErr = System.err;
capturedStdOut = new ByteArrayOutputStream();
capturedStdErr = new ByteArrayOutputStream();

System.setOut(new PrintStream(capturedStdOut));
System.setErr(new PrintStream(capturedStdErr));

return () -> {
System.setOut(originalStdOut);
System.setErr(originalStdErr);
stdout.addAll(asList(capturedStdOut.toString("UTF-8").split(System.lineSeparator())));
stderr.addAll(asList(capturedStdErr.toString("UTF-8").split(System.lineSeparator())));
};
}

public List<String> stdout() {
Expand All @@ -57,4 +59,12 @@ public List<String> stdout() {
public List<String> stderr() {
return stderr;
}

@Override
public void close() throws UnsupportedEncodingException {
System.setOut(originalStdOut);
System.setErr(originalStdErr);
stdout.addAll(asList(capturedStdOut.toString("UTF-8").split(System.lineSeparator())));
stderr.addAll(asList(capturedStdErr.toString("UTF-8").split(System.lineSeparator())));
}
}
10 changes: 3 additions & 7 deletions examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

<properties>
<rootDir>${project.basedir}/..</rootDir>
<!-- Turned off because it moves/deletes example tags and needs investigation. -->
<spotless.check.skip>true</spotless.check.skip>
<spotless.apply.skip>true</spotless.apply.skip>
</properties>

<dependencies>
Expand Down Expand Up @@ -82,13 +85,6 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs combine.self="override"/>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
* limitations under the License.
*/
package org.neo4j.docs.driver;

// tag::async-autocommit-transaction-import[]

import org.neo4j.driver.async.AsyncSession;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletionStage;
import org.neo4j.driver.async.AsyncSession;
// end::async-autocommit-transaction-import[]

public class AsyncAutocommitTransactionExample extends BaseApplication {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@

// tag::async-result-consume-import[]

import org.neo4j.driver.async.AsyncSession;

import java.util.List;
import java.util.concurrent.CompletionStage;
import org.neo4j.driver.async.AsyncSession;
// end::async-result-consume-import[]

public class AsyncResultConsumeExample extends BaseApplication {
public AsyncResultConsumeExample(String uri, String user, String password) {
super(uri, user, password);
}

@SuppressWarnings("deprecation")
// tag::async-result-consume[]
public CompletionStage<List<String>> getPeople() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,25 @@

// tag::async-result-consume-import[]

import static org.neo4j.driver.Values.parameters;
// end::async-result-consume-import[]

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import org.neo4j.driver.async.AsyncSession;
import org.neo4j.driver.async.AsyncTransaction;
import org.neo4j.driver.async.ResultCursor;
import org.neo4j.driver.summary.ResultSummary;
import org.neo4j.driver.summary.SummaryCounters;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;

import static org.neo4j.driver.Values.parameters;
// end::async-result-consume-import[]

public class AsyncRunMultipleTransactionExample extends BaseApplication {
public AsyncRunMultipleTransactionExample(String uri, String user, String password) {
super(uri, user, password);
}

@SuppressWarnings("deprecation")
// tag::async-multiple-tx[]
public CompletionStage<Integer> addEmployees(final String companyName) {
AsyncSession session = driver.asyncSession();
Expand All @@ -53,14 +54,13 @@ private static CompletionStage<List<String>> matchPersonNodes(AsyncTransaction t
cursor -> cursor.listAsync(record -> record.get("name").asString()));
}

private static CompletableFuture<Integer> createNodes(
private static CompletionStage<Integer> createNodes(
AsyncTransaction tx, String companyName, List<String> personNames) {
CompletableFuture<Integer>[] nodeCreatedCounts = personNames.stream()
return personNames.stream()
.map(personName -> createNode(tx, companyName, personName))
.toArray(size -> new CompletableFuture[size]);
return CompletableFuture.allOf(nodeCreatedCounts).thenApply(ignored -> Arrays.stream(nodeCreatedCounts)
.map(CompletableFuture::join)
.reduce(0, Integer::sum));
.reduce(
CompletableFuture.completedFuture(0),
(stage1, stage2) -> stage1.thenCombine(stage2, Integer::sum));
}

private static CompletionStage<Integer> createNode(AsyncTransaction tx, String companyName, String personName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,23 @@
* limitations under the License.
*/
package org.neo4j.docs.driver;

// tag::async-transaction-function-import[]

import org.neo4j.driver.async.AsyncSession;
import org.neo4j.driver.summary.ResultSummary;

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.CompletionStage;
import org.neo4j.driver.async.AsyncSession;
import org.neo4j.driver.summary.ResultSummary;
// end::async-transaction-function-import[]

public class AsyncTransactionFunctionExample extends BaseApplication {
public AsyncTransactionFunctionExample(String uri, String user, String password) {
super(uri, user, password);
}

@SuppressWarnings("deprecation")
// tag::async-transaction-function[]
public CompletionStage<ResultSummary> printAllProducts() {
String query = "MATCH (p:Product) WHERE p.id = $id RETURN p.title";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
* limitations under the License.
*/
package org.neo4j.docs.driver;

// tag::async-unmanaged-transaction-import[]

import org.neo4j.driver.async.AsyncSession;
import org.neo4j.driver.async.AsyncTransaction;
import org.neo4j.driver.async.ResultCursor;

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.CompletionStage;
import java.util.function.Function;
import org.neo4j.driver.async.AsyncSession;
import org.neo4j.driver.async.AsyncTransaction;
import org.neo4j.driver.async.ResultCursor;
// end::async-unmanaged-transaction-import[]

public class AsyncUnmanagedTransactionExample extends BaseApplication {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

// tag::autocommit-transaction-import[]

import org.neo4j.driver.Session;

import static org.neo4j.driver.Values.parameters;
// end::autocommit-transaction-import[]

import org.neo4j.driver.Session;

public class AutocommitTransactionExample extends BaseApplication {
public AutocommitTransactionExample(String uri, String user, String password) {
super(uri, user, password);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public BaseApplication(String uri, String user, String password) {
}

@Override
public void close() throws Exception {
public void close() throws RuntimeException {
driver.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.neo4j.docs.driver;

// tag::basic-auth-import[]

import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
Expand All @@ -36,7 +35,7 @@ public BasicAuthExample(String uri, String user, String password) {
// end::basic-auth[]

@Override
public void close() throws Exception {
public void close() throws RuntimeException {
driver.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.neo4j.docs.driver;

// tag::bearer-auth-import[]

import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
Expand All @@ -35,7 +34,7 @@ public BearerAuthExample(String uri, String bearerToken) {
// end::bearer-auth[]

@Override
public void close() throws Exception {
public void close() throws RuntimeException {
driver.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
* limitations under the License.
*/
package org.neo4j.docs.driver;

// tag::config-connection-pool-import[]
import java.util.concurrent.TimeUnit;

import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Config;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
import org.neo4j.driver.Result;

import java.util.concurrent.TimeUnit;
// end::config-connection-pool-import[]

public class ConfigConnectionPoolExample implements AutoCloseable {
Expand All @@ -42,7 +45,7 @@ public ConfigConnectionPoolExample(String uri, String user, String password) {
// end::config-connection-pool[]

@Override
public void close() throws Exception {
public void close() throws RuntimeException {
driver.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@

// tag::config-connection-timeout-import[]

import static java.util.concurrent.TimeUnit.SECONDS;
// end::config-connection-timeout-import[]

import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Config;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;

import static java.util.concurrent.TimeUnit.SECONDS;
// end::config-connection-timeout-import[]

public class ConfigConnectionTimeoutExample implements AutoCloseable {
private final Driver driver;

Expand All @@ -40,7 +40,7 @@ public ConfigConnectionTimeoutExample(String uri, String user, String password)
// end::config-connection-timeout[]

@Override
public void close() throws Exception {
public void close() throws RuntimeException {
driver.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@
* limitations under the License.
*/
package org.neo4j.docs.driver;
// tag::config-custom-resolver-import[]

import static org.neo4j.driver.SessionConfig.builder;
import static org.neo4j.driver.Values.parameters;
// tag::config-custom-resolver-import[]

import java.util.Arrays;
import java.util.HashSet;
import org.neo4j.driver.AccessMode;
import org.neo4j.driver.AuthToken;
import org.neo4j.driver.AuthTokens;
Expand All @@ -34,7 +30,13 @@
import org.neo4j.driver.Session;
import org.neo4j.driver.net.ServerAddress;

import java.util.Arrays;
import java.util.HashSet;

import static org.neo4j.driver.SessionConfig.builder;
import static org.neo4j.driver.Values.parameters;
// end::config-custom-resolver-import[]

public class ConfigCustomResolverExample implements AutoCloseable {
private final Driver driver;

Expand Down Expand Up @@ -75,7 +77,7 @@ private void addPerson(String name) {
// end::config-custom-resolver[]

@Override
public void close() throws Exception {
public void close() throws RuntimeException {
driver.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@

// tag::config-max-retry-time-import[]

import static java.util.concurrent.TimeUnit.SECONDS;
// end::config-max-retry-time-import[]

import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Config;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;

import static java.util.concurrent.TimeUnit.SECONDS;
// end::config-max-retry-time-import[]

public class ConfigMaxRetryTimeExample implements AutoCloseable {
private final Driver driver;

Expand All @@ -41,7 +41,7 @@ public ConfigMaxRetryTimeExample(String uri, String user, String password) {
// end::config-max-retry-time[]

@Override
public void close() throws Exception {
public void close() throws RuntimeException {
driver.close();
}
}
Loading

0 comments on commit 6e8c27f

Please sign in to comment.