您的位置:软件测试 > 开源软件测试 > 开源单元测试工具 >
插入式持久性提供者的承诺:Kodo、OpenJPA和Hibernate
作者:网络转载 发布时间:[ 2013/2/27 14:47:53 ] 推荐标签:

因为我们使用了O-R映射注释表示Message类的实例与数据库表和列之间的映射关系(或者说是哪个域值能够作为Message实例的主标识符),所以JPA依赖关系已经移入这个Java类中。这些映射信息可以移到一个单独的orm.xml中,从而使这个类恢复Pure Old Java Object (POJO)状态并完善这种不明确的更接近域对象模型的方法。
编码前的测试

JUnit测试用例通过JNDI查找与服务进行交互并检验两个服务方法返回的是否为预期的结果。

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下一页
软件测试工具 | 联系我们 | 投诉建议 | 诚聘英才 | 申请使用列表 | 网站地图
沪ICP备07036474 2003-2017 版权所有 上海泽众软件科技有限公司 Shanghai ZeZhong Software Co.,Ltd