View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.util.database;
5   
6   import java.io.File;
7   import java.io.FileOutputStream;
8   import java.io.PrintStream;
9   import java.util.Map.Entry;
10  import java.util.Properties;
11  import java.util.ResourceBundle;
12  
13  import org.apache.commons.io.IOUtils;
14  import org.junit.After;
15  import org.junit.Assert;
16  import org.junit.Before;
17  import org.junit.Test;
18  
19  /**
20   *
21   * @author sturton
22   */
23  public class DBTypeTest {
24  
25      private File absoluteFile;
26  
27      private Properties testProperties;
28      private Properties includeProperties;
29  
30      @Before
31      public void setUp() throws Exception {
32          testProperties = new Properties();
33          testProperties.put("prop1", "value1");
34          testProperties.put("prop2", "value2");
35          testProperties.put("prop3", "value3");
36  
37          includeProperties = new Properties();
38          includeProperties.putAll(testProperties);
39          includeProperties.put("prop3", "include3");
40  
41          PrintStream printStream = null;
42          try {
43              absoluteFile = File.createTempFile("dbtypetest", ".properties");
44              FileOutputStream fileOutputStream = new FileOutputStream(absoluteFile);
45              printStream = new PrintStream(fileOutputStream);
46  
47              for (Entry<?, ?> entry : testProperties.entrySet()) {
48                  printStream.printf("%s=%s\n", entry.getKey(), entry.getValue());
49              }
50          } finally {
51              IOUtils.closeQuietly(printStream);
52          }
53      }
54  
55      @After
56      public void tearDown() throws Exception {
57          testProperties = null;
58      }
59  
60      /**
61       * Test of getProperties method, of class DBType.
62       */
63      @Test
64      public void testGetPropertiesFromFile() throws Exception {
65          System.out.println("getPropertiesFromFile");
66          DBType instance = new DBType(absoluteFile.getAbsolutePath());
67          Properties expResult = testProperties;
68          Properties result = instance.getProperties();
69          Assert.assertEquals(expResult, result);
70          // TODO review the generated test code and remove the default call to
71          // fail.
72          // fail("The test case is a prototype.");
73      }
74  
75      /**
76       * Test of getProperties method, of class DBType.
77       */
78      @Test
79      public void testGetProperties() throws Exception {
80          System.out.println("testGetProperties");
81          DBType instance = new DBType("test");
82          Properties expResult = testProperties;
83          System.out.println("testGetProperties: expected results " + testProperties);
84          Properties result = instance.getProperties();
85          System.out.println("testGetProperties: actual results " + result);
86          Assert.assertEquals(expResult, result);
87          // TODO review the generated test code and remove the default call to
88          // fail.
89          // fail("The test case is a prototype.");
90      }
91  
92      /**
93       * Test of getProperties method, of class DBType.
94       */
95      @Test
96      public void testGetIncludeProperties() throws Exception {
97          System.out.println("testGetIncludeProperties");
98          DBType instance = new DBType("include");
99          Properties expResult = includeProperties;
100         System.out.println("testGetIncludeProperties: expected results " + includeProperties);
101         Properties result = instance.getProperties();
102         System.out.println("testGetIncludeProperties: actual results " + result);
103         Assert.assertEquals(expResult, result);
104         // TODO review the generated test code and remove the default call to
105         // fail.
106         // fail("The test case is a prototype.");
107     }
108 
109     /**
110      * Test of getResourceBundleAsProperties method, of class DBType.
111      */
112     @Test
113     public void testAsProperties() {
114         System.out.println("asProperties");
115         ResourceBundle bundle = ResourceBundle.getBundle(DBType.class.getCanonicalName() + ".test");
116         Properties expResult = testProperties;
117         Properties result = DBType.getResourceBundleAsProperties(bundle);
118         Assert.assertEquals(expResult, result);
119         // TODO review the generated test code and remove the default call to
120         // fail.
121         // fail("The test case is a prototype.");
122     }
123 }