Permalink
Browse files

Changed property loader to avoid using a default when property not al…

…ready set. Allows the caller to detect when unset. Thus allowing connection Ctor to use programmatic log level value. Added test cases to check PGProperty method working properly.
  • Loading branch information...
whitingjr committed Dec 1, 2015
1 parent e0a9b41 commit ecf4a2ca9b2dcf8f233e89d33ec983c5f51bf97d
@@ -463,7 +463,7 @@ public void set(Properties properties, int value)
*/
public boolean isPresent(Properties properties)
{
return get(properties) != null;
return getSetString(properties) != null;
}
/**
@@ -492,4 +492,17 @@ public static PGProperty forName(String name)
}
return null;
}
/**
* Return the property if exists but avoiding the default. Allowing the caller
* to detect the lack of a property.
* @param properties properties bundle
* @return the value of a set property
*/
public String getSetString(Properties properties)
{
Object o = properties.get(_name);
if (o instanceof String)
return (String) o;
return null;
}
}
@@ -8,7 +8,6 @@
package org.postgresql.jdbc2;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.*;
@@ -122,12 +121,22 @@ protected AbstractJdbc2Connection(HostSpec[] hostSpecs, String user, String data
{
this.creatingURL = url;
// Read loglevel arg and set the loglevel based on this value;
// Read loglevel arg and set the loglevel based on this value
// otherwise use the programmatic or init logger level;
// In addition to setting the log level, enable output to
// standard out if no other printwriter is set
int logLevel = Driver.getLogLevel();
String connectionLogLevel = PGProperty.LOG_LEVEL.getSetString(info);
if (connectionLogLevel != null) {
try
{
logLevel = Integer.parseInt(connectionLogLevel);
} catch (NumberFormatException nfe)
{
// ignore
}
}
synchronized (AbstractJdbc2Connection.class) {
logger = new Logger(nextConnectionID++);
logger.setLogLevel(logLevel);
@@ -218,4 +218,26 @@ public void testIsPresentWithParseURLResult() throws Exception
parsedProperties = Driver.parseURL(TestUtil.getURL() + "&ssl=true" , null);
Assert.assertTrue("SSL property should be present", PGProperty.SSL.isPresent(parsedProperties));
}
/**
* Check whether the isPresent method really works.
*/
public void testPresenceCheck()
{
Properties empty = new Properties();
Object value = PGProperty.LOG_LEVEL.get(empty);
assertNotNull(value);
Assert.assertFalse(PGProperty.LOG_LEVEL.isPresent(empty));
}
public void testNullValue()
{
Properties empty = new Properties();
assertNull(PGProperty.LOG_LEVEL.getSetString(empty));
assertNull(PGProperty.LOG_LEVEL.getSetString(empty));
Properties withLogging = new Properties();
withLogging.setProperty(PGProperty.LOG_LEVEL.getName(), "2");
assertNotNull(PGProperty.LOG_LEVEL.getSetString(withLogging));
}
}

0 comments on commit ecf4a2c

Please sign in to comment.