24.09., 9:00 - 11:00: Due to updates GitLab will be unavailable for some minutes between 09:00 and 11:00.

Commit 21647a0f authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck

add TopographyCheckerTest for Source elements

parent f1112308
package org.vadere.simulator.util;
import org.apache.commons.math3.util.Pair;
import org.junit.Before;
import org.junit.Test;
import org.vadere.state.attributes.scenario.AttributesObstacle;
import org.vadere.state.attributes.scenario.builder.AttributesSourceBuilder;
import org.vadere.state.attributes.scenario.builder.AttributesTargetBuilder;
import org.vadere.state.scenario.Obstacle;
import org.vadere.state.scenario.Topography;
import org.vadere.util.geometry.shapes.VRectangle;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.swing.*;
......@@ -15,72 +21,219 @@ import static org.junit.Assert.assertEquals;
public class TopographyCheckerTest {
@Test
public void testCheckObstacleOverlapHasOverlap(){
Topography topography = new Topography();
TopographyTestBuilder builder;
Obstacle obs1 = new Obstacle(new AttributesObstacle(0, new VRectangle(0,0,1,1)));
Obstacle obs2 = new Obstacle(new AttributesObstacle(1, new VRectangle(0,0,1,1)));
@Before
public void setup() {
builder = new TopographyTestBuilder();
}
topography.addObstacle(obs1);
topography.addObstacle(obs2);
@Test
public void testCheckObstacleOverlapHasOverlap() {
Topography topography = new Topography();
TopographyChecker topcheck = new TopographyChecker(topography);
Obstacle obs1 = new Obstacle(new AttributesObstacle(0, new VRectangle(0, 0, 1, 1)));
Obstacle obs2 = new Obstacle(new AttributesObstacle(1, new VRectangle(0, 0, 1, 1)));
List<Pair<Obstacle, Obstacle>> actualList = topcheck.checkObstacleOverlap();
topography.addObstacle(obs1);
topography.addObstacle(obs2);
assertEquals(1, actualList.size());
}
TopographyChecker topcheck = new TopographyChecker(topography);
@Test
public void tset(){
MsgDocument doc = new MsgDocument();
doc.setContentType("text/html");
doc.setText("File not found please contact:<a href='element/0023'>e-mail to</a> or call 963");
doc.addHyperlinkListener( e -> {
System.out.println(e.getURL());
});
List<Pair<Obstacle, Obstacle>> actualList = topcheck.checkObstacleOverlap();
}
assertEquals(1, actualList.size());
}
class MsgDocument extends JTextPane {
@Test
public void tset() {
MsgDocument doc = new MsgDocument();
doc.setContentType("text/html");
doc.setText("File not found please contact:<a href='element/0023'>e-mail to</a> or call 963");
doc.addHyperlinkListener(e -> {
System.out.println(e.getURL());
});
public MsgDocument(){
}
}
class MsgDocument extends JTextPane {
public MsgDocument() {
}
}
@Test
public void testCheckObstacleOverlapHasNoOverlap(){
Topography topography = new Topography();
Obstacle obs1 = new Obstacle(new AttributesObstacle(0, new VRectangle(0,0,1,1)));
Obstacle obs2 = new Obstacle(new AttributesObstacle(1, new VRectangle(1.1,0,1,1)));
topography.addObstacle(obs1);
topography.addObstacle(obs2);
}
TopographyChecker topcheck = new TopographyChecker(topography);
@Test
public void testCheckObstacleOverlapHasNoOverlap() {
Topography topography = new Topography();
List<Pair<Obstacle, Obstacle>> actualList = topcheck.checkObstacleOverlap();
Obstacle obs1 = new Obstacle(new AttributesObstacle(0, new VRectangle(0, 0, 1, 1)));
Obstacle obs2 = new Obstacle(new AttributesObstacle(1, new VRectangle(1.1, 0, 1, 1)));
topography.addObstacle(obs1);
topography.addObstacle(obs2);
assertEquals(0, actualList.size());
}
TopographyChecker topcheck = new TopographyChecker(topography);
@Test
public void testCheckObstacleOverlapReturnsNoOverlapsIfTwoSegmentsTouch(){
Topography topography = new Topography();
List<Pair<Obstacle, Obstacle>> actualList = topcheck.checkObstacleOverlap();
Obstacle obs1 = new Obstacle(new AttributesObstacle(0, new VRectangle(0,0,1,1)));
Obstacle obs2 = new Obstacle(new AttributesObstacle(1, new VRectangle(1,0,1,1)));
topography.addObstacle(obs1);
topography.addObstacle(obs2);
assertEquals(0, actualList.size());
}
TopographyChecker topcheck = new TopographyChecker(topography);
@Test
public void testCheckObstacleOverlapReturnsNoOverlapsIfTwoSegmentsTouch() {
Topography topography = new Topography();
List<Pair<Obstacle, Obstacle>> actualList = topcheck.checkObstacleOverlap();
Obstacle obs1 = new Obstacle(new AttributesObstacle(0, new VRectangle(0, 0, 1, 1)));
Obstacle obs2 = new Obstacle(new AttributesObstacle(1, new VRectangle(1, 0, 1, 1)));
topography.addObstacle(obs1);
topography.addObstacle(obs2);
TopographyChecker topcheck = new TopographyChecker(topography);
List<Pair<Obstacle, Obstacle>> actualList = topcheck.checkObstacleOverlap();
assertEquals(0, actualList.size());
}
/**
* There should be non unique ids
*/
@Test
public void testCheckUniqueSourceIdNegative() {
builder.addSource(); //id = -1 ok first
builder.addSource(); //id = -1 err
builder.addSource(); //id = -1 err
builder.addSource(2); // ok first
builder.addSource(2); // err
Topography topography = builder.build();
TopographyChecker checker = new TopographyChecker(topography);
List<TopographyCheckerMessage> out = checker.checkUniqueSourceId();
assertEquals("The sources should have the same id", 3, out.size());
out.forEach(m -> assertEquals(TopographyCheckerReason.SOURCE_ID_NOT_UNIQUE, m.getReason()));
}
/**
* There should be only unique ids
*/
@Test
public void testCheckUniqueSourceIdPositive() {
builder.addSource(1);
builder.addSource(2);
builder.addSource(3);
Topography topography = builder.build();
TopographyChecker checker = new TopographyChecker(topography);
List<TopographyCheckerMessage> out = checker.checkUniqueSourceId();
assertEquals("No warnings expected", 0, out.size());
}
@Test
public void TestCheckValidTargetsInSourceNoIdNoSpawn() {
AttributesSourceBuilder attrBuilder = AttributesSourceBuilder.anAttributesSource();
builder.addSource(attrBuilder
.spawnNumber(0)
.targetIds(new ArrayList<>())
.build()
);
Topography topography = builder.build();
TopographyChecker checker = new TopographyChecker(topography);
List<TopographyCheckerMessage> out = checker.checkValidTargetsInSource();
assertEquals(1, out.size());
assertEquals(TopographyCheckerReason.SOURCE_NO_TARGET_ID_NO_SPAWN, out.get(0).getReason());
}
@Test
public void TestCheckValidTargetsInSourceNoId() {
AttributesSourceBuilder attrBuilder = AttributesSourceBuilder.anAttributesSource();
builder.addSource(attrBuilder
.targetIds(new ArrayList<>())
.build()
);
Topography topography = builder.build();
TopographyChecker checker = new TopographyChecker(topography);
List<TopographyCheckerMessage> out = checker.checkValidTargetsInSource();
assertEquals(1, out.size());
assertEquals(TopographyCheckerReason.SOURCE_NO_TARGET_ID_SET, out.get(0).getReason());
assertEquals(TopographyCheckerMessageType.ERROR, out.get(0).getMsgType());
}
@Test
public void TestCheckValidTargetsInSourceWrongId() {
AttributesSourceBuilder attrSourceB = AttributesSourceBuilder.anAttributesSource();
AttributesTargetBuilder attrTargetB = AttributesTargetBuilder.anAttributesTarget();
builder.addSource(attrSourceB
.targetIds(Collections.singletonList(4)) // id not found !
.build()
);
builder.addTarget(attrTargetB
.id(1)
.build());
Topography topography = builder.build();
TopographyChecker checker = new TopographyChecker(topography);
List<TopographyCheckerMessage> out = checker.checkValidTargetsInSource();
assertEquals(1, out.size());
assertEquals(TopographyCheckerReason.SOURCE_TARGET_ID_NOT_FOUND, out.get(0).getReason());
assertEquals(TopographyCheckerMessageType.ERROR, out.get(0).getMsgType());
}
@Test
public void TestCheckValidTargetsInSourceWithSomeWrongId() {
AttributesSourceBuilder attrSourceB = AttributesSourceBuilder.anAttributesSource();
AttributesTargetBuilder attrTargetB = AttributesTargetBuilder.anAttributesTarget();
builder.addSource(attrSourceB
.targetIds(Arrays.asList(1, 2, 3)) // id 3 not found !
.build()
);
builder.addTarget(attrTargetB
.id(1)
.build());
builder.addTarget(attrTargetB
.id(3)
.build());
Topography topography = builder.build();
TopographyChecker checker = new TopographyChecker(topography);
List<TopographyCheckerMessage> out = checker.checkValidTargetsInSource();
assertEquals(1, out.size());
assertEquals(TopographyCheckerReason.SOURCE_TARGET_ID_NOT_FOUND, out.get(0).getReason());
assertEquals(TopographyCheckerMessageType.ERROR, out.get(0).getMsgType());
assertEquals("[2]", out.get(0).getReasonModifier());
}
@Test
public void TestCheckValidTargetsInSourceNoError() {
AttributesSourceBuilder attrSourceB = AttributesSourceBuilder.anAttributesSource();
AttributesTargetBuilder attrTargetB = AttributesTargetBuilder.anAttributesTarget();
builder.addSource(attrSourceB
.targetIds(Collections.singletonList(1))
.build()
);
builder.addTarget(attrTargetB
.id(1)
.build());
Topography topography = builder.build();
TopographyChecker checker = new TopographyChecker(topography);
List<TopographyCheckerMessage> out = checker.checkValidTargetsInSource();
assertEquals(0, out.size());
}
assertEquals(0, actualList.size());
}
}
\ No newline at end of file
package org.vadere.simulator.util;
import org.vadere.state.attributes.scenario.AttributesObstacle;
import org.vadere.state.attributes.scenario.AttributesSource;
import org.vadere.state.attributes.scenario.AttributesTarget;
import org.vadere.state.scenario.Obstacle;
import org.vadere.state.scenario.Source;
import org.vadere.state.scenario.Target;
import org.vadere.state.scenario.Topography;
import org.vadere.util.geometry.shapes.VRectangle;
import org.vadere.util.geometry.shapes.VShape;
public class TopographyTestBuilder {
Topography topography;
public TopographyTestBuilder(){
topography = new Topography();
}
public Topography build(){
Topography ret = topography;
topography = new Topography();
return ret;
}
TopographyTestBuilder addSource(){
addSource(-1);
return this;
}
TopographyTestBuilder addSource(int id){
addSource(id, new VRectangle(0,0, 10, 10));
return this;
}
TopographyTestBuilder addSource(int id, VShape s){
addSource(new AttributesSource(id,s));
return this;
}
TopographyTestBuilder addSource(AttributesSource attr){
topography.addSource(new Source(attr));
return this;
}
TopographyTestBuilder addTarget(){
addSource(-1);
return this;
}
TopographyTestBuilder addTarget(int id){
addTarget(id, new VRectangle(0,0,10,10));
return this;
}
TopographyTestBuilder addTarget(int id, VShape s){
addTarget(new AttributesTarget(s, id, true));
return this;
}
TopographyTestBuilder addTarget(AttributesTarget attr){
topography.addTarget(new Target(attr));
return this;
}
TopographyTestBuilder addObstacle(){
addObstacle(-1);
return this;
}
TopographyTestBuilder addObstacle(int id){
addObstacle(id, new VRectangle(0,0,10,10));
return this;
}
TopographyTestBuilder addObstacle(int id, VShape s){
addObstacle(new AttributesObstacle(id, s));
return this;
}
TopographyTestBuilder addObstacle(AttributesObstacle attr){
topography.addObstacle(new Obstacle(attr));
return this;
}
}
......@@ -23,7 +23,7 @@ package org.vadere.state.attributes;
*/
public abstract class Attributes extends DefaultSealable implements Cloneable {
/** Used for default ID values of some scenario elements. */
protected static final int ID_NOT_SET = -1;
public static final int ID_NOT_SET = -1;
public Attributes() {}
......
package org.vadere.state.attributes.scenario.builder;
import org.vadere.state.attributes.Attributes;
import org.vadere.state.attributes.scenario.AttributesSource;
import org.vadere.state.types.DynamicElementType;
import org.vadere.util.geometry.shapes.VShape;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public final class AttributesSourceBuilder {
private VShape shape = null;
private String interSpawnTimeDistribution = AttributesSource.CONSTANT_DISTRIBUTION;
private List<Double> distributionParameters = Collections.singletonList(1.0);
private int spawnNumber = 1;
private int maxSpawnNumberTotal = AttributesSource.NO_MAX_SPAWN_NUMBER_TOTAL;
private double startTime = 0;
private double endTime = 0;
private boolean spawnAtRandomPositions;
private boolean useFreeSpaceOnly = true;
private List<Integer> targetIds = new LinkedList<>();
private List<Double> groupSizeDistribution = Arrays.asList(1.0);
private DynamicElementType dynamicElementType = DynamicElementType.PEDESTRIAN;
private int id = Attributes.ID_NOT_SET;
private AttributesSourceBuilder() {
}
public static AttributesSourceBuilder anAttributesSource() {
return new AttributesSourceBuilder();
}
public AttributesSourceBuilder shape(VShape shape) {
this.shape = shape;
return this;
}
public AttributesSourceBuilder interSpawnTimeDistribution(String interSpawnTimeDistribution) {
this.interSpawnTimeDistribution = interSpawnTimeDistribution;
return this;
}
public AttributesSourceBuilder distributionParameters(List<Double> distributionParameters) {
this.distributionParameters = distributionParameters;
return this;
}
public AttributesSourceBuilder spawnNumber(int spawnNumber) {
this.spawnNumber = spawnNumber;
return this;
}
public AttributesSourceBuilder maxSpawnNumberTotal(int maxSpawnNumberTotal) {
this.maxSpawnNumberTotal = maxSpawnNumberTotal;
return this;
}
public AttributesSourceBuilder startTime(double startTime) {
this.startTime = startTime;
return this;
}
public AttributesSourceBuilder endTime(double endTime) {
this.endTime = endTime;
return this;
}
public AttributesSourceBuilder spawnAtRandomPositions(boolean spawnAtRandomPositions) {
this.spawnAtRandomPositions = spawnAtRandomPositions;
return this;
}
public AttributesSourceBuilder useFreeSpaceOnly(boolean useFreeSpaceOnly) {
this.useFreeSpaceOnly = useFreeSpaceOnly;
return this;
}
public AttributesSourceBuilder targetIds(List<Integer> targetIds) {
this.targetIds = targetIds;
return this;
}
public AttributesSourceBuilder groupSizeDistribution(List<Double> groupSizeDistribution) {
this.groupSizeDistribution = groupSizeDistribution;
return this;
}
public AttributesSourceBuilder dynamicElementType(DynamicElementType dynamicElementType) {
this.dynamicElementType = dynamicElementType;
return this;
}
public AttributesSourceBuilder id(int id) {
this.id = id;
return this;
}
public AttributesSource build() {
AttributesSource attributesSource = new AttributesSource(id);
attributesSource.setShape(shape);
attributesSource.setInterSpawnTimeDistribution(interSpawnTimeDistribution);
attributesSource.setDistributionParameters(distributionParameters);
attributesSource.setSpawnNumber(spawnNumber);
attributesSource.setMaxSpawnNumberTotal(maxSpawnNumberTotal);
attributesSource.setStartTime(startTime);
attributesSource.setEndTime(endTime);
attributesSource.setSpawnAtRandomPositions(spawnAtRandomPositions);
attributesSource.setUseFreeSpaceOnly(useFreeSpaceOnly);
attributesSource.setTargetIds(targetIds);
attributesSource.setGroupSizeDistribution(groupSizeDistribution);
attributesSource.setDynamicElementType(dynamicElementType);
return attributesSource;
}
}
package org.vadere.state.attributes.scenario.builder;
import org.vadere.state.attributes.Attributes;
import org.vadere.state.attributes.scenario.AttributesTarget;
import org.vadere.util.geometry.shapes.VShape;
import javax.xml.stream.events.Attribute;
public final class AttributesTargetBuilder {
private int id = Attributes.ID_NOT_SET;
private boolean absorbing = true;
private VShape shape;
private AttributesTargetBuilder() {
}
public static AttributesTargetBuilder anAttributesTarget() {
return new AttributesTargetBuilder();
}
public AttributesTargetBuilder id(int id) {
this.id = id;
return this;
}
public AttributesTargetBuilder absorbing(boolean absorbing) {
this.absorbing = absorbing;
return this;
}
public AttributesTargetBuilder shape(VShape shape) {
this.shape = shape;
return this;
}
public AttributesTarget build() {
return new AttributesTarget(shape, id, absorbing);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment