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/** 029 * Requires a compatible postgres binary on the classpath (as part of the dependencies in test 030 * 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 private final String name; 055 056 ClasspathLocator(String name) { 057 this.name = name; 058 } 059 060 @Override 061 public InputStream getInputStream() { 062 return EmbeddedPostgres.class.getResourceAsStream("/postgres-" + name + ".txz"); 063 } 064 } 065}