|
| 1 | +/* |
| 2 | + * Copyright 2002-2007 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.dao.annotation; |
| 18 | + |
| 19 | +import javax.persistence.PersistenceException; |
| 20 | + |
| 21 | +import junit.framework.TestCase; |
| 22 | + |
| 23 | +import org.springframework.aop.framework.ProxyFactory; |
| 24 | +import org.springframework.dao.DataAccessException; |
| 25 | +import org.springframework.dao.InvalidDataAccessApiUsageException; |
| 26 | +import org.springframework.dao.support.DataAccessUtilsTests.MapPersistenceExceptionTranslator; |
| 27 | +import org.springframework.dao.support.PersistenceExceptionTranslator; |
| 28 | +import org.springframework.stereotype.Repository; |
| 29 | + |
| 30 | +/** |
| 31 | + * Tests for PersistenceExceptionTranslationAdvisor's exception translation, as applied by |
| 32 | + * PersistenceExceptionTranslationPostProcessor. |
| 33 | + * |
| 34 | + * @author Rod Johnson |
| 35 | + * @author Juergen Hoeller |
| 36 | + */ |
| 37 | +public class PersistenceExceptionTranslationAdvisorTests extends TestCase { |
| 38 | + |
| 39 | + private RuntimeException doNotTranslate = new RuntimeException(); |
| 40 | + |
| 41 | + private PersistenceException persistenceException1 = new PersistenceException(); |
| 42 | + |
| 43 | + protected RepositoryInterface createProxy(RepositoryInterfaceImpl target) { |
| 44 | + MapPersistenceExceptionTranslator mpet = new MapPersistenceExceptionTranslator(); |
| 45 | + mpet.addTranslation(persistenceException1, new InvalidDataAccessApiUsageException("", persistenceException1)); |
| 46 | + ProxyFactory pf = new ProxyFactory(target); |
| 47 | + pf.addInterface(RepositoryInterface.class); |
| 48 | + addPersistenceExceptionTranslation(pf, mpet); |
| 49 | + return (RepositoryInterface) pf.getProxy(); |
| 50 | + } |
| 51 | + |
| 52 | + protected void addPersistenceExceptionTranslation(ProxyFactory pf, PersistenceExceptionTranslator pet) { |
| 53 | + pf.addAdvisor(new PersistenceExceptionTranslationAdvisor(pet, Repository.class)); |
| 54 | + } |
| 55 | + |
| 56 | + public void testNoTranslationNeeded() { |
| 57 | + RepositoryInterfaceImpl target = new RepositoryInterfaceImpl(); |
| 58 | + RepositoryInterface ri = createProxy(target); |
| 59 | + |
| 60 | + ri.noThrowsClause(); |
| 61 | + ri.throwsPersistenceException(); |
| 62 | + |
| 63 | + target.setBehavior(persistenceException1); |
| 64 | + try { |
| 65 | + ri.noThrowsClause(); |
| 66 | + fail(); |
| 67 | + } |
| 68 | + catch (RuntimeException ex) { |
| 69 | + assertSame(persistenceException1, ex); |
| 70 | + } |
| 71 | + try { |
| 72 | + ri.throwsPersistenceException(); |
| 73 | + fail(); |
| 74 | + } |
| 75 | + catch (RuntimeException ex) { |
| 76 | + assertSame(persistenceException1, ex); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public void testTranslationNotNeededForTheseExceptions() { |
| 81 | + RepositoryInterfaceImpl target = new StereotypedRepositoryInterfaceImpl(); |
| 82 | + RepositoryInterface ri = createProxy(target); |
| 83 | + |
| 84 | + ri.noThrowsClause(); |
| 85 | + ri.throwsPersistenceException(); |
| 86 | + |
| 87 | + target.setBehavior(doNotTranslate); |
| 88 | + try { |
| 89 | + ri.noThrowsClause(); |
| 90 | + fail(); |
| 91 | + } |
| 92 | + catch (RuntimeException ex) { |
| 93 | + assertSame(doNotTranslate, ex); |
| 94 | + } |
| 95 | + try { |
| 96 | + ri.throwsPersistenceException(); |
| 97 | + fail(); |
| 98 | + } |
| 99 | + catch (RuntimeException ex) { |
| 100 | + assertSame(doNotTranslate, ex); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + public void testTranslationNeededForTheseExceptions() { |
| 105 | + doTestTranslationNeededForTheseExceptions(new StereotypedRepositoryInterfaceImpl()); |
| 106 | + } |
| 107 | + |
| 108 | + public void testTranslationNeededForTheseExceptionsOnSuperclass() { |
| 109 | + doTestTranslationNeededForTheseExceptions(new MyStereotypedRepositoryInterfaceImpl()); |
| 110 | + } |
| 111 | + |
| 112 | + public void testTranslationNeededForTheseExceptionsOnInterface() { |
| 113 | + doTestTranslationNeededForTheseExceptions(new MyInterfaceStereotypedRepositoryInterfaceImpl()); |
| 114 | + } |
| 115 | + |
| 116 | + public void testTranslationNeededForTheseExceptionsOnInheritedInterface() { |
| 117 | + doTestTranslationNeededForTheseExceptions(new MyInterfaceInheritedStereotypedRepositoryInterfaceImpl()); |
| 118 | + } |
| 119 | + |
| 120 | + private void doTestTranslationNeededForTheseExceptions(RepositoryInterfaceImpl target) { |
| 121 | + RepositoryInterface ri = createProxy(target); |
| 122 | + |
| 123 | + target.setBehavior(persistenceException1); |
| 124 | + try { |
| 125 | + ri.noThrowsClause(); |
| 126 | + fail(); |
| 127 | + } |
| 128 | + catch (DataAccessException ex) { |
| 129 | + // Expected |
| 130 | + assertSame(persistenceException1, ex.getCause()); |
| 131 | + } |
| 132 | + catch (PersistenceException ex) { |
| 133 | + fail("Should have been translated"); |
| 134 | + } |
| 135 | + |
| 136 | + try { |
| 137 | + ri.throwsPersistenceException(); |
| 138 | + fail(); |
| 139 | + } |
| 140 | + catch (PersistenceException ex) { |
| 141 | + assertSame(persistenceException1, ex); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + public interface RepositoryInterface { |
| 146 | + |
| 147 | + void noThrowsClause(); |
| 148 | + |
| 149 | + void throwsPersistenceException() throws PersistenceException; |
| 150 | + } |
| 151 | + |
| 152 | + public static class RepositoryInterfaceImpl implements RepositoryInterface { |
| 153 | + |
| 154 | + private RuntimeException runtimeException; |
| 155 | + |
| 156 | + public void setBehavior(RuntimeException rex) { |
| 157 | + this.runtimeException = rex; |
| 158 | + } |
| 159 | + |
| 160 | + public void noThrowsClause() { |
| 161 | + if (runtimeException != null) { |
| 162 | + throw runtimeException; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + public void throwsPersistenceException() throws PersistenceException { |
| 167 | + if (runtimeException != null) { |
| 168 | + throw runtimeException; |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + @Repository |
| 174 | + public static class StereotypedRepositoryInterfaceImpl extends RepositoryInterfaceImpl { |
| 175 | + // Extends above class just to add repository annotation |
| 176 | + } |
| 177 | + |
| 178 | + public static class MyStereotypedRepositoryInterfaceImpl extends StereotypedRepositoryInterfaceImpl { |
| 179 | + |
| 180 | + } |
| 181 | + |
| 182 | + @Repository |
| 183 | + public interface StereotypedInterface { |
| 184 | + |
| 185 | + } |
| 186 | + |
| 187 | + public static class MyInterfaceStereotypedRepositoryInterfaceImpl extends RepositoryInterfaceImpl |
| 188 | + implements StereotypedInterface { |
| 189 | + |
| 190 | + } |
| 191 | + |
| 192 | + public interface StereotypedInheritingInterface extends StereotypedInterface { |
| 193 | + |
| 194 | + } |
| 195 | + |
| 196 | + public static class MyInterfaceInheritedStereotypedRepositoryInterfaceImpl extends RepositoryInterfaceImpl |
| 197 | + implements StereotypedInheritingInterface { |
| 198 | + |
| 199 | + } |
| 200 | + |
| 201 | +} |
0 commit comments