1   package org.unitedfront2.test;
2   
3   import org.junit.Before;
4   import org.springframework.beans.factory.annotation.Autowired;
5   import org.springframework.context.ApplicationContext;
6   import org.unitedfront2.domain.Account;
7   import org.unitedfront2.domain.Installer;
8   import org.unitedfront2.domain.MailAddrUsedByOtherException;
9   import org.unitedfront2.domain.SimpleUser;
10  import org.unitedfront2.domain.SimpleUserTable;
11  import org.unitedfront2.domain.UserCodeUsedByOtherException;
12  import org.unitedfront2.domain.UserNameUsedByOtherException;
13  import org.unitedfront2.domain.Account.Role;
14  import org.unitedfront2.domain.Account.Status;
15  
16  /**
17   * いくつかのテスト用データを事前に生成するトランザクションを伴うテストケースです。
18   *
19   * @author kurokkie
20   *
21   */
22  public abstract class TransactionalTestCaseWithInitialData
23      extends TransactionalTestCase {
24  
25      /** デフォルトの発行するパスワードの最小文字列長 (4) */
26      public static final int DEFAULT_PASSWORD_MIN_LENGTH = 4;
27  
28      /** デフォルトの発行するパスワードの最大文字列長 (8) */
29      public static final int DEFAULT_PASSWORD_MAX_LENGTH = 8;
30  
31      /** アカウント 1 */
32      protected Account account1;
33  
34      /** ユーザ 1 */
35      protected SimpleUser simpleUser1;
36  
37      /** アカウント 2 */
38      protected Account account2;
39  
40      /** ユーザ 2 */
41      protected SimpleUser simpleUser2;
42  
43      /** アカウント 3 */
44      protected Account account3;
45  
46      /** ユーザ 3 */
47      protected SimpleUser simpleUser3;
48  
49      /** ユーザテーブルモジュール */
50      @Autowired private SimpleUserTable simpleUserTable;
51  
52      /** アプリケーションコンテキスト */
53      @Autowired private ApplicationContext applicationContext;
54  
55      @Before
56      public void onSetUpInTransaction() throws Exception {
57          account1 = new Account("tester1@unitedfront2.org", "password",
58                  Status.AVAILABLE, Role.ROLE_USER);
59          account1.encrypt();
60          account1 = domainFactory.prototype(account1);
61          simpleUser1 = domainFactory.prototype(new SimpleUser(null, "tester1",
62                  "テスター1"));
63          register(account1, simpleUser1);
64  
65          account2 = new Account("tester2@unitedfront2.org", "password",
66                  Status.AVAILABLE, Role.ROLE_USER);
67          account2.encrypt();
68          account2 = domainFactory.prototype(account2);
69          simpleUser2 = domainFactory.prototype(new SimpleUser(null, "tester2",
70                  "テスター2"));
71          register(account2, simpleUser2);
72  
73          account3 = new Account("tester3@unitedfront2.org", "password",
74                  Status.AVAILABLE, Role.ROLE_USER);
75          account3.encrypt();
76          account3 = domainFactory.prototype(account3);
77          simpleUser3 = domainFactory.prototype(new SimpleUser(null, "tester3",
78                  "テスター3"));
79          register(account3, simpleUser3);
80      }
81  
82      private void register(Account account, SimpleUser user)
83          throws MailAddrUsedByOtherException, UserCodeUsedByOtherException,
84          UserNameUsedByOtherException {
85  
86          String plainPassword = Account.createRandomPassword(
87                  DEFAULT_PASSWORD_MIN_LENGTH, DEFAULT_PASSWORD_MAX_LENGTH);
88          account.setPlainPassword(plainPassword);
89          account.addRole(Account.Role.ROLE_USER);
90          account.setStatus(Account.Status.AVAILABLE);
91          account.encrypt();
92          account.store();
93  
94          user.setId(account.getId());
95          simpleUserTable.install(user);
96      }
97  
98      protected void install() {
99          for (Object o
100                 : applicationContext.getBeansOfType(Installer.class).values()) {
101             Installer i = (Installer) o;
102             i.install(simpleUser1.getId());
103             i.install(simpleUser2.getId());
104             i.install(simpleUser3.getId());
105         }
106     }
107 }