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
028public class ClasspathLocatorTest {
029
030    @Test
031    public void testClasspathLocator() throws Exception {
032        // this test only runs on Mac
033        Assumptions.assumeTrue(EmbeddedUtil.IS_OS_MAC);
034        Assumptions.assumeTrue(EmbeddedUtil.IS_ARCH_X86_64 || EmbeddedUtil.IS_ARCH_AARCH64);
035
036        try (EmbeddedPostgres pg = EmbeddedPostgres.builderWithDefaults()
037                .setNativeBinaryManager(new TarXzCompressedBinaryManager(new ClasspathLocator()))
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        @Override
051        public InputStream getInputStream() {
052            return EmbeddedPostgres.class.getResourceAsStream("/postgres-darwin-x86_64.txz");
053        }
054    }
055}