Skip to content
This repository has been archived by the owner on Apr 6, 2022. It is now read-only.

Commit

Permalink
Merge branch 'JENKINS-50073' of https://github.com/LokiNightray/analy…
Browse files Browse the repository at this point in the history
…sis-core-plugin into LokiNightray-JENKINS-50073
  • Loading branch information
uhafner committed May 8, 2018
2 parents 0ebe78b + 0430654 commit d643ce7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 73 deletions.
Expand Up @@ -16,7 +16,7 @@
import hudson.model.Run;

/**
* Tests the class {@link BuildHistory.BuildResultIterator}.
* Tests the class {@link BuildHistory}.
*
* @author Ullrich Hafner
*/
Expand Down Expand Up @@ -135,7 +135,7 @@ void shouldThrowANoSuchElementException() {
assertThrows(NoSuchElementException.class, iterator::next);
}

/** Verifies that getPreviousResult returns no results without ResultAction. */
/** Verifies that {@link BuildHistory#getPreviousResult()} returns no results without ResultAction. */
@Test
void shouldNotReturnResultsWithoutResultAction() {
ResultAction action = mock(ResultAction.class);
Expand All @@ -153,12 +153,10 @@ void shouldNotReturnResultsWithoutResultAction() {

BuildHistory history = new BuildHistory(run, selector);

Optional<AnalysisResult> actualResultAction = history.getPreviousResult();

assertThat(actualResultAction).isEqualTo(Optional.empty());
assertThat(history.getPreviousResult()).isEmpty();
}

/** Verifies that getPreviousResult returns a result. */
/** Verifies that {@link BuildHistory#getPreviousResult()} returns a result. */
@Test
void shouldOnlyReturnAnalysisResultIfResultActionIsSuccessful() {
ResultAction action = mock(ResultAction.class);
Expand All @@ -172,7 +170,7 @@ void shouldOnlyReturnAnalysisResultIfResultActionIsSuccessful() {
when(selector.get(run)).thenReturn(Optional.of(action));

ResultAction prevAction = mock(ResultAction.class);
when(prevAction.isSuccessful()).thenReturn(true, false);
when(prevAction.isSuccessful()).thenReturn(true);

AnalysisResult prevBuildResult = mock(AnalysisResult.class);
when(prevAction.getResult()).thenReturn(prevBuildResult);
Expand All @@ -183,14 +181,15 @@ void shouldOnlyReturnAnalysisResultIfResultActionIsSuccessful() {

BuildHistory history = new BuildHistory(run, selector);

Optional<AnalysisResult> actualResultActionSuccess = history.getPreviousResult();
Optional<AnalysisResult> actualResultActionNoSuccess = history.getPreviousResult();
when(prevAction.isSuccessful()).thenReturn(true);
assertThat(history.getPreviousResult()).contains(prevBuildResult);


assertThat(actualResultActionSuccess).isEqualTo(Optional.of(prevBuildResult));
assertThat(actualResultActionNoSuccess).isEqualTo(Optional.empty());
when(prevAction.isSuccessful()).thenReturn(false);
assertThat(history.getPreviousResult()).isEmpty();
}

/** Verifies that getBaselineResult returns the result of the baseline. */
/** Verifies that {@link BuildHistory#getBaselineResult()} returns the result of the baseline. */
@Test
void shouldReturnBaseLineResult() {
Run baselineRun = mock(Run.class);
Expand All @@ -201,11 +200,9 @@ void shouldReturnBaseLineResult() {
when(resultSelector.get(baselineRun)).thenReturn(Optional.of(resultAction));
when(resultAction.getResult()).thenReturn(result);

StablePluginReference stablePluginReference = new StablePluginReference(baselineRun, resultSelector, true);

final Optional<AnalysisResult> actualOptionalAnalysisResult = stablePluginReference.getBaselineResult();
BuildHistory buildHistory = new BuildHistory(baselineRun, resultSelector);

assertThat(actualOptionalAnalysisResult).isEqualTo(Optional.of(result));
assertThat(buildHistory.getBaselineResult()).contains(result);
}

}
@@ -1,5 +1,6 @@
package io.jenkins.plugins.analysis.core.history;

import java.sql.Ref;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
Expand All @@ -16,23 +17,28 @@
import hudson.model.Result;
import hudson.model.Run;

abstract class ReferenceFinderTest {

/** Should return a ReferenceFinder with overallResultSuccessMustBe set to true. */
abstract ReferenceFinder getReferenceFinder(Run baseline, ResultSelector resultSelector);

/** Verifies that the create Method creates the instances of the correct ReferenceProviders. */
/**
* Tests the abstract class {@link ReferenceFinderTest} using the abstract test pattern.
*
* @author Stephan Plöderl
*/
public abstract class ReferenceFinderTest {

/**
* Should return an instance of {@link ReferenceFinder} with overallResultSuccessMustBe set to true.
*
* @return instance of a childclass of {@link ReferenceFinder} which shall be tested.
*/
protected abstract ReferenceFinder getReferenceFinder(Run baseline, ResultSelector resultSelector);

/** Verifies that {@link ReferenceFinder#create(Run, ResultSelector, boolean, boolean)} creates the instances of the correct ReferenceProviders. */
@Test
void createsRightInstance() {

ReferenceProvider actualStablePluginReference = ReferenceFinder.create(null, null, false, false);
ReferenceProvider actualPreviousRunReference = ReferenceFinder.create(null, null, true, false);

assertThat(actualStablePluginReference).isInstanceOf(StablePluginReference.class);
assertThat(actualPreviousRunReference).isInstanceOf(PreviousRunReference.class);
assertThat(ReferenceFinder.create(null, null, false, false)).isInstanceOf(StablePluginReference.class);
assertThat(ReferenceFinder.create(null, null, true, false)).isInstanceOf(PreviousRunReference.class);
}

/** Should return the right owner. */
/** Verifies that {@link ReferenceFinder#getAnalysisRun()} returns the right owner. */
@Test
void shouldReturnRightOwner() {
Run baseline = mock(Run.class);
Expand All @@ -51,14 +57,11 @@ void shouldReturnRightOwner() {

ReferenceFinder referenceFinder = getReferenceFinder(baseline, resultSelector);

Optional<Run<?, ?>> actualOwner = referenceFinder.getAnalysisRun();
Optional<Run<?, ?>> actualNoOwner = referenceFinder.getAnalysisRun();

assertThat(actualOwner).isEqualTo(Optional.of(baseline));
assertThat(actualNoOwner).isEqualTo(Optional.empty());
assertThat(referenceFinder.getAnalysisRun()).contains(baseline);
assertThat(referenceFinder.getAnalysisRun()).isEmpty();
}

/** should get the issues of the reference job. */
/** Verifies that {@link ReferenceFinder#getIssues()} returns the issues of the reference-job. */
@Test
void getIssuesOfReferenceJob() {
Run baseline = mock(Run.class);
Expand Down Expand Up @@ -86,11 +89,8 @@ void getIssuesOfReferenceJob() {

ReferenceFinder referenceFinder = getReferenceFinder(baseline, resultSelector);

Issues<?> actualIssues = referenceFinder.getIssues();
Issues<?> actualEmptyIssues = referenceFinder.getIssues();

assertThat(actualIssues).isEqualTo(issues);
assertThat(actualEmptyIssues).isEqualTo(new Issues<>());
assertThat(referenceFinder.getIssues()).isEqualTo(issues);
assertThat(referenceFinder.getIssues()).isEqualTo(new Issues<>());
}

}
Expand Up @@ -14,9 +14,14 @@
import hudson.model.Result;
import hudson.model.Run;

/**
* Tests the class {@link StablePluginReference}.
*
* @author Stephan Plöderl
*/
class StablePluginReferenceTest extends ReferenceFinderTest {

/** Verifies that getReferenceAction returns no action if not run, yet. */
/** Verifies that {@link StablePluginReference#getReferenceAction()} returns no action if not run, yet. */
@Test
void shouldNotReturnAStableRunIfNotBuildYet() {
Run<?, ?> baseline = mock(Run.class);
Expand All @@ -25,12 +30,10 @@ void shouldNotReturnAStableRunIfNotBuildYet() {
StablePluginReference stablePluginReference = new StablePluginReference(baseline, resultSelector,
true);

Optional<ResultAction> actualOptionalResultAction = stablePluginReference.getReferenceAction();

assertThat(actualOptionalResultAction).isEqualTo(Optional.empty());
assertThat(stablePluginReference.getReferenceAction()).isEmpty();
}

/** Verifies that getPreviousAction should not return a ResultAction if no ResultAction is available. */
/** Verifies that {@link StablePluginReference#getReferenceAction()} should not return a ResultAction if no ResultAction is available. */
@Test
void shouldNotReturnAStableRunWhenThereIsNoResultAction() {
Run baseline = mock(Run.class);
Expand All @@ -43,12 +46,10 @@ void shouldNotReturnAStableRunWhenThereIsNoResultAction() {
StablePluginReference stablePluginReference = new StablePluginReference(baseline, resultSelector,
true);

Optional<ResultAction> actualOptionalResultAction = stablePluginReference.getReferenceAction();

assertThat(actualOptionalResultAction).isEqualTo(Optional.empty());
assertThat(stablePluginReference.getReferenceAction()).isEmpty();
}

/** Verifies that getPreviousAction does not return a ResultAction if there is no successful run, yet. */
/** Verifies that {@link StablePluginReference#getReferenceAction()} does not return a ResultAction if there is no successful run, yet. */
@Test
void shouldNotReturnARunWhenTheResultActionIsNotSuccessful() {
Run baseline = mock(Run.class);
Expand All @@ -65,12 +66,10 @@ void shouldNotReturnARunWhenTheResultActionIsNotSuccessful() {
StablePluginReference stablePluginReference = new StablePluginReference(baseline, resultSelector,
true);

Optional<ResultAction> actualOptionalResultAction = stablePluginReference.getReferenceAction();

assertThat(actualOptionalResultAction).isEqualTo(Optional.empty());
assertThat(stablePluginReference.getReferenceAction()).isEmpty();
}

/** Verifies that getPreviousAction does not return a ResultAction if the ResultAction is not successful. */
/** Verifies that {@link StablePluginReference#getReferenceAction()} does not return a ResultAction if the ResultAction is not successful. */
@Test
void shouldNotReturnAResultActionIfTheAnalysisResultIsNoSuccess() {
Run baseline = mock(Run.class);
Expand All @@ -88,12 +87,10 @@ void shouldNotReturnAResultActionIfTheAnalysisResultIsNoSuccess() {
StablePluginReference stablePluginReference = new StablePluginReference(baseline, resultSelector,
true);

Optional<ResultAction> actualOptionalResultAction = stablePluginReference.getReferenceAction();

assertThat(actualOptionalResultAction).isEqualTo(Optional.empty());
assertThat(stablePluginReference.getReferenceAction()).isEmpty();
}

/** Verifies that getPreviousAction returns successful ResultActions. */
/** Verifies that {@link StablePluginReference#getReferenceAction()} returns successful ResultActions. */
@Test
void shouldReturnAResultActionIfPrevRunIsSuccessful() {
Run baseline = mock(Run.class);
Expand All @@ -114,26 +111,23 @@ void shouldReturnAResultActionIfPrevRunIsSuccessful() {
StablePluginReference stablePluginReference = new StablePluginReference(baseline, resultSelector,
true);

Optional<ResultAction> actualOptionalResultAction = stablePluginReference.getReferenceAction();

assertThat(stablePluginReference.getReferenceAction()).contains(resultAction);
verify(resultSelector, times(2)).get(prevRun);
assertThat(actualOptionalResultAction).isEqualTo(Optional.of(resultAction));
}

/** Verifies that getPreviousAction only returns the wanted ResultActions. */
/** Verifies that {@link StablePluginReference#getReferenceAction()} only returns the wanted ResultActions. */
@Test
void shouldOnlyReturnPreviousNonFailureResultsOrBuildsWhereOverallResultsAreFailure() {
Run baseline = mock(Run.class);
Run prevJob = mock(Run.class);
when(baseline.getPreviousBuild()).thenReturn(prevJob);
when(prevJob.getResult()).thenReturn(Result.UNSTABLE, Result.FAILURE);

ResultAction resultAction = mock(ResultAction.class);
when(resultAction.isSuccessful()).thenReturn(true);

AnalysisResult analysisResult = mock(AnalysisResult.class);
when(resultAction.getResult()).thenReturn(analysisResult);
when(analysisResult.getOverallResult()).thenReturn(Result.UNSTABLE, Result.FAILURE);
when(analysisResult.getOverallResult()).thenReturn(Result.UNSTABLE);

ResultSelector resultSelector = mock(ResultSelector.class);
when(resultSelector.get(prevJob)).thenReturn(Optional.of(resultAction));
Expand All @@ -143,21 +137,20 @@ void shouldOnlyReturnPreviousNonFailureResultsOrBuildsWhereOverallResultsAreFail

StablePluginReference stablePluginReference = new StablePluginReference(baseline, resultSelector, false);

// prevJob.getResult() returns Unstable
Optional<ResultAction> actualOptionalResultAction = stablePluginReference.getReferenceAction();
// prevJob.getResult() returns Failure and OverallResult is Unstable
Optional<ResultAction> actualOptionalResultActionOfFailure = stablePluginReference.getReferenceAction();
// prevJob.getResult() returns Failure and OverallResult is Failure
Optional<ResultAction> actualOptionalResultActionOfFailureAndOverallFailure = stablePluginReference.getReferenceAction();
when(prevJob.getResult()).thenReturn(Result.UNSTABLE);
assertThat(stablePluginReference.getReferenceAction()).contains(resultAction);

when(prevJob.getResult()).thenReturn(Result.FAILURE);
assertThat(stablePluginReference.getReferenceAction()).isEmpty();

assertThat(actualOptionalResultAction).isEqualTo(Optional.of(resultAction));
assertThat(actualOptionalResultActionOfFailure).isEqualTo(Optional.empty());
assertThat(actualOptionalResultActionOfFailureAndOverallFailure).isEqualTo(Optional.of(resultAction));
when(analysisResult.getOverallResult()).thenReturn(Result.FAILURE);
assertThat(stablePluginReference.getReferenceAction()).contains(resultAction);

}

/** see {@link ReferenceFinderTest#getReferenceFinder(Run, ResultSelector)}.*/
@Override
ReferenceFinder getReferenceFinder(Run baseline, ResultSelector resultSelector) {
protected ReferenceFinder getReferenceFinder(final Run baseline, final ResultSelector resultSelector) {
return new StablePluginReference(baseline, resultSelector, true);
}
}

0 comments on commit d643ce7

Please sign in to comment.