Commits

Micha Kops  committed 9997e6e

JBehave BDD Tutorial sources added

  • Participants
  • Parent commits ccb7ccb

Comments (0)

Files changed (19)

File jbehave-tutorial/.classpath

+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" output="target/classes" path="src/main/java"/>
+	<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
+	<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
+	<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
+	<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
+	<classpathentry kind="output" path="target/classes"/>
+</classpath>

File jbehave-tutorial/.project

+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>jbehave-bdd-tutorial</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.maven.ide.eclipse.maven2Builder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+		<nature>org.maven.ide.eclipse.maven2Nature</nature>
+	</natures>
+</projectDescription>

File jbehave-tutorial/.settings/org.eclipse.jdt.core.prefs

+#Sat May 14 16:26:27 CEST 2011
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
+org.eclipse.jdt.core.compiler.source=1.6

File jbehave-tutorial/.settings/org.maven.ide.eclipse.prefs

+#Mon May 02 21:15:01 CEST 2011
+activeProfiles=
+eclipse.preferences.version=1
+fullBuildGoals=process-test-resources
+resolveWorkspaceProjects=true
+resourceFilterGoals=process-resources resources\:testResources
+skipCompilerPlugin=true
+version=1

File jbehave-tutorial/pom.xml

+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+	<groupId>com.hascode.tutorial</groupId>
+	<artifactId>jbehave-bdd-tutorial</artifactId>
+	<version>0.0.1-SNAPSHOT</version>
+	<name>hasCode.com jbehave BDD tutorial</name>
+	<packaging>war</packaging>
+
+	<properties>
+		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+		<jbehave.core.version>3.3.2</jbehave.core.version>
+		<jbehave.webapp.name>jbehave-webapprunner</jbehave.webapp.name>
+	</properties>
+
+	<dependencies>
+		<dependency>
+			<groupId>org.jbehave</groupId>
+			<artifactId>jbehave-core</artifactId>
+			<version>${jbehave.core.version}</version>
+		</dependency>
+		<dependency>
+			<groupId>org.jbehave.web</groupId>
+			<artifactId>jbehave-web-runner</artifactId>
+			<version>${jbehave.core.version}</version>
+		</dependency>
+		<dependency>
+			<groupId>org.jbehave.web</groupId>
+			<artifactId>jbehave-web-selenium</artifactId>
+			<version>${jbehave.core.version}</version>
+		</dependency>
+		<dependency>
+			<groupId>org.seleniumhq.selenium.client-drivers</groupId>
+			<artifactId>selenium-java-client-driver</artifactId>
+			<version>1.0.2</version>
+		</dependency>
+		<dependency>
+			<groupId>org.openqa.selenium.server</groupId>
+			<artifactId>selenium-server</artifactId>
+			<version>1.0-20081010.060147</version>
+		</dependency>
+	</dependencies>
+
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-compiler-plugin</artifactId>
+				<configuration>
+					<source>1.6</source>
+					<target>1.6</target>
+				</configuration>
+			</plugin>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-dependency-plugin</artifactId>
+				<executions>
+					<execution>
+
+						<id>unpack-resources</id>
+						<phase>process-resources</phase>
+						<goals>
+							<goal>unpack</goal>
+						</goals>
+						<configuration>
+							<outputDirectory>${project.build.directory}/resources</outputDirectory>
+
+							<overWriteReleases>true</overWriteReleases>
+							<overWriteSnapshots>true</overWriteSnapshots>
+							<excludes>**/*.class</excludes>
+							<artifactItems>
+								<artifactItem>
+									<groupId>org.jbehave.web</groupId>
+									<artifactId>jbehave-web-runner</artifactId>
+									<version>${jbehave.core.version}</version>
+								</artifactItem>
+							</artifactItems>
+						</configuration>
+					</execution>
+				</executions>
+			</plugin>
+			<plugin>
+
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-resources-plugin</artifactId>
+				<executions>
+					<execution>
+						<id>copy-custom-resources</id>
+						<phase>process-resources</phase>
+						<goals>
+
+							<goal>copy-resources</goal>
+						</goals>
+						<configuration>
+							<outputDirectory>${project.build.directory}/resources</outputDirectory>
+							<overwrite>true</overwrite>
+							<resources>
+								<resource>
+
+									<directory>${basedir}/src/main/webapp</directory>
+								</resource>
+							</resources>
+						</configuration>
+					</execution>
+				</executions>
+			</plugin>
+			<plugin>
+
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-war-plugin</artifactId>
+				<configuration>
+					<webResources>
+						<resource>
+							<directory>${project.build.directory}/resources</directory>
+						</resource>
+
+					</webResources>
+				</configuration>
+			</plugin>
+			<plugin>
+				<groupId>org.mortbay.jetty</groupId>
+				<artifactId>jetty-maven-plugin</artifactId>
+				<configuration>
+					<webAppConfig>
+
+						<contextPath>/${jbehave.webapp.name}</contextPath>
+					</webAppConfig>
+					<webApp>${project.build.directory}/${jbehave.webapp.name}.war</webApp>
+				</configuration>
+			</plugin>
+		</plugins>
+	</build>
+</project>

File jbehave-tutorial/src/main/java/com/hascode/tutorial/AddProductToShoppingCart.java

+package com.hascode.tutorial;
+
+import java.util.List;
+
+import org.jbehave.core.annotations.Given;
+import org.jbehave.core.annotations.Then;
+import org.jbehave.core.annotations.When;
+import org.jbehave.core.configuration.Configuration;
+import org.jbehave.core.configuration.MostUsefulConfiguration;
+import org.jbehave.core.io.LoadFromClasspath;
+import org.jbehave.core.junit.JUnitStory;
+import org.jbehave.core.reporters.Format;
+import org.jbehave.core.reporters.StoryReporterBuilder;
+import org.jbehave.core.steps.CandidateSteps;
+import org.jbehave.core.steps.InstanceStepsFactory;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class AddProductToShoppingCart extends JUnitStory {
+	private User			user;
+	private ShoppingCart	shoppingCart;
+	private Product			product;
+
+	@Given("a user")
+	public void aUser() {
+		user = new User();
+	}
+
+	@Given("a shopping cart")
+	public void aShoppingCart() {
+		shoppingCart = new ShoppingCart();
+	}
+
+	@Given("a product")
+	public void aProduct() {
+		product = new Product();
+		product.setName("Coffee");
+	}
+
+	@When("the user adds the product to the shopping cart")
+	public void userAddsProductToShoppingCart() {
+		shoppingCart.add(user, product);
+	}
+
+	@Then("the product must be included in the list of the shoppingcart's entries")
+	public void productMustBeListed() {
+		List<Product> entries = shoppingCart.getProductsByUser(user);
+		Assert.assertTrue(entries.contains(product));
+	}
+
+	@Override
+	public Configuration configuration() {
+		return new MostUsefulConfiguration().useStoryLoader(new LoadFromClasspath(getClass().getClassLoader())).useStoryReporterBuilder(new StoryReporterBuilder().withFormats(Format.CONSOLE));
+	}
+
+	@Override
+	public List<CandidateSteps> candidateSteps() {
+		return new InstanceStepsFactory(configuration(), this).createCandidateSteps();
+	}
+
+	@Override
+	@Test
+	public void run() throws Throwable {
+		super.run();
+	}
+}

File jbehave-tutorial/src/main/java/com/hascode/tutorial/AddUserToRepository.java

+package com.hascode.tutorial;
+
+import java.util.List;
+
+import junit.framework.Assert;
+
+import org.jbehave.core.annotations.Given;
+import org.jbehave.core.annotations.Then;
+import org.jbehave.core.annotations.When;
+import org.jbehave.core.configuration.Configuration;
+import org.jbehave.core.configuration.MostUsefulConfiguration;
+import org.jbehave.core.io.LoadFromClasspath;
+import org.jbehave.core.junit.JUnitStory;
+import org.jbehave.core.reporters.Format;
+import org.jbehave.core.reporters.StoryReporterBuilder;
+import org.jbehave.core.steps.CandidateSteps;
+import org.jbehave.core.steps.InstanceStepsFactory;
+import org.junit.Test;
+
+public class AddUserToRepository extends JUnitStory {
+	private UserDao	userDao;
+
+	@Given("a user repository")
+	public void aUserRepository() {
+		userDao = new UserDao();
+	}
+
+	@When("the user adds $amount users to the user repository")
+	public void userAddsUsersToRepository(final int amount) {
+		for (int i = 0; i < amount; i++) {
+			final User user = new User();
+			user.setName("user " + i);
+			userDao.save(user);
+		}
+	}
+
+	@Then("the user repository must contain $amount users")
+	public void productMustBeListed(final int amount) {
+		Assert.assertEquals(amount, userDao.findAll().size());
+	}
+
+	@Override
+	public Configuration configuration() {
+		return new MostUsefulConfiguration().useStoryLoader(new LoadFromClasspath(getClass().getClassLoader())).useStoryReporterBuilder(new StoryReporterBuilder().withFormats(Format.CONSOLE));
+	}
+
+	@Override
+	public List<CandidateSteps> candidateSteps() {
+		return new InstanceStepsFactory(configuration(), this).createCandidateSteps();
+	}
+
+	@Override
+	@Test
+	public void run() throws Throwable {
+		super.run();
+	}
+}

File jbehave-tutorial/src/main/java/com/hascode/tutorial/Product.java

+package com.hascode.tutorial;
+
+public class Product {
+	private String	name;
+
+	/**
+	 * @return the name
+	 */
+	public String getName() {
+		return name;
+	}
+
+	/**
+	 * @param name
+	 *            the name to set
+	 */
+	public void setName(String name) {
+		this.name = name;
+	}
+}

File jbehave-tutorial/src/main/java/com/hascode/tutorial/SearchPopularSearchEngine.java

+package com.hascode.tutorial;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.jbehave.core.Embeddable;
+import org.jbehave.core.configuration.Configuration;
+import org.jbehave.core.io.CodeLocations;
+import org.jbehave.core.io.LoadFromClasspath;
+import org.jbehave.core.io.StoryFinder;
+import org.jbehave.core.junit.JUnitStories;
+import org.jbehave.core.reporters.Format;
+import org.jbehave.core.reporters.StoryReporterBuilder;
+import org.jbehave.core.steps.CandidateSteps;
+import org.jbehave.core.steps.InstanceStepsFactory;
+import org.jbehave.core.steps.SilentStepMonitor;
+import org.jbehave.web.selenium.SeleniumConfiguration;
+import org.jbehave.web.selenium.SeleniumContext;
+import org.jbehave.web.selenium.SeleniumStepMonitor;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.thoughtworks.selenium.DefaultSelenium;
+import com.thoughtworks.selenium.Selenium;
+
+public class SearchPopularSearchEngine extends JUnitStories {
+	private final Selenium			selenium		= new DefaultSelenium("127.0.0.1", 4444, "*chrome", "http://www.google.de/");
+	private final SeleniumContext	seleniumContext	= new SeleniumContext();
+
+	@Override
+	public Configuration configuration() {
+		Class<? extends Embeddable> embeddableClass = this.getClass();
+		return new SeleniumConfiguration().useSelenium(selenium).useSeleniumContext(seleniumContext).useStepMonitor(new SeleniumStepMonitor(selenium, seleniumContext, new SilentStepMonitor()))
+				.useStoryLoader(new LoadFromClasspath(embeddableClass)).useStoryReporterBuilder(new StoryReporterBuilder().withFormats(Format.CONSOLE));
+	}
+
+	@Override
+	public List<CandidateSteps> candidateSteps() {
+		return new InstanceStepsFactory(configuration(), new SearchPopularSearchEngineSteps(selenium)).createCandidateSteps();
+	}
+
+	@Before
+	public void setUp() throws Exception {
+		selenium.start();
+	}
+
+	@Override
+	@Test
+	public void run() throws Throwable {
+		super.run();
+	}
+
+	@Override
+	protected List<String> storyPaths() {
+		return new StoryFinder().findPaths(CodeLocations.codeLocationFromClass(this.getClass()).getFile(), Arrays.asList("**/search_popular_search_engine.story"), null);
+	}
+}

File jbehave-tutorial/src/main/java/com/hascode/tutorial/SearchPopularSearchEngineSteps.java

+package com.hascode.tutorial;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.matchers.JUnitMatchers.containsString;
+
+import org.jbehave.core.annotations.Given;
+import org.jbehave.core.annotations.Then;
+import org.jbehave.core.annotations.When;
+import org.jbehave.web.selenium.SeleniumSteps;
+
+import com.thoughtworks.selenium.Selenium;
+
+public class SearchPopularSearchEngineSteps extends SeleniumSteps {
+
+	private final String	pageLoadTimeout	= "6000";
+
+	public SearchPopularSearchEngineSteps(final Selenium selenium) {
+		super(selenium);
+	}
+
+	@Given("The Google homepage")
+	public void theGoogleHomepage() {
+		selenium.open("/");
+	}
+
+	@When("I search for \"$term\"")
+	public void searchForTerm(final String term) throws InterruptedException {
+		selenium.type("q", term);
+		selenium.click("btnG");
+		Thread.sleep(4000l); // ugly
+	}
+	@Then("the text \"$textFragment\" is present")
+	public void shouldSee(final String textFragment) {
+		assertThat(selenium.getBodyText(), containsString(textFragment));
+	}
+
+	@When("I click the link \"$linkText\"")
+	public void followLinkWithText(final String linkText) {
+		selenium.click("link=" + linkText);
+		selenium.waitForPageToLoad(pageLoadTimeout);
+	}
+
+	@Then("the page's URL should be \"$url\"")
+	public void pageURLShouldBe(final String url) {
+		assertThat(selenium.getLocation(), is(url));
+	}
+}

File jbehave-tutorial/src/main/java/com/hascode/tutorial/ShoppingCart.java

+package com.hascode.tutorial;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class ShoppingCart {
+	private final Map<User, List<Product>>	entries	= new HashMap<User, List<Product>>();
+
+	public void add(final User user, final Product product) {
+		List<Product> purchases;
+		if (entries.containsKey(user)) {
+			purchases = entries.get(user);
+		} else {
+			purchases = new ArrayList<Product>();
+		}
+		purchases.add(product);
+		entries.put(user, purchases);
+	}
+
+	public List<Product> getProductsByUser(final User user) {
+		return entries.get(user);
+	}
+
+}

File jbehave-tutorial/src/main/java/com/hascode/tutorial/User.java

+package com.hascode.tutorial;
+
+public class User {
+	private String	name;
+
+	/**
+	 * @return the name
+	 */
+	public String getName() {
+		return name;
+	}
+
+	/**
+	 * @param name
+	 *            the name to set
+	 */
+	public void setName(String name) {
+		this.name = name;
+	}
+}

File jbehave-tutorial/src/main/java/com/hascode/tutorial/UserDao.java

+package com.hascode.tutorial;
+
+import java.util.HashSet;
+import java.util.Set;
+
+public class UserDao {
+	private final Set<User>	users	= new HashSet<User>();
+
+	public void save(final User user) {
+		users.add(user);
+	}
+
+	public Set<User> findAll() {
+		return users;
+	}
+}

File jbehave-tutorial/src/main/java/com/hascode/tutorial/runner/TutorialRunnerApplication.java

+package com.hascode.tutorial.runner;
+
+import java.util.List;
+
+import org.jbehave.core.steps.CandidateSteps;
+import org.jbehave.core.steps.InstanceStepsFactory;
+import org.jbehave.web.runner.wicket.WebRunnerApplication;
+
+import com.hascode.tutorial.AddUserToRepository;
+
+public class TutorialRunnerApplication extends WebRunnerApplication {
+	@Override
+	protected List<CandidateSteps> candidateSteps() {
+		return new InstanceStepsFactory(configuration(), AddUserToRepository.class).createCandidateSteps();
+	}
+}

File jbehave-tutorial/src/main/resources/com/hascode/tutorial/add_product_to_shopping_cart.story

+Scenario: When a user adds a product to the shopping cart, the product should be included in the user's shopping cart.
+
+Given a user
+Given a shopping cart
+Given a product
+When the user adds the product to the shopping cart
+Then the product must be included in the list of the shoppingcart's entries

File jbehave-tutorial/src/main/resources/com/hascode/tutorial/add_user_to_repository.story

+Scenario: When users are added to the repository they must be found afterwards in the repository
+
+Given a user repository
+When the user adds 3 users to the user repository
+Then the user repository must contain 3 users

File jbehave-tutorial/src/main/resources/log4j.properties

+log4j.rootLogger=debug, stdout
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

File jbehave-tutorial/src/main/webapp/WEB-INF/web.xml

+<?xml version="1.0" encoding="UTF-8"?>
+
+<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
+  version="2.5">
+
+  <display-name>hasCode.com - JBehave WebRunner Example</display-name>
+
+  <servlet>
+    <servlet-name>wicket</servlet-name>
+    <servlet-class>org.apache.wicket.protocol.http.WicketServlet</servlet-class>
+    <init-param>
+      <param-name>applicationClassName</param-name>
+      <param-value>com.hascode.tutorial.runner.TutorialRunnerApplication</param-value>
+    </init-param>
+  </servlet>
+
+  <servlet-mapping>
+    <servlet-name>wicket</servlet-name>
+    <url-pattern>/*</url-pattern>
+  </servlet-mapping>
+</web-app>