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 */
014
015package de.softwareforge.testing.postgres.junit5;
016
017import static de.softwareforge.testing.postgres.junit5.Junit5ClassMemberTest.createTable;
018import static de.softwareforge.testing.postgres.junit5.Junit5ClassMemberTest.existsTable;
019import static org.junit.jupiter.api.Assertions.assertEquals;
020import static org.junit.jupiter.api.Assertions.assertFalse;
021import static org.junit.jupiter.api.Assertions.assertThrows;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import java.sql.SQLException;
025
026import org.junit.jupiter.api.Test;
027import org.junit.jupiter.api.extension.RegisterExtension;
028
029public class IsolationTest {
030
031    @RegisterExtension
032    public EmbeddedPgExtension pg1 = SingleDatabaseBuilder.instanceWithDefaults().build();
033
034    @RegisterExtension
035    public EmbeddedPgExtension pg2 = SingleDatabaseBuilder.instanceWithDefaults().build();
036
037    @Test
038    public void testDoubleTable() throws Exception {
039        assertEquals(0, createTable(pg1, "table1"));
040        SQLException e = assertThrows(SQLException.class, () -> createTable(pg1, "table1"));
041        // https://www.postgresql.org/docs/8.2/errcodes-appendix.html 42P07 - DUPLICATE TABLE
042        assertEquals("42P07", e.getSQLState());
043        assertTrue(existsTable(pg1, "table1"));
044    }
045
046
047    @Test
048    public void testSameTable() throws Exception {
049        assertEquals(0, createTable(pg1, "table1"));
050        assertEquals(0, createTable(pg2, "table1"));
051
052        assertTrue(existsTable(pg1, "table1"));
053        assertTrue(existsTable(pg2, "table1"));
054    }
055
056    @Test
057    public void testDifferentTable() throws Exception {
058        assertEquals(0, createTable(pg1, "table1"));
059        assertEquals(0, createTable(pg2, "table2"));
060
061        assertTrue(existsTable(pg1, "table1"));
062        assertFalse(existsTable(pg1, "table2"));
063        assertTrue(existsTable(pg2, "table2"));
064        assertFalse(existsTable(pg2, "table1"));
065    }
066}