|
| 1 | +package ai.chat2db.plugin.h2; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import java.lang.reflect.Proxy; |
| 6 | +import java.sql.Connection; |
| 7 | +import java.sql.PreparedStatement; |
| 8 | +import java.sql.ResultSet; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 13 | + |
| 14 | +class H2MetaSchemaBindingTest { |
| 15 | + |
| 16 | + @Test |
| 17 | + void routineAndTriggerDetailsFilterBySchemaInsteadOfCatalog() { |
| 18 | + List<String> sql = new ArrayList<>(); |
| 19 | + Connection connection = connection(sql); |
| 20 | + H2Meta metaData = new H2Meta(); |
| 21 | + |
| 22 | + metaData.function(connection, "CATALOG", "APP", "F1"); |
| 23 | + metaData.procedure(connection, "CATALOG", "APP", "P1"); |
| 24 | + metaData.trigger(connection, "CATALOG", "APP", "T1"); |
| 25 | + |
| 26 | + assertTrue(sql.get(0).contains("ROUTINE_SCHEMA ='APP'"), sql.get(0)); |
| 27 | + assertTrue(sql.get(1).contains("ROUTINE_SCHEMA ='APP'"), sql.get(1)); |
| 28 | + assertTrue(sql.get(2).contains("TRIGGER_SCHEMA = 'APP'"), sql.get(2)); |
| 29 | + } |
| 30 | + |
| 31 | + private static Connection connection(List<String> sql) { |
| 32 | + ResultSet resultSet = (ResultSet) Proxy.newProxyInstance( |
| 33 | + H2MetaSchemaBindingTest.class.getClassLoader(), |
| 34 | + new Class<?>[]{ResultSet.class}, |
| 35 | + (proxy, method, args) -> switch (method.getName()) { |
| 36 | + case "next" -> false; |
| 37 | + case "close" -> null; |
| 38 | + default -> throw new AssertionError("Unexpected ResultSet call: " + method.getName()); |
| 39 | + }); |
| 40 | + return (Connection) Proxy.newProxyInstance( |
| 41 | + H2MetaSchemaBindingTest.class.getClassLoader(), |
| 42 | + new Class<?>[]{Connection.class}, |
| 43 | + (proxy, method, args) -> { |
| 44 | + if (!"prepareStatement".equals(method.getName())) { |
| 45 | + throw new AssertionError("Unexpected Connection call: " + method.getName()); |
| 46 | + } |
| 47 | + sql.add((String) args[0]); |
| 48 | + return Proxy.newProxyInstance( |
| 49 | + H2MetaSchemaBindingTest.class.getClassLoader(), |
| 50 | + new Class<?>[]{PreparedStatement.class}, |
| 51 | + (statement, statementMethod, statementArgs) -> switch (statementMethod.getName()) { |
| 52 | + case "execute" -> true; |
| 53 | + case "getResultSet" -> resultSet; |
| 54 | + case "close" -> null; |
| 55 | + default -> throw new AssertionError( |
| 56 | + "Unexpected PreparedStatement call: " + statementMethod.getName()); |
| 57 | + }); |
| 58 | + }); |
| 59 | + } |
| 60 | +} |
0 commit comments