|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package io.microsphere.mybatis.plugin; |
| 18 | + |
| 19 | +import io.microsphere.mybatis.test.executor.LoggingExecutor; |
| 20 | +import org.apache.ibatis.executor.Executor; |
| 21 | +import org.apache.ibatis.executor.parameter.ParameterHandler; |
| 22 | +import org.apache.ibatis.executor.resultset.ResultSetHandler; |
| 23 | +import org.apache.ibatis.executor.statement.StatementHandler; |
| 24 | +import org.apache.ibatis.plugin.Interceptor; |
| 25 | +import org.apache.ibatis.plugin.Intercepts; |
| 26 | +import org.apache.ibatis.plugin.Invocation; |
| 27 | +import org.apache.ibatis.plugin.Plugin; |
| 28 | +import org.apache.ibatis.plugin.PluginException; |
| 29 | +import org.apache.ibatis.plugin.Signature; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | + |
| 32 | +import java.lang.reflect.Method; |
| 33 | +import java.sql.SQLException; |
| 34 | +import java.util.Map; |
| 35 | +import java.util.Set; |
| 36 | + |
| 37 | +import static io.microsphere.mybatis.plugin.Plugins.TARGET_CLASSES; |
| 38 | +import static io.microsphere.mybatis.plugin.Plugins.TARGET_CLASSES_SIZE; |
| 39 | +import static io.microsphere.mybatis.plugin.Plugins.getPlugin; |
| 40 | +import static io.microsphere.mybatis.plugin.Plugins.getSignatureMap; |
| 41 | +import static org.apache.ibatis.plugin.Plugin.wrap; |
| 42 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 43 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 44 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 45 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 46 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 47 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 48 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 49 | + |
| 50 | +/** |
| 51 | + * {@link Plugins} Test |
| 52 | + * |
| 53 | + * @author <a href="mailto:mercyblitz@gmail.com">Mercy<a/> |
| 54 | + * @see Plugins |
| 55 | + * @since 1.0.0 |
| 56 | + */ |
| 57 | +public class PluginsTest { |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testTargetClasses() { |
| 61 | + assertEquals(4, TARGET_CLASSES.size()); |
| 62 | + assertEquals(TARGET_CLASSES_SIZE, TARGET_CLASSES.size()); |
| 63 | + assertTrue(TARGET_CLASSES.contains(Executor.class)); |
| 64 | + assertTrue(TARGET_CLASSES.contains(ParameterHandler.class)); |
| 65 | + assertTrue(TARGET_CLASSES.contains(ResultSetHandler.class)); |
| 66 | + assertTrue(TARGET_CLASSES.contains(StatementHandler.class)); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testGetSignatureMap() { |
| 71 | + Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(new TestAnnotatedExecutorInterceptor()); |
| 72 | + assertEquals(1, signatureMap.size()); |
| 73 | + assertTrue(signatureMap.containsKey(Executor.class)); |
| 74 | + assertFalse(signatureMap.containsKey(ParameterHandler.class)); |
| 75 | + assertFalse(signatureMap.containsKey(ResultSetHandler.class)); |
| 76 | + assertFalse(signatureMap.containsKey(StatementHandler.class)); |
| 77 | + Set<Method> methods = signatureMap.get(Executor.class); |
| 78 | + assertEquals(3, methods.size()); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testGetSignatureMapOnFailed() { |
| 83 | + assertThrows(PluginException.class, () -> getSignatureMap(new NoOpInterceptor())); |
| 84 | + assertThrows(PluginException.class, () -> getSignatureMap(new WrongTargetSignatureInterceptor())); |
| 85 | + assertThrows(PluginException.class, () -> getSignatureMap(new WrongMethodSignatureInterceptor())); |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testGetPlugin() throws SQLException { |
| 91 | + Executor executor = newExecutor(); |
| 92 | + Object proxy = wrap(executor, new TestAnnotatedExecutorInterceptor()); |
| 93 | + assertTrue(proxy instanceof Executor); |
| 94 | + |
| 95 | + Plugin plugin = getPlugin(proxy); |
| 96 | + assertNotNull(plugin); |
| 97 | + |
| 98 | + Executor proxyExecutor = (Executor) proxy; |
| 99 | + assertEquals(0, proxyExecutor.update(null, null)); |
| 100 | + |
| 101 | + // No Proxy |
| 102 | + proxy = wrap(executor, new SignatureInterceptor()); |
| 103 | + assertSame(executor, proxy); |
| 104 | + plugin = getPlugin(proxy); |
| 105 | + assertNull(plugin); |
| 106 | + } |
| 107 | + |
| 108 | + private Executor newExecutor() { |
| 109 | + return new LoggingExecutor(); |
| 110 | + } |
| 111 | + |
| 112 | + @Intercepts( |
| 113 | + value = {} |
| 114 | + ) |
| 115 | + static class SignatureInterceptor implements Interceptor { |
| 116 | + @Override |
| 117 | + public Object intercept(Invocation invocation) throws Throwable { |
| 118 | + return invocation.proceed(); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + @Intercepts( |
| 124 | + @Signature( |
| 125 | + type = Object.class, |
| 126 | + method = "equals", |
| 127 | + args = {Object.class} |
| 128 | + ) |
| 129 | + ) |
| 130 | + static class WrongTargetSignatureInterceptor implements Interceptor { |
| 131 | + @Override |
| 132 | + public Object intercept(Invocation invocation) throws Throwable { |
| 133 | + return invocation.proceed(); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @Intercepts( |
| 138 | + @Signature( |
| 139 | + type = Executor.class, |
| 140 | + method = "methodNotFound", |
| 141 | + args = {} |
| 142 | + ) |
| 143 | + ) |
| 144 | + static class WrongMethodSignatureInterceptor implements Interceptor { |
| 145 | + @Override |
| 146 | + public Object intercept(Invocation invocation) throws Throwable { |
| 147 | + return invocation.proceed(); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments