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