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/executerImpl/src/test/java/name/angoca/zemucan/executer/impl/ImplementationExecuterTest.java $:
28   */
29  package name.angoca.zemucan.executer.impl;
30  
31  import name.angoca.zemucan.ParameterNullException;
32  import name.angoca.zemucan.tools.Constants;
33  import name.angoca.zemucan.tools.configurator.Configurator;
34  import name.angoca.zemucan.tools.test.RandomTestRunner;
35  
36  import org.junit.After;
37  import org.junit.Assert;
38  import org.junit.Test;
39  import org.junit.runner.RunWith;
40  
41  /**
42   * Set of test for the Executer.
43   * <p>
44   * <b>Control Version</b>
45   * <p>
46   * <ul>
47   * <li>0.0.1 Class creation.</li>
48   * <li>1.0.0 Moved to version 1.</li>
49   * <li>1.1.0 Name changed.</li>
50   * <li>1.2.0 Set of tests.</li>
51   * <li>1.3.0 Setup method before each call.</li>
52   * <li>1.4.0 throws.</li>
53   * <li>1.5.0 null command.</li>
54   * <li>1.5.1 Reestablishes the os.name property.</li>
55   * <li>1.5.2 Execution in Linux.</li>
56   * <li>1.5.3 After method instead of before.</li>
57   * </ul>
58   *
59   * @author Andres Gomez Casanova <a
60   *         href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a>
61   * @version 1.5.3 2010-09-25
62   */
63  @RunWith(RandomTestRunner.class)
64  public final class ImplementationExecuterTest {
65  
66      /**
67       * Default constructor.
68       */
69      public ImplementationExecuterTest() {
70          // Nothing.
71      }
72  
73      /**
74       * Destroys the singletons.
75       */
76      @After
77      public void tearDown() {
78          // Destroys the configurator instance.
79          Configurator.destroyInstance();
80  
81          // Destroys the executer instance.
82          ImplementationExecuter.destroyInstance();
83      }
84  
85      /**
86       * Tests if the configuration file has a true value for DB2 native
87       * execution.
88       */
89      @Test
90      public void testDB2Native() {
91          // Deletes the property, loading other configuration file.
92          System.setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
93                  "sa_conf-test-DB2Native.xml"); //$NON-NLS-1$
94  
95          final String actualString = Configurator.getInstance().getProperty(
96                  ImplementationExecuter.DB2_NATIVE_PROPERTY);
97          final boolean condition = Boolean.parseBoolean(actualString);
98  
99          Assert.assertTrue("Yes DB2 native", condition); //$NON-NLS-1$
100     }
101 
102     /**
103      * Tests the execution in Linux environment without db2 parameters.
104      *
105      * @throws ParameterNullException
106      *             Never.
107      */
108     @Test
109     public void testExecutionInLinuxDB2WithoutParams()
110             throws ParameterNullException {
111         // Deletes the property, loading other configuration file.
112         System.setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
113                 "sa_conf-test-NoDB2Native.xml"); //$NON-NLS-1$
114 
115         // Sets an environment property.
116         System.clearProperty(ImplementationExecuter.DB2_PARAMS);
117         System.setProperty("os.name", "Linux Virtual"); //$NON-NLS-1$ //$NON-NLS-2$
118 
119         final String command = "db2 !ls"; //$NON-NLS-1$
120         final String[] actuals = ImplementationExecuter.getInstance()
121                 .getCommandsToExecute(command);
122 
123         final String[] expecteds = new String[] { "db2", "!ls" }; //$NON-NLS-1$ //$NON-NLS-2$
124 
125         Assert.assertArrayEquals("DB2 commands in Linux without parameters", //$NON-NLS-1$
126                 expecteds, actuals);
127     }
128 
129     /**
130      * Tests the execution in Linux environment with db2 parameters.
131      *
132      * @throws ParameterNullException
133      *             Never.
134      */
135     @Test
136     public void testExecutionInLinuxDB2WithtParams()
137             throws ParameterNullException {
138         // Deletes the property, loading other configuration file.
139         System.setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
140                 "sa_conf-test-NoDB2Native.xml"); //$NON-NLS-1$
141 
142         // Sets an environment property.
143         System.setProperty(ImplementationExecuter.DB2_PARAMS, "-v"); //$NON-NLS-1$
144         System.setProperty("os.name", "Linux Virtual"); //$NON-NLS-1$ //$NON-NLS-2$
145 
146         final String command = "db2 !ls"; //$NON-NLS-1$
147         final String[] actuals = ImplementationExecuter.getInstance()
148                 .getCommandsToExecute(command);
149 
150         final String[] expecteds = new String[] { "db2", "-v", "!ls" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
151 
152         Assert.assertArrayEquals("DB2 commands in Linux with parameters", //$NON-NLS-1$
153                 expecteds, actuals);
154     }
155 
156     /**
157      * Tests the execution in Linux environment no db2 command.
158      *
159      * @throws ParameterNullException
160      *             Never.
161      */
162     @Test
163     public void testExecutionInLinuxNoDB2() throws ParameterNullException {
164         // Deletes the property, loading other configuration file.
165         System.setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
166                 "sa_conf-test-NoDB2Native.xml"); //$NON-NLS-1$
167 
168         // Sets an environment property.
169         System.clearProperty(ImplementationExecuter.DB2_PARAMS);
170         System.setProperty("os.name", "Linux Virtual"); //$NON-NLS-1$ //$NON-NLS-2$
171 
172         final String command = "ls"; //$NON-NLS-1$
173         final String[] actuals = ImplementationExecuter.getInstance()
174                 .getCommandsToExecute(command);
175 
176         final String[] expecteds = new String[] { "ls" }; //$NON-NLS-1$
177 
178         Assert.assertArrayEquals("No DB2 commands in Linux", expecteds, //$NON-NLS-1$
179                 actuals);
180     }
181 
182     /**
183      * Tests the execution in an unknown OS.
184      *
185      * @throws ParameterNullException
186      *             Never.
187      */
188     @Test
189     public void testExecutionInUnknownOS() throws ParameterNullException {
190         // Deletes the property, loading other configuration file.
191         System.setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
192                 "sa_conf-test-NoDB2Native.xml"); //$NON-NLS-1$
193 
194         // Sets an environment property.
195         System.setProperty(ImplementationExecuter.DB2_PARAMS, "-v"); //$NON-NLS-1$
196         System.setProperty("os.name", "Unknown OS"); //$NON-NLS-1$ //$NON-NLS-2$
197 
198         final String command = "db2 !dir"; //$NON-NLS-1$
199         final String[] actuals = ImplementationExecuter.getInstance()
200                 .getCommandsToExecute(command);
201 
202         final String[] expecteds = new String[] { "!dir" }; //$NON-NLS-1$
203 
204         Assert.assertArrayEquals("DB2 commands in an unknown OS", expecteds, //$NON-NLS-1$
205                 actuals);
206     }
207 
208     /**
209      * Tests the execution in Windows environment without db2 parameters.
210      *
211      * @throws ParameterNullException
212      *             Never.
213      */
214     @Test
215     public void testExecutionInWindowsDB2WithoutParams()
216             throws ParameterNullException {
217         // Deletes the property, loading other configuration file.
218         System.setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
219                 "sa_conf-test-NoDB2Native.xml"); //$NON-NLS-1$
220 
221         // Sets an environment property.
222         System.clearProperty(ImplementationExecuter.DB2_PARAMS);
223         final String command = "db2 !dir"; //$NON-NLS-1$
224         final String os = System.getProperty("os.name"); //$NON-NLS-1$
225 
226         System.setProperty("os.name", "Windows 7"); //$NON-NLS-1$ //$NON-NLS-2$
227         final String[] actuals = ImplementationExecuter.getInstance()
228                 .getCommandsToExecute(command);
229         System.setProperty("os.name", os);
230 
231         final String[] expecteds = new String[] {
232                 "cmd.exe", "/c", "db2", "!dir" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
233 
234         Assert.assertArrayEquals("DB2 commands in Windows without parameters", //$NON-NLS-1$
235                 expecteds, actuals);
236     }
237 
238     /**
239      * Tests the execution in Windows environment with db2 parameters.
240      *
241      * @throws ParameterNullException
242      *             Never.
243      */
244     @Test
245     public void testExecutionInWindowsDB2WithParams()
246             throws ParameterNullException {
247         // Deletes the property, loading other configuration file.
248         System.setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
249                 "sa_conf-test-NoDB2Native.xml"); //$NON-NLS-1$
250 
251         // Sets an environment property.
252         System.setProperty(ImplementationExecuter.DB2_PARAMS, "-v"); //$NON-NLS-1$
253         final String command = "db2 !dir"; //$NON-NLS-1$
254         final String os = System.getProperty("os.name"); //$NON-NLS-1$
255 
256         System.setProperty("os.name", "Windows 7"); //$NON-NLS-1$ //$NON-NLS-2$
257         final String[] actuals = ImplementationExecuter.getInstance()
258                 .getCommandsToExecute(command);
259         System.setProperty("os.name", os); //$NON-NLS-1$
260 
261         final String[] expecteds = new String[] { "cmd.exe", "/c", "db2", "-v", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
262                 "!dir" }; //$NON-NLS-1$
263 
264         Assert.assertArrayEquals("DB2 commands in Windows with parameters", //$NON-NLS-1$
265                 expecteds, actuals);
266     }
267 
268     /**
269      * Tests the execution in Windows environment no db2 command.
270      *
271      * @throws ParameterNullException
272      *             Never.
273      */
274     @Test
275     public void testExecutionInWindowsNoDB2() throws ParameterNullException {
276         // Deletes the property, loading other configuration file.
277         System.setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
278                 "sa_conf-test-NoDB2Native.xml"); //$NON-NLS-1$
279 
280         // Sets an environment property.
281         System.clearProperty(ImplementationExecuter.DB2_PARAMS);
282         final String command = "dir"; //$NON-NLS-1$
283         final String os = System.getProperty("os.name"); //$NON-NLS-1$
284 
285         System.setProperty("os.name", "Windows 7"); //$NON-NLS-1$ //$NON-NLS-2$
286         final String[] actuals = ImplementationExecuter.getInstance()
287                 .getCommandsToExecute(command);
288         System.setProperty("os.name", os); //$NON-NLS-1$
289 
290         final String[] expecteds = new String[] { "cmd.exe", "/c", "dir" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
291 
292         Assert.assertArrayEquals("No DB2 commands in Windows", expecteds, //$NON-NLS-1$
293                 actuals);
294     }
295 
296     /**
297      * Tests the execution with db2 parameters.
298      *
299      * @throws ParameterNullException
300      *             Never.
301      */
302     @Test
303     public void testExecutionWithDB2Params() throws ParameterNullException {
304         // Deletes the property, loading other configuration file.
305         System.setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
306                 "sa_conf-test-DB2Native.xml"); //$NON-NLS-1$
307 
308         // Sets an environment property.
309         System.setProperty(ImplementationExecuter.DB2_PARAMS, "-v"); //$NON-NLS-1$
310 
311         final String command = "db2 !dir"; //$NON-NLS-1$
312         final String[] actuals = ImplementationExecuter.getInstance()
313                 .getCommandsToExecute(command);
314 
315         final String[] expecteds = new String[] { "db2", "-v", "!dir" }; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
316 
317         Assert.assertArrayEquals("DB2 commands with parameters", expecteds, //$NON-NLS-1$
318                 actuals);
319     }
320 
321     /**
322      * Tests the execution without db2 parameters.
323      *
324      * @throws ParameterNullException
325      *             Never.
326      */
327     @Test
328     public void testExecutionWithNoDB2Params() throws ParameterNullException {
329         // Deletes the property, loading other configuration file.
330         System.setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
331                 "sa_conf-test-DB2Native.xml"); //$NON-NLS-1$
332 
333         // Sets an environment property.
334         System.clearProperty(ImplementationExecuter.DB2_PARAMS);
335 
336         final String command = "db2 !dir"; //$NON-NLS-1$
337         final String[] actuals = ImplementationExecuter.getInstance()
338                 .getCommandsToExecute(command);
339 
340         final String[] expecteds = new String[] { "db2", "!dir" }; //$NON-NLS-1$//$NON-NLS-2$
341 
342         Assert.assertArrayEquals("DB2 commands without parameters", expecteds, //$NON-NLS-1$
343                 actuals);
344     }
345 
346     /**
347      * Tests the execution of a null command.
348      *
349      * @throws ParameterNullException
350      *             Never.
351      */
352     @Test
353     public void testExecutionWithNullParam() throws ParameterNullException {
354         final String command = null;
355 
356         try {
357             ImplementationExecuter.getInstance().getCommandsToExecute(command);
358         } catch (final Exception exception) {
359             Assert.assertTrue("ParameterNullException",
360                     exception instanceof ParameterNullException);
361         }
362     }
363 
364     /**
365      * Tests the default token to exit the application.
366      */
367     @Test
368     public void testExitEmpty() {
369         // Deletes the property, loading other configuration file.
370         System.setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
371                 "sa_conf-test-exitEmpty.xml"); //$NON-NLS-1$
372 
373         final String actual = ImplementationExecuter.getInstance().getTokens();
374 
375         final String expected = ImplementationExecuter.EXIT_VALUE;
376 
377         Assert.assertEquals("Tokens to finish the application", expected, //$NON-NLS-1$
378                 actual);
379     }
380 
381     /**
382      * Tests if the configuration file has a false value for DB2 native
383      * execution.
384      */
385     @Test
386     public void testNoDB2Native() {
387         // Deletes the property, loading other configuration file.
388         System.setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
389                 "sa_conf-test-NoDB2Native.xml"); //$NON-NLS-1$
390 
391         final String actualString = Configurator.getInstance().getProperty(
392                 ImplementationExecuter.DB2_NATIVE_PROPERTY);
393         final boolean condition = Boolean.parseBoolean(actualString);
394 
395         Assert.assertFalse("No DB2 native", condition); //$NON-NLS-1$
396     }
397 }