1
+ /*
2
+ *Copyright [2024] [The Original Author]
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 flipkart .tef .bizlogics ;
18
+
19
+ import com .google .inject .AbstractModule ;
20
+ import com .google .inject .Guice ;
21
+ import com .google .inject .Injector ;
22
+ import com .google .inject .internal .BytecodeGen ;
23
+ import com .google .inject .matcher .Matchers ;
24
+ import flipkart .tef .annotations .EmitData ;
25
+ import org .aopalliance .intercept .MethodInterceptor ;
26
+ import org .aopalliance .intercept .MethodInvocation ;
27
+ import org .junit .Test ;
28
+
29
+ import static org .junit .Assert .assertEquals ;
30
+ import static org .junit .Assert .assertTrue ;
31
+
32
+ public class DataAdapterBizlogicTest {
33
+
34
+ @ EmitData (name = "testData" )
35
+ static class TestDataAdapterBizlogic extends DataAdapterBizlogic <Object > {
36
+ @ Override
37
+ public Object adapt (TefContext tefContext ) {
38
+ return null ;
39
+ }
40
+ }
41
+
42
+ static class TestDataAdapterBizlogic1 extends DataAdapterBizlogic <Object > {
43
+ @ Override
44
+ public Object adapt (TefContext tefContext ) {
45
+ return null ;
46
+ }
47
+ }
48
+
49
+ @ Test
50
+ public void testGetEmittedDataName () {
51
+ //setup
52
+ Class <? extends DataAdapterBizlogic > clazz = TestDataAdapterBizlogic .class ;
53
+
54
+ //test
55
+ String emittedDataName = DataAdapterBizlogic .getEmittedDataName (clazz );
56
+
57
+ //validate
58
+ assertEquals ("testData" , emittedDataName );
59
+ }
60
+
61
+ @ Test
62
+ public void testGetEmittedDataNameForAnnotationAbsence () {
63
+ //setup
64
+ Class <? extends DataAdapterBizlogic > clazz = TestDataAdapterBizlogic1 .class ;
65
+
66
+ //test
67
+ String emittedDataName = DataAdapterBizlogic .getEmittedDataName (clazz );
68
+
69
+ //validate
70
+ assertEquals ("" , emittedDataName );
71
+ }
72
+
73
+ @ Test
74
+ public void testGetEmittedDataNameWithGuiceProxy () {
75
+ // setup
76
+ Injector injector = Guice .createInjector (new GuiceModule ());
77
+ TestDataAdapterBizlogic dataAdapterBizlogic = injector .getInstance (TestDataAdapterBizlogic .class );
78
+
79
+ // test
80
+ String emittedDataName = DataAdapterBizlogic .getEmittedDataName (dataAdapterBizlogic .getClass ());
81
+
82
+ // validate
83
+ assertTrue (dataAdapterBizlogic .getClass ().getName ().contains (BytecodeGen .ENHANCER_BY_GUICE_MARKER ));
84
+ assertEquals ("testData" , emittedDataName );
85
+ }
86
+
87
+ class GuiceModule extends AbstractModule {
88
+ @ Override
89
+ protected void configure () {
90
+ bindInterceptor (
91
+ Matchers .subclassesOf (TestDataAdapterBizlogic .class ),
92
+ Matchers .any (),
93
+ new CustomInterceptor ()
94
+ );
95
+ }
96
+ }
97
+
98
+ public class CustomInterceptor implements MethodInterceptor {
99
+ @ Override
100
+ public Object invoke (MethodInvocation invocation ) throws Throwable {
101
+ // Proceed with the actual method invocation
102
+ return invocation .proceed ();
103
+ }
104
+ }
105
+ }
0 commit comments