From 2e2b53e0c8c4bdce21b09fca987321008f55b815 Mon Sep 17 00:00:00 2001 From: Nick Rupley Date: Wed, 22 May 2019 08:37:37 -0700 Subject: [PATCH] Fixing issue where the database-readonly.driver property wasn't correctly inheriting from the database.driver property when empty. --- .../mirth/connect/model/DatabaseSettings.java | 14 +-- .../connect/model/DatabaseSettingsTest.java | 89 +++++++++++++++++++ 2 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 server/test/com/mirth/connect/model/DatabaseSettingsTest.java diff --git a/server/src/com/mirth/connect/model/DatabaseSettings.java b/server/src/com/mirth/connect/model/DatabaseSettings.java index 38b2148d61..b6c0eb4ab6 100644 --- a/server/src/com/mirth/connect/model/DatabaseSettings.java +++ b/server/src/com/mirth/connect/model/DatabaseSettings.java @@ -226,17 +226,21 @@ public void setDirBase(String dirBase) { this.dirBase = dirBase; } - private String getMappedDatabaseDriver() { - if (StringUtils.isEmpty(databaseDriver)) { + String getMappedDatabaseDriver() { + if (StringUtils.isBlank(databaseDriver)) { return MapUtils.getString(databaseDriverMap, getDatabase()); } else { return databaseDriver; } } - private String getMappedReadOnlyDatabaseDriver() { - if (StringUtils.isEmpty(databaseReadOnlyDriver)) { - return MapUtils.getString(databaseDriverMap, StringUtils.defaultIfBlank(getDatabaseReadOnly(), getDatabase())); + String getMappedReadOnlyDatabaseDriver() { + if (StringUtils.isBlank(databaseReadOnlyDriver)) { + if (StringUtils.isBlank(getDatabaseReadOnly())) { + return getMappedDatabaseDriver(); + } else { + return MapUtils.getString(databaseDriverMap, getDatabaseReadOnly()); + } } else { return databaseReadOnlyDriver; } diff --git a/server/test/com/mirth/connect/model/DatabaseSettingsTest.java b/server/test/com/mirth/connect/model/DatabaseSettingsTest.java new file mode 100644 index 0000000000..aeb2163883 --- /dev/null +++ b/server/test/com/mirth/connect/model/DatabaseSettingsTest.java @@ -0,0 +1,89 @@ +/* + * Copyright (c) Mirth Corporation. All rights reserved. + * + * http://www.mirthcorp.com + * + * The software in this package is published under the terms of the MPL license a copy of which has + * been included with this distribution in the LICENSE.txt file. + */ + +package com.mirth.connect.model; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import org.junit.Test; + +public class DatabaseSettingsTest { + + @Test + public void getMappedDatabaseDriver_NullDriver_InvalidDatabase() throws Exception { + DatabaseSettings databaseSettings = new DatabaseSettings(); + databaseSettings.setDatabase("dummy"); + databaseSettings.setDatabaseDriver(null); + assertNull(databaseSettings.getMappedDatabaseDriver()); + } + + @Test + public void getMappedDatabaseDriver_BlankDriver_ValidDatabase() throws Exception { + DatabaseSettings databaseSettings = new DatabaseSettings(); + databaseSettings.setDatabase("postgres"); + databaseSettings.setDatabaseDriver(" "); + assertEquals("org.postgresql.Driver", databaseSettings.getMappedDatabaseDriver()); + } + + @Test + public void getMappedDatabaseDriver_NonBlankDriver() throws Exception { + DatabaseSettings databaseSettings = new DatabaseSettings(); + databaseSettings.setDatabase("mysql"); + databaseSettings.setDatabaseDriver("test"); + assertEquals("test", databaseSettings.getMappedDatabaseDriver()); + } + + @Test + public void getMappedReadOnlyDatabaseDriver_NullDriver_NullRODriver_NullDatabaseRO() throws Exception { + DatabaseSettings databaseSettings = spy(new DatabaseSettings()); + databaseSettings.setDatabase("oracle"); + databaseSettings.setDatabaseDriver(null); + databaseSettings.setDatabaseReadOnly(null); + databaseSettings.setDatabaseReadOnlyDriver(null); + assertEquals("oracle.jdbc.OracleDriver", databaseSettings.getMappedReadOnlyDatabaseDriver()); + verify(databaseSettings, times(1)).getMappedDatabaseDriver(); + } + + @Test + public void getMappedReadOnlyDatabaseDriver_NullDriver_NullRODriver_InvalidDatabaseRO() throws Exception { + DatabaseSettings databaseSettings = spy(new DatabaseSettings()); + databaseSettings.setDatabase("oracle"); + databaseSettings.setDatabaseDriver(null); + databaseSettings.setDatabaseReadOnly("dummy"); + databaseSettings.setDatabaseReadOnlyDriver(null); + assertNull(databaseSettings.getMappedReadOnlyDatabaseDriver()); + verify(databaseSettings, times(0)).getMappedDatabaseDriver(); + } + + @Test + public void getMappedReadOnlyDatabaseDriver_NullDriver_NullRODriver_ValidDatabaseRO() throws Exception { + DatabaseSettings databaseSettings = spy(new DatabaseSettings()); + databaseSettings.setDatabase("oracle"); + databaseSettings.setDatabaseDriver(null); + databaseSettings.setDatabaseReadOnly("sqlserver"); + databaseSettings.setDatabaseReadOnlyDriver(null); + assertEquals("net.sourceforge.jtds.jdbc.Driver", databaseSettings.getMappedReadOnlyDatabaseDriver()); + verify(databaseSettings, times(0)).getMappedDatabaseDriver(); + } + + @Test + public void getMappedReadOnlyDatabaseDriver_NonBlankRODriver() throws Exception { + DatabaseSettings databaseSettings = spy(new DatabaseSettings()); + databaseSettings.setDatabase("oracle"); + databaseSettings.setDatabaseDriver(null); + databaseSettings.setDatabaseReadOnly(null); + databaseSettings.setDatabaseReadOnlyDriver("test"); + assertEquals("test", databaseSettings.getMappedReadOnlyDatabaseDriver()); + verify(databaseSettings, times(0)).getMappedDatabaseDriver(); + } +}