Project Report: fawkez

Packagesummary org.jcoderz.phoenix.sqlparser

org.jcoderz.phoenix.sqlparser.SqlScannerTest

LineHitsNoteSource
1  /*
2   * $Id: SqlScannerTest.java 1011 2008-06-16 17:57:36Z amandel $
3   *
4   * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5 (1) * 
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   */
33  package org.jcoderz.phoenix.sqlparser;
34  
35  import java.io.File;
36  import java.io.FileInputStream;
37  import java.io.FileReader;
38  import java.io.FilenameFilter;
39  import java.io.LineNumberReader;
40  import java.io.StringReader;
41  import java.io.StringWriter;
42  import java.util.ArrayList;
43  import java.util.Iterator;
44  import java.util.List;
45  import junit.framework.Test;
46  import junit.framework.TestCase;
47  import junit.framework.TestSuite;
48  
49  /**
50   * JUnit test for the {@link org.jcoderz.phoenix.sqlparser.SqlScanner}.
51   *  
52   * @author Michael Griffel
53   */
540 public 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     {
64100       final File dir = new File(getSqlDirectory());
65100       final String[] sqlFiles = dir.list(new SqlFilenameFilter());
66100       if (sqlFiles == null || sqlFiles.length == 0)
67        {
680          throw new RuntimeException("[Error] "
69                 + "No SQL files found at '" + dir + "'!");
70        }
71  
72100       final TestSuite suite = new TestSuite();
73100       for (int i = 0; i < sqlFiles.length; i++)
74        {
75100          suite.addTest(new XmlFileValidation(dir, sqlFiles[i]));
76        }
77        
78100       return suite;
79     }
80     
81     private static String getSqlDirectory ()
82     {
83100       return System.getProperty("basedir", ".")
84           + File.separator + SQL_DIRECTORY;
85     }
86  
87100    static class SqlFilenameFilter
88           implements FilenameFilter
89     {
90        /** {@inheritDoc} */
91        public boolean accept (File dir, String name)
92        {
93100          return name.endsWith(".sql");
94        }
95     }
96  
97  
980    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        {
106100          super(sqlFileName);
107100          mSqlFileName = sqlFileName;
108100          mDir = dir;
109100       }
110  
111        public void runTest ()
112              throws Throwable
113        {
114100          testSqlFile();
115100       }
116  
117        public void testSqlFile ()
118              throws Exception
119        {
120100          final File file = new File(mDir, mSqlFileName);
121100          final StringWriter writer = new StringWriter();
122           try
123           {
124100             final SqlScanner scanner
125                    = new SqlScanner(new FileInputStream(file));
126  
127100             final List tokens = new ArrayList();
128     
129              for (;;)
130              {
131100                final Token t = scanner.nextToken();
132100                if (t.getType() == TokenType.EOF)
133                 {
134100                   break;
135                 }
136100                tokens.add(t);
137100             }
138     
139100             for (final Iterator iterator = tokens.iterator();
140100                   iterator.hasNext();)
141              {
142100                final Token t = (Token) iterator.next();
143100                writer.write(t.getValue());
144100             }
145100             writer.close();
146           }
1470          catch (Exception e)
148           {
1490(2)            e.printStackTrace();
1500             fail("Expected well-formed SQL file " + file + " : " + e);
151100          }
152100          diff(file, new LineNumberReader(new StringReader(writer.toString())));
153100       }
154  
155        private void diff (File file, LineNumberReader reader)
156              throws Exception
157        {
158100(3)         final LineNumberReader org
159                 = new LineNumberReader(new FileReader(file));
160           for (;;)
161           {
162100             final String line1 = org.readLine();
163100             if (line1 == null) // EOF
164              {
165100                break;
166              }
167100             final String line2 = reader.readLine();
168100             if (line2 == null) // EOF
169              {
1700                fail("Unexpected end of file");
171              }
172100             assertEquals("File: " + file + " Line " + org.getLineNumber()
173                    + " should be equals", line1, line2);
174                 
175100          }
176100       }
177    
178     }
179  }

Findings in this File

f (4) A method/constructor shouldn't explicitly throw java.lang.Exception Not required for testcode.
c (1) 5 : 0 Line does not match expected header line of '^ \*$'.
d (2) 149 : 13 Avoid printStackTrace(); use a logger call instead.
i (3) 158 : 0 org.jcoderz.phoenix.sqlparser.SqlScannerTest$XmlFileValidation.diff(File, LineNumberReader) may fail to close stream (test code) Decreased severity from 'warning' for testcode.