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