Skip to content

Commit

Permalink
Reformat code with google-java-format 1.2. (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
nlativy authored Jan 28, 2017
1 parent 9839261 commit a8a2f46
Show file tree
Hide file tree
Showing 18 changed files with 347 additions and 317 deletions.
58 changes: 30 additions & 28 deletions src/main/java/com/google/acai/Acai.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,33 @@

package com.google.acai;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.acai.TestScope.TestScopeModule;
import com.google.acai.TestingServiceModule.NoopTestingServiceModule;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkNotNull;
import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;

/**
* Acai rule for integrating Guice with a JUnit4 test.
*
* <p>Use to inject a test with a module:
*
* <pre>
* public class MyTest {
* {@literal @}Rule Acai acai = new Acai(MyModule.class);
Expand All @@ -58,8 +56,8 @@
* }
* </pre>
*
* <p>To configure services to run before or between tests see {@link TestingService} and
* {@link TestingServiceModule}.
* <p>To configure services to run before or between tests see {@link TestingService} and {@link
* TestingServiceModule}.
*
* <p>See the {@code README.md} file for detailed usage examples.
*/
Expand All @@ -75,10 +73,11 @@ public Acai(Class<? extends Module> module) {
}

@Override
public Statement apply(final Statement statement, FrameworkMethod frameworkMethod,
final Object target) {
public Statement apply(
final Statement statement, FrameworkMethod frameworkMethod, final Object target) {
return new Statement() {
@Override public void evaluate() throws Throwable {
@Override
public void evaluate() throws Throwable {
TestEnvironment testEnvironment = getOrCreateTestEnvironment(module);
testEnvironment.beforeSuiteIfNotAlreadyRun();
testEnvironment.enterTestScope();
Expand All @@ -98,21 +97,23 @@ public Statement apply(final Statement statement, FrameworkMethod frameworkMetho
}

/**
* Returns the {@code TestEnvironment} for the module, creating it if this
* is the first time it has been requested.
* Returns the {@code TestEnvironment} for the module, creating it if this is the first time it
* has been requested.
*/
private static TestEnvironment getOrCreateTestEnvironment(Class<? extends Module> module) {
if (environments.containsKey(module)) {
return environments.get(module);
}
Injector injector = Guice.createInjector(
instantiateModule(module), new NoopTestingServiceModule(), new TestScopeModule());
TestEnvironment testEnvironment = new TestEnvironment(
injector,
Dependencies.inOrder(injector.getInstance(TESTING_SERVICES_KEY))
.stream()
.map(TestingServiceManager::new)
.collect(Collectors.toList()));
Injector injector =
Guice.createInjector(
instantiateModule(module), new NoopTestingServiceModule(), new TestScopeModule());
TestEnvironment testEnvironment =
new TestEnvironment(
injector,
Dependencies.inOrder(injector.getInstance(TESTING_SERVICES_KEY))
.stream()
.map(TestingServiceManager::new)
.collect(Collectors.toList()));
environments.put(module, testEnvironment);
return testEnvironment;
}
Expand Down Expand Up @@ -146,13 +147,14 @@ private static Module instantiateModule(Class<? extends Module> module) {
*
* <p>For use in unit tests of Acai itself.
*/
@VisibleForTesting static void testOnlyResetEnvironments() {
@VisibleForTesting
static void testOnlyResetEnvironments() {
environments.clear();
}

/**
* A TestEnvironment represents a configuration for running tests derived
* from a single Guice module.
* A TestEnvironment represents a configuration for running tests derived from a single Guice
* module.
*/
private static class TestEnvironment {
private final Injector injector;
Expand All @@ -161,8 +163,8 @@ private static class TestEnvironment {
private final TestScope testScope;

/**
* Initializes a newly created {@code TestEnvironment} with an {@code injector} and
* a list of {@code testingServices} in the order they will be executed.
* Initializes a newly created {@code TestEnvironment} with an {@code injector} and a list of
* {@code testingServices} in the order they will be executed.
*/
TestEnvironment(Injector injector, Iterable<TestingServiceManager> testingServices) {
this.injector = checkNotNull(injector);
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/com/google/acai/AcaiInternal.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@

package com.google.acai;

import com.google.inject.BindingAnnotation;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import com.google.inject.BindingAnnotation;
import java.lang.annotation.Retention;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Annotates bindings which are internal to Acai.
*/
/** Annotates bindings which are internal to Acai. */
@Retention(RUNTIME)
@BindingAnnotation
@interface AcaiInternal {
}
@interface AcaiInternal {}
13 changes: 5 additions & 8 deletions src/main/java/com/google/acai/AfterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@

package com.google.acai;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Annotates a method in a {@link TestingService} to run after each test method.
*/
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

/** Annotates a method in a {@link TestingService} to run after each test method. */
@Retention(RUNTIME)
@Target(METHOD)
public @interface AfterTest {
}
public @interface AfterTest {}
13 changes: 5 additions & 8 deletions src/main/java/com/google/acai/BeforeSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@

package com.google.acai;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Annotates a method in a {@link TestingService} to run before a test suite.
*/
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

/** Annotates a method in a {@link TestingService} to run before a test suite. */
@Retention(RUNTIME)
@Target(METHOD)
public @interface BeforeSuite {
}
public @interface BeforeSuite {}
13 changes: 5 additions & 8 deletions src/main/java/com/google/acai/BeforeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@

package com.google.acai;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Annotates a method in a {@link TestingService} to run before each test method.
*/
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

/** Annotates a method in a {@link TestingService} to run before each test method. */
@Retention(RUNTIME)
@Target(METHOD)
public @interface BeforeTest {
}
public @interface BeforeTest {}
59 changes: 20 additions & 39 deletions src/main/java/com/google/acai/Dependencies.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,27 @@

package com.google.acai;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;

import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Set;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;

/**
* Internal helper methods for managing {@code TestingService} dependencies
* which have been declared using the {@code DependsOn} annotation.
* Internal helper methods for managing {@code TestingService} dependencies which have been declared
* using the {@code DependsOn} annotation.
*/
class Dependencies {

/**
* Returns a valid execution order for {@code testingServices} based on
* any {@link DependsOn} annotations present.
* Returns a valid execution order for {@code testingServices} based on any {@link DependsOn}
* annotations present.
*
* @throws IllegalArgumentException if the dependency graph contains a cycle
*/
Expand All @@ -49,8 +48,8 @@ static ImmutableList<TestingService> inOrder(Set<TestingService> testingServices
* Returns a topological sorting.
*
* <p>Algorithm due to Kahn, Arthur B. (1962), Topological sorting of large networks,
* Communications of the ACM 5 (11): 558–562,
* <a href="http://dl.acm.org/citation.cfm?doid=368996.369025">doi:10.1145/368996.369025</a>.
* Communications of the ACM 5 (11): 558–562, <a
* href="http://dl.acm.org/citation.cfm?doid=368996.369025">doi:10.1145/368996.369025</a>.
*/
private static ImmutableList<TestingService> topologicalSorting(
DirectedGraph<TestingService> dependencyGraph) {
Expand All @@ -72,9 +71,7 @@ private static ImmutableList<TestingService> topologicalSorting(
return ordered.build();
}

/**
* Returns a directed graph representing the dependencies of {@code testingServices}.
*/
/** Returns a directed graph representing the dependencies of {@code testingServices}. */
private static DirectedGraph<TestingService> buildDependencyGraph(
Set<TestingService> testingServices) {
DirectedGraph<TestingService> dependencyGraph = new DirectedGraph<>(testingServices);
Expand All @@ -91,9 +88,7 @@ private static DirectedGraph<TestingService> buildDependencyGraph(
return dependencyGraph;
}

/**
* Returns the set of services which {@code testingService} depends upon.
*/
/** Returns the set of services which {@code testingService} depends upon. */
private static ImmutableSet<TestingService> getDependencies(
TestingService testingService,
Multimap<Class<? extends TestingService>, TestingService> servicesByClass) {
Expand All @@ -111,62 +106,48 @@ private static ImmutableSet<TestingService> getDependencies(
/**
* Simple representation of a directed graph.
*
* <p>The set of vertices in the graph is immutable but edges may be
* added and removed from an instance.
* <p>The set of vertices in the graph is immutable but edges may be added and removed from an
* instance.
*/
private static class DirectedGraph<T> {
private final Multimap<T, T> successors = HashMultimap.create();
private final Multimap<T, T> predecessors = HashMultimap.create();
private final ImmutableSet<T> vertices;

/**
* Initializes a graph containing {@code vertices}.
*/
/** Initializes a graph containing {@code vertices}. */
DirectedGraph(Set<T> vertices) {
this.vertices = ImmutableSet.copyOf(vertices);
}

/**
* Adds a directed edge from {@code tail} to {@code head}.
*/
/** Adds a directed edge from {@code tail} to {@code head}. */
void addEdge(T tail, T head) {
successors.put(tail, head);
predecessors.put(head, tail);
}

/**
* Removes the directed edge from {@code tail} to {@code head}.
*/
/** Removes the directed edge from {@code tail} to {@code head}. */
void removeEdge(T tail, T head) {
checkArgument(successors.remove(tail, head), "Attempt to remove non-existent edge");
checkState(predecessors.remove(head, tail), "Graph state was invalid.");
}

/**
* Returns the set of vertices which are not the tail of any directed edge.
*/
/** Returns the set of vertices which are not the tail of any directed edge. */
Set<T> getRootVertices() {
return Sets.difference(vertices, predecessors.keySet());
}

/**
* Returns true if there are no directed edges whose tail is {@code vertex}.
*/
/** Returns true if there are no directed edges whose tail is {@code vertex}. */
boolean isRootVertex(T vertex) {
checkArgument(vertices.contains(vertex));
return !predecessors.containsKey(vertex);
}

/**
* Returns true if the graph has any edges.
*/
/** Returns true if the graph has any edges. */
boolean hasEdges() {
return !successors.isEmpty();
}

/**
* Returns the set of vertices who are at the head of an edge whose tail is {@code vertex}.
*/
/** Returns the set of vertices who are at the head of an edge whose tail is {@code vertex}. */
ImmutableSet<T> getSuccessors(T vertex) {
return ImmutableSet.copyOf(successors.get(vertex));
}
Expand Down
24 changes: 11 additions & 13 deletions src/main/java/com/google/acai/DependsOn.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,23 @@

package com.google.acai;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

/**
* Annotates a {@link com.google.acai.TestingService} as depending on another
* testing service.
* Annotates a {@link com.google.acai.TestingService} as depending on another testing service.
*
* <p>A testing service annotated as depending on another its methods will be executed
* after the methods of the service it depends upon when executing
* {@code BeforeSuite} or {@code BeforeTest} methods. When running {@code AfterTest}
* methods the dependent service's methods will be run before those of the
* service it depends upon (i.e., the setup ordering is the opposite of the setup
* ordering).
* <p>A testing service annotated as depending on another its methods will be executed after the
* methods of the service it depends upon when executing {@code BeforeSuite} or {@code BeforeTest}
* methods. When running {@code AfterTest} methods the dependent service's methods will be run
* before those of the service it depends upon (i.e., the setup ordering is the opposite of the
* setup ordering).
*
* <p>This is useful in the case where your test environment starts multiple
* services which depend on each other being available in order to start.
* <p>This is useful in the case where your test environment starts multiple services which depend
* on each other being available in order to start.
*/
@Retention(RUNTIME)
@Target(TYPE)
Expand Down
Loading

0 comments on commit a8a2f46

Please sign in to comment.