|
| 1 | +/** |
| 2 | + * Copyright 2009-2016 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 | +package org.apache.ibatis.mapping; |
| 17 | + |
| 18 | +import org.apache.ibatis.builder.InitializingObject; |
| 19 | +import org.apache.ibatis.cache.Cache; |
| 20 | +import org.apache.ibatis.cache.CacheException; |
| 21 | +import org.apache.ibatis.cache.impl.PerpetualCache; |
| 22 | +import org.hamcrest.core.Is; |
| 23 | +import org.junit.Assert; |
| 24 | +import org.junit.Rule; |
| 25 | +import org.junit.Test; |
| 26 | +import org.junit.rules.ExpectedException; |
| 27 | + |
| 28 | +import java.lang.reflect.Field; |
| 29 | + |
| 30 | +public class CacheBuilderTest { |
| 31 | + |
| 32 | + @Rule |
| 33 | + public ExpectedException expectedException = ExpectedException.none(); |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testInitializing() throws Exception { |
| 37 | + InitializingCache cache = unwrap(new CacheBuilder("test").implementation(InitializingCache.class).build()); |
| 38 | + |
| 39 | + Assert.assertThat(cache.initialized, Is.is(true)); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testInitializingFailure() throws Exception { |
| 44 | + expectedException.expect(CacheException.class); |
| 45 | + expectedException.expectMessage("Failed cache initialization for 'test' on 'org.apache.ibatis.mapping.CacheBuilderTest$InitializingFailureCache'"); |
| 46 | + |
| 47 | + new CacheBuilder("test").implementation(InitializingFailureCache.class).build(); |
| 48 | + } |
| 49 | + |
| 50 | + @SuppressWarnings("unchecked") |
| 51 | + private <T> T unwrap(Cache cache){ |
| 52 | + Field field; |
| 53 | + try { |
| 54 | + field = cache.getClass().getDeclaredField("delegate"); |
| 55 | + } catch (NoSuchFieldException e) { |
| 56 | + throw new IllegalStateException(e); |
| 57 | + } |
| 58 | + try { |
| 59 | + field.setAccessible(true); |
| 60 | + return (T)field.get(cache); |
| 61 | + } catch (IllegalAccessException e) { |
| 62 | + throw new IllegalStateException(e); |
| 63 | + } finally { |
| 64 | + field.setAccessible(false); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + private static class InitializingCache extends PerpetualCache implements InitializingObject { |
| 70 | + |
| 71 | + private boolean initialized; |
| 72 | + |
| 73 | + public InitializingCache(String id) { |
| 74 | + super(id); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void initialize() { |
| 79 | + this.initialized = true; |
| 80 | + } |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + private static class InitializingFailureCache extends PerpetualCache implements InitializingObject { |
| 85 | + |
| 86 | + public InitializingFailureCache(String id) { |
| 87 | + super(id); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void initialize() throws Exception { |
| 92 | + throw new IllegalStateException("error"); |
| 93 | + } |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments