001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.persistence.callbacks;
020:
021: import javax.persistence.Entity;
022: import javax.persistence.Id;
023: import javax.persistence.PostLoad;
024: import javax.persistence.PrePersist;
025: import javax.persistence.PreUpdate;
026: import javax.persistence.Version;
027: import javax.persistence.PreRemove;
028: import javax.persistence.PostRemove;
029: import javax.persistence.PostUpdate;
030: import javax.persistence.PostPersist;
031: import javax.persistence.GeneratedValue;
032: import javax.persistence.Transient;
033:
034: @Entity
035: public class ExceptionsFromCallbacksEntity {
036: @Id
037: @GeneratedValue
038: private long id;
039: @Version
040: private int version;
041: @Transient
042: private boolean throwOnPrePersist;
043: @Transient
044: private boolean throwOnPostPersist;
045: @Transient
046: private boolean throwOnPreUpdate;
047: @Transient
048: private boolean throwOnPostUpdate;
049: private boolean throwOnPostLoad;
050: @Transient
051: private boolean throwOnPreRemove;
052: @Transient
053: private boolean throwOnPostRemove;
054: private String stringField;
055:
056: public void setThrowOnPrePersist(boolean b) {
057: throwOnPrePersist = b;
058: }
059:
060: public void setThrowOnPostPersist(boolean b) {
061: throwOnPostPersist = b;
062: }
063:
064: public void setThrowOnPreUpdate(boolean b) {
065: throwOnPreUpdate = b;
066: }
067:
068: public void setThrowOnPostUpdate(boolean b) {
069: throwOnPostUpdate = b;
070: }
071:
072: public void setThrowOnPostLoad(boolean b) {
073: throwOnPostLoad = b;
074: }
075:
076: public void setThrowOnPreRemove(boolean b) {
077: throwOnPreRemove = b;
078: }
079:
080: public void setThrowOnPostRemove(boolean b) {
081: throwOnPostRemove = b;
082: }
083:
084: public void setStringField(String s) {
085: stringField = s;
086: }
087:
088: @PrePersist
089: public void prePersist() {
090: if (throwOnPrePersist)
091: throw new CallbackTestException();
092: }
093:
094: @PostPersist
095: public void postPersist() {
096: if (throwOnPostPersist)
097: throw new CallbackTestException();
098: }
099:
100: @PostLoad
101: public void postLoad() {
102: if (throwOnPostLoad && isInvokedFromTestMethod())
103: throw new CallbackTestException();
104: }
105:
106: private boolean isInvokedFromTestMethod() {
107: return TestExceptionsFromCallbacks.testRunning;
108: }
109:
110: @PreUpdate
111: public void preUpdate() {
112: if (throwOnPreUpdate)
113: throw new CallbackTestException();
114: }
115:
116: @PostUpdate
117: public void postUpdate() {
118: if (throwOnPostUpdate)
119: throw new CallbackTestException();
120: }
121:
122: @PreRemove
123: public void preRemove() {
124: if (throwOnPreRemove && isInvokedFromTestMethod())
125: throw new CallbackTestException();
126: }
127:
128: @PostRemove
129: public void postRemove() {
130: if (throwOnPostRemove && isInvokedFromTestMethod())
131: throw new CallbackTestException();
132: }
133:
134: public Object getId() {
135: return id;
136: }
137:
138: public class CallbackTestException extends RuntimeException {
139: }
140: }
|