1   package com.sourceforge.jpatterns.core.configuration.model;
2   
3   import com.sourceforge.jpatterns.core.JPConstants;
4   import com.sourceforge.jpatterns.schema.JPatternsConfig;
5   import com.zmicer.utils.LoggingUtils;
6   import junit.framework.Test;
7   import junit.framework.TestCase;
8   import junit.framework.TestSuite;
9   import org.apache.log4j.Logger;
10  
11  /**
12   * $Author:: zmicer             $<br/>
13   * $Rev:: 57                    $<br/> * $Date:: 2007-08-23 09:16:37 #$<br/>
14   * $Date:: 2007-08-23 09:16:37 #$<br/>
15   */
16  public class JPatternsConfigBeanTest extends TestCase
17  {
18      /**
19       * Logger instance.
20       */
21      final public static Logger LOG = Logger.getLogger(JPatternsConfigBeanTest.class);
22  
23      /**
24       * Instance of the covered class to be used below in the tests
25       */
26      private JPatternsConfigBean m_jPatternsConfigBean;
27  
28      /**
29       * Contructor with name of test attribute.
30       *
31       * @param name name of the test
32       */
33      public JPatternsConfigBeanTest(String name)
34      {
35          super(name);
36          m_jPatternsConfigBean = new JPatternsConfigBean();
37      }
38  
39      /**
40       * Perform the set up functionality for the test.
41       *
42       * @throws Exception may occur in the case of some problems
43       */
44      public void setUp() throws Exception
45      {
46          super.setUp();
47      }
48  
49      /**
50       * Perform the tear down functionality for the test
51       *
52       * @throws Exception may occur in the case of some problems
53       */
54      public void tearDown() throws Exception
55      {
56          super.tearDown();
57      }
58  
59      /**
60       * Test suite method
61       *
62       * @return the built test suite
63       */
64      public static Test suite()
65      {
66          return new TestSuite(JPatternsConfigBeanTest.class);
67      }
68  
69      /**
70       * Tests {@link JPatternsConfigBean#check}
71       *
72       * @throws Exception in the case smth. wrong occuried.
73       * @forversion 1.0
74       */
75      public void testCheck() throws Exception
76      {
77          // normal sub-case: no exception: just assert the newly instantiated bean is in the incorrect composition and could not be used
78          // without addtional operations (e.g. setting the castor configuration object)
79          try
80          {
81              assertFalse(m_jPatternsConfigBean.check());
82              JPatternsConfig config = new JPatternsConfig();
83              m_jPatternsConfigBean.setCastorConfig(config);
84              assertTrue(m_jPatternsConfigBean.check());
85              assertNotNull(m_jPatternsConfigBean.getDefaultScope());
86              assertEquals(m_jPatternsConfigBean.getDefaultScope(), JPConstants.DEFAULT_SCOPE_NAME);
87          }
88          catch (Exception ex)
89          {
90              LoggingUtils.logException(LOG, ex, null, null);
91              fail("Exception should not occur for that case params" + ex.getMessage());
92          }
93      }
94  
95      /**
96       * Tests {@link JPatternsConfigBean#set/getCastorConfig}
97       *
98       * @throws Exception in the case smth. wrong occuried.
99       * @forversion 1.0
100      */
101     public void testSetGetCastorConfig() throws Exception
102     {
103         // normal sub-case: no exception: default value
104         try
105         {
106             assertFalse(m_jPatternsConfigBean.check());
107             assertNull(m_jPatternsConfigBean.getCastorConfig());
108             assertNull(m_jPatternsConfigBean.getDefaultScope());
109         }
110         catch (Exception ex)
111         {
112             LoggingUtils.logException(LOG, ex, null, null);
113             fail("Exception should not occur for that case params" + ex.getMessage());
114         }
115         // failed sub-case: exception occuried: set the null castor config
116         try
117         {
118             m_jPatternsConfigBean.setCastorConfig(null);
119             fail("Exception should have been occuried before this line.");
120         }
121         catch (Exception ex)
122         {
123             assertTrue(ex instanceof IllegalArgumentException);
124         }
125 
126         // normal sub-case: no exception: set the castor config
127         try
128         {
129             // 00: without the default scope
130             final JPatternsConfig config = new JPatternsConfig();
131             m_jPatternsConfigBean.setCastorConfig(config);
132             assertNotNull(m_jPatternsConfigBean.getCastorConfig());
133             assertEquals(m_jPatternsConfigBean.getCastorConfig(), config);
134             assertNotNull(m_jPatternsConfigBean.getDefaultScope());
135             assertEquals(m_jPatternsConfigBean.getDefaultScope(), JPConstants.DEFAULT_SCOPE_NAME);
136 
137             // 01: with default scope
138             final JPatternsConfig config2 = new JPatternsConfig();
139             config2.setDefaultScope(JPConstants.DEFAULT_SCOPE_NAME + "_");
140             m_jPatternsConfigBean.setCastorConfig(config2);
141             assertNotNull(m_jPatternsConfigBean.getCastorConfig());
142             assertEquals(m_jPatternsConfigBean.getCastorConfig(), config2);
143             assertNotNull(m_jPatternsConfigBean.getDefaultScope());
144             assertEquals(m_jPatternsConfigBean.getDefaultScope(), JPConstants.DEFAULT_SCOPE_NAME + "_");
145         }
146         catch (Exception ex)
147         {
148             LoggingUtils.logException(LOG, ex, null, null);
149             fail("Exception should not occur for that case params" + ex.getMessage());
150         }
151     }
152 
153     /**
154      * Tests {@link JPatternsConfigBean#set/getDefaultScope}
155      *
156      * @throws Exception in the case smth. wrong occuried.
157      * @forversion 1.0
158      */
159     public void testSetGetDefaultScope() throws Exception
160     {
161         // normal sub-case: no exception: check default value + check get/set
162         try
163         {
164             assertNull(m_jPatternsConfigBean.getDefaultScope());
165 
166             m_jPatternsConfigBean.setDefaultScope(null);
167             assertNotNull(m_jPatternsConfigBean.getDefaultScope());
168             assertEquals(m_jPatternsConfigBean.getDefaultScope(), JPConstants.DEFAULT_SCOPE_NAME);
169 
170             m_jPatternsConfigBean.setDefaultScope(JPConstants.DEFAULT_SCOPE_NAME + "_");
171             assertNotNull(m_jPatternsConfigBean.getDefaultScope());
172             assertEquals(m_jPatternsConfigBean.getDefaultScope(), JPConstants.DEFAULT_SCOPE_NAME + "_");
173         }
174         catch (Exception ex)
175         {
176             LoggingUtils.logException(LOG, ex, null, null);
177             fail("Exception should not occur for that case params" + ex.getMessage());
178         }
179     }
180 }