|
| 1 | +/** |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 3 | + * the License. You may obtain a copy of the License at |
| 4 | + * |
| 5 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 8 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 9 | + * specific language governing permissions and limitations under the License. |
| 10 | + * |
| 11 | + * Copyright 2012-2016 the original author or authors. |
| 12 | + */ |
| 13 | +package org.assertj.db.api; |
| 14 | + |
| 15 | +import org.assertj.core.api.ObjectArrayAssert; |
| 16 | +import org.assertj.core.internal.cglib.proxy.MethodInterceptor; |
| 17 | +import org.assertj.core.internal.cglib.proxy.MethodProxy; |
| 18 | +import org.assertj.core.util.Arrays; |
| 19 | +import org.assertj.db.type.Change; |
| 20 | +import org.assertj.db.type.Column; |
| 21 | +import org.assertj.db.type.Row; |
| 22 | +import org.assertj.db.type.Value; |
| 23 | + |
| 24 | +import java.lang.reflect.Array; |
| 25 | +import java.lang.reflect.Method; |
| 26 | +import java.lang.reflect.ParameterizedType; |
| 27 | +import java.lang.reflect.Type; |
| 28 | + |
| 29 | +import static org.assertj.db.util.Proxies.isProxified; |
| 30 | +import static org.assertj.db.util.Proxies.unProxy; |
| 31 | + |
| 32 | +/** |
| 33 | + * Method interceptor that proxify result of assertions methods. |
| 34 | + * Useful for navigation assertion methods like ( column(...) , row(...) ). |
| 35 | + * |
| 36 | + * @author Julien Roy |
| 37 | + */ |
| 38 | +class ProxifyPositionResult implements MethodInterceptor { |
| 39 | + private final SoftProxies proxies; |
| 40 | + |
| 41 | + ProxifyPositionResult(SoftProxies proxies) { |
| 42 | + this.proxies = proxies; |
| 43 | + } |
| 44 | + |
| 45 | + public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { |
| 46 | + Object result = proxy.invokeSuper(obj, args); |
| 47 | + if (isProxified(result.getClass()) || actual(result) == null) { |
| 48 | + return result; |
| 49 | + } |
| 50 | + return this.proxies.create(result.getClass(), actualClass(result), actual(result)); |
| 51 | + } |
| 52 | + |
| 53 | + private static Class[] actualClass(Object result) { |
| 54 | + |
| 55 | + if (result instanceof AbstractColumnAssert) { |
| 56 | + return Arrays.array( |
| 57 | + unProxy(((AbstractColumnAssert) result).origin.getClass()), |
| 58 | + Column.class |
| 59 | + ); |
| 60 | + } else if (result instanceof AbstractRowAssert) { |
| 61 | + return Arrays.array( |
| 62 | + unProxy(((AbstractRowAssert) result).origin.getClass()), |
| 63 | + Row.class |
| 64 | + ); |
| 65 | + } else if (result instanceof AbstractValueAssert) { |
| 66 | + return Arrays.array( |
| 67 | + unProxy(((AbstractValueAssert) result).origin.getClass()), |
| 68 | + Value.class |
| 69 | + ); |
| 70 | + } else if (result instanceof ChangeAssert) { |
| 71 | + return Arrays.array( |
| 72 | + unProxy(((ChangeAssert) result).origin.getClass()), |
| 73 | + Change.class |
| 74 | + ); |
| 75 | + } else if (result instanceof ChangeColumnAssert) { |
| 76 | + return Arrays.array( |
| 77 | + unProxy(((ChangeColumnAssert) result).origin.getClass()), |
| 78 | + String.class, |
| 79 | + Value.class, |
| 80 | + Value.class |
| 81 | + ); |
| 82 | + } else if (result instanceof ObjectArrayAssert) { |
| 83 | + return Arrays.array(Array.newInstance(Object.class, 0).getClass()); |
| 84 | + } else { |
| 85 | + Type actualType = ((ParameterizedType) result.getClass().getGenericSuperclass()).getActualTypeArguments()[1]; |
| 86 | + Class type = actualType instanceof ParameterizedType ? |
| 87 | + (Class) ((ParameterizedType) actualType).getRawType() : |
| 88 | + (Class) actualType; |
| 89 | + return Arrays.array(type); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private static Object[] actual(Object result) { |
| 94 | + if (result instanceof AbstractColumnAssert) { |
| 95 | + return Arrays.array( |
| 96 | + ((AbstractColumnAssert) result).origin, |
| 97 | + ((AbstractColumnAssert) result).column |
| 98 | + ); |
| 99 | + } else if (result instanceof AbstractRowAssert) { |
| 100 | + return Arrays.array( |
| 101 | + ((AbstractRowAssert) result).origin, |
| 102 | + ((AbstractRowAssert) result).row |
| 103 | + ); |
| 104 | + } else if (result instanceof AbstractValueAssert) { |
| 105 | + return Arrays.array( |
| 106 | + ((AbstractValueAssert) result).origin, |
| 107 | + ((AbstractValueAssert) result).value |
| 108 | + ); |
| 109 | + } else if (result instanceof ChangeAssert) { |
| 110 | + return Arrays.array( |
| 111 | + ((ChangeAssert) result).origin, |
| 112 | + ((ChangeAssert) result).change |
| 113 | + ); |
| 114 | + } else if (result instanceof ChangeColumnAssert) { |
| 115 | + return Arrays.array( |
| 116 | + ((ChangeColumnAssert) result).origin, |
| 117 | + ((ChangeColumnAssert) result).columnName, |
| 118 | + ((ChangeColumnAssert) result).valueAtStartPoint, |
| 119 | + ((ChangeColumnAssert) result).valueAtEndPoint |
| 120 | + ); |
| 121 | + } else { |
| 122 | + return null; |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments