Skip to content

Commit

Permalink
some refactoring
Browse files Browse the repository at this point in the history
- added abstract report descriptor
- simplified tool descriptors
- NPE checks while traversing reports
  • Loading branch information
cpoenisch committed Sep 16, 2016
1 parent 4e4b3be commit 3a98d05
Show file tree
Hide file tree
Showing 21 changed files with 165 additions and 218 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Copyright (c) 2015-2016 TraceTronic GmbH
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* 3. Neither the name of TraceTronic GmbH nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER 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 de.tracetronic.jenkins.plugins.ecutest.report;

import hudson.model.AbstractProject;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Publisher;
import hudson.tools.ToolInstallation;
import de.tracetronic.jenkins.plugins.ecutest.tool.installation.ETInstallation;

/**
* Common base descriptor class for all report related publisher descriptors implemented in this plugin.
*
* @author Christian Pönisch <christian.poenisch@tracetronic.de>
*/
public abstract class AbstractReportDescriptor extends BuildStepDescriptor<Publisher> {

/**
* Gets the tool installations.
*
* @return the installations
*/
public ETInstallation[] getToolInstallations() {
return getToolDescriptor().getInstallations();
}

/**
* Gets the tool descriptor holding the installations.
*
* @return the tool descriptor
*/
public ETInstallation.DescriptorImpl getToolDescriptor() {
return ToolInstallation.all().get(ETInstallation.DescriptorImpl.class);
}

@SuppressWarnings("rawtypes")
@Override
public boolean isApplicable(final Class<? extends AbstractProject> jobType) {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import hudson.model.Run;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Recorder;
import hudson.tools.ToolInstallation;

import java.io.File;
import java.io.IOException;
Expand All @@ -49,10 +50,10 @@
import java.util.Collections;
import java.util.List;

import jenkins.model.Jenkins;
import javax.annotation.CheckForNull;

import jenkins.tasks.SimpleBuildStep;

import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundSetter;

import de.tracetronic.jenkins.plugins.ecutest.ETPluginException;
Expand All @@ -70,7 +71,6 @@
*
* @author Christian Pönisch <christian.poenisch@tracetronic.de>
*/
@SuppressWarnings("unchecked")
public abstract class AbstractReportPublisher extends Recorder implements SimpleBuildStep {

private boolean allowMissing;
Expand Down Expand Up @@ -251,11 +251,6 @@ public void perform(final Run<?, ?> run, final FilePath workspace, final Launche
protected abstract void performReport(final Run<?, ?> run, final FilePath workspace, final Launcher launcher,
final TaskListener listener) throws InterruptedException, IOException, ETPluginException;

@Override
public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.NONE;
}

/**
* Returns whether this publisher can continue processing. Returns {@code true} if the property {@code runOnFailed}
* is set or if the build is not aborted or failed.
Expand Down Expand Up @@ -314,18 +309,17 @@ protected ETInstallation configureToolInstallation(final String toolName, final
* @return the tool installation
*/
public ETInstallation getToolInstallation(final String toolName, final EnvVars envVars) {
final Jenkins instance = Jenkins.getInstance();
if (instance != null) {
final ETInstallation[] installations = instance.getDescriptorByType(
ETInstallation.DescriptorImpl.class).getInstallations();
final String expToolName = envVars.expand(toolName);
for (final ETInstallation installation : installations) {
if (StringUtils.equals(expToolName, installation.getName())) {
return installation;
}
}
}
return null;
final String expToolName = envVars.expand(toolName);
return getToolDescriptor().getInstallation(expToolName);
}

/**
* Gets the tool descriptor holding the installations.
*
* @return the tool descriptor
*/
public ETInstallation.DescriptorImpl getToolDescriptor() {
return ToolInstallation.all().get(ETInstallation.DescriptorImpl.class);
}

/**
Expand Down Expand Up @@ -439,6 +433,7 @@ protected List<FilePath> getReportFiles(final Run<?, ?> run, final Launcher laun
* @throws InterruptedException
* if the build gets interrupted
*/
@CheckForNull
public static FilePath getFirstReportFile(final FilePath reportDir) throws IOException, InterruptedException {
final FilePath[] files = reportDir.list("*" + TRFPublisher.TRF_EXTENSION);
return files.length > 0 ? files[0] : null;
Expand Down Expand Up @@ -467,4 +462,14 @@ public static void removePreviousReports(final Run<?, ?> run,
prevBuild = prevBuild.getPreviousBuild();
}
}

@Override
public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.NONE;
}

@Override
public AbstractReportDescriptor getDescriptor() {
return (AbstractReportDescriptor) super.getDescriptor();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,9 @@
import hudson.Launcher;
import hudson.model.Result;
import hudson.model.TaskListener;
import hudson.model.AbstractProject;
import hudson.model.Descriptor;
import hudson.model.Run;
import hudson.remoting.Callable;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Publisher;
import hudson.tools.ToolInstallation;
import hudson.util.FormValidation;

import java.io.IOException;
Expand All @@ -70,6 +66,7 @@
import de.tracetronic.jenkins.plugins.ecutest.ETPlugin;
import de.tracetronic.jenkins.plugins.ecutest.ETPluginException;
import de.tracetronic.jenkins.plugins.ecutest.log.TTConsoleLogger;
import de.tracetronic.jenkins.plugins.ecutest.report.AbstractReportDescriptor;
import de.tracetronic.jenkins.plugins.ecutest.report.AbstractReportPublisher;
import de.tracetronic.jenkins.plugins.ecutest.report.atx.installation.ATXConfig;
import de.tracetronic.jenkins.plugins.ecutest.report.atx.installation.ATXCustomSetting;
Expand Down Expand Up @@ -356,17 +353,12 @@ public String getUrlName() {
return URL_NAME;
}

@Override
public DescriptorImpl getDescriptor() {
return (DescriptorImpl) super.getDescriptor();
}

/**
* DescriptorImpl for {@link ATXPublisher}.
*/
@SuppressWarnings("rawtypes")
@Extension(ordinal = 1004)
public static class DescriptorImpl extends BuildStepDescriptor<Publisher> {
public static class DescriptorImpl extends AbstractReportDescriptor {

@CopyOnWrite
private volatile ATXInstallation[] installations = new ATXInstallation[0];
Expand All @@ -390,15 +382,6 @@ public DescriptorImpl() {
syncWithDefaultConfig();
}

/**
* Gets the ATX version that this ATX configuration is based on.
*
* @return the related ATX version
*/
public static String getATXVersion() {
return ETPlugin.ATX_VERSION.toShortString();
}

/**
* @return the list of ATX installations
*/
Expand Down Expand Up @@ -428,9 +411,9 @@ public ATXConfig getDefaultConfig() {
public boolean configure(final StaplerRequest req, final JSONObject json) {
final List<ATXInstallation> list = new ArrayList<ATXInstallation>();
final JSONArray instArray = new JSONArray();
final JSONArray inst = json.optJSONArray("inst");
final JSONArray inst = json.optJSONArray("installation");
if (inst == null) {
instArray.add(json.getJSONObject("inst"));
instArray.add(json.getJSONObject("installation"));
} else {
instArray.addAll(inst);
}
Expand Down Expand Up @@ -497,8 +480,8 @@ private void syncWithDefaultConfig() {
}
}
final List<ATXCustomSetting> customSettings = currentConfig.getCustomSettings();
newConfig.setCustomSettings(customSettings == null ? new ArrayList<ATXCustomSetting>()
: customSettings);
newConfig.setCustomSettings(customSettings == null ?
new ArrayList<ATXCustomSetting>() : customSettings);
}

// Fill installations
Expand Down Expand Up @@ -549,6 +532,15 @@ private Map<String, List<ATXSetting>> updateCurrentValues(final JSONObject instJ
return newConfigMap;
}

/**
* Gets the ATX version that this ATX configuration is based on.
*
* @return the related ATX version
*/
public static String getATXVersion() {
return ETPlugin.ATX_VERSION.toShortString();
}

/**
* Gets the target type for Jelly.
*
Expand All @@ -566,8 +558,8 @@ public static Class<ATXPublisher> getTargetType() {
* @return the custom settings list
*/
public List<ATXCustomSetting> getCustomSettings(final ATXInstallation installation) {
return installation == null ? new ArrayList<ATXCustomSetting>() : installation.getConfig()
.getCustomSettings();
return installation == null ?
new ArrayList<ATXCustomSetting>() : installation.getConfig().getCustomSettings();
}

/**
Expand All @@ -587,18 +579,6 @@ public List<Descriptor<? extends ATXCustomSetting>> getApplicableCustomSettings(
return list;
}

/**
* @return the tool descriptor
*/
public ETInstallation.DescriptorImpl getToolDescriptor() {
return ToolInstallation.all().get(ETInstallation.DescriptorImpl.class);
}

@Override
public boolean isApplicable(final Class<? extends AbstractProject> jobType) {
return true;
}

@Override
public String getDisplayName() {
return Messages.ATXPublisher_DisplayName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public boolean generate(final FilePath archiveTarget, final boolean allowMissing
final List<TestEnvInvisibleAction> testEnvActions = run.getActions(TestEnvInvisibleAction.class);
for (final TestEnvInvisibleAction testEnvAction : testEnvActions) {
final FilePath testReportDir = new FilePath(launcher.getChannel(), testEnvAction.getTestReportDir());
final FilePath reportFile = AbstractReportPublisher.getFirstReportFile(testReportDir); // TODO: trf name
if (reportFile.exists()) {
final FilePath reportFile = AbstractReportPublisher.getFirstReportFile(testReportDir);
if (reportFile != null && reportFile.exists()) {
reportFiles.addAll(Arrays.asList(testReportDir.list("**/*" + TRFPublisher.TRF_EXTENSION)));
} else {
if (allowMissing) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ public boolean upload(final boolean allowMissing, final ATXInstallation installa
final List<TestEnvInvisibleAction> testEnvActions = run.getActions(TestEnvInvisibleAction.class);
for (final TestEnvInvisibleAction testEnvAction : testEnvActions) {
final FilePath testReportDir = new FilePath(launcher.getChannel(), testEnvAction.getTestReportDir());
final FilePath reportFile = AbstractReportPublisher.getFirstReportFile(testReportDir); // TODO: trf name
if (reportFile.exists()) {
final FilePath reportFile = AbstractReportPublisher.getFirstReportFile(testReportDir);
if (reportFile != null && reportFile.exists()) {
uploadFiles.addAll(Arrays.asList(testReportDir.list("**/*" + TRFPublisher.TRF_EXTENSION)));

// Prepare ATX report information
Expand Down Expand Up @@ -224,8 +224,8 @@ private int traverseSubReports(final ATXReport atxReport, final FilePath testRep
final String baseUrl, final String from, final String to)
throws IOException, InterruptedException {
for (final FilePath subDir : testReportDir.listDirectories()) {
final FilePath reportFile = AbstractReportPublisher.getFirstReportFile(subDir); // TODO: trf name
if (reportFile.exists()) {
final FilePath reportFile = AbstractReportPublisher.getFirstReportFile(subDir);
if (reportFile != null && reportFile.exists()) {
// Prepare ATX report information for sub-report
final String testName = reportFile.getParent().getName().replaceFirst("^Report\\s", "");
final String atxTestName = ATXUtil.getValidATXName(testName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@
import hudson.Util;
import hudson.model.Result;
import hudson.model.TaskListener;
import hudson.model.AbstractProject;
import hudson.model.Run;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Publisher;
import hudson.tools.ToolInstallation;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -56,6 +52,7 @@
import de.tracetronic.jenkins.plugins.ecutest.ETPluginException;
import de.tracetronic.jenkins.plugins.ecutest.env.TestEnvInvisibleAction;
import de.tracetronic.jenkins.plugins.ecutest.log.TTConsoleLogger;
import de.tracetronic.jenkins.plugins.ecutest.report.AbstractReportDescriptor;
import de.tracetronic.jenkins.plugins.ecutest.report.AbstractReportPublisher;
import de.tracetronic.jenkins.plugins.ecutest.tool.StartETBuilder;
import de.tracetronic.jenkins.plugins.ecutest.tool.client.ETClient;
Expand Down Expand Up @@ -409,38 +406,11 @@ protected String getUrlName() {
return URL_NAME;
}

@Override
public DescriptorImpl getDescriptor() {
return (DescriptorImpl) super.getDescriptor();
}

/**
* DescriptorImpl for {@link ReportGeneratorPublisher}.
*/
@Extension(ordinal = 1001)
public static class DescriptorImpl extends BuildStepDescriptor<Publisher> {

/**
* Instantiates a new {@link DescriptorImpl}.
*/
public DescriptorImpl() {
super();
}

/**
* Gets the tool descriptor.
*
* @return the tool descriptor
*/
public ETInstallation.DescriptorImpl getToolDescriptor() {
return ToolInstallation.all().get(ETInstallation.DescriptorImpl.class);
}

@SuppressWarnings("rawtypes")
@Override
public boolean isApplicable(final Class<? extends AbstractProject> jobType) {
return true;
}
public static class DescriptorImpl extends AbstractReportDescriptor {

@Override
public String getDisplayName() {
Expand Down
Loading

0 comments on commit 3a98d05

Please sign in to comment.