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

org.databene.dbsanity.model.CheckHolderSupport Maven / Gradle / Ivy

Go to download

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

The newest version!
/*
 * (c) Copyright 2011-2012 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.model;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.databene.commons.ConfigurationError;
import org.databene.commons.Filter;
import org.databene.commons.IOUtil;
import org.databene.commons.OrderedMap;
import org.databene.commons.Visitor;
import org.databene.commons.collection.OrderedNameMap;
import org.databene.commons.version.VersionNumber;
import org.databene.dbsanity.Reporter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Default implementation of the {@link CheckHolder} interface.

* Created: 13.11.2011 06:30:49 * @since 0.9.2 * @author Volker Bergmann */ public class CheckHolderSupport extends AbstractSanityCheckSuite implements CheckHolder { private static final Logger LOGGER = LoggerFactory.getLogger(CheckHolderSupport.class); protected OrderedMap checks; private Date startDate; private int executionTime; public CheckHolderSupport(String name, File reportFolder, SuiteHolder parent) { super(name, reportFolder, parent); this.checks = new OrderedNameMap(); this.executionTime = 0; } public void addCheck(SanityCheck check) { String checkName = check.getName(); if (checks.containsKey(checkName)) throw new ConfigurationError("Duplicate sanity check name: " + checkName); this.checks.put(checkName, check); } public List getChecks() { return checks.values(); } public int countChecks() { return checks.size(); } public void perform(CheckContext checkContext, Reporter reporter) { startDate = new Date(); executionTime = 0; for (SanityCheck check : checks.values()) performCheck(checkContext, reporter, check); executionTime = (int) (System.currentTimeMillis() - startDate.getTime()); } @Override public int countChecksWithVerdict(SanityCheckVerdict verdict) { int count = 0; for (SanityCheck check : checks.values()) if (check.getVerdict() == verdict) count++; return count; } @Override public int countChecks(Filter filter) { int count = 0; for (SanityCheck check : checks.values()) if (filter.accept(check)) count++; return count; } public long getDefectCount() { int count = 0; for (SanityCheck check : checks.values()) count += check.getDefectCount(); return count; } public Date getStartDate() { return startDate; } public int getExecutionTime() { return executionTime; } @Override public String toString() { return name; } public void accept(Visitor visitor) { visitor.visit(this); for (SanityCheck check : checks.values()) check.accept(visitor); } public void checkApplicability(VersionNumber appVersion, Set tablesToCheck, Set tags) { for (SanityCheck check : checks.values()) check.checkApplicability(appVersion, tablesToCheck, tags); } protected void performCheck(CheckContext checkContext, Reporter reporter, SanityCheck check) { // check applicability if (check.getVerdict() == SanityCheckVerdict.NOT_APPLICABLE) return; // check if it should be skipped if (check.getVerdict() == SanityCheckVerdict.SKIPPED) { LOGGER.info("Skipping check '{}'", check.getName()); reporter.skipped(check); return; } else { // perform the check LOGGER.info("Performing check '{}'", check.getName()); long start = System.nanoTime(); DefectIterator defectIterator = null; try { defectIterator = check.perform(checkContext); reporter.report(check, defectIterator); } catch (Exception e) { reporter.error(check, e); } finally { int elapsed = (int) ((System.nanoTime() - start) / 1000000); check.setExecutionTime(elapsed); IOUtil.close(defectIterator); } checkContext.checkCompleted(); logCompletionMessage(checkContext); Thread.yield(); } } public void reset() { for (SanityCheck check : checks.values()) check.reset(); } protected void logCompletionMessage(CheckContext context) { int totalChecks = context.getTotalCheckCount(); int pendingChecks = context.getPendingCheckCount(); int completedChecks = totalChecks - pendingChecks; if (completedChecks < totalChecks) LOGGER.info("{} checks completed, {} pending", completedChecks, pendingChecks); else LOGGER.info("All checks completed"); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy