1package com.xtremelabs.robolectric.util;
2
3import com.xtremelabs.robolectric.WithTestDefaultsRunner;
4import org.junit.Before;
5import org.junit.Test;
6import org.junit.runner.RunWith;
7
8import java.sql.ResultSet;
9import java.sql.SQLException;
10
11import static org.hamcrest.CoreMatchers.equalTo;
12import static org.junit.Assert.assertThat;
13
14@RunWith(WithTestDefaultsRunner.class)
15public class H2MapTest {
16
17    H2Map map;
18
19    @Before
20    public void setUp() throws Exception {
21        map = new H2Map();
22    }
23
24    @Test
25    public void testDriverClassName() {
26        assertThat(map.getDriverClassName(), equalTo("org.h2.Driver"));
27    }
28
29    @Test
30    public void testConnectionString() {
31        assertThat(map.getConnectionString(), equalTo("jdbc:h2:mem:"));
32    }
33
34    @Test
35    public void testScrubSQLReplacesAutoIncrement() throws SQLException {
36        assertThat(map.getScrubSQL("autoincrement"), equalTo("auto_increment"));
37    }
38
39    @Test
40    public void testScrubSQLReplacesIntegerWithBigInt() throws SQLException {
41        assertThat(map.getScrubSQL("integer"), equalTo("bigint(19)"));
42    }
43
44    @Test
45    public void testScrubSQLAcceptsIntegerPrimaryKey() throws SQLException {
46        map.getScrubSQL("INTEGER PRIMARY KEY AUTOINCREMENT");
47    }
48
49    @Test(expected = SQLException.class)
50    public void testScrubSQLRejectsIntPrimaryKeyThrowsException() throws SQLException {
51        map.getScrubSQL("INT PRIMARY KEY AUTOINCREMENT");
52    }
53
54    @Test(expected = SQLException.class)
55    public void testScrubSQLRejectsCharPrimaryKeyThrowsException2() throws SQLException {
56        map.getScrubSQL("CHAR PRIMARY KEY AUTOINCREMENT");
57    }
58
59    @Test
60    public void testGetSelectLastInsertIdentity() throws SQLException {
61        assertThat(map.getSelectLastInsertIdentity(), equalTo("SELECT IDENTITY();"));
62    }
63
64    @Test
65    public void testGetH2ResultSetIs_TYPE_SCROLL_INSENSITIVE() throws SQLException {
66        assertThat(map.getResultSetType(), equalTo(ResultSet.TYPE_SCROLL_INSENSITIVE));
67    }
68
69    @Test
70    public void scrubSQL_shouldRemoveConflictAlgorithms() throws Exception {
71        assertThat(map.getScrubSQL("INSERT INTO "), equalTo("INSERT INTO "));
72        assertThat(map.getScrubSQL("INSERT OR ROLLBACK INTO "), equalTo("INSERT INTO "));
73        assertThat(map.getScrubSQL("INSERT OR ABORT INTO "), equalTo("INSERT INTO "));
74        assertThat(map.getScrubSQL("INSERT OR FAIL INTO "), equalTo("INSERT INTO "));
75        assertThat(map.getScrubSQL("INSERT OR IGNORE INTO "), equalTo("INSERT INTO "));
76        assertThat(map.getScrubSQL("INSERT OR REPLACE INTO "), equalTo("INSERT INTO "));
77    }
78}
79