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.embedded;
015
016import static org.junit.jupiter.api.Assertions.assertEquals;
017import static org.junit.jupiter.api.Assertions.assertFalse;
018import static org.junit.jupiter.api.Assertions.assertTrue;
019
020import java.io.InputStream;
021import java.sql.Connection;
022import java.sql.ResultSet;
023import java.sql.Statement;
024
025import org.junit.jupiter.api.Assumptions;
026import org.junit.jupiter.api.Test;
027
028
029public class ClasspathLocatorTest {
030
031    @Test
032    public void testClasspathLocator() throws Exception {
033        String name = System.getProperty("pg-embedded.test.binary-name");
034        Assumptions.assumeTrue(name != null && !name.isEmpty(), "No binary name set, skipping test");
035
036        try (EmbeddedPostgres pg = EmbeddedPostgres.builderWithDefaults()
037                .setNativeBinaryManager(new TarXzCompressedBinaryManager(new ClasspathLocator(name)))
038                .build();
039                Connection c = pg.createDefaultDataSource().getConnection();
040                Statement s = c.createStatement()) {
041            try (ResultSet rs = s.executeQuery("SELECT 1")) {
042                assertTrue(rs.next());
043                assertEquals(1, rs.getInt(1));
044                assertFalse(rs.next());
045            }
046        }
047    }
048
049    static class ClasspathLocator implements NativeBinaryLocator {
050        private final String name;
051
052        ClasspathLocator(String name) {
053            this.name = name;
054        }
055
056        @Override
057        public InputStream getInputStream() {
058            return EmbeddedPostgres.class.getResourceAsStream("/postgres-" + name + ".txz");
059        }
060    }
061}