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

ru.yandex.qatools.allure.report.AllureReportBuilder Maven / Gradle / Ivy

The newest version!
package ru.yandex.qatools.allure.report;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.settings.Settings;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.yandex.qatools.clay.Aether;
import ru.yandex.qatools.clay.AetherResult;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import static ru.yandex.qatools.allure.report.AllureArtifacts.getReportDataArtifact;
import static ru.yandex.qatools.allure.report.internal.RegexJarEntryFilter.filterByRegex;
import static ru.yandex.qatools.clay.Aether.MAVEN_CENTRAL_URL;
import static ru.yandex.qatools.clay.Aether.aether;
import static ru.yandex.qatools.clay.maven.settings.FluentProfileBuilder.newProfile;
import static ru.yandex.qatools.clay.maven.settings.FluentRepositoryBuilder.newRepository;
import static ru.yandex.qatools.clay.maven.settings.FluentSettingsBuilder.loadSettings;
import static ru.yandex.qatools.clay.utils.archive.ArchiveUtil.unpackJar;

/**
 * @author Artem Eroshenko [email protected]
 *         Date: 29.05.14
 *         

* Uses this class you can generate allure report any version. */ public class AllureReportBuilder { public static final String DEFAULT_ALLURE_REPORT_VERSION = "RELEASE"; public static final String ALLURE_REPORT_GENERATOR_CLASS = "ru.yandex.qatools.allure.data.AllureReportGenerator"; public static final String ALLURE_REPORT_FACE_FILE_REGEX = "^((?!(META\\-INF|WEB-INF)).)*$"; public static final String METHOD_NAME = "generate"; private static final Logger log = LoggerFactory.getLogger(AllureReportBuilder.class); private String version; private File outputDirectory; private ClassLoader classLoader; private Aether aether; private List extensions = Lists.newArrayList(); public AllureReportBuilder(String version, File outputDirectory, Aether aether) throws AllureReportBuilderException { checkDirectory(outputDirectory); this.outputDirectory = outputDirectory; this.aether = aether; this.version = version; } public AllureReportBuilder(String version, File outputDirectory) throws AllureReportBuilderException { this(version, outputDirectory, aether(mavenSettings())); } public AllureReportBuilder(File outputDirectory) throws AllureReportBuilderException { this(DEFAULT_ALLURE_REPORT_VERSION, outputDirectory); } public static Settings mavenSettings() throws AllureReportBuilderException { try { return loadSettings() .withActiveProfile( newProfile() .withId("allure") .withRepository(newRepository().withUrl(MAVEN_CENTRAL_URL)) ).build(); } catch (Exception e) { throw new AllureReportBuilderException(e); } } /** * Set class loader for resolved dependencies * * @param classLoader class loader */ @SuppressWarnings("unused") public void setClassLoader(ClassLoader classLoader) { this.classLoader = classLoader; } /** * Return class loader for resolved dependencies * * @return class loader */ public ClassLoader getClassLoader() { return classLoader; } /** * Set allure version for report generation * * @param version allure version in maven format */ @SuppressWarnings("unused") public void setVersion(String version) { this.version = version; } /** * Get allure version for report generation * * @return allure version */ @SuppressWarnings("unused") public String getVersion() { return this.version; } /** * Set extension artifacts in Aether format * ({@code :[:[:]]:}). * * @param extensions extension GAVs */ public void addExtensions(String... extensions) { for (String extension : extensions) { addExtension(extension); } } /** * Set extension artifact in Aether format * ({@code :[:[:]]:}). * * @param extension extension GAV */ public void addExtension(String extension) { if (!StringUtils.isEmpty(extension)) { this.extensions.add(new DefaultArtifact(extension)); log.info(String.format("Allure extension %s added", extension)); } } @VisibleForTesting protected List getExtensions() { return extensions; } /** * Process test results in given directories. Generates report data to {@link #outputDirectory}. * * @param inputDirectories a directories with test results * @throws AllureReportBuilderException if one of given directories doesn't exists and can't be created * AllureReportBuilderException if can't resolve {@link AllureArtifacts#getReportDataArtifact(String)} (String)} * using {@link #aether} * AllureReportBuilderException if resolved DependencyResult contains artifact with invalid path. * AllureReportBuilderException if can't find class {@link #ALLURE_REPORT_GENERATOR_CLASS} in classpath * AllureReportBuilderException if can't create instance of {@link #ALLURE_REPORT_GENERATOR_CLASS} * AllureReportBuilderException if can't find {@link #METHOD_NAME} method in class * {@link #ALLURE_REPORT_GENERATOR_CLASS} * AllureReportBuilderException if can't invoke method {@link #METHOD_NAME} */ public void processResults(File... inputDirectories) throws AllureReportBuilderException { try { checkDirectories(inputDirectories); DefaultArtifact artifact = getReportDataArtifact(version); Set urls = Sets.newHashSet(Arrays.asList(aether.resolve(artifact).getAsUrls())); for (Artifact extension : extensions) { urls.addAll(Arrays.asList(aether.resolve(extension).getAsUrls())); } URLClassLoader urlClassLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]), getClassLoader()); Class clazz = urlClassLoader.loadClass(ALLURE_REPORT_GENERATOR_CLASS); Object generator = clazz.getConstructor(File[].class).newInstance(new Object[]{inputDirectories}); clazz.getMethod(METHOD_NAME, File.class).invoke(generator, outputDirectory); } catch (Exception e) { throw new AllureReportBuilderException(e); } } /** * Unpack report face to {@link #outputDirectory}. * * @throws AllureReportBuilderException if can't resolve report face artifact * if can't unpack report war to {@link #outputDirectory} * @see ru.yandex.qatools.clay.utils.archive.ArchiveUtil#unpackJar(java.io.File, java.io.File, * ru.yandex.qatools.clay.utils.archive.JarEntryFilter) */ public void unpackFace() throws AllureReportBuilderException { try { DefaultArtifact artifact = AllureArtifacts.getReportFaceArtifact(version); AetherResult aetherResult = aether.resolve(artifact); File allureReportFace = aetherResult.get().get(0).getArtifact().getFile(); unpackJar(allureReportFace, outputDirectory, filterByRegex(ALLURE_REPORT_FACE_FILE_REGEX)); } catch (Exception e) { throw new AllureReportBuilderException(e); } } /** * Check given directories. For each directory try to create directory if doesn't exist. * * @param dirs a array of directories to check * @throws AllureReportBuilderException if can't create directory * @see #checkDirectory(java.io.File) */ private void checkDirectories(File... dirs) throws AllureReportBuilderException { for (File dir : dirs) { checkDirectory(dir); } } /** * Check given directory. Try to create directory if doesn't exist. * * @param dir a directory to check * @throws AllureReportBuilderException if can't create directory */ private void checkDirectory(File dir) throws AllureReportBuilderException { if (!dir.exists() && !dir.mkdirs()) { throw new AllureReportBuilderException("Report directory doesn't exists and can't be created."); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy