Skip to content

Commit

Permalink
#993 tests for Picocontainer lifecycle management
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardBradley committed May 18, 2016
1 parent ec76465 commit f2bb89b
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 4 deletions.
@@ -0,0 +1,42 @@
package cucumber.runtime.java.picocontainer;

import org.picocontainer.Disposable;

import java.util.List;

/**
* A test helper class which simulates a class that holds system resources
* which need disposing at the end of the test.
*
* In a real app, this could be a database connector or similar.
*/
public class DisposableCucumberBelly
implements Disposable {

private List<String> contents;
private boolean isDisposed = false;

public void setContents(List<String> contents) {
assert !isDisposed;
this.contents = contents;
}

public List<String> getContents() {
assert !isDisposed;
return contents;
}

/**
* "dispose()" is useful in addition to @After, as it is guaranteed to run
* after all @After hooks, which is useful if this class is needed by the
* After hooks themselves.
*/
@Override
public void dispose() {
isDisposed = true;
}

public boolean isDisposed() {
return isDisposed;
}
}
Expand Up @@ -5,6 +5,8 @@


import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;


public class PicoFactoryTest { public class PicoFactoryTest {
@Test @Test
Expand All @@ -26,4 +28,23 @@ public void shouldGiveUsNewInstancesForEachScenario() {
assertNotSame(o1, o2); assertNotSame(o1, o2);
} }


@Test
public void shouldDisposeOnStop() {
// Given
ObjectFactory factory = new PicoFactory();
factory.addClass(StepDefs.class);

// When
factory.start();
StepDefs steps = factory.getInstance(StepDefs.class);

// Then
assertFalse(steps.getBelly().isDisposed());

// When
factory.stop();

// Then
assertTrue(steps.getBelly().isDisposed());
}
} }
Expand Up @@ -7,12 +7,22 @@
import cucumber.api.java.en.Given; import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then; import cucumber.api.java.en.Then;


import java.util.Collections;
import java.util.List; import java.util.List;


import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;


public class StepDefs { public class StepDefs {
private int amount;
private final DisposableCucumberBelly belly;

public StepDefs(DisposableCucumberBelly belly) {
this.belly = belly;
}

DisposableCucumberBelly getBelly() {
return belly;
}


@Before @Before
public void before() { public void before() {
Expand All @@ -22,9 +32,15 @@ public void before() {
public void gh20() { public void gh20() {
} }


@After
public void after() {
// We might need to clean up the belly here, if it represented an external resource.
assert !belly.isDisposed();
}

@Given("^I have (\\d+) (.*) in my belly$") @Given("^I have (\\d+) (.*) in my belly$")
public void I_have_n_things_in_my_belly(int amount, String what) { public void I_have_n_things_in_my_belly(int n, String what) {
this.amount = amount; belly.setContents(Collections.nCopies(n, what));
} }


@Given("^I have this in my basket:$") @Given("^I have this in my basket:$")
Expand All @@ -38,7 +54,7 @@ public void throw_pending() {


@Then("^there are (\\d+) cukes in my belly") @Then("^there are (\\d+) cukes in my belly")
public void checkCukes(int n) { public void checkCukes(int n) {
assertEquals(amount, n); assertEquals(belly.getContents(), Collections.nCopies(n, "cukes"));
} }


@Then("^the (.*) contains (.*)") @Then("^the (.*) contains (.*)")
Expand Down

0 comments on commit f2bb89b

Please sign in to comment.