2 Copyright 2010 Sun Microsystems, Inc.
3 All rights reserved. Use is subject to license terms.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; version 2 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 package testsuite.clusterj;
21 import testsuite.clusterj.model.CrazyDelegate;
22 import testsuite.clusterj.model.ThrowNullPointerException;
25 * This test verifies the behavior of the core implementation to provide for
26 * loadable DomainTypeHandlerFactory implementations. This is the way to
27 * support abstract classes or concrete classes as domain types.
29 * If a file META-INF/services/com.mysql.clusterj.spi.DomainTypeHandlerFactory
30 * exists that names a loadable class, the core implementation should use it
31 * preferentially to load a DomainTypeHandler for an unregistered domain type.
33 * This test includes an implementation of DomainTypeHandlerFactory, called
34 * CrazyDomainTypeHandlerFactoryImpl, that has three modes of operation:
35 * <ol><li> if the simple domain class name begins with "Throw", then an
36 * exception is thrown immediately. This should cause clusterj to save the
37 * exception information and proceed to the next factory. The test will succeed
38 * when the standard implementation does not accept the domain class as a
40 * </li><li> if the simple domain class name is "CrazyDelegate", then an
41 * implementation of DomainTypeHandlerFactory is returned. The returned
42 * implementation doesn't immediately throw an exception but throws an
43 * exception when the method getProxyClass is called. This exception will be
44 * reported to clusterj and the test succeeds.
45 * </li><li> in all other cases, null is returned. This should cause clusterj
46 * to go to the next prospective factory. This case is exercised by every test
47 * case that includes the CrazyDomainTypeHandlerFactoryImpl in the services
48 * file (i.e. all other test cases).
51 public class DomainTypeHandlerFactoryTest extends AbstractClusterJModelTest {
54 public void localSetUp() {
55 createSessionFactory();
56 session = sessionFactory.getSession();
59 public void testThrowNullPointerException() {
61 ThrowNullPointerException instance = new ThrowNullPointerException();
62 // This should cause CrazyDomainTypeHandlerFactoryImpl to throw
63 // a NullPointerException which should be ignored. But the standard
64 // implementation doesn't like ThrowNullPointerException either,
65 // so it should throw an exception. But the error message should
66 // contain the original NullPointerException that was thrown by
67 // CrazyDomainTypeHandlerFactoryImpl.
68 session.makePersistent(instance);
69 error("Failed to catch RuntimeException");
70 } catch (RuntimeException e) {
71 if (!e.getMessage().contains("java.lang.NullPointerException")) {
72 error("Failed to catch correct exception, but caught: " + e.toString());
79 public void testCrazyDelegate() {
81 CrazyDelegate instance = new CrazyDelegate();
82 // This should fail at CrazyDomainTypeHandlerFactoryImpl.getProxyClass()
83 // It's a bit of a cheat since it relies on the internal implementation
84 // of SessionFactoryImpl.getDomainTypeHandler
85 session.makePersistent(instance);
86 error("Failed to catch UnsupportedOperationException");
87 } catch (UnsupportedOperationException ex) {
88 String message = ex.getMessage();
89 Throwable cause = ex.getCause();
90 String causeMessage = cause==null?"null":cause.getMessage();
91 if (!message.contains("Nice Job!")) {
92 error("Failed to catch correct exception, but caught: " + message + "; cause: " + causeMessage);