|
| 1 | +/* |
| 2 | + * Copyright 2004-2010 the Seasar Foundation and the Others. |
| 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, |
| 13 | + * either express or implied. See the License for the specific language |
| 14 | + * governing permissions and limitations under the License. |
| 15 | + */ |
| 16 | +package org.seasar.doma.internal; |
| 17 | + |
| 18 | +import java.util.function.Supplier; |
| 19 | + |
| 20 | +import javax.sql.DataSource; |
| 21 | + |
| 22 | +import junit.framework.TestCase; |
| 23 | + |
| 24 | +import org.seasar.doma.jdbc.Config; |
| 25 | +import org.seasar.doma.jdbc.ConfigException; |
| 26 | +import org.seasar.doma.jdbc.SimpleDataSource; |
| 27 | +import org.seasar.doma.jdbc.dialect.Dialect; |
| 28 | +import org.seasar.doma.jdbc.dialect.StandardDialect; |
| 29 | +import org.seasar.doma.jdbc.entity.EntityListener; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author backpaper0 |
| 33 | + * |
| 34 | + */ |
| 35 | +public class RuntimeConfigTest extends TestCase { |
| 36 | + |
| 37 | + public void testGetEntityListener() throws Exception { |
| 38 | + Config originalConfig = new MockConfig() { |
| 39 | + |
| 40 | + @Override |
| 41 | + public <ENTITY, LISTENER extends EntityListener<ENTITY>> LISTENER getEntityListener( |
| 42 | + Class<LISTENER> listenerClass, |
| 43 | + Supplier<LISTENER> listenerSupplier) { |
| 44 | + return listenerSupplier.get(); |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + RuntimeConfig runtimeConfig = new RuntimeConfig(originalConfig); |
| 49 | + |
| 50 | + MockEntityListener entityListener = runtimeConfig.getEntityListener( |
| 51 | + MockEntityListener.class, MockEntityListener::new); |
| 52 | + assertNotNull(entityListener); |
| 53 | + } |
| 54 | + |
| 55 | + public void testGetEntityListenerNullCheck() throws Exception { |
| 56 | + Config originalConfig = new MockConfig() { |
| 57 | + |
| 58 | + @Override |
| 59 | + public <ENTITY, LISTENER extends EntityListener<ENTITY>> LISTENER getEntityListener( |
| 60 | + Class<LISTENER> listenerClass, |
| 61 | + Supplier<LISTENER> listenerSupplier) { |
| 62 | + return null; |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + RuntimeConfig runtimeConfig = new RuntimeConfig(originalConfig); |
| 67 | + |
| 68 | + try { |
| 69 | + runtimeConfig.getEntityListener(MockEntityListener.class, |
| 70 | + MockEntityListener::new); |
| 71 | + fail(); |
| 72 | + } catch (ConfigException e) { |
| 73 | + assertEquals(originalConfig.getClass().getName(), e.getClassName()); |
| 74 | + assertEquals("getEntityListener", e.getMethodName()); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private interface MockConfig extends Config { |
| 79 | + |
| 80 | + @Override |
| 81 | + default Dialect getDialect() { |
| 82 | + return new StandardDialect(); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + default DataSource getDataSource() { |
| 87 | + return new SimpleDataSource(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private static class MockEntityListener implements EntityListener<Object> { |
| 92 | + } |
| 93 | +} |
0 commit comments