package sft.integration.use;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import sft.*;
import sft.decorators.Breadcrumb;
import sft.integration.fixtures.JUnitHtmlHelper;
import sft.integration.fixtures.JavaResource;
import sft.integration.use.sut.DisplayContext;
import sft.integration.use.sut.DisplayContextGeneratingDuringContextEnding;
import sft.integration.use.sut.subUseCase.DisplayContextFromHelper;
import sft.integration.use.sut.subUseCase.DisplayableFixturesHelper;
import sft.integration.use.sut.subUseCase.HelperGeneratingDisplayableWhenHandlingContext;
import java.io.IOException;
@RunWith(SimpleFunctionalTest.class)
@Decorate(decorator = Breadcrumb.class)
public class DisplayingTestContext {
@FixturesHelper
private JUnitHtmlHelper functionalTest=new JUnitHtmlHelper();
@Displayable
private String helperClassSource;
@Test
public void displayTestContextWithObjectsAnnotatedByDisplayable() throws IOException {
allPrivateObjectsAnnotatedByDisplayableShouldBeDisplayedAfterScenario();
displayableObjectsAreDisplayedOnlyIfThereAreNotNull();
allDisplayableObjectsAreDisplayedAfterScenario();
allDisplayableObjectsAreUnsetBetweenScenarios();
}
@Test
public void usingDisplayableObjectFromFixturesHelper() throws IOException {
aScenarioUsingFixturesHelper();
displaysPublicFieldAnnotatedWithDisplayableAnnotationFromFixturesHelper();
}
@Test
public void displayableObjectGeneratedDuringContextEnding() throws Exception{
aScenarioUsingFixturesHelperAndGeneratingContextDuringContextHandling();
displaysContextGeneratedWhenHandlingContext();
evenWhenAnErrorOccurs();
}
@Text("Displays context generated by methods annotated with @Before and @After (methods of use cases and helpers)")
private void displaysContextGeneratedWhenHandlingContext() {
Element secondScenario = functionalTest.html.select("*.scenario").get(0);
Elements contextDisplayed = secondScenario.select("*.displayableContext > div");
Assert.assertEquals("before scenario",contextDisplayed.get(0).text());
Assert.assertEquals("during scenario",contextDisplayed.get(1).text());
Assert.assertEquals("after scenario",contextDisplayed.get(2).text());
Assert.assertEquals("helper: before scenario",contextDisplayed.get(3).text());
Assert.assertEquals("helper: after scenario",contextDisplayed.get(4).text());
}
private void evenWhenAnErrorOccurs() {
Element secondScenario = functionalTest.html.select("*.scenario").get(1);
Elements contextDisplayed = secondScenario.select("*.displayableContext > div");
Assert.assertEquals("before scenario",contextDisplayed.get(0).text());
Assert.assertEquals("during scenario",contextDisplayed.get(1).text());
Assert.assertEquals("after scenario",contextDisplayed.get(2).text());
Assert.assertEquals("helper: before scenario",contextDisplayed.get(3).text());
Assert.assertEquals("helper: after scenario", contextDisplayed.get(4).text());
}
private void aScenarioUsingFixturesHelperAndGeneratingContextDuringContextHandling() throws Exception {
functionalTest.run(this.getClass(),DisplayContextGeneratingDuringContextEnding.class);
JavaResource fixtureHelperSource = new JavaResource(HelperGeneratingDisplayableWhenHandlingContext.class);
helperClassSource ="<div class=\"resources\">"+
fixtureHelperSource.getOpenResourceHtmlLink(this.getClass(), "helper", "alert-info")+ "</div>" ;
}
private void aScenarioUsingFixturesHelper() throws IOException {
functionalTest.run(this.getClass(),DisplayContextFromHelper.class);
JavaResource fixtureHelperSource = new JavaResource(DisplayableFixturesHelper.class);
helperClassSource ="<div class=\"resources\">"+
fixtureHelperSource.getOpenResourceHtmlLink(this.getClass(), "helper", "alert-info")+ "</div>" ;
}
private void displaysPublicFieldAnnotatedWithDisplayableAnnotationFromFixturesHelper() {
Element secondScenario = functionalTest.html.select("*.scenario").get(0);
Element contextDisplayed = secondScenario.select("*.displayableContext").get(0);
Assert.assertNotNull("Missing displayableContext", contextDisplayed);
Elements texts = contextDisplayed.select(">div");
Assert.assertEquals("It is displayed", texts.get(0).text());
}
private void allPrivateObjectsAnnotatedByDisplayableShouldBeDisplayedAfterScenario() throws IOException {
functionalTest.run(this.getClass(),DisplayContext.class);
}
private void displayableObjectsAreDisplayedOnlyIfThereAreNotNull() {
Element firstScenario = functionalTest.html.select("*.scenario").get(0);
Assert.assertTrue(firstScenario.select("*.displayableContext").isEmpty());
}
private void allDisplayableObjectsAreDisplayedAfterScenario() {
Element secondScenario = functionalTest.html.select("*.scenario").get(1);
Element contextDisplayed = secondScenario.select("*.displayableContext").get(0);
Assert.assertNotNull("Missing displayableContext", contextDisplayed);
Elements texts = contextDisplayed.select(">div");
Assert.assertEquals("first context display", texts.get(0).text());
Assert.assertEquals("second context display", texts.get(1).text());
}
private void allDisplayableObjectsAreUnsetBetweenScenarios() {
Element thirdScenario = functionalTest.html.select("*.scenario").get(2);
Element contextDisplayed = thirdScenario.select("*.displayableContext").get(0);
Assert.assertNotNull("Missing displayableContext", contextDisplayed);
Elements texts = contextDisplayed.select(">div");
Assert.assertEquals("second context display", texts.get(0).text());
}
}