您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 >
插入式持久性提供者的承諾:Kodo、OpenJPA和Hibernate
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2013/2/27 14:47:53 ] 推薦標簽:

因為我們使用了O-R映射注釋表示Message類的實例與數(shù)據(jù)庫表和列之間的映射關(guān)系(或者說是哪個域值能夠作為Message實例的主標識符),所以JPA依賴關(guān)系已經(jīng)移入這個Java類中。這些映射信息可以移到一個單獨的orm.xml中,從而使這個類恢復Pure Old Java Object (POJO)狀態(tài)并完善這種不明確的更接近域?qū)ο竽P偷姆椒ā?br /> 編碼前的測試

JUnit測試用例通過JNDI查找與服務進行交互并檢驗兩個服務方法返回的是否為預期的結(jié)果。

TestJPAService.java

01 package junit;
02
03 import java.util.Properties;
04
05 import javax.naming.Context;
06 import javax.naming.InitialContext;
07
08 import service.JPAService;
09 import service.Message;
10 import junit.framework.TestCase;
11
12 /**
13  * A JUnit test case to verify that a Session Bean is using the correct provider.
14  *
15  * The test must be invoked with two mandatory Java System Properties:
16  * <li>
                       
jndi.name : the JNDI name of the registered Session Bean.
17  * <li>
                       
persistence.provider: The 
                       
logical name of the
18  * persistence provider. Allowed values are 
                       
kodo or 
                       
hibernate.
19  *
                       


20  * The optional Java System Properties are:
21  * <li>
                       
weblogic.user : The authenticated user to contact Weblogic server. Defaults to 
                       
weblogic
22  * <li>
                       
weblogic.password : The valid password to contact Weblogic server. Defaults to 
                       
weblogic
23  * <li>
                       
weblogic.url : The URL where Weblogic Server is running. Defaults to 
                       
t3://localhost:7001
24  *
25   *
26  * @author ppoddar
27  *
28  */
29 public class TestJPAService extends TestCase {
30   private static final String USER = System.getProperty("weblogic.user",     "weblogic");
31   private static final String PWD  = System.getProperty("weblogic.password", "weblogic");
32   private static final String URL  = System.getProperty("weblogic.url",    "t3://localhost:7001");
33   private static final String JNDI_NAME = System.getProperty("jndi.name");
34   private static final String PERSISTENCE_PROVIDER = System.getProperty("persistence.provider");
35  
36   private static JPAService service;
37  
38   /**
39    * Sets up the test by contacting Weblogic Server and looking up in JNDI
40    * for the registered Session Bean.
41    *
42    */
43   @Override
44   public void setUp() throws Exception {
45     assertNotNull("Must specify JVM System property -Djndi.name=<jndi> to run this test", JNDI_NAME);
46     assertNotNull("Must specify JVM System property -Dpersistence.provider=[kodo|hibernate] to run this test", PERSISTENCE_PROVIDER);
47     if (service == null) {
48           Properties p = new Properties();
49           p.put(Context.INITIAL_CONTEXT_FACTORY,  "weblogic.jndi.WLInitialContextFactory");
50           p.put(Context.SECURITY_PRINCIPAL,       USER);
51           p.put(Context.SECURITY_CREDENTIALS,     PWD);
52           p.put(Context.PROVIDER_URL, URL);
53           System.out.println("Contacting server " + URL + " as " + USER + " for " + JNDI_NAME);
54           InitialContext ctx = new InitialContext(p);
55           service = (JPAService)ctx.lookup(JNDI_NAME);
56     }
57   }
58  
59   /**
60    * Asserts that the Persistence Provider class name contains the 
                       
logical
61    * name of the intended provider i.e. 
                       
kodo or 
                       
hibernate.
62    *
63    */
64   public void testProvider() {
65     String actual = service.getProvider();
66     System.err.println("Logical Persistence Provider is [" + PERSISTENCE_PROVIDER + "]");
67     System.err.println("Actual  Persistence Provider is [" + actual + "]");
68     assertTrue("*** ERROR: " + actual + " is not provided by " + PERSISTENCE_PROVIDER, actual.indexOf(PERSISTENCE_PROVIDER) != -1);
69   }
70  
71   /**
72    * Simply logs (persists) a message via the remote service.
73    * The service will affix a timestamp with the persisted message it returns.
74    * Verifies that the timestamp against the time when the message has been
75    * sent. 
76    *
77    */
78   public void testLog() {
79     long senderTime = System.currentTimeMillis();
80     String body = "A message sent for logging on " + senderTime;
81     Message message = service.log(body);
82     long createTime = message.getTimestamp().getTime();
83     long elapsedTime = createTime - senderTime;
84     System.err.println("Persisted Message [id:" + message.getId() + " timestamp:" +
85         message.getTimestamp().getTime() + " body:"+ message.getBody() + "]");
86     System.err.println("Time elapsed between the message to send and persisted is " + elapsedTime + "ms");
87     assertTrue(elapsedTime>=0);
88   }
89 }

上一頁123456下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd