org.conqat.engine.commons.findings.TestDetachedFindingBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of teamscale-commons-test-fixtures Show documentation
Show all versions of teamscale-commons-test-fixtures Show documentation
Provides common DTOs for Teamscale
/*
* Copyright (c) CQSE GmbH
*
* 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.
*/
package org.conqat.engine.commons.findings;
import static java.util.Objects.requireNonNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;
import org.conqat.engine.commons.findings.location.ElementLocation;
import org.conqat.engine.commons.findings.location.TextRegionLocation;
import org.conqat.lib.commons.assessment.ETrafficLightColor;
/**
* Builds {@link DetachedFinding}s for test purposes.
*/
public class TestDetachedFindingBuilder {
/**
* The source of a unique but otherwise arbitrary instance number for each instance created by this
* builder. Initialized with a random value to make it impossible for tests to rely on any specific
* value. If a test depends on a particular value, it must make this dependence explicit by calling
* one of the builder's methods.
*/
private static final AtomicInteger instanceNumber = new AtomicInteger(ThreadLocalRandom.current().nextInt(1000));
private String group;
private String category;
private String message;
private ElementLocation location;
private ETrafficLightColor assessment;
private final List statementPathElements = new ArrayList<>();
private final Map findingProperties = new HashMap<>();
/**
* @return a builder creating some fresh {@link DetachedFinding} whose fields are all initialized
* with valid values.
*/
public static TestDetachedFindingBuilder someDetachedFinding() {
int instance = instanceNumber.incrementAndGet();
TestDetachedFindingBuilder someDetachedFinding = new TestDetachedFindingBuilder();
return someDetachedFinding.withGroupName("Some Group") //
.withCategory("Some Category") //
.withMessage("Some Finding") //
.withLocation(new TextRegionLocation(String.format("org/example/Example%d.java", instance), //
instance * 1000, instance * 1001, // offsets
instance, instance + 1)) // lines
.withAssessment(ETrafficLightColor.RED);
}
private TestDetachedFindingBuilder() {
// Intentionally private. Clients must obtain Builder via someDetachedFinding().
}
/**
* Ensures that the {@link DetachedFinding} is {@linkplain #build() built} with the given
* {@linkplain DetachedFinding#getGroupName() group}.
*/
public TestDetachedFindingBuilder withGroupName(String group) {
this.group = requireNonNull(group);
return this;
}
/**
* Ensures that the {@link DetachedFinding} is {@linkplain #build() built} with the given
* {@linkplain DetachedFinding#getCategoryName() category}.
*/
public TestDetachedFindingBuilder withCategory(String category) {
this.category = requireNonNull(category);
return this;
}
/**
* Ensures that the {@link DetachedFinding} is {@linkplain #build() built} with the given
* {@linkplain DetachedFinding#getMessage() message}.
*/
public TestDetachedFindingBuilder withMessage(String message) {
this.message = requireNonNull(message);
return this;
}
/**
* Ensures that the {@link DetachedFinding} is {@linkplain #build() built} with the given
* {@linkplain DetachedFinding#getAssessment() assessment}.
*/
public TestDetachedFindingBuilder withAssessment(ETrafficLightColor assessment) {
this.assessment = assessment;
return this;
}
/**
* Ensures that the {@link DetachedFinding} is {@linkplain #build() built} with the given
* {@linkplain DetachedFinding#getLocation() location}.
*/
public TestDetachedFindingBuilder withLocation(ElementLocation location) {
this.location = requireNonNull(location);
return this;
}
/**
* Ensures that the {@link DetachedFinding} is {@linkplain #build() built} with an element location
* that has the {@link ElementLocation#getUniformPath()} set to the given uniform path.
*/
public TestDetachedFindingBuilder withLocation(String uniformPath) {
TextRegionLocation oldLocation = (TextRegionLocation) location;
location = new TextRegionLocation(uniformPath, oldLocation.getRawStartOffset(), oldLocation.getRawEndOffset(),
oldLocation.getRawStartLine(), oldLocation.getRawEndLine());
return this;
}
/**
* Ensures that the {@link DetachedFinding} is {@linkplain #build() built} with the given
* {@linkplain TextRegionLocation#getRawStartOffset() start} and
* {@linkplain TextRegionLocation#getRawEndOffset() end} offset
*/
public TestDetachedFindingBuilder withStartAndEndOffsets(int startOffset, int endOffset) {
TextRegionLocation oldLocation = (TextRegionLocation) location;
this.location = new TextRegionLocation(oldLocation.getUniformPath(), startOffset, endOffset,
oldLocation.getRawStartLine(), oldLocation.getRawEndLine());
return this;
}
/**
* Ensures that the {@link DetachedFinding} is {@linkplain #build() built} with the given
* {@linkplain TextRegionLocation#getRawStartLine() start} and
* {@linkplain TextRegionLocation#getRawEndLine() end} lines
*/
public TestDetachedFindingBuilder withStartAndEndLine(int startLine, int endLine) {
TextRegionLocation oldLocation = (TextRegionLocation) location;
this.location = new TextRegionLocation(oldLocation.getUniformPath(), oldLocation.getRawStartOffset(),
oldLocation.getRawEndOffset(), startLine, endLine);
return this;
}
/**
* Ensures that the {@link DetachedFinding} is {@linkplain #build() built} with the given
* {@linkplain StatementPathElement}.
*/
public TestDetachedFindingBuilder withStatementPathElement(StatementPathElement pathElement) {
statementPathElements.add(pathElement);
return this;
}
/**
* Ensures that the {@link DetachedFinding} is {@linkplain #build() built} with the given property.
*/
public TestDetachedFindingBuilder withFindingProperty(String key, Object property) {
findingProperties.put(key, property);
return this;
}
/**
* Builds the {@link DetachedFinding}.
*
* @return {@link DetachedFinding} whose fields are all initialized with arbitrary but valid values,
* unless overwritten using this builder's methods.
*/
public DetachedFinding build() {
DetachedFinding detachedFinding = new DetachedFinding(group, category, message, location, assessment,
findingProperties);
detachedFinding.setStatementPath(statementPathElements);
return detachedFinding;
}
}