1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package javax.jdo;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import javax.jdo.spi.PersistenceCapable;
import javax.jdo.util.AbstractTest;
import javax.jdo.util.BatchTestRunner;
/*
* ObjectStateTest.java
* This test class verifies the proper returned ObjectState for
* each life cycle state. See Table 3 in section 7.4.
*
* @since 2.1
*/
public class ObjectStateTest extends AbstractTest {
static final int PERSISTENT = 1;
static final int TRANSACTIONAL = 2;
static final int DIRTY = 4;
static final int NEW = 8;
static final int DELETED = 16;
static final int DETACHED = 32;
private static final Method jdoIsPersistent = getDeclaredMethod(
PersistenceCapable.class,
"jdoIsPersistent",
null);
private static final Method jdoIsTransactional = getDeclaredMethod(
PersistenceCapable.class,
"jdoIsTransactional",
null);
private static final Method jdoIsDirty = getDeclaredMethod(
PersistenceCapable.class,
"jdoIsDirty",
null);
private static final Method jdoIsNew = getDeclaredMethod(
PersistenceCapable.class,
"jdoIsNew",
null);
private static final Method jdoIsDeleted = getDeclaredMethod(
PersistenceCapable.class,
"jdoIsDeleted",
null);
private static final Method jdoIsDetached = getDeclaredMethod(
PersistenceCapable.class,
"jdoIsDetached",
null);
/** */
public static void main(String args[]) {
BatchTestRunner.run(ObjectStateTest.class);
}
public void testNull() {
PersistenceCapable mock = null;
assertObjectState("null", null, mock);
}
public void testTransient() {
PersistenceCapable mock = newMock(0);
assertObjectState("transient", ObjectState.TRANSIENT, mock);
}
public void testTransientClean() {
PersistenceCapable mock = newMock(TRANSACTIONAL);
assertObjectState("transient-clean", ObjectState.TRANSIENT_CLEAN, mock);
}
public void testTransientDirty() {
PersistenceCapable mock = newMock(TRANSACTIONAL+DIRTY);
assertObjectState("transient-dirty", ObjectState.TRANSIENT_DIRTY, mock);
}
public void testPersistentNew() {
PersistenceCapable mock = newMock(PERSISTENT+TRANSACTIONAL+NEW+DIRTY);
assertObjectState("persistent-new", ObjectState.PERSISTENT_NEW, mock);
}
public void testPersistentNontransactional() {
PersistenceCapable mock = newMock(PERSISTENT);
assertObjectState("persistent-nontransactional", ObjectState.HOLLOW_PERSISTENT_NONTRANSACTIONAL, mock);
}
public void testPersistentNontransactionalDirty() {
PersistenceCapable mock = newMock(PERSISTENT+DIRTY);
assertObjectState("persistent-nontransactional-dirty", ObjectState.PERSISTENT_NONTRANSACTIONAL_DIRTY, mock);
}
public void testPersistentClean() {
PersistenceCapable mock = newMock(PERSISTENT+TRANSACTIONAL);
assertObjectState("persistent-clean", ObjectState.PERSISTENT_CLEAN, mock);
}
public void testPersistentDirty() {
PersistenceCapable mock = newMock(PERSISTENT+TRANSACTIONAL+DIRTY);
assertObjectState("persistent-dirty", ObjectState.PERSISTENT_DIRTY, mock);
}
public void testPersistentDeleted() {
PersistenceCapable mock = newMock(PERSISTENT+TRANSACTIONAL+DIRTY+DELETED);
assertObjectState("persistent-deleted", ObjectState.PERSISTENT_DELETED, mock);
}
public void testPersistentNewDeleted() {
PersistenceCapable mock = newMock(PERSISTENT+TRANSACTIONAL+NEW+DIRTY+DELETED);
assertObjectState("persistent-new-deleted", ObjectState.PERSISTENT_NEW_DELETED, mock);
}
public void testDetachedClean() {
PersistenceCapable mock = newMock(DETACHED);
assertObjectState("detached-clean", ObjectState.DETACHED_CLEAN, mock);
}
public void testDetachedDirty() {
PersistenceCapable mock = newMock(DETACHED+DIRTY);
assertObjectState("detached-dirty", ObjectState.DETACHED_DIRTY, mock);
}
private void assertObjectState(String string,
ObjectState expected,
PersistenceCapable pc) {
ObjectState actual = JDOHelper.getObjectState(pc);
// test for == here because enums should be singleton
if (actual == expected)
return;
fail("ObjectState failure for " + string + NL +
"expected: " + expected +
", actual: " + actual);
}
/**
* Construct a new mock instance of PersistenceCapable that responds
* to methods jdoIsXXX by returning the bit value of its constructor
* masked with the value of XXX.
* @param i the sum of bit masks representing the life cycle state
* @return a mock instance of PersistenceCapable
*/
private PersistenceCapable newMock(final int i) {
return (PersistenceCapable)
Proxy.newProxyInstance(
PersistenceCapable.class.getClassLoader(),
new Class[] {PersistenceCapable.class},
new MockInvocationHandler(i));
}
private class MockInvocationHandler implements InvocationHandler {
/** States is the sum of all life cycle interrogatives.
*/
private int states;
/**
* Constructs an invocation handler with the specified bit fields set
* according to the sum of values of PERSISTENT, TRANSACTIONAL, DIRTY,
* NEW, DELETED, and DETACHED.
* @param i the bit field values for the life cycle interrogatives
*/
private MockInvocationHandler(int i) {
states = i;
}
/**
*
* @param object the PersistenceCapable instance
* @param method the method being invoked
* @param parameters parameters (should be null)
* @throws java.lang.Throwable unused
* @return for jdoIsXXX methods only, returns whether the
* bit field selected by the method is set in the
* mock handler
*/
public Object invoke(Object object, Method method, Object[] parameters)
throws Throwable {
if (method.equals(jdoIsPersistent)) {
return (0 != (states & PERSISTENT));
}
if (method.equals(jdoIsTransactional)) {
return (0 != (states & TRANSACTIONAL));
}
if (method.equals(jdoIsDirty)) {
return (0 != (states & DIRTY));
}
if (method.equals(jdoIsNew)) {
return (0 != (states & NEW));
}
if (method.equals(jdoIsDeleted)) {
return (0 != (states & DELETED));
}
if (method.equals(jdoIsDetached)) {
return (0 != (states & DETACHED));
}
fail("Unexpected method called: " + method.getName());
return Boolean.FALSE; // not reached
}
}
private static Method getDeclaredMethod
(Class clazz, String methodName, Class[] parameters) {
try {
@SuppressWarnings("unchecked")
Method result = clazz.getDeclaredMethod(methodName, parameters);
return result;
} catch (Exception ex) {
// human-readable class.methodName(parameter[,parameter])
StringBuffer sb = new StringBuffer(methodName);
String delimiter = "(";
for (Class parameter: parameters) {
sb.append(delimiter);
sb.append(parameter.getName());
delimiter = ",";
}
sb.append(")");
throw new RuntimeException
("getDeclaredMethod for " + clazz.getName() + "." +
sb + " threw..." + ex);
}
}
}
|