1   /*
2    * Copyright (C) 2006  Tom Gibara
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17   */
18  package com.tomgibara.pronto.util;
19  
20  import java.util.HashMap;
21  
22  import junit.framework.TestCase;
23  
24  public class FactoryHelperTest extends TestCase {
25  
26      public void testCreateClassOnly() throws Exception {
27          FactoryHelper<HashMap> factory = new FactoryHelper<HashMap>("java.util.HashMap", null, null);
28          HashMap map = factory.getInstance();
29      }
30  
31      public void testCreatePropertyOnly() {
32          System.setProperty("factory.test", "java.util.HashMap");
33          new FactoryHelper(null, null, "factory.test");
34      }
35  
36      public void testCreateBadClass() throws Exception {
37          FactoryHelper<HashMap> factory = new FactoryHelper<HashMap>("non.package.NonClass123", null, null);
38          try {
39              HashMap map = factory.getInstance();
40          } catch (Exception e) {
41              /* expected behaviour */
42              return;
43          }
44          fail("Did not fail to create non-existent class");
45      }
46  
47      public void testCreateNonProperty() throws Exception {
48          FactoryHelper<HashMap> factory = new FactoryHelper<HashMap>(null, null, "non.package.NonProperty123");
49          try {
50              HashMap map = factory.getInstance();
51          } catch (Exception e) {
52              /* expected behaviour */
53              return;
54          }
55          fail("Did not fail to create non-existent class");
56      }
57  
58      public void testCreateBadProperty() throws Exception {
59          System.setProperty("factory.test", "non.package.NonClass123");
60          FactoryHelper<HashMap> factory = new FactoryHelper<HashMap>(null, null, "factory.test");
61          try {
62              HashMap map = factory.getInstance();
63          } catch (Exception e) {
64              /* expected behaviour */
65              return;
66          }
67          fail("Did not fail to create non-existent class");
68      }
69  
70      public void testNullCheck() {
71          try {
72              new FactoryHelper(null, null, null);
73          } catch (IllegalArgumentException e) {
74              /* expected */
75              return;
76          }
77          fail("Accepted null parameters");
78  
79      }
80  
81  }