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

org.databene.dbsanity.report.AbstractReportModule 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 2010 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.FileNotFoundException;
import java.io.IOException;

import org.databene.commons.ArrayFormat;
import org.databene.commons.IOUtil;
import org.databene.commons.StringUtil;
import org.databene.commons.file.FilePrintStream;
import org.databene.commons.math.Interval;
import org.databene.commons.math.Intervals;
import org.databene.commons.version.VersionNumber;
import org.databene.dbsanity.DbSanity;
import org.databene.dbsanity.model.SanityCheck;
import org.databene.dbsanity.model.SanityCheckSuite;
import org.databene.html.HTMLUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Abstract implementation of the {@link ReportModule} interface.
 * It provides a {@link PrintStream} attribute named out
 * and methods to open and close an HTML file on this attribute.

* Created: 13.10.2010 16:39:39 * @since 1.0 * @author Volker Bergmann */ public abstract class AbstractReportModule implements ReportModule { protected final Logger logger = LoggerFactory.getLogger(getClass()); protected ReportContext context; protected String style = DbSanity.DEFAULT_STYLE; public int getDashboardColSpan(ReportScope scope) { return 0; } public void setContext(ReportContext context) { this.context = (ReportContext) context; } public void setStyle(String style) { this.style = style; } public void success(SanityCheck check) { } public void failure(SanityCheck check, Object[] errorInfo, String[] infoHeaders) { } public void error(SanityCheck check, Exception e) { } public void skipped(SanityCheck check) { } public void summary(SanityCheckSuite rootSuite) { } public void renderDashboardView(ReportScope scope, SanityCheckSuite suite, FilePrintStream out) { } public void close() { } // non-public helpers ---------------------------------------------------------------------------------------------- protected FilePrintStream openNewFile(File file, String title) throws FileNotFoundException { return ReportUtil.openNewFile(file, title, new File(context.getReportRoot(), style + ".css")); } protected void closeFile(FilePrintStream out) { out.println(context.footer()); out.println(" "); out.println(" "); out.println(""); IOUtil.close(out); } protected void startModule(FilePrintStream out) { out.println("
"); } protected void modHeader(String title, FilePrintStream out) { out.println("

" + title + "

"); } protected void modSeparator(String title, FilePrintStream out) { out.println("

" + title + "

"); } protected void startModBody(FilePrintStream out) { out.println("
"); } protected void endModBody(FilePrintStream out) { out.println("
"); } protected void modFooter(String href, FilePrintStream out) { out.println(" "); } protected void endModule(FilePrintStream out) { out.println("
"); } protected void renderCheckInfo(SanityCheck check, FilePrintStream out) { startModule(out); modHeader("Check Info", out); startModBody(out); out.println(""); printNameValueRow("Name;", check.getName(), false, false, out); try { printNameValueRow("Source File:", check.getSourceFile().getCanonicalPath(), false, false, out); } catch (IOException e) { logger.error(e.getMessage(), e); } printNameValueRow("Table:", check.getTable(), false, false, out); printNameValueRow("Defect Type:", check.getDefectType(), false, false, out); printNameValueRow("Description:", check.getDescription(), true, false, out); printNameValueRow("Requirement:", check.getRequirement(), true, false, out); String version = renderVersions(check); printNameValueRow("Affected versions:", version, false, false, out); printNameValueRow("Tags:", ArrayFormat.format(check.getTags().toArray()), true, false, out); printNameValueRow("Author:", check.getAuthor(), true, false, out); printNameValueRow("Details:", check.getDetails(), true, true, out); out.println("
"); endModBody(out); endModule(out); } protected String renderVersions(SanityCheck check) { Intervals versions = check.getAffectedVersions(); StringBuilder builder = new StringBuilder(); for (int i = 0; i < versions.intervalCount(); i++) builder.append(i > 0 ? ", " : "").append(renderInterval(versions.getInterval(i))); return builder.toString(); } static String renderInterval(Interval interval) { VersionNumber min = interval.getMin(); VersionNumber max = interval.getMax(); if (min != null) { // min != null if (max != null) return interval.toString(); else if (interval.isMinInclusive()) return "since " + min; else return "after " + min; } else { // min == null if (max == null) return "any"; else if (interval.isMaxInclusive()) return "until " + max + " inclusively"; else return "before " + max; } } protected void printNameValueRow(String name, String value, boolean dropIfEmpty, boolean pre, FilePrintStream out) { if (dropIfEmpty && StringUtil.isEmpty(value)) return; out.println(" "); out.print(" " + name +""); out.print(" "); if (!StringUtil.isEmpty(value)) { if (pre) out.print("
");
			out.print(HTMLUtil.escape(value));
			if (pre)
				out.print("
"); } out.println(""); out.println(" "); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy