1
1
package com .linkedin .hoptimator .jdbc ;
2
2
3
+ import com .linkedin .hoptimator .Validator ;
4
+ import com .linkedin .hoptimator .ValidatorProvider ;
5
+ import java .io .IOException ;
6
+ import java .net .URL ;
7
+ import java .net .URLClassLoader ;
8
+ import java .nio .file .Files ;
9
+ import java .nio .file .Path ;
10
+ import java .util .Collection ;
11
+ import java .util .List ;
12
+ import org .apache .calcite .sql .ddl .SqlCreateMaterializedView ;
13
+ import org .apache .calcite .sql .ddl .SqlCreateView ;
14
+ import org .junit .jupiter .api .Assertions ;
3
15
import org .junit .jupiter .api .Test ;
16
+ import org .opentest4j .AssertionFailedError ;
4
17
5
18
6
19
public class TestSqlScripts extends QuidemTestBase {
@@ -9,4 +22,72 @@ public class TestSqlScripts extends QuidemTestBase {
9
22
public void basicDdlScript () throws Exception {
10
23
run ("basic-ddl.id" );
11
24
}
25
+
26
+ @ Test
27
+ public void createViewWithAValidatorRejectingCreateViewThrowsException () throws Exception {
28
+ // Runs the test in a separate thread to isolate the context class loader changes.
29
+ Thread testThread = new Thread (() -> {
30
+ useTestValidatorsUnchecked ();
31
+
32
+ AssertionFailedError exception = Assertions .assertThrows (AssertionFailedError .class , () -> run ("basic-ddl.id" ));
33
+ Assertions .assertTrue (exception .getMessage ().contains (SqlCreateViewValidator .ERROR_MESSAGE ),
34
+ "Expected error message not found: " + exception .getMessage ());
35
+ });
36
+ testThread .start ();
37
+ testThread .join (); // Wait for the thread to finish
38
+ }
39
+
40
+ @ Test
41
+ public void createMaterializedViewWithAValidatorRejectingCreateViewThrowsException () throws Exception {
42
+ // Runs the test in a separate thread to isolate the context class loader changes.
43
+ Thread testThread = new Thread (() -> {
44
+ useTestValidatorsUnchecked ();
45
+
46
+ AssertionFailedError exception =
47
+ Assertions .assertThrows (AssertionFailedError .class , () -> run ("create-materialized-view-ddl.id" ));
48
+ Assertions .assertTrue (exception .getMessage ().contains (SqlCreateViewValidator .ERROR_MESSAGE ),
49
+ "Expected error message not found: " + exception .getMessage ());
50
+ });
51
+ testThread .start ();
52
+ testThread .join (); // Wait for the thread to finish
53
+ }
54
+
55
+ private void useTestValidatorsUnchecked () {
56
+ try {
57
+ useTestValidators ();
58
+ } catch (IOException e ) {
59
+ throw new RuntimeException ("Failed to set up test validators" , e );
60
+ }
61
+ }
62
+
63
+ private void useTestValidators () throws IOException {
64
+ Path tempDir = Files .createTempDirectory ("spi-test" );
65
+ Path servicesDir = tempDir .resolve ("META-INF/services" );
66
+ Files .createDirectories (servicesDir );
67
+ Files .writeString (servicesDir .resolve ("com.linkedin.hoptimator.ValidatorProvider" ),
68
+ "com.linkedin.hoptimator.jdbc.TestSqlScripts$CreateViewValidatorProvider\n " );
69
+
70
+ URLClassLoader cl = new URLClassLoader (new URL []{tempDir .toUri ().toURL ()}, getClass ().getClassLoader ());
71
+ Thread .currentThread ().setContextClassLoader (cl );
72
+ }
73
+
74
+ @ SuppressWarnings ("unused" )
75
+ public static class CreateViewValidatorProvider implements ValidatorProvider {
76
+ @ Override
77
+ public <T > Collection <Validator > validators (T obj ) {
78
+ if (obj instanceof SqlCreateView || obj instanceof SqlCreateMaterializedView ) {
79
+ return List .of (new SqlCreateViewValidator ());
80
+ }
81
+ return List .of ();
82
+ }
83
+ }
84
+
85
+ static class SqlCreateViewValidator implements Validator {
86
+ static final String ERROR_MESSAGE = "Create view is not allowed in this test." ;
87
+
88
+ @ Override
89
+ public void validate (Issues issues ) {
90
+ issues .error (ERROR_MESSAGE );
91
+ }
92
+ }
12
93
}
0 commit comments