1   ////////////////////////////////////////////////////////////////////////////////
2   //
3   //                  ObjectLab is sponsoring QALab
4   // 
5   // Based in London, we are world leaders in the design and development 
6   // of bespoke applications for the Securities Financing markets.
7   // 
8   // <a href="http://www.objectlab.co.uk/open">Click here to learn more</a>
9   //           ___  _     _           _   _          _
10  //          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
11  //         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
12  //         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
13  //          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
14  //                   |__/
15  //
16  //                   http://www.ObjectLab.co.uk
17  // ---------------------------------------------------------------------------
18  //
19  //QALab is released under the GNU General Public License.
20  //
21  //QALab: Collects QA Statistics from your build over time.
22  //2005+, ObjectLab Ltd
23  //
24  //This library is free software; you can redistribute it and/or
25  //modify it under the terms of the GNU General Public
26  //License as published by the Free Software Foundation; either
27  //version 2.1 of the License, or (at your option) any later version.
28  //
29  //This library is distributed in the hope that it will be useful,
30  //but WITHOUT ANY WARRANTY; without even the implied warranty of
31  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
32  //General Public License for more details.
33  //
34  //You should have received a copy of the GNU General Public
35  //License along with this library; if not, write to the Free Software
36  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
37  //
38  ////////////////////////////////////////////////////////////////////////////////
39  package net.objectlab.qalab.parser;
40  
41  import java.io.File;
42  import java.io.FileNotFoundException;
43  import java.io.FileReader;
44  import java.io.IOException;
45  import java.util.Arrays;
46  import java.util.HashSet;
47  import java.util.Set;
48  
49  import javax.xml.parsers.ParserConfigurationException;
50  import javax.xml.parsers.SAXParser;
51  import javax.xml.parsers.SAXParserFactory;
52  
53  import junit.framework.Test;
54  import junit.framework.TestSuite;
55  import net.objectlab.qalab.AbstractTestCase;
56  import net.objectlab.qalab.util.SilentLogger;
57  
58  import org.xml.sax.InputSource;
59  import org.xml.sax.SAXException;
60  
61  public class BuildStatChartTest extends AbstractTestCase {
62      private static final String CHART_DIRECTORY = "target/test-classes/net/objectlab/qalab/parser/test-charts/";
63      /** Default SAX parser. **/
64      protected static final String DEFAULT_PARSER_NAME =
65          "org.apache.xerces.parsers.SAXParser";
66      private BuildStatForChartParser myForChartParser;
67  
68      /**
69       * Create the test case
70       *
71       * @param testName name of the test case
72       */
73      public BuildStatChartTest(String testName) {
74          super(testName);
75      }
76  
77      /**
78       * remove all charts in CHART directory.
79       */
80      public void setUp() {
81  //        System.out.println("------------ SETUP ----------------------------");
82          File dir = new File(CHART_DIRECTORY);
83          if (!dir.exists()) {
84              dir.mkdir();
85          } else {
86              deleteDirectory(dir);
87          }        
88          myForChartParser = new BuildStatForChartParser(new SilentLogger());
89          myForChartParser.setChartHeight(10);
90          myForChartParser.setChartWidth(10);
91          myForChartParser.setToDir(CHART_DIRECTORY);
92          myForChartParser.setMovingAverage(0);
93          myForChartParser.setQuiet(true);
94          myForChartParser.setFilePrefix("");
95      }
96      
97      public void tearDown() {
98  //        System.out.println("################## TEAR DOWN ##################");
99          File dir = new File(CHART_DIRECTORY);
100         deleteDirectory(dir);
101         if (dir.exists()) {
102             dir.delete();
103         }
104     }
105 
106     private void deleteDirectory(File dir) {
107         if (dir.exists() &&  dir.isDirectory()) {
108             // now empty the directory
109             File[] list = dir.listFiles();
110             for(int i=0; i<list.length; i++) {
111                 list[i].delete();
112             }
113         }
114     }
115     
116     private void checkGeneratedFiles(final Set expectedFiles, final String text) {
117         File dir = new File(CHART_DIRECTORY);
118         assertTrue("directory exists",dir.exists());
119         assertTrue("it is a directory",dir.isDirectory());
120         String[] files = dir.list();
121         for(int i=0; i<files.length; i++) {
122 //            System.out.println("FILE=======" + files[i]);
123             assertTrue("file not expected: " + files[i], expectedFiles.contains(files[i]));
124         }
125         assertEquals(text,expectedFiles.size(),files.length);
126     }
127     
128     /**
129      * @return the suite of tests being tested
130      */
131     public static Test suite() {
132         return new TestSuite(BuildStatChartTest.class);
133     }
134 
135     public void testCheckstyleCharts() {
136         File testInput = new File(
137                 "target/test-classes/net/objectlab/qalab/parser/qastats.xml");
138 
139         myForChartParser.setSummaryOnly(false);
140         myForChartParser.setAcceptedStyle("checkstyle");
141         myForChartParser.setSummaryStyle("checkstyle");        
142 
143         runCharts(testInput, myForChartParser);
144         Set expectedFiles = new HashSet( Arrays.asList(new String[] {
145                 "net_objectlab_test_MoveUpAndDownToZero.java.png",                
146                 "net_objectlab_test_MoveUpAndDown.java.png",                
147                 "net_objectlab_test_MoveUpAndUp.java.png",                
148                 "net_objectlab_test_MoveUp.java.png",                
149                 "net_objectlab_test_NewUp.java.png",                
150                 "net_objectlab_test_MoveDownToZero.java.png",                
151                 "net_objectlab_test_MoveDown.java.png",                
152                 "net_objectlab_test_MoveDownAndDown.java.png",                
153                 "net_objectlab_test_NoMove.java.png",                
154                 "summary.png"                
155         }) );
156         checkGeneratedFiles(expectedFiles,"checkstyle - all");        
157     }
158 
159     public void testPMDCharts() {
160         File testInput = new File(
161                 "target/test-classes/net/objectlab/qalab/parser/qastats.xml");
162 
163         myForChartParser.setSummaryOnly(false);
164         myForChartParser.setAcceptedStyle("pmd");
165         myForChartParser.setSummaryStyle("pmd");        
166         myForChartParser.setQuiet(false);
167 
168         runCharts(testInput, myForChartParser);
169         Set expectedFiles = new HashSet( Arrays.asList(new String[] {
170                 "net_objectlab_test_NoMovePMD.java.png",                
171                 "net_objectlab_test_MoveDownPMD.java.png",                
172                 "net_objectlab_test_WeekendAdjustUpAndDownToZero.java.png",                
173                 "net_objectlab_test_WeekendAdjustDown.java.png",                
174                 "net_objectlab_test_NewUp.java.png",                
175                 "net_objectlab_test_MoveUp.java.png",                
176                 "summary.png"                
177         }) );
178         checkGeneratedFiles(expectedFiles,"pmd - all");        
179     }
180 
181     public void testCheckstyleSummaryOnlyCharts() {
182         File testInput = new File(
183                 "target/test-classes/net/objectlab/qalab/parser/qastats.xml");
184 
185         myForChartParser.setSummaryOnly(true);
186         myForChartParser.setAcceptedStyle("checkstyle");
187         myForChartParser.setSummaryStyle("checkstyle");        
188         myForChartParser.setMovingAverage(3);
189 
190         runCharts(testInput, myForChartParser);
191         Set expectedFiles = new HashSet( Arrays.asList(new String[] {
192                 "summary.png"                
193         }) );
194         checkGeneratedFiles(expectedFiles,"checkstyle - summary only");        
195     }
196 
197     public void testUnknownStatsCharts() {
198         File testInput = new File(
199                 "target/test-classes/net/objectlab/qalab/parser/qastats.xml");
200 
201         myForChartParser.setAcceptedStyle("blabla");
202         myForChartParser.setSummaryStyle("blibli");        
203         runCharts(testInput, myForChartParser);
204         Set expectedFiles = new HashSet( Arrays.asList(new String[] {
205         }) );
206         checkGeneratedFiles(expectedFiles,"Unknown stats - all");        
207     }
208 
209     private void runCharts(File testInput, BuildStatForChartParser myForChartParser) {
210 //        XMLReader parser = null;
211 
212         try {
213             SAXParserFactory factory = SAXParserFactory.newInstance();
214             SAXParser saxParser = factory.newSAXParser();
215             saxParser.parse(new InputSource(new FileReader(testInput)), myForChartParser);
216 
217 //            parser = XMLReaderFactory.createXMLReader(DEFAULT_PARSER_NAME);
218 //            parser.setContentHandler(myForChartParser);
219 //            parser.setErrorHandler(myForChartParser);
220 //
221 //            parser.parse(new InputSource(new FileInputStream(testInput)));
222         } catch (SAXException e) {
223             fail(e.toString());
224         } catch (FileNotFoundException e) {
225             fail(e.toString());
226         } catch (IOException e) {
227             fail(e.toString());
228         } catch (ParserConfigurationException e) {
229             fail(e.toString());
230         }
231     }
232 }
233 /*
234  *                   ObjectLab is sponsoring QALab
235  * 
236  * Based in London, we are world leaders in the design and development 
237  * of bespoke applications for the securities financing markets.
238  * 
239  * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
240  *           ___  _     _           _   _          _
241  *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
242  *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
243  *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
244  *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
245  *                   |__/
246  *
247  *                     www.ObjectLab.co.uk
248  */