From 03a9aa3dec303ac12f69cb9c1bff835c47eee441 Mon Sep 17 00:00:00 2001 From: Robin Bygrave Date: Thu, 24 Dec 2015 15:23:25 +1300 Subject: [PATCH] #507 - PropertiesWrapper support null enum values with getEnum() --- .../com/avaje/ebean/config/PropertiesWrapper.java | 4 ++-- .../avaje/ebean/config/PropertiesWrapperTest.java | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/avaje/ebean/config/PropertiesWrapper.java b/src/main/java/com/avaje/ebean/config/PropertiesWrapper.java index 66bcf9ac0..4bda1c763 100644 --- a/src/main/java/com/avaje/ebean/config/PropertiesWrapper.java +++ b/src/main/java/com/avaje/ebean/config/PropertiesWrapper.java @@ -141,8 +141,8 @@ public class PropertiesWrapper { * Return a Enum property value. */ public > T getEnum(Class enumType, String key, T defaultValue) { - String level = get(key, defaultValue.name()); - return Enum.valueOf(enumType, level.toUpperCase()); + String level = get(key, null); + return (level == null) ? defaultValue : Enum.valueOf(enumType, level.toUpperCase()); } } diff --git a/src/test/java/com/avaje/ebean/config/PropertiesWrapperTest.java b/src/test/java/com/avaje/ebean/config/PropertiesWrapperTest.java index 24422d657..43f8606c9 100644 --- a/src/test/java/com/avaje/ebean/config/PropertiesWrapperTest.java +++ b/src/test/java/com/avaje/ebean/config/PropertiesWrapperTest.java @@ -1,5 +1,6 @@ package com.avaje.ebean.config; +import com.avaje.ebean.config.dbplatform.DbPlatformName; import org.junit.Test; import java.util.Properties; @@ -17,6 +18,18 @@ public class PropertiesWrapperTest { assertEquals("myserver", pw.getServerName()); } + @Test + public void testGetEnum() { + + Properties properties = new Properties(); + properties.put("platform","postgres"); + + PropertiesWrapper pw = new PropertiesWrapper("pref", "myserver", properties); + assertEquals(DbPlatformName.POSTGRES, pw.getEnum(DbPlatformName.class, "platform", DbPlatformName.H2)); + assertEquals(DbPlatformName.H2, pw.getEnum(DbPlatformName.class, "junk", DbPlatformName.H2)); + assertNull(pw.getEnum(DbPlatformName.class, "junk", null)); + } + @Test public void testTrimPropertyValues() {