From 2b51ec2c0eb62c995e79256f7336787701fb5aa6 Mon Sep 17 00:00:00 2001
From: Nils Christian Ehmke <nie@informatik.uni-kiel.de>
Date: Fri, 30 Jan 2015 10:32:36 +0100
Subject: [PATCH] Checkstyle issues; Added various tests

---
 config/checkstyle/checkstyle.xml              |  1 -
 .../gui/common/domain/AbstractExecution.java  | 19 ++--
 .../kieker/gui/common/domain/Execution.java   | 35 ++++---
 .../common/domain/AbstractExecutionTest.java  | 22 +++--
 .../gui/common/domain/ExecutionTest.java      | 93 +++++++++++++++++++
 5 files changed, 138 insertions(+), 32 deletions(-)

diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml
index 3bfef2b1..c7ceec8d 100644
--- a/config/checkstyle/checkstyle.xml
+++ b/config/checkstyle/checkstyle.xml
@@ -49,7 +49,6 @@
     <module name="IllegalThrows"/>
     <module name="PackageDeclaration"/>
     <module name="ReturnCount"/>
-    <module name="IllegalType"/>
     <module name="DeclarationOrder"/>
     <module name="ParameterAssignment"/>
     <module name="ExplicitInitialization"/>
diff --git a/src/main/java/kieker/gui/common/domain/AbstractExecution.java b/src/main/java/kieker/gui/common/domain/AbstractExecution.java
index e979d696..a07b5b06 100644
--- a/src/main/java/kieker/gui/common/domain/AbstractExecution.java
+++ b/src/main/java/kieker/gui/common/domain/AbstractExecution.java
@@ -21,13 +21,13 @@ import java.util.List;
 
 public abstract class AbstractExecution<T extends AbstractExecution<T>> {
 
-	protected final String container;
-	protected final String component;
-	protected final String operation;
+	private final String container;
+	private final String component;
+	private final String operation;
 
-	protected String failedCause;
-	protected T parent;
-	protected final List<T> children = new ArrayList<>();
+	private String failedCause;
+	private T parent;
+	private final List<T> children = new ArrayList<>();
 
 	public AbstractExecution(final String container, final String component, final String operation) {
 		this.container = container;
@@ -98,13 +98,18 @@ public abstract class AbstractExecution<T extends AbstractExecution<T>> {
 		return this.children;
 	}
 
+	@SuppressWarnings("unchecked")
 	public final void addExecutionEntry(final T entry) {
 		this.children.add(entry);
-		entry.parent = (T) this;
+		entry.setParent((T) this);
 	}
 
 	public final T getParent() {
 		return this.parent;
 	}
 
+	public void setParent(final T parent) {
+		this.parent = parent;
+	}
+
 }
diff --git a/src/main/java/kieker/gui/common/domain/Execution.java b/src/main/java/kieker/gui/common/domain/Execution.java
index 53a91f10..7f1a65fb 100644
--- a/src/main/java/kieker/gui/common/domain/Execution.java
+++ b/src/main/java/kieker/gui/common/domain/Execution.java
@@ -56,12 +56,12 @@ public final class Execution extends AbstractExecution<Execution> {
 	}
 
 	private void updatePercent() {
-		if (this.parent != null) {
-			this.percent = (this.duration * 100.0f) / this.parent.duration;
+		if (this.getParent() != null) {
+			this.percent = (this.duration * 100.0f) / this.getParent().duration;
 		} else {
 			this.percent = 100.0f;
 		}
-		for (final Execution executionEntry : this.children) {
+		for (final Execution executionEntry : this.getChildren()) {
 			executionEntry.updatePercent();
 		}
 	}
@@ -70,11 +70,11 @@ public final class Execution extends AbstractExecution<Execution> {
 	public int hashCode() {
 		final int prime = 31;
 		int result = 1;
-		result = (prime * result) + ((this.children == null) ? 0 : this.children.hashCode());
-		result = (prime * result) + ((this.component == null) ? 0 : this.component.hashCode());
-		result = (prime * result) + ((this.container == null) ? 0 : this.container.hashCode());
-		result = (prime * result) + ((this.failedCause == null) ? 0 : this.failedCause.hashCode());
-		result = (prime * result) + ((this.operation == null) ? 0 : this.operation.hashCode());
+		result = (prime * result) + ((this.getChildren() == null) ? 0 : this.getChildren().hashCode());
+		result = (prime * result) + ((this.getComponent() == null) ? 0 : this.getComponent().hashCode());
+		result = (prime * result) + ((this.getContainer() == null) ? 0 : this.getContainer().hashCode());
+		result = (prime * result) + ((this.getFailedCause() == null) ? 0 : this.getFailedCause().hashCode());
+		result = (prime * result) + ((this.getOperation() == null) ? 0 : this.getOperation().hashCode());
 		return result;
 	}
 
@@ -87,30 +87,30 @@ public final class Execution extends AbstractExecution<Execution> {
 			return false;
 		}
 		final Execution otherEntry = (Execution) other;
-		if (!this.container.equals(otherEntry.container)) {
+		if (!this.getContainer().equals(otherEntry.getContainer())) {
 			return false;
 		}
-		if (!this.component.equals(otherEntry.component)) {
+		if (!this.getComponent().equals(otherEntry.getComponent())) {
 			return false;
 		}
-		if (!this.operation.equals(otherEntry.operation)) {
+		if (!this.getOperation().equals(otherEntry.getOperation())) {
 			return false;
 		}
-		if (this.failedCause == null) {
-			if (otherEntry.failedCause != null) {
+		if (this.getFailedCause() == null) {
+			if (otherEntry.getFailedCause() != null) {
 				return false;
 			}
 		} else {
-			if (!this.failedCause.equals(otherEntry.failedCause)) {
+			if (!this.getFailedCause().equals(otherEntry.getFailedCause())) {
 				return false;
 			}
 		}
-		if (this.children.size() != otherEntry.children.size()) {
+		if (this.getChildren().size() != otherEntry.getChildren().size()) {
 			return false;
 		}
 
-		final Iterator<Execution> ownChildrenIterator = this.children.iterator();
-		final Iterator<Execution> otherChildrenIterator = otherEntry.children.iterator();
+		final Iterator<Execution> ownChildrenIterator = this.getChildren().iterator();
+		final Iterator<Execution> otherChildrenIterator = otherEntry.getChildren().iterator();
 
 		while (ownChildrenIterator.hasNext()) {
 			final Execution ownChild = ownChildrenIterator.next();
@@ -123,5 +123,4 @@ public final class Execution extends AbstractExecution<Execution> {
 
 		return true;
 	}
-
 }
diff --git a/src/test/java/kieker/gui/common/domain/AbstractExecutionTest.java b/src/test/java/kieker/gui/common/domain/AbstractExecutionTest.java
index 6b878661..32eaffbe 100644
--- a/src/test/java/kieker/gui/common/domain/AbstractExecutionTest.java
+++ b/src/test/java/kieker/gui/common/domain/AbstractExecutionTest.java
@@ -9,44 +9,54 @@ public abstract class AbstractExecutionTest<T extends AbstractExecution<T>> {
 
 	@Test
 	public void traceDepthCalculationInCommonCaseShouldWork() {
-		final AbstractExecution<T> execution = this.createEmptyExecution();
+		final T execution = this.createEmptyExecution();
 
 		execution.addExecutionEntry(this.createEmptyExecution());
 		execution.addExecutionEntry(this.createEmptyExecution());
 		execution.addExecutionEntry(this.createEmptyExecution());
 
-		execution.children.get(0).addExecutionEntry(this.createEmptyExecution());
+		execution.getChildren().get(0).addExecutionEntry(this.createEmptyExecution());
 
 		assertThat(execution.getTraceDepth(), is(2));
 	}
 
 	@Test
 	public void traceDepthCalculationForNoChildrenShouldWork() {
-		final AbstractExecution<T> execution = this.createEmptyExecution();
+		final T execution = this.createEmptyExecution();
 
 		assertThat(execution.getTraceDepth(), is(0));
 	}
 
 	@Test
 	public void traceSizeCalculationInCommonCaseShouldWork() {
-		final AbstractExecution<T> execution = this.createEmptyExecution();
+		final T execution = this.createEmptyExecution();
 
 		execution.addExecutionEntry(this.createEmptyExecution());
 		execution.addExecutionEntry(this.createEmptyExecution());
 		execution.addExecutionEntry(this.createEmptyExecution());
 
-		execution.children.get(0).addExecutionEntry(this.createEmptyExecution());
+		execution.getChildren().get(0).addExecutionEntry(this.createEmptyExecution());
 
 		assertThat(execution.getTraceSize(), is(5));
 	}
 
 	@Test
 	public void traceSizeCalculationForNoChildrenShouldWork() {
-		final AbstractExecution<T> execution = this.createEmptyExecution();
+		final T execution = this.createEmptyExecution();
 
 		assertThat(execution.getTraceSize(), is(1));
 	}
 
+	@Test
+	public void addingChildrenShouldUpdateTheParent() {
+		final T execution = this.createEmptyExecution();
+		final T child = this.createEmptyExecution();
+
+		execution.addExecutionEntry(child);
+
+		assertThat(child.getParent(), is(execution));
+	}
+
 	protected abstract T createEmptyExecution();
 
 }
diff --git a/src/test/java/kieker/gui/common/domain/ExecutionTest.java b/src/test/java/kieker/gui/common/domain/ExecutionTest.java
index dae8de9b..695ccab2 100644
--- a/src/test/java/kieker/gui/common/domain/ExecutionTest.java
+++ b/src/test/java/kieker/gui/common/domain/ExecutionTest.java
@@ -1,5 +1,13 @@
 package kieker.gui.common.domain;
 
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.hamcrest.core.IsNot.not;
+import static org.hamcrest.number.IsCloseTo.closeTo;
+import static org.junit.Assert.assertThat;
+
+import org.junit.Test;
+
 public final class ExecutionTest extends AbstractExecutionTest<Execution> {
 
 	@Override
@@ -7,4 +15,89 @@ public final class ExecutionTest extends AbstractExecutionTest<Execution> {
 		return new Execution(0, "", "", "");
 	}
 
+	@Test
+	public void equalsWithNullShouldNotLeadToException() {
+		final Execution fstExecution = new Execution(0, "", "", "");
+		final Execution sndExecution = null;
+
+		assertThat(fstExecution, is(not(equalTo(sndExecution))));
+	}
+
+	@Test
+	public void equalsForSameInstanceShouldWork() {
+		final Execution fstExecution = new Execution(0, "", "", "");
+		final Execution sndExecution = fstExecution;
+
+		assertThat(fstExecution, is(equalTo(sndExecution)));
+	}
+
+	@Test
+	public void equalsForSameValuesShouldWork() {
+		final Execution fstExecution = new Execution(42, "container", "component", "operation");
+		final Execution sndExecution = new Execution(42, "container", "component", "operation");
+
+		assertThat(fstExecution, is(equalTo(sndExecution)));
+	}
+
+	@Test
+	public void equalsForDifferentTraceIDsShouldWork() {
+		final Execution fstExecution = new Execution(42, "container", "component", "operation");
+		final Execution sndExecution = new Execution(43, "container", "component", "operation");
+
+		assertThat(fstExecution, is(equalTo(sndExecution)));
+	}
+
+	@Test
+	public void equalsForDifferentContainerShouldReturnFalse() {
+		final Execution fstExecution = new Execution(42, "container1", "component", "operation");
+		final Execution sndExecution = new Execution(42, "container2", "component", "operation");
+
+		assertThat(fstExecution, is(not(equalTo(sndExecution))));
+	}
+
+	@Test
+	public void equalsForDifferentComponentsShouldReturnFalse() {
+		final Execution fstExecution = new Execution(42, "container", "component1", "operation");
+		final Execution sndExecution = new Execution(42, "container", "component2", "operation");
+
+		assertThat(fstExecution, is(not(equalTo(sndExecution))));
+	}
+
+	@Test
+	public void equalsForDifferentOperationsShouldReturnFalse() {
+		final Execution fstExecution = new Execution(42, "container", "component", "operation1");
+		final Execution sndExecution = new Execution(42, "container", "component", "operation2");
+
+		assertThat(fstExecution, is(not(equalTo(sndExecution))));
+	}
+
+	@Test
+	public void percentCalculationShouldWork() {
+		final Execution execution = new Execution(42, "", "", "");
+		final Execution child1 = new Execution(42, "", "", "");
+		final Execution child2 = new Execution(42, "", "", "");
+		final Execution child3 = new Execution(42, "", "", "");
+		final Execution child4 = new Execution(42, "", "", "");
+
+		execution.setDuration(100);
+		child1.setDuration(70);
+		child2.setDuration(15);
+		child3.setDuration(36);
+		child4.setDuration(18);
+
+		execution.addExecutionEntry(child1);
+		execution.addExecutionEntry(child2);
+		execution.addExecutionEntry(child3);
+
+		child3.addExecutionEntry(child4);
+
+		execution.recalculateValues();
+
+		assertThat((double) execution.getPercent(), is(closeTo(100.0, 1e-3)));
+		assertThat((double) child1.getPercent(), is(closeTo(70.0, 1e-3)));
+		assertThat((double) child2.getPercent(), is(closeTo(15.0, 1e-3)));
+		assertThat((double) child3.getPercent(), is(closeTo(36.0, 1e-3)));
+		assertThat((double) child4.getPercent(), is(closeTo(50.0, 1e-3)));
+	}
+
 }
-- 
GitLab