Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 3 commits
  • 6 files changed
  • 0 comments
  • 1 contributor
9 README.md
Source Rendered
... ... @@ -1 +1,8 @@
1   -Modeling a card-playing session in Java
  1 +Modeling a card-playing session in Java using Apache Camel as the integration framework.
  2 +
  3 +To run, please make sure Maven is installed.
  4 +
  5 +The command line to run this solution is as follows:
  6 +
  7 +
  8 +user@computer$ mvn camel:run
7 deck-of-cards/pom.xml
@@ -8,6 +8,13 @@
8 8 <artifactId>deck-of-cards</artifactId>
9 9 <version>1.0</version>
10 10
  11 + <name>Camel Deals Cards</name>
  12 + <description>
  13 + An example solution for reading a set of names from a file (that represent players) and
  14 + dealing cards to them. The dealt cards will be stored in XML
  15 + </description>
  16 +
  17 +
11 18 <dependencies>
12 19 <dependency>
13 20 <groupId>log4j</groupId>
20 deck-of-cards/src/main/java/com/christianposta/fuse/Dealer.java
@@ -6,11 +6,7 @@
6 6 import java.util.List;
7 7
8 8 /**
9   - * Created by IntelliJ IDEA.
10   - * User: ceposta
11   - * Date: 8/24/11
12   - * Time: 11:47 AM
13   - * To change this template use File | Settings | File Templates.
  9 + * Main abstraction for 'dealing' cards to players
14 10 */
15 11 public class Dealer {
16 12 public static final int DEFAULT_NUM_CARDS_TO_DEAL = 5;
@@ -18,16 +14,30 @@
18 14 private CardShoe shoe;
19 15 private int numCardsToDeal;
20 16
  17 + /**
  18 + * Create a dealer with a default number of decks with the default
  19 + * shuffle algorithm
  20 + */
21 21 public Dealer() {
22 22 this(CardShoe.createShoeWithDefaultCards());
23 23 }
24 24
  25 + /**
  26 + * Allows more control over how the card shuffling happens, how many card decks, etc.
  27 + * @param shoe
  28 + */
25 29 public Dealer(CardShoe shoe) {
26 30 this.shoe = shoe;
27 31 this.shoe.shuffle();
28 32 this.numCardsToDeal = DEFAULT_NUM_CARDS_TO_DEAL;
29 33 }
30 34
  35 + /**
  36 + * Deals cards to a list of players. The number of cards that are dealt can be specified
  37 + * by the numCardsToDeal property
  38 + *
  39 + * @param players list of players to which cards will be dealt
  40 + */
31 41 public void dealCardsToPlayers(List<Player> players) {
32 42 int numberOfDealRounds = 0;
33 43
6 deck-of-cards/src/main/java/com/christianposta/fuse/integration/CardRoutingDefinition.java
@@ -4,6 +4,7 @@
4 4 import com.thoughtworks.xstream.XStream;
5 5 import org.apache.camel.Exchange;
6 6 import org.apache.camel.Processor;
  7 +import org.apache.camel.builder.xml.XPathBuilder;
7 8 import org.apache.camel.dataformat.xstream.XStreamDataFormat;
8 9 import org.apache.camel.spring.SpringRouteBuilder;
9 10
@@ -21,6 +22,8 @@ public void configure() throws Exception {
21 22 xStream.processAnnotations(Player.class);
22 23 XStreamDataFormat dataFormat = new XStreamDataFormat();
23 24 dataFormat.setXstream(xStream);
  25 + dataFormat.setEncoding("UTF-8");
  26 +
24 27 from("file:data/users?noop=true")
25 28 .split(body(String.class).tokenize("\n"), new PlayerAggregationStrategy())
26 29 .log("Player names: ${body}")
@@ -29,7 +32,8 @@ public void configure() throws Exception {
29 32 .log("What we have so far \"${body}\"")
30 33 .beanRef("dealer")
31 34 .split(body())
32   - .marshal(dataFormat).setHeader(Exchange.FILE_NAME, xpath("/player/name/text()"))
  35 + .marshal(dataFormat)
  36 + .setHeader(Exchange.FILE_NAME, XPathBuilder.xpath("concat(/player/name, '.xml')", String.class))
33 37 .to("file:data/cards");
34 38 }
35 39 }
14 deck-of-cards/src/main/resources/META-INF/spring/camelContext.xml
@@ -8,9 +8,21 @@
8 8 http://camel.apache.org/schema/spring
9 9 http://camel.apache.org/schema/spring/camel-spring.xsd">
10 10
  11 +
  12 + <!--
  13 + Set the properties on the dealer, one of which can be the Shoe from which to deal cards.
  14 + Note, by injecting the shoe (optional), you can control how many decks of cards are involved,
  15 + the shuffling strategy, etc. See CardShoe.java for more info.
  16 + -->
  17 + <bean id="dealer" class="com.christianposta.fuse.Dealer">
  18 + <property name="numCardsToDeal" value="5" />
  19 + </bean>
  20 +
  21 +
  22 + <!-- This is where the routing definitions are written, in the camel DSL language (java) -->
11 23 <bean id="routingBean" class="com.christianposta.fuse.integration.CardRoutingDefinition" />
12   - <bean id="dealer" class="com.christianposta.fuse.Dealer" />
13 24
  25 + <!-- Instantiate the context for the camel container -->
14 26 <camel:camelContext>
15 27 <camel:routeBuilder ref="routingBean" />
16 28 </camel:camelContext>
15 ...hristianposta/fuse/integration/DealCardsTest.java → ...ianposta/fuse/integration/DealCardsCamelTest.java
@@ -4,12 +4,14 @@
4 4 import com.thoughtworks.xstream.XStream;
5 5 import org.apache.camel.Exchange;
6 6 import org.apache.camel.builder.RouteBuilder;
  7 +import org.apache.camel.builder.xml.XPathBuilder;
7 8 import org.apache.camel.component.mock.MockEndpoint;
8 9 import org.apache.camel.dataformat.xstream.XStreamDataFormat;
9 10 import org.apache.camel.test.junit4.CamelSpringTestSupport;
10 11 import org.junit.Test;
11 12 import org.springframework.context.support.AbstractApplicationContext;
12 13 import org.springframework.context.support.ClassPathXmlApplicationContext;
  14 +import static org.apache.camel.builder.xml.XPathBuilder.xpath;
13 15
14 16 import java.util.List;
15 17
@@ -20,7 +22,7 @@
20 22 * Time: 8:53 AM
21 23 * To change this template use File | Settings | File Templates.
22 24 */
23   -public class DealCardsTest extends CamelSpringTestSupport{
  25 +public class DealCardsCamelTest extends CamelSpringTestSupport{
24 26 @Override
25 27 protected AbstractApplicationContext createApplicationContext() {
26 28 return new ClassPathXmlApplicationContext("/spring/deal-cards-context.xml");
@@ -36,6 +38,7 @@ public void configure() throws Exception {
36 38 xStream.processAnnotations(Player.class);
37 39 XStreamDataFormat dataFormat = new XStreamDataFormat();
38 40 dataFormat.setXstream(xStream);
  41 + dataFormat.setEncoding("UTF-8");
39 42
40 43 from("direct:file")
41 44 .split(body(String.class).tokenize("\n"), new PlayerAggregationStrategy())
@@ -47,7 +50,10 @@ public void configure() throws Exception {
47 50 .beanRef("dealer")
48 51 .to("mock:players")
49 52 .split(body())
50   - .marshal(dataFormat).to("stream:out");
  53 + .marshal(dataFormat)
  54 + .setHeader(Exchange.FILE_NAME, XPathBuilder.xpath("concat(/player/name, '.xml')", String.class))
  55 + .to("mock:files")
  56 + .to("stream:out");
51 57
52 58
53 59 }
@@ -74,6 +80,11 @@ public void run() {
74 80 splitMockEndpoint.expectedMessageCount(2);
75 81 splitMockEndpoint.allMessages().body(Player.class);
76 82
  83 + MockEndpoint filesEndpoint = getMockEndpoint("mock:files");
  84 + filesEndpoint.expectedMessageCount(2);
  85 + filesEndpoint.message(0).header(Exchange.FILE_NAME).isEqualTo("christian.xml");
  86 + filesEndpoint.message(1).header(Exchange.FILE_NAME).isEqualTo("todd.xml");
  87 +
77 88 template.sendBody("direct:file", "christian\ntodd");
78 89
79 90 assertMockEndpointsSatisfied();

No commit comments for this range

Something went wrong with that request. Please try again.