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.assertTrue;
018
019import java.sql.SQLException;
020import java.util.concurrent.ThreadLocalRandom;
021
022import org.junit.jupiter.api.Test;
023
024public class DatabaseInfoTest {
025
026    @Test
027    public void testMinimal() {
028        DatabaseInfo databaseInfo = DatabaseInfo.builder().port(12345).build();
029        assertEquals(DatabaseInfo.PG_DEFAULT_USER, databaseInfo.user());
030        assertEquals(DatabaseInfo.PG_DEFAULT_DB, databaseInfo.dbName());
031        assertEquals(12345, databaseInfo.port());
032        assertTrue(databaseInfo.connectionProperties().isEmpty());
033        assertTrue(databaseInfo.exception().isEmpty());
034    }
035
036    @Test
037    public void testFull() {
038        String dbName = EmbeddedUtil.randomLowercase(12);
039        String user = EmbeddedUtil.randomLowercase(12);
040        int port = ThreadLocalRandom.current().nextInt(65536);
041
042        String propertyName = EmbeddedUtil.randomLowercase(12);
043        String propertyValue = EmbeddedUtil.randomLowercase(12);
044
045        DatabaseInfo databaseInfo = DatabaseInfo.builder()
046                .dbName(dbName)
047                .user(user)
048                .port(port)
049                .addConnectionProperty(propertyName, propertyValue)
050                .build();
051
052        assertEquals(user, databaseInfo.user());
053        assertEquals(dbName, databaseInfo.dbName());
054        assertEquals(port, databaseInfo.port());
055        assertEquals(1, databaseInfo.connectionProperties().size());
056        assertEquals(propertyValue, databaseInfo.connectionProperties().get(propertyName));
057        assertTrue(databaseInfo.exception().isEmpty());
058    }
059
060    @Test
061    public void testException() {
062        DatabaseInfo databaseInfo = DatabaseInfo.forException(new SQLException());
063        assertTrue(databaseInfo.exception().isPresent());
064        assertEquals(SQLException.class, databaseInfo.exception().get().getClass());
065    }
066
067}