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

com.nathanaelrota.cmmi.maven.requirements.RequirementMojoDelegate Maven / Gradle / Ivy

The newest version!
package com.nathanaelrota.cmmi.maven.requirements;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.nathanaelrota.cmmi.maven.requirements.bind.xml.Requirements;
import com.nathanaelrota.cmmi.maven.requirements.bind.xml.RequirementsHandler;

/**
 * @author Nathanael ROTA
 */
public class RequirementMojoDelegate {

	/**
	 * Requirements Handler for main source
	 */
	private RequirementsHandler handler;

	/**
	 * the classloader
	 */
	private RequirementClassHandler requirementClassHandler;
	/**
	 * toggle the integration with surefire report result
	 */
	private boolean surefireReport;

	/**
	 * Reporting format xml|html
	 */
	private String format = "html";

	/**
	 * Project Name
	 */
	private String projectName;

	/**
	 * @return Requirements
	 * @see com.nathanaelrota.cmmi.maven.requirements.bind.xml.RequirementsHandler#getRequirements()
	 */
	public Requirements getHandlerRequirements() {
		return handler.getRequirements();
	}

	/**
	 * @param log
	 *            the log to set
	 */
	public void setLog(org.apache.maven.plugin.logging.Log log) {
		if (this.requirementClassHandler != null) {
			this.requirementClassHandler.setLog(log);
		}
	}

	/**
	 * @param format
	 *            the format to set
	 */
	public void setFormat(String format) {
		this.format = format;
	}

	/**
	 * @param projectName
	 *            the projectName to set
	 */
	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	/**
	 * @param surefireReport
	 *            the surefireReport to set
	 */
	public void setSurefireReport(boolean surefireReport) {
		this.surefireReport = surefireReport;
	}

	public RequirementMojoDelegate() {
		handler = new RequirementsHandler();
		requirementClassHandler = new RequirementClassHandler(this.getClass().getClassLoader());
	}

	/**
	 * Delegation of Mojo.execute()
	 * @param requirements requirements filename
	 * @param testDirectory directory containing the test class
	 * @param testPattern pattern of test class file
	 * @param mainDirectory directory containing the main class
	 * @param classPattern pattern of main class file
	 * @param traceability report filename
	 */
	public void execute(String requirements, String testDirectory, String testPattern, String mainDirectory, String classPattern, String traceability) {
		this.handler.unmarshall(requirements);
		this.requirementClassHandler.loadClasses(mainDirectory, classPattern);
		this.requirementClassHandler.loadClasses(testDirectory, testPattern);
		this.visitMain(mainDirectory, classPattern);
		this.visitTest(testDirectory, testPattern);
		try {
			if (format.equalsIgnoreCase("xml")) {
				handler.marshall(traceability);
			} else {
				handler.toHtml(traceability, this.projectName, surefireReport);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Visit tests class method
	 * @param testDir directory containing the test classes
	 * @param testPattern pattern of test classes file
	 */
	public void visitTest(String testDir, String testPattern) {
		for (String classfile : RequirementClassLoader.getClasses(testPattern, testDir)) {
			String className = RequirementClassLoader.getClassName(classfile);
			Map> values = this.requirementClassHandler.check(className);
			this.updateTest(values);
		}
	}

	/**
	 * Update handler method for test part
	 * @param classloader the initialezd classloader
	 */
	private void updateTest(Map> values) {
		for (String requirement : values.keySet()) {
			for (String testcase : values.get(requirement)) {
				handler.addTest(requirement, testcase);
			}
		}
	}


	/**
	 * Visit main class method
	 * @param handler the requirements handler
	 * @param mainDir directory containing the main classes
	 * @param classPattern pattern of main classes file
	 */
	public void visitMain(String mainDir, String classPattern) {
		for (String classfile : RequirementClassLoader.getClasses(classPattern, mainDir)) {
			String className = RequirementClassLoader.getClassName(classfile);
			Map> values = this.requirementClassHandler.check(className);
			this.updateMain(values);
		}
	}

	/**
	 * Update handler method for main part
	 * @param values the requirements handler
	 */
	private void updateMain(Map> values) {
		for (String requirement : values.keySet()) {
			for (String method : values.get(requirement)) {
				handler.addMain(requirement, method);
			}
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy