1   package com.sourceforge.jpatterns.utils.junit;
2   
3   import com.sourceforge.jpatterns.core.JPConstants;
4   import com.sourceforge.jpatterns.schema.JPatternsConfig;
5   import com.zmicer.utils.LoggingUtils;
6   import com.zmicer.utils.ReflexionUtils;
7   import com.zmicer.utils.junit.ZmicerTestUtils;
8   import junit.framework.Test;
9   import junit.framework.TestCase;
10  import junit.framework.TestSuite;
11  import org.apache.log4j.Logger;
12  
13  /**
14   * $Author:: zmicer             $<br/>
15   * $Rev:: 67                    $<br/> * $Date:: 2007-08-28 21:37:07 #$<br/>
16   */
17  public class JPatternsTestUtilsTest extends TestCase
18  {
19      /**
20       * Logger instance.
21       */
22      final public static Logger LOG = Logger.getLogger(JPatternsTestUtilsTest.class);
23  
24      /**
25       * Interface defining the cases to be used
26       */
27      interface Cases
28      {
29          String CASE = "case";
30          String ALL_PROPS = "allPropertiesCase";
31          String CUSTOM_PROP = "customPropertyCase";
32          String DEFAULT_PROP = "defaultPropertyCase";
33          String NO_PROPS = "noPropertiesCase";
34      }
35  
36      /**
37       * Contructor with name of test attribute.
38       *
39       * @param name name of the test
40       */
41      public JPatternsTestUtilsTest(String name)
42      {
43          super(name);
44      }
45  
46      /**
47       * Perform the set up functionality for the test.
48       *
49       * @throws Exception may occur in the case of some problems
50       */
51      public void setUp() throws Exception
52      {
53          super.setUp();
54      }
55  
56      /**
57       * Perform the tear down functionality for the test
58       *
59       * @throws Exception may occur in the case of some problems
60       */
61      public void tearDown() throws Exception
62      {
63          super.tearDown();
64      }
65  
66      /**
67       * Test suite method
68       *
69       * @return the built test suite
70       */
71      public static Test suite()
72      {
73          return new TestSuite(JPatternsTestUtilsTest.class);
74      }
75  
76      /**
77       * Tests {@link JPatternsTestUtils#getConfig}
78       *
79       * @throws Exception in the case smth. wrong occuried.
80       * @forversion 1.0
81       */
82      public void testGetConfig() throws Exception
83      {
84          // failed sub-case: exception occuried: null inputs
85          try
86          {
87              JPatternsTestUtils.getConfig((String) null, (String) null);
88              fail("Exception should have been occuried before this line.");
89          }
90          catch (Exception ex)
91          {
92              assertTrue(ex instanceof IllegalArgumentException);
93          }
94          // failed sub-case: exception occuried: null inputs
95          try
96          {
97              JPatternsTestUtils.getConfig("dir", null);
98              fail("Exception should have been occuried before this line.");
99          }
100         catch (Exception ex)
101         {
102             assertTrue(ex instanceof IllegalArgumentException);
103         }
104         // normal sub-case: no exception: the real tests - all the cases
105         try
106         {
107             assertNotNull(JPatternsTestUtils.getConfig(ReflexionUtils.getBaseName(JPatternsTestUtilsTest.class),
108                 "jpatterns_framework.xml"));
109             assertNotNull(JPatternsTestUtils.getConfig("JPatternsConfigBeansBuilderImplTest", "jpatterns_framework1.xml"));
110         }
111         catch (Exception ex)
112         {
113             LoggingUtils.logException(LOG, ex, null, null);
114             fail("Exception should not occur for that case " + ex.getMessage());
115         }
116         // failed sub-case: exception occuried: not existed file
117         try
118         {
119             JPatternsTestUtils.getConfig(JPatternsTestUtilsTest.class, Cases.CASE, "jpatterns_notExisted.xml");
120             fail("Exception should have been occuried before this line.");
121         }
122         catch (Exception ex)
123         {
124             assertTrue(ex instanceof IllegalArgumentException);
125         }
126         // normal sub-case: no exception: check it exists
127         try
128         {
129             final JPatternsConfig config = JPatternsTestUtils.getConfig(JPatternsTestUtilsTest.class, Cases.CASE, "jpatterns.xml");
130             assertNotNull(config);
131         }
132         catch (Exception ex)
133         {
134             LoggingUtils.logException(LOG, ex, null, null);
135             fail("Exception should not occur for that case " + ex.getMessage());
136         }
137     }
138 
139     /**
140      * Tests {@link JPatternsTestUtils#setJVMPropsParams}
141      *
142      * @throws Exception in the case smth. wrong occuried.
143      * @forversion 1.0
144      */
145     public void testSetJVMPropsParams() throws Exception
146     {
147         // normal sub-case: no exception: there are properties
148         try
149         {
150             JPatternsTestUtils.setJVMPropsParams(JPatternsTestUtilsTest.class, false, Cases.ALL_PROPS, null, null, true);
151             assertNotNull(System.getProperty(JPConstants.PropertiesConfigFilesConstants.DEFAULT_PROPERTIES_FILE_NAME_JVM_PARAM));
152             assertNotNull(System.getProperty(JPConstants.PropertiesConfigFilesConstants.CUSTOM_PROPERTIES_FILE_NAME_JVM_PARAM));
153             JPatternsTestUtils.resetJVMProps();
154 
155             JPatternsTestUtils.setJVMPropsParams(JPatternsTestUtilsTest.class, false, Cases.ALL_PROPS, "jpatterns_jpatterns",
156                 "jpatterns_jpatterns_custom", true);
157             assertNotNull(System.getProperty(JPConstants.PropertiesConfigFilesConstants.DEFAULT_PROPERTIES_FILE_NAME_JVM_PARAM));
158             assertNotNull(System.getProperty(JPConstants.PropertiesConfigFilesConstants.CUSTOM_PROPERTIES_FILE_NAME_JVM_PARAM));
159             JPatternsTestUtils.resetJVMProps();
160 
161             JPatternsTestUtils.setJVMPropsParams(JPatternsTestUtilsTest.class, false, Cases.ALL_PROPS, "jpatterns_jpatterns",
162                 "jpatterns_jpatterns_custom_not_existed", true);
163             assertNotNull(System.getProperty(JPConstants.PropertiesConfigFilesConstants.DEFAULT_PROPERTIES_FILE_NAME_JVM_PARAM));
164             assertNotNull(System.getProperty(JPConstants.PropertiesConfigFilesConstants.CUSTOM_PROPERTIES_FILE_NAME_JVM_PARAM));
165             JPatternsTestUtils.resetJVMProps();
166 
167             JPatternsTestUtils.setJVMPropsParams(JPatternsTestUtilsTest.class, false, Cases.DEFAULT_PROP, null, null, true);
168             assertNotNull(System.getProperty(JPConstants.PropertiesConfigFilesConstants.DEFAULT_PROPERTIES_FILE_NAME_JVM_PARAM));
169             assertNotNull(System.getProperty(JPConstants.PropertiesConfigFilesConstants.CUSTOM_PROPERTIES_FILE_NAME_JVM_PARAM));
170             JPatternsTestUtils.resetJVMProps();
171         }
172         catch (Exception ex)
173         {
174             LoggingUtils.logException(LOG, ex, null, null);
175             fail("Exception should not occur for that case " + ex.getMessage());
176         }
177     }
178 
179     /**
180      * Tests {@link JPatternsTestUtils#setJVMXmlParams}
181      *
182      * @throws Exception in the case smth. wrong occuried.
183      * @forversion 1.0
184      */
185     public void testSetJVMXmlParams() throws Exception
186     {
187         // normal sub-case: no exception: check the normal case
188         try
189         {
190             final String fileName = "jpatterns.properties";
191             JPatternsTestUtils.setJVMXmlParams(JPatternsTestUtilsTest.class, false, null, fileName, false);
192             String jvmFolder = System.getProperty(JPConstants.XMLConfigFilesConstants.DEFAULT_XML_CONSUMER_CONFIG_DIR_NAME_JVM_PARAM);
193             String jvmFile = System.getProperty(JPConstants.XMLConfigFilesConstants.DEFAULT_XML_FRAMEWORK_CONFIG_FILE_NAME_JVM_PARAM);
194             assertNotNull(jvmFolder);
195             assertNotNull(jvmFile);
196             assertEquals(jvmFolder, ZmicerTestUtils.getFullTestDataFolderName(JPatternsTestUtilsTest.class, false, null, true));
197             assertEquals(jvmFile, ZmicerTestUtils.getFullTestDataFileName(JPatternsTestUtilsTest.class, false, null, fileName, false));
198         }
199         catch (Exception ex)
200         {
201             LoggingUtils.logException(LOG, ex, null, null);
202             fail("Exception should not occur for that case " + ex.getMessage());
203         }
204     }
205 }