1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2001-2004 Open World Ltd
4   //
5   // This file is part of MillScript.
6   //
7   // MillScript is free software; you can redistribute it and/or modify it under
8   // the terms of the GNU General Public License as published by the Free
9   // Software Foundation; either version 2 of the License, or (at your option)
10  // any later version.
11  //
12  // MillScript is distributed in the hope that it will be useful, but WITHOUT
13  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  // more details.
16  //
17  // You should have received a copy of the GNU General Public License along with
18  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
19  // Place, Suite 330, Boston, MA  02111-1307  USA
20  ////////////////////////////////////////////////////////////////////////////////
21  package org.millscript.millscript;
22  
23  import org.millscript.commons.util.IMap;
24  import org.millscript.commons.util.ListIterator;
25  import org.millscript.commons.util.list.ELinkedList;
26  import org.millscript.millscript.datatypes.DatabaseSource;
27  import org.millscript.millscript.datatypes.Fragment;
28  import org.millscript.millscript.expr.GlobalIdent;
29  import org.millscript.millscript.functions.Function;
30  import org.millscript.millscript.vm.Package;
31  
32  import java.util.Properties;
33  
34  import junit.framework.Test;
35  import junit.framework.TestSuite;
36  
37  /**
38   * Unit test for Millscript autoloading system. This actually allows us to test
39   * a large portion of the Millscript sub system. We cannot check the results of
40   * autoloading if large portions of the language are not functioning.
41   *
42   * @author  <a href="mailto:moya@users.sourceforge.net">Kevin Rogers</a>
43   */
44  public class AutoloadingTest extends AbstractTestCase {
45  
46      /**
47       * This will hold the package we load from disk.
48       */
49      private Package autoloadedPackage;
50  
51      /**
52       * We will import the autoloaded package into this one. This lets us test
53       * if imports are working properly.
54       */
55      private Package importPackage;
56  
57      /**
58       * Create the test case
59       *
60       * @param   testName    name of the test case
61       */
62      public AutoloadingTest( String testName ) {
63          super( testName );
64      }
65  
66      /**
67       * @return  the suite of tests being tested
68       */
69      public static Test suite() {
70        return new TestSuite( AutoloadingTest.class );
71      }
72  
73      /**
74       * Initialize the test fixture. In this instance we require a machine with a
75       * standard environment, from which we get the "autoloading" package
76       * environment(i.e. we import it). We can then proceed with testing the
77       * autoloading mechanism.
78       */
79      @Override
80      public void setUp() {
81  
82          super.setUp();
83  
84          // We want to load packages from here..
85          engine.addInventory( getResource( "test/millscript" ) );
86  
87          // We need an environment to load the package into.
88          importPackage = engine.newPackage( "import.into" );
89  
90          // Try to import the autoloading package.
91          autoloadedPackage =  engine.getPackage( "autoloading" );
92  
93          // Check that the package import didn't fail
94          assertNotNull( autoloadedPackage );
95  
96          importPackage.importPackage( "autoloading", autoloadedPackage );
97  
98      }
99  
100     /**
101      * Clean up the test fixture by reseting the millscript machine.
102      */
103     @Override
104     public void tearDown() {
105         engine.getMachine().reset();
106     }
107 
108     /**
109      * Get the ident for the supplied symbol.
110      *
111      * @param   symbol  the string containing the symbol name
112      * @return  the ident for the specified symbol
113      */
114     public GlobalIdent findIdent( String symbol ) {
115 
116         // Find the ident for the symbol in the autoloaded package environment
117         GlobalIdent id = autoloadedPackage.findIdentFor( null, symbol );
118 
119         // Assert that an ident was found
120         assertNotNull( id );
121 
122         // Find the ident for the symbol via the standard environment
123         GlobalIdent stdId = importPackage.findIdentFor( "autoloading", symbol );
124 
125         // Check that the ident can be found through the standard environment
126         assertNotNull( stdId );
127 
128         assertEquals( id, stdId );
129 
130         return id;
131 
132     }
133 
134     /**
135      * Get the value associated with the supplied symbol.
136      *
137      * @param   symbol  the string containing the symbol name
138      * @return  the object associated with the symbol
139      */
140     public Object getValue( String symbol ) {
141 
142         return findIdent( symbol ).getValue();
143 
144     }
145 
146     /**
147      * Check items in the package loader millscript file are defined correctly.
148      */
149     public void testPackageFileVars() {
150 
151         // Check that the symbol is declared in the autoloaded package
152         // environment
153         assertTrue( autoloadedPackage.hasBindingFor( "runtests" ) );
154 
155         assertTrue( getValue( "runtests" ) instanceof Function );
156 
157         // Check that the symbol is declared in the autoloaded package
158         // environment
159         assertTrue( autoloadedPackage.hasBindingFor( "testvar" ) );
160 
161         assertNull( getValue( "testvar" ) );
162 
163     }
164 
165     /**
166      * Check the lines loader is functioning correctly.
167      */
168     public void testLines() {
169 
170         // Get the object associated with the symbol "autolines".
171         Object unknown = getValue( "autolines" );
172 
173         // Assert that the object is an ELinkedList, the type of object
174         // returned by the lines loader.
175         assertTrue( unknown instanceof ELinkedList );
176 
177         // Assert that each item in the ELinkedList is a String. The structure
178         // returned by the lines loader is a list-of-strings.
179         ListIterator< ? > it = ((ELinkedList) unknown).iterator( true );
180 
181         while( it.hasNext() ) {
182             assertTrue( it.nextValue() instanceof String );
183         }
184 
185     }
186 
187     /**
188      * Check the map loader is functioning correctly.
189      */
190     @SuppressWarnings( "unchecked" )
191     public void testMap() {
192 
193         Object unknown = getValue( "automap" );
194 
195         assertTrue( unknown instanceof IMap );
196 
197         IMap< Object, Object > automap = (IMap) unknown;
198 
199         assertTrue( automap.get( "autolines" ) instanceof ELinkedList );
200         assertTrue( automap.get( "automapmillscript" ) instanceof Function );
201         assertEquals( "automapmillscript", ((Function) automap.get( "automapmillscript" )).getName() );
202         assertTrue( automap.get( "autoproperties" ) instanceof Properties );
203         assertTrue( automap.get( "autoskeleton" ) instanceof Function );
204         assertTrue( automap.get( "autostring" ) instanceof String );
205         assertTrue( automap.get( "autotemplate" ) instanceof Function );
206         assertTrue( automap.get( "autotemplate2" ) instanceof Function );
207         assertTrue( automap.get( "autoxml" ) instanceof Fragment );
208         assertTrue( automap.get( "localhost" ) instanceof DatabaseSource );
209         assertTrue( automap.get( "autoquery" ) instanceof Function );
210         assertTrue( automap.get( "autoupdate" ) instanceof Function );
211 
212     }
213 
214     /**
215      * Check the millscript loader is functioning correctly.
216      */
217     public void testMs() {
218 
219         Object unknown = getValue( "automillscript" );
220 
221         assertTrue( unknown instanceof Function );
222         assertEquals( "automillscript", ((Function)unknown).getName() );
223 
224     }
225 
226     /**
227      * Check the properties loader is functioning correctly.
228      */
229     public void testProperties() {
230 
231         Object unknown = getValue( "autoproperties" );
232 
233         assertTrue( unknown instanceof Properties );
234         assertEquals( 3, ((Properties)unknown).size() );
235     }
236 
237     /**
238      * Check the skeleton file loader is functioning correctly.
239      */
240     public void testSk() {
241 
242         assertTrue( getValue( "automillscript" ) instanceof Function );
243 
244     }
245 
246     /**
247      * Check the template file loader is functioning correctly.
248      */
249     public void testTp() {
250 
251         assertTrue( getValue( "autotemplate" ) instanceof Function );
252         assertTrue( getValue( "autotemplate2" ) instanceof Function );
253 
254     }
255 
256     /**
257      * Check the text file loader is functioning correctly.
258      */
259     public void testTxt() {
260 
261         Object unknown = getValue( "autostring" );
262 
263         assertTrue( unknown instanceof String );
264         assertEquals( "this is a string", ((String)unknown) );
265 
266     }
267 
268     /**
269      * Check the XML file loader is functioning correctly.
270      */
271     public void testXml() {
272 
273         assertTrue( getValue( "autoxml" ) instanceof Fragment );
274 
275     }
276 
277     /**
278      * Check the JDBC connection file loader is functioning correctly.
279      */
280     public void testJDBCConnection() {
281 
282         assertTrue( getValue( "localhost" ) instanceof DatabaseSource );
283 
284     }
285 
286     /**
287      * Check the SQL query file loader is functioning correctly.
288      */
289     public void testSQLQuery() {
290 
291         assertTrue( getValue( "autoquery" ) instanceof Function );
292 
293     }
294 
295     /**
296      * Check the SQL update file loader is functioning correctly.
297      */
298     public void testSQLUpdate() {
299 
300         assertTrue( getValue( "autoupdate" ) instanceof Function );
301 
302     }
303 
304 }