1 package org.unitedfront2.test;
2
3 import java.lang.reflect.Method;
4
5 import org.junit.Assert;
6 import org.junit.Test;
7 import org.unitedfront2.dao.SimpleFindable;
8 import org.unitedfront2.domain.Domain;
9 import org.unitedfront2.domain.SimpleTableModule;
10 import org.unitedfront2.util.PropertyUtils;
11
12 /**
13 * <code>exist</code>, <code>find</code>, <code>get</code>,
14 * <code>prototype</code> メソッドのテストケースを持った、{@link SimpleTableModule}
15 * 用の抽象テストケースです。
16 *
17 * @author kurokkie
18 *
19 * @param <D> ドメイン
20 */
21 public abstract class TableModuleTestCaseSupport<D extends Domain>
22 extends TransactionalTestCaseWithInitialData {
23
24 /** ID のプロパティ名のデフォルト (id) */
25 public static final String DEFAULT_PRIMARY_KEY_PROPERTY_NAME = "id";
26
27 @Test
28 public void testFindFound() throws Exception {
29 D expected = createDomain();
30 store(expected);
31 D actual = getTableModule().find((Integer)
32 PropertyUtils.getProperty(expected, getPrimaryKeyPropertyName()));
33 Assert.assertEquals(expected, actual);
34 }
35
36 @Test
37 public void testFindNotFound() {
38 D actual = getTableModule().find(-1);
39 Assert.assertNull(actual);
40 }
41
42 @Test
43 public void testGetFound() throws Exception {
44 D expected = createDomain();
45 store(expected);
46 D actual = getTableModule().get((Integer)
47 PropertyUtils.getProperty(expected, getPrimaryKeyPropertyName()));
48 Assert.assertEquals(expected, actual);
49 }
50
51 @Test(expected = IllegalArgumentException.class)
52 public void testGetNotFound() {
53 getTableModule().get(-1);
54 }
55
56 @Test
57 public void testExistExist() throws Exception {
58 D expected = createDomain();
59 store(expected);
60 Assert.assertTrue(getTableModule().exist((Integer)
61 PropertyUtils.getProperty(expected, getPrimaryKeyPropertyName())));
62 }
63
64 @Test
65 public void testExistNotExist() {
66 Assert.assertFalse(getTableModule().exist(-1));
67 }
68
69 /**
70 * 主キーとなるプロパティ名を取得します。デフォルトでは
71 * {@link #DEFAULT_PRIMARY_KEY_PROPERTY_NAME} を返します。必要に応じてオーバーライドして
72 * ください。
73 *
74 * @return 主キーとなるプロパティ名
75 */
76 protected String getPrimaryKeyPropertyName() {
77 return DEFAULT_PRIMARY_KEY_PROPERTY_NAME;
78 }
79
80 /**
81 * テストデータを設定済みのドメインオブジェクトを生成します。
82 *
83 * @return テストデータを設定済みのドメインオブジェクト
84 */
85 protected abstract D createDomain();
86
87 /**
88 * テスト対象となる {@link SimpleTableModule} を取得します。
89 *
90 * @return {@link SimpleTableModule}
91 */
92 protected abstract SimpleTableModule<D> getTableModule();
93
94 /**
95 * データアクセスオブジェクトを取得します。ドメインのパッケージ内では、Domains#getSimpleDao() を
96 * 使うことができます。
97 *
98 * @return {@link SimpleFindable}
99 */
100 protected abstract SimpleFindable<D> getSimpleDao();
101
102 protected void store(D domain) throws Exception {
103 if (logger.isDebugEnabled()) {
104 logger.debug("The domain is '" + domain + "'");
105 }
106 Method save = domain.getClass().getMethod("store");
107 if (logger.isInfoEnabled()) {
108 logger.info("Call save method.");
109 }
110 save.invoke(domain);
111 }
112 }