Permalink
Browse files

Align with the Cucumber book

  • Loading branch information...
1 parent 81d3e44 commit 2c6e62480d5f8a885f90003cfd2e7259576f68ba @aslakhellesoy aslakhellesoy committed Oct 11, 2012
View
9 examples/spring-txn/src/main/java/cucumber/examples/spring/txn/BreakfastRepository.java
@@ -1,9 +0,0 @@
-package cucumber.examples.spring.txn;
-
-import java.util.List;
-
-public interface BreakfastRepository {
- void save(List<Breakfast> breakfasts);
-
- List<Breakfast> findAll();
-}
View
20 examples/spring-txn/src/main/java/cucumber/examples/spring/txn/JpaMessageRepository.java
@@ -0,0 +1,20 @@
+package cucumber.examples.spring.txn;
+
+import org.springframework.stereotype.Repository;
+import org.springframework.transaction.annotation.Transactional;
+
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import java.util.List;
+
+@Repository
+public class JpaMessageRepository implements MessageRepository {
+ @PersistenceContext
+ private EntityManager entityManager;
+
+ @Transactional
+ @Override
+ public void save(Message message) {
+ entityManager.persist(message);
+ }
+}
View
12 ...es/spring/txn/JpaBreakfastRepository.java → ...xamples/spring/txn/JpaUserRepository.java
@@ -8,20 +8,18 @@
import java.util.List;
@Repository
-public class JpaBreakfastRepository implements BreakfastRepository {
+public class JpaUserRepository implements UserRepository {
@PersistenceContext
private EntityManager entityManager;
@Transactional
@Override
- public void save(List<Breakfast> breakfasts) {
- for (Breakfast breakfast : breakfasts) {
- entityManager.persist(breakfast);
- }
+ public void save(User user) {
+ entityManager.persist(user);
}
@Override
- public List<Breakfast> findAll() {
- return entityManager.createQuery("SELECT b FROM Breakfast b", Breakfast.class).getResultList();
+ public List<User> findAll() {
+ return entityManager.createQuery("SELECT b FROM User b", User.class).getResultList();
}
}
View
24 examples/spring-txn/src/main/java/cucumber/examples/spring/txn/Message.java
@@ -0,0 +1,24 @@
+package cucumber.examples.spring.txn;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.ManyToOne;
+
+@Entity
+public class Message {
+ @SuppressWarnings("unused")
+ @Id
+ @GeneratedValue(strategy = GenerationType.AUTO)
+ private Long id;
+
+ @ManyToOne
+ private User author;
+
+ private String content;
+
+ public void setAuthor(User author) {
+ this.author = author;
+ }
+}
View
5 examples/spring-txn/src/main/java/cucumber/examples/spring/txn/MessageRepository.java
@@ -0,0 +1,5 @@
+package cucumber.examples.spring.txn;
+
+public interface MessageRepository {
+ void save(Message message);
+}
View
9 ...cumber/examples/spring/txn/Breakfast.java → ...va/cucumber/examples/spring/txn/User.java
@@ -6,12 +6,15 @@
import javax.persistence.Id;
@Entity
-public class Breakfast {
+public class User {
@SuppressWarnings("unused")
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
- private String name;
- private int cukes;
+ private String username;
+
+ public void setUsername(String username) {
+ this.username = username;
+ }
}
View
9 examples/spring-txn/src/main/java/cucumber/examples/spring/txn/UserRepository.java
@@ -0,0 +1,9 @@
+package cucumber.examples.spring.txn;
+
+import java.util.List;
+
+public interface UserRepository {
+ void save(User user);
+
+ List<User> findAll();
+}
View
24 examples/spring-txn/src/test/java/cucumber/examples/spring/txn/BellyStepdefs.java
@@ -1,24 +0,0 @@
-package cucumber.examples.spring.txn;
-
-import cucumber.api.java.en.Given;
-import cucumber.api.java.en.Then;
-import org.springframework.beans.factory.annotation.Autowired;
-
-import java.util.List;
-
-import static org.junit.Assert.assertEquals;
-
-public class BellyStepdefs {
- @Autowired
- private BreakfastRepository breakfastRepository;
-
- @Given("^we had this for breakfast:$")
- public void we_had_this_for_breakfast(List<Breakfast> breakfasts) throws Throwable {
- breakfastRepository.save(breakfasts);
- }
-
- @Then("^the total number of breakfasts should be (\\d+)$")
- public void the_total_number_of_breakfasts_should_be(int total) throws Throwable {
- assertEquals(total, breakfastRepository.findAll().size());
- }
-}
View
31 examples/spring-txn/src/test/java/cucumber/examples/spring/txn/UserStepdefs.java
@@ -0,0 +1,31 @@
+package cucumber.examples.spring.txn;
+
+import cucumber.api.java.en.Given;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import java.util.List;
+
+public class UserStepdefs {
+ @Autowired
+ private UserRepository userRepository;
+
+ @Autowired
+ private MessageRepository messageRepository;
+
+ private User user;
+
+ public void thereIsAuser() {
+ user = new User();
+ user.setUsername("testuser");
+ userRepository.save(user);
+ }
+
+ @Given("^a User has posted the following messages:$")
+ public void a_User_has_posted_the_following_messages(List<Message> messages) throws Throwable {
+ thereIsAuser();
+ for (Message m : messages) {
+ m.setAuthor(user);
+ messageRepository.save(m);
+ }
+ }
+}
View
19 examples/spring-txn/src/test/resources/cucumber/examples/spring/txn/clean_slates.feature
@@ -1,19 +0,0 @@
-# The @txn tag enables a Transaction open-rollback around each Scenario,
-# Preventing persisted data from leaking between Scenarios.
-# Try removing the @txn tag and see what happens.
-@txn
-Feature: Clean slates
-
- Scenario: Gaia and Aslak have cukes for breakfast
- Given we had this for breakfast:
- | name | cukes |
- | Gaia | 3 |
- | Aslak | 8 |
- Then the total number of breakfasts should be 2
-
- Scenario: Gaia and Patty have cukes for breakfast
- Given we had this for breakfast:
- | name | cukes |
- | Gaia | 9 |
- | Aslak | 80 |
- Then the total number of breakfasts should be 2
View
18 examples/spring-txn/src/test/resources/cucumber/examples/spring/txn/search.feature
@@ -0,0 +1,18 @@
+# The @txn tag enables a Transaction open-rollback around each Scenario,
+# Preventing persisted data from leaking between Scenarios.
+# Try removing the @txn tag and see what happens.
+@txn
+Feature: Search
+ Scenario: Find messages by content
+ Given a User has posted the following messages:
+ | content |
+ | I am making dinner |
+ | I just woke up |
+ | I am going to work |
+
+ Scenario: Find messages by content using auto-search
+ Given a User has posted the following messages:
+ | content |
+ | I am making dinner |
+ | I just woke up |
+ | I am going to work |
View
2 examples/spring-txn/src/test/resources/jdbc.properties
@@ -1,5 +1,5 @@
database.driver=org.hsqldb.jdbcDriver
-database.url=jdbc:hsqldb:mem:breakfast
+database.url=jdbc:hsqldb:mem:user
database.user=sa
database.password=

0 comments on commit 2c6e624

Please sign in to comment.