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/main/src/test/java/name/angoca/zemucan/IntegrationTest.java $:
28   */
29  package name.angoca.zemucan;
30  
31  import name.angoca.zemucan.core.syntactic.impl.ImplementationSyntacticAnalyzer;
32  import name.angoca.zemucan.executer.api.ExecutionState;
33  import name.angoca.zemucan.executer.impl.ImplementationExecuter;
34  import name.angoca.zemucan.grammarReader.api.GrammarReaderController;
35  import name.angoca.zemucan.tools.Constants;
36  import name.angoca.zemucan.tools.configurator.Configurator;
37  import name.angoca.zemucan.tools.test.RandomTestRunner;
38  import name.angoca.zemucan.ui.api.OutputWriter;
39  import name.angoca.zemucan.ui.impl.system.SystemOutputWriter;
40  
41  import org.junit.After;
42  import org.junit.Assert;
43  import org.junit.BeforeClass;
44  import org.junit.Test;
45  import org.junit.runner.RunWith;
46  
47  /**
48   * Integration tests.
49   * <p>
50   * <b>Control Version</b>
51   * <p>
52   * <ul>
53   * <li>1.0.0 Class creation.</li>
54   * <li>1.1.0 Refactored tests and Executer not in interface.</li>
55   * <li>1.1.1 After method.</li>
56   * </ul>
57   *
58   * @author Andres Gomez Casanova <a
59   *         href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a>
60   * @version 1.1.1 2010-09-25
61   */
62  @RunWith(RandomTestRunner.class)
63  public final class IntegrationTest {
64  
65      /**
66       * Refreshes the singletons.
67       *
68       * @throws AbstractZemucanException
69       *             Never.
70       */
71      @BeforeClass
72      public static void setUpBeforeClass() throws AbstractZemucanException {
73          System.setProperty(Constants.ZEMUCAN_CONF_XML_PROPERTY,
74                  "sa_conf-test.xml"); //$NON-NLS-1$
75          Configurator.getInstance().setProperty(
76                  Constants.GRAMMAR_READER_NAME_PROPERTY,
77                  Configurator.GRAMMAR_READER_NAME_VALUE);
78          Configurator.getInstance()
79                  .setProperty(Constants.GRAMMAR_FILE_DESCRIPTORS_PROPERTY,
80                          "grammar-test.xml"); //$NON-NLS-1$
81          ImplementationSyntacticAnalyzer.getInstance();
82      }
83  
84      /**
85       * Default constructor.
86       */
87      public IntegrationTest() {
88          // Nothing
89      }
90  
91      /**
92       * Destroys the singletons.
93       */
94      @After
95      public void tearDown() {
96          // Recreates the configurator instance.
97          Configurator.destroyInstance();
98  
99          GrammarReaderController.destroyInstance();
100 
101         // Recreates the syntax analyzer instance.
102         ImplementationSyntacticAnalyzer.destroyInstance();
103         // Destroys the executer instance.
104         ImplementationExecuter.destroyInstance();
105     }
106 
107     /**
108      * Tests the execution of a command 'hostname'.
109      *
110      * @throws AbstractZemucanException
111      *             Never.
112      */
113     @Test
114     public void testExecute() throws AbstractZemucanException {
115         final String command = "hostname"; //$NON-NLS-1$
116         final OutputWriter writer = new SystemOutputWriter();
117 
118         final ExecutionState actual = ImplementationExecuter.getInstance()
119                 .execute(command, writer);
120 
121         final ExecutionState expected = ExecutionState.EXECUTED;
122 
123         Assert.assertEquals("Execution of 'hostname'", expected, actual); //$NON-NLS-1$
124     }
125 
126     /**
127      * Tests the execution of a exit command.
128      *
129      * @throws AbstractZemucanException
130      *             Never.
131      */
132     @Test
133     public void testExecuteExit() throws AbstractZemucanException {
134         final String command = "exit"; //$NON-NLS-1$
135         final OutputWriter writer = new SystemOutputWriter();
136 
137         final ExecutionState actual = ImplementationExecuter.getInstance()
138                 .execute(command, writer);
139 
140         final ExecutionState expected = ExecutionState.APPLICATION_EXIT;
141 
142         Assert.assertEquals("Execution of 'exit'", expected, actual); //$NON-NLS-1$
143     }
144 
145     /**
146      * Tests to execute a null command.
147      *
148      * @throws AbstractZemucanException
149      *             Never.
150      */
151     @Test
152     public void testExecuteNullCommand() throws AbstractZemucanException {
153         final String command = null;
154 
155         final OutputWriter writer = null;
156 
157         Exception e = null;
158         try {
159             ImplementationExecuter.getInstance().execute(command, writer);
160         } catch (final Exception exception) {
161             e = exception;
162         }
163         Assert.assertTrue("ParameterNullException",
164                 e instanceof ParameterNullException);
165     }
166 
167     /**
168      * Tests to execute with a null writer.
169      *
170      * @throws AbstractZemucanException
171      *             Never.
172      */
173     @Test
174     public void testExecuteNullWriter() throws AbstractZemucanException {
175         final String command = "command"; //$NON-NLS-1$
176 
177         final OutputWriter writer = null;
178 
179         Exception e = null;
180         try {
181             ImplementationExecuter.getInstance().execute(command, writer);
182         } catch (final Exception exception) {
183             e = exception;
184         }
185         Assert.assertTrue("ParameterNullException",
186                 e instanceof ParameterNullException);
187     }
188 
189     /**
190      * Tests the execution of a quit command.
191      *
192      * @throws AbstractZemucanException
193      *             Never.
194      */
195     @Test
196     public void testExecuteQuit() throws AbstractZemucanException {
197         final String command = "quit"; //$NON-NLS-1$
198         final OutputWriter writer = new SystemOutputWriter();
199 
200         final ExecutionState actual = ImplementationExecuter.getInstance()
201                 .execute(command, writer);
202 
203         final ExecutionState expected = ExecutionState.APPLICATION_EXIT;
204 
205         Assert.assertEquals("Execution of 'quit'", expected, actual); //$NON-NLS-1$
206     }
207 }