-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuardedJdbcTemplateTest.java
More file actions
121 lines (99 loc) · 3.97 KB
/
GuardedJdbcTemplateTest.java
File metadata and controls
121 lines (99 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package io.github.evoschema;
import io.github.evoschema.processor.dbscanner.QueryOnlyJdbcTemplate;
import io.github.evoschema.processor.dbscanner.RestrictedJdbcTemplate;
import io.github.evoschema.processor.exception.EvoSchemaException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Logger;
import javax.sql.DataSource;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.jdbc.core.PreparedStatementCreator;
public class GuardedJdbcTemplateTest
{
@Test
public void shouldRejectDdlInRestrictedJdbcTemplateBeforeTouchingDatasource()
{
RestrictedJdbcTemplate template = new RestrictedJdbcTemplate(new UnsupportedDataSource(), "demo.script");
EvoSchemaException error = Assert.assertThrows(
EvoSchemaException.class,
() -> template.execute("ALTER TABLE demo_table ADD COLUMN note VARCHAR(32)")
);
Assert.assertEquals(EvoSchemaException.ProcesssError.DML_SCRIPT_ERROR, error.getReason());
}
@Test
public void shouldRejectPreparedStatementCreatorInRestrictedJdbcTemplate()
{
RestrictedJdbcTemplate template = new RestrictedJdbcTemplate(new UnsupportedDataSource(), "demo.script");
PreparedStatementCreator psc = connection -> connection.prepareStatement("SELECT COUNT(1) FROM demo_table");
EvoSchemaException error = Assert.assertThrows(EvoSchemaException.class, () -> template.update(psc));
Assert.assertEquals(EvoSchemaException.ProcesssError.DML_SCRIPT_ERROR, error.getReason());
Assert.assertTrue(error.getMessage().contains("demo.script"));
}
@Test
public void shouldRejectDmlInQueryOnlyJdbcTemplate()
{
QueryOnlyJdbcTemplate template = new QueryOnlyJdbcTemplate(new UnsupportedDataSource(), "demo.assert");
EvoSchemaException error = Assert.assertThrows(
EvoSchemaException.class,
() -> template.update("UPDATE demo_table SET status = 'DONE'")
);
Assert.assertEquals(EvoSchemaException.ProcesssError.DML_CONFIRM, error.getReason());
}
@Test
public void shouldRejectPreparedStatementCreatorInQueryOnlyJdbcTemplate()
{
QueryOnlyJdbcTemplate template = new QueryOnlyJdbcTemplate(new UnsupportedDataSource(), "demo.assert");
PreparedStatementCreator psc = connection -> connection.prepareStatement("SELECT COUNT(1) FROM demo_table");
EvoSchemaException error = Assert.assertThrows(EvoSchemaException.class, () -> template.update(psc));
Assert.assertEquals(EvoSchemaException.ProcesssError.DML_CONFIRM, error.getReason());
Assert.assertTrue(error.getMessage().contains("demo.assert"));
}
private static class UnsupportedDataSource implements DataSource
{
@Override
public Connection getConnection() throws SQLException
{
throw new UnsupportedOperationException("No real database should be touched in this test");
}
@Override
public Connection getConnection(String username, String password) throws SQLException
{
throw new UnsupportedOperationException("No real database should be touched in this test");
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException
{
throw new SQLException("unwrap is not supported");
}
@Override
public boolean isWrapperFor(Class<?> iface)
{
return false;
}
@Override
public PrintWriter getLogWriter()
{
return null;
}
@Override
public void setLogWriter(PrintWriter out)
{
}
@Override
public void setLoginTimeout(int seconds)
{
}
@Override
public int getLoginTimeout()
{
return 0;
}
@Override
public Logger getParentLogger()
{
return Logger.getGlobal();
}
}
}