001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 * http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package de.softwareforge.testing.postgres.junit5;
015
016import static de.softwareforge.testing.postgres.junit5.Junit5ClassMemberTest.createTable;
017import static de.softwareforge.testing.postgres.junit5.Junit5ClassMemberTest.existsTable;
018import static org.junit.jupiter.api.Assertions.assertEquals;
019import static org.junit.jupiter.api.Assertions.assertFalse;
020import static org.junit.jupiter.api.Assertions.assertThrows;
021import static org.junit.jupiter.api.Assertions.assertTrue;
022
023import java.sql.SQLException;
024
025import org.junit.jupiter.api.Test;
026import org.junit.jupiter.api.extension.RegisterExtension;
027
028public class IsolationTest {
029
030    @RegisterExtension
031    public EmbeddedPgExtension pg1 = SingleDatabaseBuilder.instanceWithDefaults().build();
032
033    @RegisterExtension
034    public EmbeddedPgExtension pg2 = SingleDatabaseBuilder.instanceWithDefaults().build();
035
036    @Test
037    public void testDoubleTable() throws Exception {
038        assertEquals(0, createTable(pg1, "table1"));
039        SQLException e = assertThrows(SQLException.class, () -> createTable(pg1, "table1"));
040        // https://www.postgresql.org/docs/8.2/errcodes-appendix.html 42P07 - DUPLICATE TABLE
041        assertEquals("42P07", e.getSQLState());
042        assertTrue(existsTable(pg1, "table1"));
043    }
044
045
046    @Test
047    public void testSameTable() throws Exception {
048        assertEquals(0, createTable(pg1, "table1"));
049        assertEquals(0, createTable(pg2, "table1"));
050
051        assertTrue(existsTable(pg1, "table1"));
052        assertTrue(existsTable(pg2, "table1"));
053    }
054
055    @Test
056    public void testDifferentTable() throws Exception {
057        assertEquals(0, createTable(pg1, "table1"));
058        assertEquals(0, createTable(pg2, "table2"));
059
060        assertTrue(existsTable(pg1, "table1"));
061        assertFalse(existsTable(pg1, "table2"));
062        assertTrue(existsTable(pg2, "table2"));
063        assertFalse(existsTable(pg2, "table1"));
064    }
065}