11/*
2- * Copyright 2009-2023 the original author or authors.
2+ * Copyright 2009-2024 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1616package org .apache .ibatis .plugin ;
1717
1818import static org .junit .jupiter .api .Assertions .assertEquals ;
19- import static org .junit .jupiter .api .Assertions .assertNotEquals ;
19+ import static org .junit .jupiter .api .Assertions .fail ;
2020
21+ import java .io .Reader ;
22+ import java .sql .Connection ;
2123import java .util .HashMap ;
2224import java .util .Map ;
2325
26+ import org .apache .ibatis .BaseDataTest ;
27+ import org .apache .ibatis .executor .statement .StatementHandler ;
28+ import org .apache .ibatis .io .Resources ;
29+ import org .apache .ibatis .session .SqlSession ;
30+ import org .apache .ibatis .session .SqlSessionFactory ;
31+ import org .apache .ibatis .session .SqlSessionFactoryBuilder ;
32+ import org .junit .jupiter .api .BeforeAll ;
2433import org .junit .jupiter .api .Test ;
2534
2635class PluginTest {
2736
37+ private static SqlSessionFactory sqlSessionFactory ;
38+
39+ @ BeforeAll
40+ static void setUp () throws Exception {
41+ try (Reader reader = Resources .getResourceAsReader ("org/apache/ibatis/plugin/mybatis-config.xml" )) {
42+ sqlSessionFactory = new SqlSessionFactoryBuilder ().build (reader );
43+ }
44+ BaseDataTest .runScript (sqlSessionFactory .getConfiguration ().getEnvironment ().getDataSource (),
45+ "org/apache/ibatis/plugin/CreateDB.sql" );
46+ }
47+
2848 @ Test
29- void mapPluginShouldInterceptGet () {
30- Map map = new HashMap ();
31- map = (Map ) new AlwaysMapPlugin ().plugin (map );
32- assertEquals ("Always" , map .get ("Anything" ));
49+ void shouldPluginSwitchSchema () {
50+ try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
51+ Mapper mapper = sqlSession .getMapper (Mapper .class );
52+ assertEquals ("Public user 1" , mapper .selectNameById (1 ));
53+ }
54+
55+ SchemaHolder .set ("MYSCHEMA" );
56+
57+ try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
58+ Mapper mapper = sqlSession .getMapper (Mapper .class );
59+ assertEquals ("Private user 1" , mapper .selectNameById (1 ));
60+ }
61+ }
62+
63+ static class SchemaHolder {
64+ private static ThreadLocal <String > value = ThreadLocal .withInitial (() -> "PUBLIC" );
65+
66+ public static void set (String tenantName ) {
67+ value .set (tenantName );
68+ }
69+
70+ public static String get () {
71+ return value .get ();
72+ }
73+ }
74+
75+ @ Intercepts (@ Signature (type = StatementHandler .class , method = "prepare" , args = { Connection .class , Integer .class }))
76+ public static class SwitchCatalogInterceptor implements Interceptor {
77+ @ Override
78+ public Object intercept (Invocation invocation ) throws Throwable {
79+ Object [] args = invocation .getArgs ();
80+ Connection con = (Connection ) args [0 ];
81+ con .setSchema (SchemaHolder .get ());
82+ return invocation .proceed ();
83+ }
3384 }
3485
3586 @ Test
36- void shouldNotInterceptToString () {
37- Map map = new HashMap ();
38- map = (Map ) new AlwaysMapPlugin ().plugin (map );
39- assertNotEquals ("Always" , map .toString ());
87+ void shouldPluginNotInvokeArbitraryMethod () {
88+ Map <?, ?> map = new HashMap <>();
89+ map = (Map <?, ?>) new AlwaysMapPlugin ().plugin (map );
90+ try {
91+ map .get ("Anything" );
92+ fail ("Exected IllegalArgumentException, but no exception was thrown." );
93+ } catch (IllegalArgumentException e ) {
94+ assertEquals (
95+ "Method 'public abstract java.lang.Object java.util.Map.get(java.lang.Object)' is not supported as a plugin target." ,
96+ e .getMessage ());
97+ } catch (Exception e ) {
98+ fail ("Exected IllegalArgumentException, but was " + e .getClass (), e );
99+ }
40100 }
41101
42102 @ Intercepts ({ @ Signature (type = Map .class , method = "get" , args = { Object .class }) })
@@ -45,7 +105,5 @@ public static class AlwaysMapPlugin implements Interceptor {
45105 public Object intercept (Invocation invocation ) {
46106 return "Always" ;
47107 }
48-
49108 }
50-
51109}
0 commit comments