/**
 * Copyright 2009 University of Oxford
 *
 * Written by Tim Pizey for the Erewhon Project
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the University of Oxford nor the names of its 
 *    contributors may be used to endorse or promote products derived from this 
 *    software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 */
/**
 * 
 */
package net.sf.gaboto.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import net.sf.json.JSONObject;
import net.sf.json.test.JSONAssert;

import org.custommonkey.xmlunit.XMLAssert;

import junit.framework.TestCase;

/**
 * @author Tim Pizey
 * @since 15 May 2009
 *
 */
public class GabotoTestCase extends TestCase {
  
  protected static String referenceOutputDir = "src/test/reference";
  protected static String generatedOutputDir = "target";

  /**
   * Default constructor.
   */
  public GabotoTestCase() {
    super();
  }

  /**
   * Constructor, with name.
   * @param name
   */
  public GabotoTestCase(String name) {
    super(name);
  }

  protected boolean generateReferenceCopy() { 
    return false;
  }
  protected void assertXmlEqual(String actual, String fileName) throws Exception { 
    File actualFile = new File(generatedOutputDir, fileName);
    FileOutputStream generatedOutput = new FileOutputStream(actualFile);
    generatedOutput.write(actual.getBytes());
    generatedOutput.close();
    
    File referenceFile = new File(referenceOutputDir, fileName);
    if (referenceFile.exists() && ! generateReferenceCopy()) {
      FileInputStream file = new FileInputStream (referenceFile);
      byte[] b = new byte[file.available()];
      file.read(b);
      file.close ();
      String cached = new String(b);
      XMLAssert.assertXMLEqual("Cached not equal to generated", cached, actual);
    } else { 
      actualFile.renameTo(referenceFile);
      fail("Reference output file generated: " + referenceFile.getCanonicalPath() + " modify generateCached and rerun");
    }
  }

  
  protected void assertPageJsonEqual(String actual, String referenceFileName) throws Exception { 
    JSONObject actualJson = JSONObject.fromObject(tidy(actual));
    File generatedFile = new File(generatedOutputDir, referenceFileName);
    FileOutputStream generatedOutput = new FileOutputStream(generatedFile);
    generatedOutput.write(actual.getBytes());
    generatedOutput.close();
    
    File referenceFile = new File(referenceOutputDir, referenceFileName);
    if (referenceFile.exists() && ! generateReferenceCopy()) {
      FileInputStream file = new FileInputStream (referenceFile);
      byte[] b = new byte[file.available()];
      file.read(b);
      file.close ();
      String cached = new String(b);
      JSONObject expectedJson = JSONObject.fromObject(tidy(cached));
      JSONAssert.assertEquals(expectedJson, actualJson);
    } else { 
      generatedFile.renameTo(referenceFile);
      fail("Reference output file generated: " + referenceFile.getCanonicalPath() + " modify generateCached and rerun");
    }
  }
  protected static String tidy(String json) { 
    json = json.trim();
    if (json.startsWith("[")) { 
      json = json.substring(1);
      if (json.endsWith("]")) 
        json = json.substring(0, json.length() -1);
      else
        throw new RuntimeException("Unbalanced square brackets");
    }
    return json;
  }
  
}

@@ -2121,25 +2121,22 @@
  String 
-generated
+actual
 OutputDi
@@ -2533,33 +2533,30 @@
  = new File(
-generated
+actual
 OutputDir, f
@@ -2582,39 +2582,42 @@
 utputStream 
-generatedOutput
+actualOutputStream
  = new FileO
@@ -2641,39 +2641,42 @@
 lFile);%0A    
-generatedOutput
+actualOutputStream
 .write(actua
@@ -2690,39 +2690,42 @@
 tes());%0A    
-generatedOutput
+actualOutputStream
 .close();%0A  
@@ -3513,25 +3513,22 @@
 ew File(
-generated
+actual
 OutputDi

Tidy names