All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.databene.dbsanity.report.CSVSummaryModule Maven / Gradle / Ivy

Go to download

DB Sanity is a tool for verifying a database's sanity and data integrity.

There is a newer version: 0.9.4
Show newest version
/*
 * (c) Copyright 2011 by Volker Bergmann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, is permitted under the terms of the
 * GNU General Public License (GPL).
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
 * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
 * HEREBY EXCLUDED. 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 org.databene.dbsanity.report;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import org.databene.commons.IOUtil;
import org.databene.commons.file.FilePrintStream;
import org.databene.dbsanity.model.SanityCheck;
import org.databene.dbsanity.model.SanityCheckSuite;
import org.databene.dbsanity.model.SanityCheckVerdict;
import org.databene.dbsanity.util.CheckVisitor;
import org.databene.document.csv.CSVUtil;
import org.databene.jdbacl.SQLUtil;

import static org.databene.dbsanity.model.SanityCheckVerdict.*;

/**
 * Creates a summary report in CSV format.

* Created: 14.09.2011 15:20:36 * @since 0.8.3 * @author Volker Bergmann */ public class CSVSummaryModule extends AbstractReportModule { private static final char DEFAULT_SEPARATOR = ','; private char separator; public CSVSummaryModule() { this(DEFAULT_SEPARATOR); } public CSVSummaryModule(char separator) { this.separator = separator; } // ReportModule interface implementation --------------------------------------------------------------------------- @Override public int getDashboardColSpan(ReportScope scope) { return (scope == ReportScope.ROOT ? 1 : 0); } @Override public void summary(SanityCheckSuite rootSuite) { createCSVFile(rootSuite); } @Override public void renderDashboardView(ReportScope scope, SanityCheckSuite suite, FilePrintStream out) { startModule(out); modHeader("CSV Report", out); startModBody(out); out.println("Download CSV Report Summary"); endModBody(out); endModule(out); } // private helpers ------------------------------------------------------------------------------------------------- private void createCSVFile(SanityCheckSuite rootSuite) { PrintWriter out = null; try { File file = new File(rootSuite.getReportFolder(), "dbsanity-report-summary.csv"); out = new PrintWriter(new FileWriter(file)); String line = CSVUtil.formatHeaderWithLineFeed(separator, "Check", "Table", "Defect Type", "Verdict", "Defect Count", "Duration [ms]", "Details"); out.print(line); exportSuite(rootSuite, out); } catch (IOException e) { throw new RuntimeException(e); } finally { IOUtil.close(out); } } private void exportSuite(SanityCheckSuite suite, PrintWriter out) throws IOException { suite.accept(new CSVExporter(out)); } protected void exportCheck(SanityCheck check, PrintWriter out) { try { String details = check.getDetails(); String normalizedQuery = (details != null ? SQLUtil.normalize(details, true) : null); CSVUtil.writeRow(out, separator, check.getName(), check.getTable(), check.getDefectType(), check.getVerdict().name(), String.valueOf(check.getDefectCount()), String.valueOf(check.getExecutionTime()), normalizedQuery ); } catch (IOException e) { throw new RuntimeException("error exorting checks to CSV file", e); } } public class CSVExporter extends CheckVisitor { private PrintWriter out; public CSVExporter(PrintWriter out) { this.out = out; } public void visitCheck(SanityCheck check) { SanityCheckVerdict verdict = check.getVerdict(); if (verdict != NOT_APPLICABLE && (!context.isCondensed() || verdict == ERROR || verdict == FAILED)) exportCheck(check, out); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy