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 }
testProvider()测试方法检验这个服务是否为JPA提供者的包含提供者逻辑名的 EntityManager的实现返回了一个类名。另一个测试方法检验服务返回的Message实例是否拥有提供者分配的正确身份。
构建JPA应用程序和切换提供者的Ant 脚本
在运行代码之前,最后一步是使用 Ant 编写的 构建脚本。
文章来源于领测软件测试网 https://www.ltesting.net/