瀏覽代碼

Added CargoService

jorgen_falk 19 年之前
父節點
當前提交
34a1072170

+ 9
- 0
dddsample/src/main/java/se/citerus/dddsample/service/CargoService.java 查看文件

@@ -0,0 +1,9 @@
1
+package se.citerus.dddsample.service;
2
+
3
+import se.citerus.dddsample.domain.Cargo;
4
+
5
+public interface CargoService {
6
+
7
+  Cargo find(String string);
8
+
9
+}

+ 32
- 0
dddsample/src/main/java/se/citerus/dddsample/service/CargoServiceImpl.java 查看文件

@@ -0,0 +1,32 @@
1
+package se.citerus.dddsample.service;
2
+
3
+import se.citerus.dddsample.domain.Cargo;
4
+import se.citerus.dddsample.domain.CargoRepository;
5
+import se.citerus.dddsample.domain.TrackingId;
6
+
7
+public class CargoServiceImpl implements CargoService {
8
+  private CargoRepository repository;
9
+  
10
+  /**
11
+   * Finds a Cargo based on given trackingId
12
+   * 
13
+   * @return null if no Cargo is found
14
+   * 
15
+   */
16
+  public Cargo find(String trackingId) {
17
+    return getCargoRepository().find(new TrackingId(trackingId));
18
+  }
19
+
20
+  
21
+
22
+  public CargoRepository getCargoRepository() {
23
+    return repository;
24
+  }
25
+
26
+
27
+  public void setCargoRepository(CargoRepository repository) {
28
+    this.repository = repository;
29
+  }
30
+
31
+  
32
+}

+ 18
- 15
dddsample/src/test/java/applicationContext.xml 查看文件

@@ -5,7 +5,7 @@
5 5
 
6 6
 	<!-- 
7 7
 		Properties
8
-	 -->
8
+	-->
9 9
 	<bean id="propertyConfigurer"
10 10
 		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
11 11
 		<property name="locations">
@@ -18,32 +18,35 @@
18 18
 
19 19
 	<!-- 
20 20
 		Beans
21
-	 -->
21
+	-->
22 22
 	<bean id="cargoRepository" class="se.citerus.dddsample.repository.CargoRepositoryInMem">
23 23
 	</bean>
24 24
 
25
+	<bean id="cargoService" class="se.citerus.dddsample.service.CargoServiceImpl">
26
+		<property name="cargoRepository" ref="cargoRepository" />
27
+	</bean>
25 28
 
26 29
 
27
-<!-- 
28
-	<bean id="testDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
30
+	<!-- 
31
+		<bean id="testDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
29 32
 		<property name="driverClassName" value="${jdbc.driverClassName}" />
30 33
 		<property name="url" value="${jdbc.url}" />
31 34
 		<property name="username" value="${jdbc.username}" />
32 35
 		<property name="password" value="${jdbc.password}" />
33
-	</bean>
34
-	
35
-	<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
36
+		</bean>
37
+		
38
+		<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
36 39
 		<property name="dataSource" ref="testataSource" />
37 40
 		<property name="mappingResources">
38
-			<list>
39
-				<value>cargo.hbm.xml</value>
40
-			</list>
41
+		<list>
42
+		<value>cargo.hbm.xml</value>
43
+		</list>
41 44
 		</property>
42 45
 		<property name="hibernateProperties">
43
-			<value>
44
-				hibernate.dialect=org.hibernate.dialect.HSQLDialect
45
-			</value>
46
+		<value>
47
+		hibernate.dialect=org.hibernate.dialect.HSQLDialect
48
+		</value>
46 49
 		</property>
47
-	</bean>
48
- -->
50
+		</bean>
51
+	-->
49 52
 </beans>

+ 3
- 18
dddsample/src/test/java/se/citerus/dddsample/domain/CargoRepositoryTest.java 查看文件

@@ -1,17 +1,12 @@
1 1
 package se.citerus.dddsample.domain;
2 2
 
3
-import org.springframework.context.ApplicationContext;
4
-import org.springframework.context.support.ClassPathXmlApplicationContext;
5
-
6 3
 import junit.framework.TestCase;
4
+import se.citerus.dddsample.repository.CargoRepositoryInMem;
7 5
 
8 6
 public class CargoRepositoryTest extends TestCase {
9 7
 
10
-  private static final String CTX = "applicationContext.xml";
11
-
12
-  public void testFindByCargoId() {
13
-
14
-    CargoRepository repository = getCargoRepository();
8
+  public void testFindByCargoId() throws Exception {
9
+    CargoRepository repository = new CargoRepositoryInMem();
15 10
     final TrackingId trackingId = new TrackingId("XYZ");
16 11
     Cargo cargo = repository.find(trackingId);
17 12
 
@@ -19,14 +14,4 @@ public class CargoRepositoryTest extends TestCase {
19 14
 
20 15
   }
21 16
 
22
-  private CargoRepository getCargoRepository() {
23
-    return (CargoRepository) getBean("cargoRepository");
24
-  }
25
-
26
-  private Object getBean(String bean) {
27
-    ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { CTX });
28
-
29
-    return context.getBean(bean);
30
-  }
31
-
32 17
 }

+ 32
- 0
dddsample/src/test/java/se/citerus/dddsample/service/CargoServiceTest.java 查看文件

@@ -0,0 +1,32 @@
1
+package se.citerus.dddsample.service;
2
+
3
+import junit.framework.TestCase;
4
+
5
+import org.springframework.context.ApplicationContext;
6
+import org.springframework.context.support.ClassPathXmlApplicationContext;
7
+
8
+import se.citerus.dddsample.domain.Cargo;
9
+import se.citerus.dddsample.domain.TrackingId;
10
+
11
+
12
+public class CargoServiceTest extends TestCase {
13
+
14
+  public void testCargoServiceFindByTrackingIdScenario() throws Exception {
15
+    CargoService service = getCargoService();
16
+    
17
+    Cargo cargo = service.find("XYZ");
18
+    
19
+    assertEquals(new TrackingId("XYZ"), cargo.trackingId());
20
+  }
21
+  
22
+
23
+  private CargoService getCargoService() {
24
+    return (CargoService) getBean("cargoService");
25
+  }
26
+
27
+  private Object getBean(String bean) {
28
+    ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
29
+
30
+    return context.getBean(bean);
31
+  }
32
+}