root/trunk/test/java/org/jcoderz/phoenix/sqlparser/SqlScannerTest.java

Revision 1011, 5.4 kB (checked in by amandel, 4 years ago)

Aligned svn keyword settings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 *
4 * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *    * Redistributions of source code must retain the above copyright
11 *      notice, this list of conditions and the following disclaimer.
12 *    * Redistributions in binary form must reproduce the above
13 *      copyright notice, this list of conditions and the following
14 *      disclaimer in the documentation and/or other materials
15 *      provided with the distribution.
16 *    * Neither the name of the jCoderZ.org Project nor the names of
17 *      its contributors may be used to endorse or promote products
18 *      derived from this software without specific prior written
19 *      permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33package org.jcoderz.phoenix.sqlparser;
34
35import java.io.File;
36import java.io.FileInputStream;
37import java.io.FileReader;
38import java.io.FilenameFilter;
39import java.io.LineNumberReader;
40import java.io.StringReader;
41import java.io.StringWriter;
42import java.util.ArrayList;
43import java.util.Iterator;
44import java.util.List;
45import junit.framework.Test;
46import junit.framework.TestCase;
47import junit.framework.TestSuite;
48
49/**
50 * JUnit test for the {@link org.jcoderz.phoenix.sqlparser.SqlScanner}.
51 * 
52 * @author Michael Griffel
53 */
54public class SqlScannerTest 
55      extends TestCase
56{
57   private static final String SQL_DIRECTORY = "test/data";
58   /**
59    * Generating this test suite.
60    * @return this test suite.
61    */
62   public static Test suite ()
63   {
64      final File dir = new File(getSqlDirectory());
65      final String[] sqlFiles = dir.list(new SqlFilenameFilter());
66      if (sqlFiles == null || sqlFiles.length == 0)
67      {
68         throw new RuntimeException("[Error] " 
69               + "No SQL files found at '" + dir + "'!");
70      }     
71
72      final TestSuite suite = new TestSuite();
73      for (int i = 0; i < sqlFiles.length; i++)
74      {
75         suite.addTest(new XmlFileValidation(dir, sqlFiles[i]));
76      }
77     
78      return suite;
79   }
80   
81   private static String getSqlDirectory ()
82   {
83      return System.getProperty("basedir", ".") 
84         + File.separator + SQL_DIRECTORY;
85   }
86
87   static class SqlFilenameFilter 
88         implements FilenameFilter
89   {
90      /** {@inheritDoc} */
91      public boolean accept (File dir, String name)
92      {
93         return name.endsWith(".sql");
94      }
95   }
96
97
98   static class XmlFileValidation 
99         extends TestCase
100   {     
101      private final String mSqlFileName;
102      private final File mDir;
103
104      public XmlFileValidation (File dir, String sqlFileName)
105      {
106         super(sqlFileName);
107         mSqlFileName = sqlFileName;
108         mDir = dir;
109      }
110
111      public void runTest () 
112            throws Throwable
113      {
114         testSqlFile();
115      }
116
117      public void testSqlFile ()
118            throws Exception
119      {
120         final File file = new File(mDir, mSqlFileName);
121         final StringWriter writer = new StringWriter();
122         try
123         {
124            final SqlScanner scanner
125                  = new SqlScanner(new FileInputStream(file));
126
127            final List tokens = new ArrayList();
128   
129            for (;;)
130            {
131               final Token t = scanner.nextToken();
132               if (t.getType() == TokenType.EOF)
133               {
134                  break;
135               }
136               tokens.add(t);
137            }
138   
139            for (final Iterator iterator = tokens.iterator(); 
140                  iterator.hasNext();)
141            {
142               final Token t = (Token) iterator.next();
143               writer.write(t.getValue());
144            }
145            writer.close();
146         }
147         catch (Exception e)
148         {
149            e.printStackTrace();
150            fail("Expected well-formed SQL file " + file + " : " + e);
151         }
152         diff(file, new LineNumberReader(new StringReader(writer.toString())));
153      }
154
155      private void diff (File file, LineNumberReader reader) 
156            throws Exception
157      {
158         final LineNumberReader org
159               = new LineNumberReader(new FileReader(file));
160         for (;;)
161         {
162            final String line1 = org.readLine();
163            if (line1 == null) // EOF
164            {
165               break; 
166            }
167            final String line2 = reader.readLine();
168            if (line2 == null) // EOF
169            {
170               fail("Unexpected end of file"); 
171            }
172            assertEquals("File: " + file + " Line " + org.getLineNumber() 
173                  + " should be equals", line1, line2);
174               
175         }
176      }
177 
178   }
179}
Note: See TracBrowser for help on using the browser.