package com.zaxxer.hikari;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintWriter;
import java.util.Properties;
import java.util.Set;

import javax.sql.DataSource;

import org.junit.Assert;
import org.junit.Test;

import com.zaxxer.hikari.util.PropertyElf;

public class TestPropertySetter
{
   @Test
   public void testProperty1()
   {
      File file = new File("/propfile1.properties");
      HikariConfig config = new HikariConfig(file.getPath());
      config.validate();

      Assert.assertEquals(5, config.getMinimumIdle());
      Assert.assertEquals("SELECT 1", config.getConnectionTestQuery());
   }

   @Test
   public void testProperty2() throws Exception
   {
      File file = new File("/propfile2.properties");
      HikariConfig config = new HikariConfig(file.getPath());
      config.validate();

      Class<?> clazz = this.getClass().getClassLoader().loadClass(config.getDataSourceClassName());
      DataSource dataSource = (DataSource) clazz.newInstance();
      PropertyElf.setTargetFromProperties(dataSource, config.getDataSourceProperties());
   }

   @Test
   public void testObjectProperty() throws Exception
   {
      HikariConfig config = new HikariConfig();
      config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
      PrintWriter writer = new PrintWriter(new ByteArrayOutputStream());
      config.addDataSourceProperty("logWriter", writer);

      Class<?> clazz = this.getClass().getClassLoader().loadClass(config.getDataSourceClassName());
      DataSource dataSource = (DataSource) clazz.newInstance();
      PropertyElf.setTargetFromProperties(dataSource, config.getDataSourceProperties());

      Assert.assertSame(PrintWriter.class, dataSource.getLogWriter().getClass());
   }

   @Test
   public void testPropertyUpperCase() throws Exception
   {
      File file = new File("/propfile3.properties");
      HikariConfig config = new HikariConfig(file.getPath());
      config.validate();

      Class<?> clazz = this.getClass().getClassLoader().loadClass(config.getDataSourceClassName());
      DataSource dataSource = (DataSource) clazz.newInstance();
      PropertyElf.setTargetFromProperties(dataSource, config.getDataSourceProperties());
   }

   @Test
   public void testGetPropertyNames() throws Exception
   {
      Set<String> propertyNames = PropertyElf.getPropertyNames(HikariConfig.class);
      Assert.assertTrue(propertyNames.contains("dataSourceClassName"));
   }

   @Test
   public void testSetNonExistantPropertyName() throws Exception
   {
      try {
         Properties props = new Properties();
         props.put("what", "happened");
         PropertyElf.setTargetFromProperties(new HikariConfig(), props);
         Assert.fail();
      }
      catch (RuntimeException e) {
      }
   }
}

@@ -63,29 +63,8 @@
 am;%0A
-import java.io.File;%0A
 impo
@@ -339,40 +339,143 @@
 y1()
-%0A   %7B%0A      File 
+ throws Exception%0A   %7B%0A      Properties prop
 file
+1
  = new 
-File
+Properties();%0A      propfile1.load(TestPropertySetter.class.getResourceAsStream
 (%22/p
@@ -487,32 +487,33 @@
 le1.properties%22)
+)
 ;%0A      HikariCo
@@ -539,38 +539,33 @@
 ikariConfig(
-file.getPath()
+propfile1
 );%0A      con
@@ -782,36 +782,122 @@
 %7B%0A      
-File 
+Properties prop
 file
+2
  = new 
-File
+Properties();%0A      propfile2.load(TestPropertySetter.class.getResourceAsStream
 (%22/propf
@@ -905,32 +905,33 @@
 le2.properties%22)
+)
 ;%0A      HikariCo
@@ -957,38 +957,33 @@
 ikariConfig(
-file.getPath()
+propfile2
 );%0A      con
@@ -2005,28 +2005,114 @@
     
-File 
+Properties prop
 file
+3
  = new 
-File
+Properties();%0A      propfile3.load(TestPropertySetter.class.getResourceAsStream
 (%22/p
@@ -2132,16 +2132,17 @@
 erties%22)
+)
 ;%0A      
@@ -2184,22 +2184,17 @@
 fig(
-file.getPath()
+propfile3
 );%0A 

Fix resolving property files