Clover coverage report - anttestsetgen - 1.1
Coverage timestamp: zo jan 4 2004 17:34:16 CET
file stats: LOC: 179   Methods: 5
NCLOC: 79   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
PropertyFileRenderer.java 92,9% 95,2% 100% 95,1%
coverage coverage
 1   
 /*
 2   
  * Copyright (C) 2003, Marco Jansen / Contrado Technologies  (http://www.contrado.nl)
 3   
  *
 4   
  * This program is free software; you can redistribute it and/or
 5   
  * modify it under the terms of the GNU General Public License as
 6   
  * published by the Free Software Foundation; either version 2 of the
 7   
  * License, or (at your option) any later version.
 8   
  *
 9   
  * This program is distributed in the hope that it will be useful, but
 10   
  * WITHOUT ANY WARRANTY; without even the implied warranty of
 11   
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 12   
  * General Public License for more details.
 13   
  */
 14   
 
 15   
 package net.sourceforge.anttestsetgen.renderer;
 16   
 
 17   
 import net.sourceforge.anttestsetgen.AntTestSetGenUtil;
 18   
 import net.sourceforge.anttestsetgen.Entry;
 19   
 import net.sourceforge.anttestsetgen.Output;
 20   
 import net.sourceforge.anttestsetgen.TestSet;
 21   
 import net.sourceforge.anttestsetgen.TestSetGenerator;
 22   
 import org.apache.tools.ant.BuildException;
 23   
 import org.apache.tools.ant.Project;
 24   
 
 25   
 import java.io.BufferedWriter;
 26   
 import java.io.FileWriter;
 27   
 import java.io.IOException;
 28   
 import java.io.PrintWriter;
 29   
 import java.text.SimpleDateFormat;
 30   
 import java.util.Date;
 31   
 import java.util.Iterator;
 32   
 import java.util.List;
 33   
 
 34   
 /**
 35   
  * This renderer generates a property file with the standard key/value format.
 36   
  * <BR>
 37   
  * Here's an example how the property file can look like:<BR>
 38   
  * <BR>
 39   
  * #===========================<BR>
 40   
  * # TestSetGenerator Example 1<BR>
 41   
  * #===========================<BR>
 42   
  * # Generated by: Ant TestSetGenerator<BR>
 43   
  * # Generated on: 28-9-03 19:12<BR>
 44   
  * # URL: jdbc:hsqldb:../../database/ant_test_db<BR>
 45   
  * # Userid: sa<BR>
 46   
  *<BR>
 47   
  * #--------------------------<BR>
 48   
  * # Customer in San Francisco<BR>
 49   
  * #--------------------------<BR>
 50   
  * # Row: First<BR>
 51   
  * CUSTOMER_ID = 1<BR>
 52   
  * CUSTOMER_FIRST_NAME = Michael<BR>
 53   
  * CUSTOMER_LAST_NAME = Clancy<BR>
 54   
  * <BR>
 55   
  * #---------------------------<BR>
 56   
  * # Customers in San Francisco<BR>
 57   
  * #---------------------------<BR>
 58   
  * # Row: All<BR>
 59   
  * CUSTOMER_ID.1 = 3<BR>
 60   
  * CUSTOMER_FIRST_NAME.1 = Michael<BR>
 61   
  * CUSTOMER_LAST_NAME.1 = Clancy<BR>
 62   
  * CUSTOMER_ID.1 = 5<BR>
 63   
  * CUSTOMER_FIRST_NAME.2 = James<BR>
 64   
  * CUSTOMER_LAST_NAME.2 = Peterson<BR>
 65   
  * CUSTOMER_ID.1 = 7<BR>
 66   
  * CUSTOMER_FIRST_NAME.3 = Robert<BR>
 67   
  * CUSTOMER_LAST_NAME.3 = White<BR>
 68   
  */
 69   
 public class PropertyFileRenderer implements Renderer {
 70   
 
 71  42
   public void render(TestSetGenerator testSetGenerator, Output output) {
 72   
 
 73  42
     AntTestSetGenUtil.requiredCheck("file", output.getFile());
 74   
 
 75  40
     String fileName = output.getFile();
 76   
 
 77  40
     try {
 78  40
       PrintWriter propertyFile = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
 79   
 
 80  40
       printPropertyFileHeader(testSetGenerator, propertyFile);
 81   
 
 82  40
       printTestSets(testSetGenerator, propertyFile);
 83   
 
 84  40
       propertyFile.close();
 85  40
       testSetGenerator.log("TestSet rendererd to property file  " + fileName, Project.MSG_INFO);
 86   
 
 87   
     } catch (IOException e) {
 88  0
       throw new BuildException("Could not create property fileName: " + fileName);
 89   
     }
 90   
   }
 91   
 
 92   
   /**
 93   
    * Print the header of the property file.
 94   
    * 
 95   
    * @param propertyFile The property file where the header needs to be added to
 96   
    */
 97  40
   private static void printPropertyFileHeader(TestSetGenerator testSetGenerator, PrintWriter propertyFile) {
 98  40
     printGroupHeader(propertyFile, testSetGenerator.getDescription(), "=");
 99  40
     propertyFile.println("# Generated by: Ant TestSetGenerator");
 100  40
     SimpleDateFormat dateFormat = new SimpleDateFormat();
 101   
 
 102  40
     Date currentDate = new Date(System.currentTimeMillis());
 103  40
     propertyFile.println("# Generated on: " + dateFormat.format(currentDate));
 104  40
     propertyFile.println("# URL: " + testSetGenerator.getUrl());
 105  40
     propertyFile.println("# Userid: " + testSetGenerator.getUserId());
 106   
   }
 107   
 
 108   
   /**
 109   
    * Print the results of a test set.
 110   
    * 
 111   
    * @param propertyFile The property files where the results needs to be added to
 112   
    */
 113  40
   private static void printTestSets(TestSetGenerator testSetGenerator, PrintWriter propertyFile) {
 114  40
     for (Iterator iterator = testSetGenerator.getTestSets().iterator(); iterator.hasNext();) {
 115  54
       TestSet testSet = (TestSet) iterator.next();
 116   
 
 117  54
       propertyFile.println("");
 118  54
       printGroupHeader(propertyFile, testSet.getName(), "-");
 119  54
       propertyFile.println("# Row: " + testSet.getRowtoselect());
 120  54
       testSetGenerator.log("rowtoselect set to " + testSet.getRowtoselect(), Project.MSG_VERBOSE);
 121   
 
 122  54
       for (int i = 0; i < testSet.getEntries().size(); i++) {
 123  78
         Entry entry = (Entry) testSet.getEntries().get(i);
 124   
 
 125  78
         if (entry.getValues().size() == 0) {
 126   
           // Nothing Found
 127  6
           propertyFile.println("### The next constant could not be given a value, since the query returned no rows.");
 128  6
           propertyFile.println(entry.getKey() + "=" + 0);
 129   
 
 130  72
         } else if (TestSet.SELECT_FIRST_ROW.equals(testSet.getRowtoselect())) {
 131   
 
 132   
           // One found
 133  46
           propertyFile.println(entry.getKey() + " = " + entry.getValues().get(0));
 134   
 
 135   
         } else {
 136   
           // Multiple found
 137  26
           List values = entry.getValues();
 138  26
           for (int j = 0; j < values.size(); j++) {
 139  142
             String value = (String) values.get(j);
 140  142
             propertyFile.println(entry.getKey() + "." + (j + 1) + " = " + value);
 141   
           }
 142   
         }
 143   
       }
 144   
     }
 145   
   }
 146   
 
 147   
   /**
 148   
    * Print a group header in the property file.
 149   
    * 
 150   
    * @param propertyFile  The property file where the group header needs to be added to.
 151   
    * @param header        The name of the group header.
 152   
    * @param lineCharacter The character that is used to print a line above and below the header.
 153   
    */
 154  94
   public static void printGroupHeader(PrintWriter propertyFile, String header, String lineCharacter) {
 155  94
     if (header == null) {
 156  0
       header = "TestSetGenerator output";
 157   
     }
 158  94
     propertyFile.println("#" + repeatString(lineCharacter, header.length() + 1));
 159  94
     propertyFile.println("# " + header);
 160  94
     propertyFile.println("#" + repeatString(lineCharacter, header.length() + 1));
 161   
   }
 162   
 
 163   
   /**
 164   
    * Create a string with identical characters.
 165   
    * 
 166   
    * @param repeatText The character that needs to be repeated.
 167   
    * @param count      The number of times the character needs to be repeated.
 168   
    * @return A string with identical characters.
 169   
    */
 170  188
   private static String repeatString(String repeatText, int count) {
 171  188
     StringBuffer stringBuffer = new StringBuffer();
 172  188
     for (int i = 0; i < count; i++) {
 173  4724
       stringBuffer.append(repeatText);
 174   
     }
 175  188
     return stringBuffer.toString();
 176   
   }
 177   
 
 178   
 }
 179