View Javadoc

1   /*
2    * Zemucan: A Syntax Assistant for DB2
3    * Copyright (C) 2009, 2010 Andres Gomez Casanova
4    *
5    * This file is part of Zemucan.
6    *
7    * Zemucan is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as published by
9    * the Free Software Foundation; either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * Zemucan is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public License
18   * along with this library; if not, see <http://www.gnu.org/licenses/>.
19   *
20   * Contact:
21   * a n g o c a  at  y a h o o  dot  c o m
22   * Cra. 45 No 61 - 31, Bogota, Colombia.
23   *
24   * Author:   $LastChangedBy: angoca $:
25   * Date:     $LastChangedDate: 2011-03-06 22:39:08 -0500 (dom, 06 mar 2011) $:
26   * Revision: $LastChangedRevision: 1916 $:
27   * URL:      $HeadURL: https://zemucan.svn.sourceforge.net/svnroot/zemucan/branches/zemucan_v1/source-code/tools/src/test/java/name/angoca/zemucan/tools/configurator/ConfiguratorTest.java $:
28   */
29  package name.angoca.zemucan.tools.configurator;
30  
31  import name.angoca.zemucan.tools.Constants;
32  import name.angoca.zemucan.tools.test.RandomTestRunner;
33  
34  import org.junit.After;
35  import org.junit.AfterClass;
36  import org.junit.Assert;
37  import org.junit.Before;
38  import org.junit.Test;
39  import org.junit.runner.RunWith;
40  
41  /**
42   * Tests for the configurator.
43   * <p>
44   * <b>Control Version</b>
45   * <p>
46   * <ul>
47   * <li>0.0.1 Class creation.</li>
48   * <li>0.1.0 More tests.</li>
49   * <li>0.1.1 imports and final.</li>
50   * <li>0.2.0 Setup class, and configuration file name.</li>
51   * <li>1.0.0 Moved to version 1.</li>
52   * <li>1.0.1 Grammar file for tests.</li>
53   * <li>1.1.0 Delimiters deleted.</li>
54   * <li>1.1.1 Exit and prompt properties.</li>
55   * <li>1.2.0 New tests.</li>
56   * <li>1.2.1 Extra tokens problem.</li>
57   * <li>1.2.2 Destroy instance in after method.</li>
58   * </ul>
59   *
60   * @author Andres Gomez Casanova <a
61   *         href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a>
62   * @version 1.2.2 2010-09-25
63   */
64  @RunWith(RandomTestRunner.class)
65  public class ConfiguratorTest {
66  
67      /**
68       * Word 'A'.
69       */
70      private static final String A = "A"; //$NON-NLS-1$
71      /**
72       * Words 'About token'.
73       */
74      private static final String ABOUT_TOKEN = "About token"; //$NON-NLS-1$
75      /**
76       * Words 'Grammar fileName'.
77       */
78      private static final String GRAMMAR_FILE_NAME = "Grammar fileName"; //$NON-NLS-1$
79      /**
80       * Words 'Help token'.
81       */
82      private static final String HELP_TOKEN = "Help token"; //$NON-NLS-1$
83      /**
84       * Words 'sa_conf-test.xml'.
85       */
86      private static final String SA_CONF_TEST_XML = "sa_conf-test.xml"; //$NON-NLS-1$
87  
88      /**
89       * Clears the configuration file name property at the end of the test.
90       */
91      @AfterClass
92      public static final void tearDownAfterClass() {
93          System.clearProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY);
94      }
95  
96      /**
97       * Clears the configuration file name property before each test.
98       */
99      @Before
100     public final void setUp() {
101         System.setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
102                 ConfiguratorTest.SA_CONF_TEST_XML);
103     }
104 
105     /**
106      * Destroys the configurator instance.
107      */
108     @After
109     public void tearDown() {
110         Configurator.destroyInstance();
111     }
112 
113     /**
114      * Tests a configuration file.
115      */
116     @Test
117     public final void testGetInstanceConf() {
118         // Sets a configuration file.
119         System.clearProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY);
120 
121         final Configurator instance = Configurator.getInstance();
122 
123         Assert.assertNotNull("Configurator instanced", instance); //$NON-NLS-1$
124     }
125 
126     /**
127      * Tests a configuration file for tests.
128      */
129     @Test
130     public final void testGetInstanceConfFileTest() {
131         // Sets a configuration file.
132         System.setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
133                 ConfiguratorTest.SA_CONF_TEST_XML);
134 
135         // Compares all the default values.
136         String actual = Configurator.getInstance().getProperty(
137                 Constants.ABOUT_TOKEN_PROPERTY);
138         String expected = "about"; //$NON-NLS-1$
139         Assert.assertEquals(ConfiguratorTest.ABOUT_TOKEN, expected, actual);
140 
141         actual = Configurator.getInstance().getProperty(
142                 Constants.GRAMMAR_FILE_DESCRIPTORS_PROPERTY);
143         expected = "grammar-test.xml"; //$NON-NLS-1$
144         Assert.assertEquals(ConfiguratorTest.GRAMMAR_FILE_NAME, expected,
145                 actual);
146 
147         actual = Configurator.getInstance().getProperty(
148                 Constants.HELP_TOKEN_PROPERTY);
149         expected = "?"; //$NON-NLS-1$
150         Assert.assertEquals(ConfiguratorTest.HELP_TOKEN, expected, actual);
151     }
152 
153     /**
154      * Tests the default values, when the configuration file cannot be found.
155      */
156     @Test
157     public final void testGetInstanceNoFile() {
158         // Sets an inexistent configuration file.
159         System
160                 .setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
161                         "NoFileSpecified"); //$NON-NLS-1$
162 
163         // Compares all the default values.
164         String actual = Configurator.getInstance().getProperty(
165                 Constants.ABOUT_TOKEN_PROPERTY);
166         String expected = Constants.ABOUT_TOKEN_VALUE;
167         Assert.assertEquals(ConfiguratorTest.ABOUT_TOKEN, expected, actual);
168 
169         actual = Configurator.getInstance().getProperty(
170                 Constants.GRAMMAR_FILE_DESCRIPTORS_PROPERTY);
171         expected = Configurator.GRAMMAR_FILE_DESCRIPTORS_VALUE;
172         Assert.assertEquals(ConfiguratorTest.GRAMMAR_FILE_NAME, expected,
173                 actual);
174 
175         actual = Configurator.getInstance().getProperty(
176                 Constants.HELP_TOKEN_PROPERTY);
177         expected = Constants.HELP_TOKEN_VALUE;
178         Assert.assertEquals(ConfiguratorTest.HELP_TOKEN, expected, actual);
179     }
180 
181     /**
182      * Tests a required property cannot be null.
183      */
184     @Test(expected = AssertionError.class)
185     public final void testGetNull() {
186         Configurator.getInstance().getProperty(null);
187     }
188 
189     /**
190      * Tests the load of the default properties after the read of a
191      * configuration file with an invalid structure.
192      */
193     @Test
194     public final void testReadInvalidFile() {
195         // Deletes the property, loading other configuration file.
196         System.setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
197                 "sa_conf-test-InvalidFormat.properties"); //$NON-NLS-1$
198 
199         final String actual = Configurator.getInstance().getProperty(
200                 Constants.HELP_TOKEN_PROPERTY);
201 
202         final String expected = Constants.HELP_TOKEN_VALUE;
203 
204         Assert.assertEquals("Defaults Loaded", expected, actual); //$NON-NLS-1$
205     }
206 
207     /**
208      *Test that the key cannot be null when setting.
209      */
210     @Test(expected = AssertionError.class)
211     public final void testSetNull1() {
212         Configurator.getInstance().setProperty(null, ConfiguratorTest.A);
213     }
214 
215     /**
216      * Tests that the value cannot be null when setting.
217      */
218     @Test(expected = AssertionError.class)
219     public final void testSetNull2() {
220         Configurator.getInstance().setProperty(ConfiguratorTest.A, null);
221     }
222 }