Skip to content

Commit

Permalink
#36 Clean up tests after module separation
Browse files Browse the repository at this point in the history
- Keep animal samples for the injector-extras module tests
- Remove animal samples from core module in favor of new Vehicle samples
  • Loading branch information
ljacqu committed Jun 12, 2017
1 parent 6a648b8 commit dc43052
Show file tree
Hide file tree
Showing 40 changed files with 399 additions and 579 deletions.
3 changes: 2 additions & 1 deletion injector-extras/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<artifactId>injector-extras</artifactId>

<dependencies>
<!-- Compile dependencies -->
<dependency>
<groupId>ch.jalu</groupId>
<artifactId>injector</artifactId>
Expand All @@ -23,12 +24,12 @@
<scope>compile</scope>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>java-hamcrest</artifactId>
Expand Down
4 changes: 4 additions & 0 deletions injector/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
<artifactId>javax.inject</artifactId>
<scope>compile</scope>
</dependency>

<!-- Compiler-only dependencies -->
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<scope>provided</scope>
</dependency>

<!-- Optional dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand All @@ -34,6 +37,7 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>

<!-- Testing-only dependencies -->
<dependency>
<groupId>org.hamcrest</groupId>
Expand Down
60 changes: 30 additions & 30 deletions injector/src/test/java/ch/jalu/injector/InjectorBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
import ch.jalu.injector.samples.BetaManager;
import ch.jalu.injector.samples.GammaService;
import ch.jalu.injector.samples.ProvidedClass;
import ch.jalu.injector.samples.animals.Animal;
import ch.jalu.injector.samples.animals.Bird;
import ch.jalu.injector.samples.animals.Chicken;
import ch.jalu.injector.samples.animals.Reptile;
import ch.jalu.injector.samples.animals.Snake;
import ch.jalu.injector.samples.animals.Sparrow;
import ch.jalu.injector.samples.animals.services.HissService;
import ch.jalu.injector.samples.animals.services.HissServiceProvider;
import ch.jalu.injector.samples.subpackage.SubpackageClass;
import ch.jalu.injector.samples.vehicles.Car;
import ch.jalu.injector.samples.vehicles.Ship;
import ch.jalu.injector.samples.vehicles.ShoppingCart;
import ch.jalu.injector.samples.vehicles.UnidentifiableVehicle;
import ch.jalu.injector.samples.vehicles.Vehicle;
import ch.jalu.injector.samples.vehicles.VehicleWithHorn;
import ch.jalu.injector.samples.vehicles.services.SailService;
import ch.jalu.injector.samples.vehicles.services.SailServiceProvider;
import org.junit.Test;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -75,32 +75,32 @@ public void shouldSupplyInjectorWithPackageSetting() {
}

/**
* Tests the order of handlers (important!) and that custom handlers are
* Tests the order of handlers (important!) and that custom handlers are included.
*/
@Test
public void shouldAllowCustomHandlers() {
// Instantiate handlers
ImplementationClassHandler implementationClassHandler = new ImplementationClassHandler();
implementationClassHandler.register(Animal.class, Reptile.class);
implementationClassHandler.register(Reptile.class, Snake.class);
implementationClassHandler.register(Bird.class, Sparrow.class);
implementationClassHandler.register(Vehicle.class, UnidentifiableVehicle.class);
implementationClassHandler.register(UnidentifiableVehicle.class, ShoppingCart.class);
implementationClassHandler.register(VehicleWithHorn.class, Ship.class);
PreConstructPackageValidator packageValidator = new PreConstructPackageValidator("ch.jalu");

SavedAnnotationsHandler savedAnnotationsHandler = new SavedAnnotationsHandler();
ListeningDependencyHandler listeningDependencyHandler = new ListeningDependencyHandler();

PostConstructHandler postConstructHandler = new PostConstructMethodInvoker();
// throw when Chicken gets instantiated
ThrowingPostConstructHandler throwingPostConstructHandler = new ThrowingPostConstructHandler(Chicken.class);
// throw when Car gets instantiated
ThrowingPostConstructHandler throwingPostConstructHandler = new ThrowingPostConstructHandler(Car.class);

// Create Injector with all handlers
InjectorBuilder builder = new InjectorBuilder();
List<InstantiationProvider> instantiationProviders = InjectorBuilder.createInstantiationProviders();
Injector injector = builder
Injector injector = new InjectorBuilder()
.addHandlers(implementationClassHandler, packageValidator, savedAnnotationsHandler,
listeningDependencyHandler, postConstructHandler, throwingPostConstructHandler)
.addHandlers(instantiationProviders)
.create();
injector.register(ProvidedClass.class, new ProvidedClass(""));

// Check presence of handlers and their order
InjectorConfig config = ((InjectorImpl) injector).getConfig();
Expand All @@ -111,26 +111,26 @@ public void shouldAllowCustomHandlers() {
assertThat(config.getPostConstructHandlers(),
contains(implementationClassHandler, postConstructHandler, throwingPostConstructHandler));

// Set Provider for HissService
injector.registerProvider(HissService.class, HissServiceProvider.class);
// Set Provider for SailService
injector.registerProvider(SailService.class, SailServiceProvider.class);

// Request Animal singleton -> mapped to Reptile -> Snake
Animal animal = injector.getSingleton(Animal.class);
assertThat(animal, instanceOf(Snake.class));
Snake snake = injector.getSingleton(Snake.class);
assertThat(snake, sameInstance(animal));
// Request Vehicle singleton -> mapped to UnidentifiableVehicle -> ShoppingCart
Vehicle vehicle = injector.getSingleton(Vehicle.class);
assertThat(vehicle, instanceOf(ShoppingCart.class));
ShoppingCart cart = injector.getSingleton(ShoppingCart.class);
assertThat(cart, sameInstance(vehicle));

// Check counts
assertThat(implementationClassHandler.getCounter(), equalTo(4)); // Snake, Configuration, HissServiceProvider, HissService
// Snake depends on Configuration and HissService (2)
// HissService comes from HissServiceProvider, which needs to be instantiated (1)
// and it depends on Configuration (1) = 4.
assertThat(listeningDependencyHandler.getCounter(), equalTo(4));
assertThat(throwingPostConstructHandler.getCounter(), equalTo(4)); // Snake, Configuration, HissService, HissServiceProvider
assertThat(implementationClassHandler.getCounter(), equalTo(4)); // ShoppingCart, Alpha/Beta/Gamma
// ShoppingCart depends on BetaManager (2)
// BetaManager -> ProvidedClass, AlphaService, GammaService (3)
// GammaService -> AlphaService (1)
assertThat(listeningDependencyHandler.getCounter(), equalTo(6));
assertThat(throwingPostConstructHandler.getCounter(), equalTo(4)); // ShoppingCart, Alpha/Beta/Gamma

// Check correct behavior of ThrowingPostHandler
try {
injector.getSingleton(Chicken.class);
injector.getSingleton(Car.class);
fail("Expected exception to occur");
} catch (InjectorException e) {
// noop
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package ch.jalu.injector.handlers.postconstruct;

//import ch.jalu.injector.InjectorBuilder;
//import ch.jalu.injector.InjectorImpl;
//import ch.jalu.injector.handlers.dependency.AllInstancesAnnotationHandler;
//import ch.jalu.injector.handlers.postconstruct.PostConstructHandler;
//import ch.jalu.injector.handlers.testimplementations.ProfilePostConstructHandler;
//import ch.jalu.injector.samples.animals.Sparrow;
//import ch.jalu.injector.samples.animals.services.Configuration;
//import ch.jalu.injector.samples.animals.services.HissService;
//import ch.jalu.injector.samples.animals.services.HissServiceProvider;
//import ch.jalu.injector.samples.animals.services.NameService;
//import ch.jalu.injector.samples.animals.services.SoundServiceSupervisor;
//import javassist.util.proxy.Proxy;
//import org.junit.Test;
//
//import static java.util.Collections.singletonList;
//import static org.hamcrest.Matchers.contains;
//import static org.hamcrest.Matchers.instanceOf;
//import static org.hamcrest.Matchers.not;
//import static org.junit.Assert.assertThat;
import ch.jalu.injector.InjectorBuilder;
import ch.jalu.injector.InjectorImpl;
import ch.jalu.injector.handlers.testimplementations.ProfilePostConstructHandler;
import ch.jalu.injector.samples.ProvidedClass;
import ch.jalu.injector.samples.vehicles.Bicycle;
import ch.jalu.injector.samples.vehicles.Plane;
import ch.jalu.injector.samples.vehicles.VehiclesJourneyService;
import ch.jalu.injector.samples.vehicles.services.DriveService;
import ch.jalu.injector.samples.vehicles.services.SailService;
import ch.jalu.injector.samples.vehicles.services.SailServiceProvider;
import ch.jalu.injector.samples.vehicles.services.SteerServiceManager;
import javassist.util.proxy.Proxy;
import org.junit.Test;

import static java.util.Collections.singletonList;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;

/**
* Tests that {@link PostConstructHandler} implementations
Expand All @@ -28,29 +28,29 @@ public class PostConstructRemappingTest {

private static final String PACKAGE = "ch.jalu.injector";

// TODO #36: Fix test
@Test
public void shouldInitializeProfiledClassesWithProxy() {
// given
InjectorImpl injector = (InjectorImpl) new InjectorBuilder()
.addDefaultHandlers(PACKAGE)
.create();
ProfilePostConstructHandler profileHandler = new ProfilePostConstructHandler(injector);
injector.getConfig().addPostConstructHandlers(singletonList(profileHandler));
injector.register(ProvidedClass.class, new ProvidedClass(""));
injector.registerProvider(SailService.class, SailServiceProvider.class);

// when (trigger initialization + invoke some methods)
VehiclesJourneyService journeyService = injector.getSingleton(VehiclesJourneyService.class);
journeyService.startJourney();
SteerServiceManager steerManager = injector.getSingleton(SteerServiceManager.class);
steerManager.brakeAll();
injector.getSingleton(Plane.class).identify();

// @Test
// public void shouldInitializeProfiledClassesWithProxy() {
// // given
// InjectorImpl injector = (InjectorImpl) new InjectorBuilder()
// .addDefaultHandlers(PACKAGE)
// .addHandlers(new AllInstancesAnnotationHandler(PACKAGE))
// .create();
// ProfilePostConstructHandler profileHandler = new ProfilePostConstructHandler(injector);
// injector.getConfig().addPostConstructHandlers(singletonList(profileHandler));
// injector.registerProvider(HissService.class, HissServiceProvider.class);
//
// // when (trigger initialization + invoke some methods)
// SoundServiceSupervisor supervisor = injector.getSingleton(SoundServiceSupervisor.class);
// supervisor.muteAll();
// injector.getSingleton(Sparrow.class).getName();
//
// // then
// assertThat(profileHandler.getInvocations(), contains("SoundServiceSupervisor#muteAll", "Configuration#getLang",
// "NameService#constructName", "Configuration#getLang"));
// assertThat(injector.getIfAvailable(Configuration.class), instanceOf(Proxy.class));
// assertThat(injector.getIfAvailable(Sparrow.class), not(instanceOf(Proxy.class)));
// assertThat(injector.getIfAvailable(NameService.class), instanceOf(Proxy.class));
// }
// then
assertThat(profileHandler.getInvocations(), contains("Car#startJourney", "DriveService#brake",
"SteerServiceManager#brakeAll", "DriveService#brake", "Plane#identify"));
assertThat(injector.getIfAvailable(Plane.class), instanceOf(Proxy.class));
assertThat(injector.getIfAvailable(Bicycle.class), not(instanceOf(Proxy.class)));
assertThat(injector.getIfAvailable(DriveService.class), instanceOf(Proxy.class));
}
}

This file was deleted.

28 changes: 0 additions & 28 deletions injector/src/test/java/ch/jalu/injector/samples/animals/Bird.java

This file was deleted.

This file was deleted.

31 changes: 0 additions & 31 deletions injector/src/test/java/ch/jalu/injector/samples/animals/Frog.java

This file was deleted.

33 changes: 0 additions & 33 deletions injector/src/test/java/ch/jalu/injector/samples/animals/Lion.java

This file was deleted.

Loading

0 comments on commit dc43052

Please sign in to comment.