[Minor] Activity.getElement() now throws exception if element does not exist

This commit is contained in:
Robert von Burg 2020-02-19 22:23:27 +01:00
parent 119408d782
commit c2ddc0c7ab
2 changed files with 28 additions and 30 deletions

View File

@ -209,11 +209,14 @@ public class Activity extends AbstractStrolchRootElement
* *
* @return IActivityElement * @return IActivityElement
*/ */
@SuppressWarnings("unchecked")
public <T extends IActivityElement> T getElement(String id) { public <T extends IActivityElement> T getElement(String id) {
if (this.elements == null) if (this.elements == null)
return null; throw new IllegalArgumentException("Element " + id + " does not exist on " + getLocator());
return (T) this.elements.get(id); @SuppressWarnings("unchecked")
T t = (T) this.elements.get(id);
if (t == null)
throw new IllegalArgumentException("Element " + id + " does not exist on " + getLocator());
return t;
} }
public Optional<IActivityElement> getPreviousElement(IActivityElement element) { public Optional<IActivityElement> getPreviousElement(IActivityElement element) {

View File

@ -15,13 +15,7 @@
*/ */
package li.strolch.model.activity; package li.strolch.model.activity;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import java.io.StringWriter;
import java.util.List;
import java.util.Optional;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
@ -31,17 +25,19 @@ import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;
import org.junit.Assert; import java.util.List;
import org.junit.Before; import java.util.Optional;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import li.strolch.exception.StrolchException; import li.strolch.exception.StrolchException;
import li.strolch.model.ModelGenerator; import li.strolch.model.ModelGenerator;
import li.strolch.model.State; import li.strolch.model.State;
import li.strolch.model.xml.StrolchElementToDomVisitor; import li.strolch.model.xml.StrolchElementToDomVisitor;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class ActivityTest { public class ActivityTest {
@ -85,23 +81,23 @@ public class ActivityTest {
this.activity.addElement(this.childActivity); this.activity.addElement(this.childActivity);
Assert.assertEquals(2, this.activity.getElements().size()); assertEquals(2, this.activity.getElements().size());
Assert.assertEquals(2, this.childActivity.getElements().size()); assertEquals(2, this.childActivity.getElements().size());
} }
@Test @Test
public void testStart() { public void testStart() {
Assert.assertEquals(this.action_1.getStart(), this.activity.getStart()); assertEquals(this.action_1.getStart(), this.activity.getStart());
} }
@Test @Test
public void testEnd() { public void testEnd() {
Assert.assertEquals(this.action_3.getEnd(), this.activity.getEnd()); assertEquals(this.action_3.getEnd(), this.activity.getEnd());
} }
@Test @Test
public void testState() { public void testState() {
Assert.assertEquals(State.PLANNING, this.activity.getState()); assertEquals(State.PLANNING, this.activity.getState());
} }
@Test(expected = StrolchException.class) @Test(expected = StrolchException.class)
@ -271,29 +267,28 @@ public class ActivityTest {
@Test @Test
public void getElementTest() { public void getElementTest() {
Assert.assertNull(this.activity.getElement("not contained")); assertEquals(this.action_1, this.activity.getElement(this.action_1.getId()));
Assert.assertEquals(this.action_1, this.activity.getElement(this.action_1.getId()));
} }
@Test @Test
public void cloneTest() { public void cloneTest() {
Activity clone = this.activity.getClone(); Activity clone = this.activity.getClone();
Assert.assertEquals(this.activity.toString(), clone.toString()); assertEquals(this.activity.toString(), clone.toString());
Assert.assertEquals(this.activity.getElements().size(), clone.getElements().size()); assertEquals(this.activity.getElements().size(), clone.getElements().size());
} }
@Test @Test
public void parentTests() { public void parentTests() {
Assert.assertNull(this.activity.getParent()); Assert.assertNull(this.activity.getParent());
Assert.assertEquals(this.activity, this.activity.getRootElement()); assertEquals(this.activity, this.activity.getRootElement());
Assert.assertTrue(this.activity.isRootElement()); Assert.assertTrue(this.activity.isRootElement());
Assert.assertEquals(this.activity, this.childActivity.getParent()); assertEquals(this.activity, this.childActivity.getParent());
Assert.assertEquals(this.activity, this.childActivity.getRootElement()); assertEquals(this.activity, this.childActivity.getRootElement());
Assert.assertFalse(this.childActivity.isRootElement()); Assert.assertFalse(this.childActivity.isRootElement());
Assert.assertEquals(this.childActivity, this.action_2.getParent()); assertEquals(this.childActivity, this.action_2.getParent());
Assert.assertEquals(this.activity, this.action_2.getRootElement()); assertEquals(this.activity, this.action_2.getRootElement());
Assert.assertFalse(this.action_2.isRootElement()); Assert.assertFalse(this.action_2.isRootElement());
} }