diff --git a/model-command/pom.xml b/model-command/pom.xml index df81c883631b7962fb23df500f944a35a3fd1328..a6d6b9166e5e5d8ba19f8ceef4098a0756356950 100644 --- a/model-command/pom.xml +++ b/model-command/pom.xml @@ -33,6 +33,14 @@ <version>${log4j.version}</version> </dependency> + <!-- mockito used for testing --> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <version>${mockito.version}</version> + <scope>test</scope> + </dependency> + <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> @@ -40,6 +48,7 @@ <scope>test</scope> </dependency> + </dependencies> </project> \ No newline at end of file diff --git a/model-command/src/main/java/lcsb/mapviewer/commands/MoveElementsCommand.java b/model-command/src/main/java/lcsb/mapviewer/commands/MoveElementsCommand.java index ab6002da40becb5143b83e4a654e27efbc8f82af..36777580da478ab196fb9570c05a7fef767a0cc5 100644 --- a/model-command/src/main/java/lcsb/mapviewer/commands/MoveElementsCommand.java +++ b/model-command/src/main/java/lcsb/mapviewer/commands/MoveElementsCommand.java @@ -2,12 +2,16 @@ package lcsb.mapviewer.commands; import java.awt.geom.Point2D; import java.util.ArrayList; +import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Set; +import org.apache.log4j.Logger; + import lcsb.mapviewer.common.exception.InvalidArgumentException; import lcsb.mapviewer.common.exception.InvalidStateException; +import lcsb.mapviewer.model.map.BioEntity; import lcsb.mapviewer.model.map.compartment.Compartment; import lcsb.mapviewer.model.map.model.Model; import lcsb.mapviewer.model.map.reaction.Modifier; @@ -18,8 +22,6 @@ import lcsb.mapviewer.model.map.reaction.Reaction; import lcsb.mapviewer.model.map.reaction.ReactionNode; import lcsb.mapviewer.model.map.species.Element; -import org.apache.log4j.Logger; - /** * Command which moves elements in model by dx, dy coordinates. * @@ -28,154 +30,154 @@ import org.apache.log4j.Logger; */ public class MoveElementsCommand extends ModelCommand { - /** - * Default class logger. - */ - private final Logger logger = Logger.getLogger(MoveElementsCommand.class); - - /** - * Delta x. - */ - private double dx; - - /** - * Delta y. - */ - private double dy; - - /** - * List of objects to move. - */ - private List<Object> objectsToMove = new ArrayList<>(); - - /** - * Default constructor. - * - * @param model - * model to move - * @param elements - * elements that should be moved - * @param dx - * delta x - * @param dy - * delat y - */ - public MoveElementsCommand(Model model, List<Object> elements, double dx, double dy) { - super(model); - this.dx = dx; - this.dy = dy; - for (Object object : elements) { - if (object instanceof Element) { - if (((Element) object).getModel() != model) { - throw new InvalidArgumentException("Object doesnt belong to specified model: " + object); - } - } else if (object instanceof Reaction) { - if (((Reaction) object).getModel() != model) { - throw new InvalidArgumentException("Object doesnt belong to specified model: " + object); - } - } else { - throw new InvalidArgumentException("Cannot move element: " + object); - } - } - objectsToMove = elements; - } - - @Override - protected void undoImplementation() { - dx = -dx; - dy = -dy; - executeImplementation(); - dx = -dx; - dy = -dy; - setStatus(ModelCommandStatus.UNDONE); - } - - @Override - protected void redoImplementation() { - executeImplementation(); - setStatus(ModelCommandStatus.EXECUTED); - } - - @Override - protected void executeImplementation() { - Set<Element> aliases = new HashSet<>(); - - for (Object object : objectsToMove) { - if (object instanceof Element) { - Element alias = (Element) object; - - includeInAffectedRegion(alias); - - alias.setX(alias.getX() + dx); - alias.setY(alias.getY() + dy); - if (alias instanceof Compartment) { - ((Compartment) alias) - .setNamePoint(((Compartment) alias).getNamePoint().getX() + dx, ((Compartment) alias).getNamePoint().getY() + dy); - } - - includeInAffectedRegion(alias); - - aliases.add(alias); - } else if (object instanceof Reaction) { - Reaction reaction = (Reaction) object; - for (Reactant node : reaction.getReactants()) { - for (int i = 1; i < node.getLine().getPoints().size(); i++) { - Point2D point = node.getLine().getPoints().get(i); - logger.debug("Moving point: " + point); - point.setLocation(point.getX() + dx, point.getY() + dy); - } - } - for (Product node : reaction.getProducts()) { - for (int i = 0; i < node.getLine().getPoints().size() - 1; i++) { - Point2D point = node.getLine().getPoints().get(i); - point.setLocation(point.getX() + dx, point.getY() + dy); - } - } - for (Modifier node : reaction.getModifiers()) { - for (int i = 1; i < node.getLine().getPoints().size(); i++) { - Point2D point = node.getLine().getPoints().get(i); - point.setLocation(point.getX() + dx, point.getY() + dy); - } - } - for (NodeOperator node : reaction.getOperators()) { - for (int i = 0; i < node.getLine().getPoints().size(); i++) { - Point2D point = node.getLine().getPoints().get(i); - point.setLocation(point.getX() + dx, point.getY() + dy); - } - } - includeInAffectedRegion(reaction); - } else { - throw new InvalidStateException("Unknown class type: " + object); - } - } - if (aliases.size() > 0) { - // TODO this must be improved, we cannot do full search on every move - for (Reaction reaction : getModel().getReactions()) { - for (ReactionNode node : reaction.getReactionNodes()) { - if (aliases.contains(node.getElement())) { - if (node instanceof Reactant) { - Point2D point = node.getLine().getBeginPoint(); - point.setLocation(point.getX() + dx, point.getY() + dy); - - // we don't have to include point that we change as it's already - // on the border of the element - includeInAffectedRegion(node.getLine().getPoints().get(1)); - } else if (node instanceof Modifier) { - Point2D point = node.getLine().getBeginPoint(); - point.setLocation(point.getX() + dx, point.getY() + dy); - // we don't have to include point that we change as it's already - // on the border of the element - includeInAffectedRegion(node.getLine().getPoints().get(1)); - } else if (node instanceof Product) { - Point2D point = node.getLine().getEndPoint(); - point.setLocation(point.getX() + dx, point.getY() + dy); - // we don't have to include point that we change as it's already - // on the border of the element - includeInAffectedRegion(node.getLine().getPoints().get(node.getLine().getPoints().size() - 1)); - } - } - } - } - } - } + /** + * Default class logger. + */ + private final Logger logger = Logger.getLogger(MoveElementsCommand.class); + + /** + * Delta x. + */ + private double dx; + + /** + * Delta y. + */ + private double dy; + + /** + * List of objects to move. + */ + private List<Object> objectsToMove = new ArrayList<>(); + + /** + * Default constructor. + * + * @param model + * model to move + * @param elements + * elements that should be moved + * @param dx + * delta x + * @param dy + * delta y + */ + public MoveElementsCommand(Model model, Collection<? extends BioEntity> elements, double dx, double dy) { + super(model); + this.dx = dx; + this.dy = dy; + for (Object object : elements) { + if (object instanceof Element) { + if (((Element) object).getModel() != model) { + throw new InvalidArgumentException("Object doesnt belong to specified model: " + object); + } + } else if (object instanceof Reaction) { + if (((Reaction) object).getModel() != model) { + throw new InvalidArgumentException("Object doesnt belong to specified model: " + object); + } + } else { + throw new InvalidArgumentException("Cannot move element: " + object); + } + } + objectsToMove.addAll(elements); + } + + @Override + protected void undoImplementation() { + dx = -dx; + dy = -dy; + executeImplementation(); + dx = -dx; + dy = -dy; + setStatus(ModelCommandStatus.UNDONE); + } + + @Override + protected void redoImplementation() { + executeImplementation(); + setStatus(ModelCommandStatus.EXECUTED); + } + + @Override + protected void executeImplementation() { + Set<Element> aliases = new HashSet<>(); + + for (Object object : objectsToMove) { + if (object instanceof Element) { + Element alias = (Element) object; + + includeInAffectedRegion(alias); + + alias.setX(alias.getX() + dx); + alias.setY(alias.getY() + dy); + if (alias instanceof Compartment) { + ((Compartment) alias) + .setNamePoint(((Compartment) alias).getNamePoint().getX() + dx, + ((Compartment) alias).getNamePoint().getY() + dy); + } + + includeInAffectedRegion(alias); + + aliases.add(alias); + } else if (object instanceof Reaction) { + Reaction reaction = (Reaction) object; + for (Reactant node : reaction.getReactants()) { + for (int i = 1; i < node.getLine().getPoints().size(); i++) { + Point2D point = node.getLine().getPoints().get(i); + point.setLocation(point.getX() + dx, point.getY() + dy); + } + } + for (Product node : reaction.getProducts()) { + for (int i = 0; i < node.getLine().getPoints().size() - 1; i++) { + Point2D point = node.getLine().getPoints().get(i); + point.setLocation(point.getX() + dx, point.getY() + dy); + } + } + for (Modifier node : reaction.getModifiers()) { + for (int i = 1; i < node.getLine().getPoints().size(); i++) { + Point2D point = node.getLine().getPoints().get(i); + point.setLocation(point.getX() + dx, point.getY() + dy); + } + } + for (NodeOperator node : reaction.getOperators()) { + for (int i = 0; i < node.getLine().getPoints().size(); i++) { + Point2D point = node.getLine().getPoints().get(i); + point.setLocation(point.getX() + dx, point.getY() + dy); + } + } + includeInAffectedRegion(reaction); + } else { + throw new InvalidStateException("Unknown class type: " + object); + } + } + if (aliases.size() > 0) { + // TODO this must be improved, we cannot do full search on every move + for (Reaction reaction : getModel().getReactions()) { + for (ReactionNode node : reaction.getReactionNodes()) { + if (aliases.contains(node.getElement())) { + if (node instanceof Reactant) { + Point2D point = node.getLine().getBeginPoint(); + point.setLocation(point.getX() + dx, point.getY() + dy); + + // we don't have to include point that we change as it's already + // on the border of the element + includeInAffectedRegion(node.getLine().getPoints().get(1)); + } else if (node instanceof Modifier) { + Point2D point = node.getLine().getBeginPoint(); + point.setLocation(point.getX() + dx, point.getY() + dy); + // we don't have to include point that we change as it's already + // on the border of the element + includeInAffectedRegion(node.getLine().getPoints().get(1)); + } else if (node instanceof Product) { + Point2D point = node.getLine().getEndPoint(); + point.setLocation(point.getX() + dx, point.getY() + dy); + // we don't have to include point that we change as it's already + // on the border of the element + includeInAffectedRegion(node.getLine().getPoints().get(node.getLine().getPoints().size() - 1)); + } + } + } + } + } + } } diff --git a/model-command/src/test/java/lcsb/mapviewer/commands/MoveElementsCommandTest.java b/model-command/src/test/java/lcsb/mapviewer/commands/MoveElementsCommandTest.java index 17bb61852042416a7a9606c5d082de6ac04f4b9b..efc14fabd446a75566b134306fdf54b86caedeb7 100644 --- a/model-command/src/test/java/lcsb/mapviewer/commands/MoveElementsCommandTest.java +++ b/model-command/src/test/java/lcsb/mapviewer/commands/MoveElementsCommandTest.java @@ -13,9 +13,11 @@ import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.mockito.Mockito; import lcsb.mapviewer.common.Configuration; import lcsb.mapviewer.common.exception.InvalidArgumentException; +import lcsb.mapviewer.model.map.BioEntity; import lcsb.mapviewer.model.map.model.Model; import lcsb.mapviewer.model.map.model.ModelComparator; import lcsb.mapviewer.model.map.reaction.Reaction; @@ -23,255 +25,255 @@ import lcsb.mapviewer.model.map.species.Element; public class MoveElementsCommandTest extends CommandTestFunctions { - ModelComparator modelComparator = new ModelComparator(); - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testMoveAlias() throws Exception { - try { - Model model = getModelForFile("testFiles/spliting_test_Case.xml", false); - Model model2 = getModelForFile("testFiles/spliting_test_Case.xml", false); - Element alias = model.getElementByElementId("sa2"); - Element alias2 = model.getElementByElementId("sa1"); - double anotherAliasX = alias2.getX(); - double anotherAliasY = alias2.getY(); - - List<Object> list = new ArrayList<>(); - list.add(alias); - double x = alias.getX(); - double y = alias.getY(); - - double dx = 10; - double dy = 2; - - // models should be equal before move - assertEquals(0, modelComparator.compare(model, model2)); - - MoveElementsCommand moveCommand = new MoveElementsCommand(model, list, dx, dy); - moveCommand.execute(); - - // after move models should be different - assertTrue(0 != modelComparator.compare(model, model2)); - - // check new coordinates - assertEquals(x + dx, alias.getX(), Configuration.EPSILON); - assertEquals(y + dy, alias.getY(), Configuration.EPSILON); - - // and check if another alias didn't change coordinates - assertEquals(anotherAliasX, alias2.getX(), Configuration.EPSILON); - assertEquals(anotherAliasY, alias2.getY(), Configuration.EPSILON); - - list = new ArrayList<>(); - list.add(model2.getElementByElementId("sa2")); - MoveElementsCommand moveCommand2 = new MoveElementsCommand(model2, list, dx, dy); - moveCommand2.execute(); - - // if we perfomr the same operator on the second model then they should be - // equal - assertEquals(0, modelComparator.compare(model, model2)); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testMoveSpecies() throws Exception { - try { - Model model = getModelForFile("testFiles/spliting_test_Case.xml", false); - List<Object> list = new ArrayList<>(); - list.add(new Object()); - - MoveElementsCommand moveCommand = new MoveElementsCommand(model, list, 10, 10); - moveCommand.execute(); - fail("Exception expected"); - - } catch (InvalidArgumentException e) { - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testAliasWithReaction() throws Exception { - try { - Model model = getModelForFile("testFiles/spliting_test_Case.xml", false); - Model model2 = getModelForFile("testFiles/spliting_test_Case.xml", false); - Element alias = model.getElementByElementId("sa1"); - Element alias2 = model.getElementByElementId("sa2"); - double anotherAliasX = alias2.getX(); - double anotherAliasY = alias2.getY(); - Reaction reaction = model.getReactionByReactionId("re1"); - - List<Object> list = new ArrayList<>(); - list.add(alias); - double x = reaction.getReactants().get(0).getLine().getBeginPoint().getX(); - double y = reaction.getReactants().get(0).getLine().getBeginPoint().getY(); - - double dx = 10; - double dy = 2; - - // models should be equal before move - assertEquals(0, modelComparator.compare(model, model2)); - - MoveElementsCommand moveCommand = new MoveElementsCommand(model, list, dx, dy); - moveCommand.execute(); - - // after move models should be different - assertTrue(0 != modelComparator.compare(model, model2)); - - // check new coordinates of reaction - assertEquals(x + dx, reaction.getReactants().get(0).getLine().getBeginPoint().getX(), Configuration.EPSILON); - assertEquals(y + dy, reaction.getReactants().get(0).getLine().getBeginPoint().getY(), Configuration.EPSILON); - - // and check if another alias didn't change coordinates - assertEquals(anotherAliasX, alias2.getX(), Configuration.EPSILON); - assertEquals(anotherAliasY, alias2.getY(), Configuration.EPSILON); - - list = new ArrayList<>(); - list.add(model2.getElementByElementId("sa1")); - MoveElementsCommand moveCommand2 = new MoveElementsCommand(model2, list, dx, dy); - moveCommand2.execute(); - - // if we perfomr the same operator on the second model then they should be - // equal - assertEquals(0, modelComparator.compare(model, model2)); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testMoveReaction() throws Exception { - try { - Model model = getModelForFile("testFiles/spliting_test_Case.xml", false); - Element alias2 = model.getElementByElementId("sa1"); - double anotherAliasX = alias2.getX(); - double anotherAliasY = alias2.getY(); - Reaction reaction = model.getReactionByReactionId("re1"); - - List<Object> list = new ArrayList<>(); - list.add(reaction); - double x = reaction.getReactants().get(0).getLine().getBeginPoint().getX(); - double y = reaction.getReactants().get(0).getLine().getBeginPoint().getY(); - - double x2 = reaction.getReactants().get(0).getLine().getEndPoint().getX(); - double y2 = reaction.getReactants().get(0).getLine().getEndPoint().getY(); - - double dx = 10; - double dy = 2; - - MoveElementsCommand moveCommand = new MoveElementsCommand(model, list, dx, dy); - moveCommand.execute(); - - // check new coordinates of reaction (point attached to alias shouldn't - // move, the one not attached should move) - assertEquals(x, reaction.getReactants().get(0).getLine().getBeginPoint().getX(), Configuration.EPSILON); - assertEquals(y, reaction.getReactants().get(0).getLine().getBeginPoint().getY(), Configuration.EPSILON); - assertEquals(x2 + dx, reaction.getReactants().get(0).getLine().getEndPoint().getX(), Configuration.EPSILON); - assertEquals(y2 + dy, reaction.getReactants().get(0).getLine().getEndPoint().getY(), Configuration.EPSILON); - - // and check if another alias didn't change coordinates - assertEquals(anotherAliasX, alias2.getX(), Configuration.EPSILON); - assertEquals(anotherAliasY, alias2.getY(), Configuration.EPSILON); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testUndo() throws Exception { - try { - Model model = getModelForFile("testFiles/spliting_test_Case.xml", false); - Model model2 = getModelForFile("testFiles/spliting_test_Case.xml", false); - Element alias = model.getElementByElementId("sa2"); - - List<Object> list = new ArrayList<>(); - list.add(alias); - - double dx = 10; - double dy = 2; - - // models should be equal before move - assertEquals(0, modelComparator.compare(model, model2)); - - MoveElementsCommand moveCommand = new MoveElementsCommand(model, list, dx, dy); - moveCommand.execute(); - - // after move models should be different - assertTrue(0 != modelComparator.compare(model, model2)); - - // undo command - moveCommand.undo(); - - // after undo they should be the same again - assertEquals(0, modelComparator.compare(model, model2)); - - moveCommand.redo(); - - // after redo they should be different again - assertTrue(0 != modelComparator.compare(model, model2)); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testGetAffectedRegion() throws Exception { - try { - Model model = getModelForFile("testFiles/spliting_test_Case.xml", false); - Element alias = model.getElementByElementId("sa2"); - - List<Object> list = new ArrayList<>(); - list.add(alias); - double x = alias.getX(); - double y = alias.getY(); - - double dx = 10; - double dy = 2; - - double minx = x; - double maxx = alias.getWidth() + x + dx; - - double miny = y; - double maxy = alias.getHeight() + y + dy; - - MoveElementsCommand moveCommand = new MoveElementsCommand(model, list, dx, dy); - assertNull(moveCommand.getAffectedRegion()); - moveCommand.execute(); - assertNotNull(moveCommand.getAffectedRegion()); - Rectangle2D affectedRegion = moveCommand.getAffectedRegion(); - assertEquals(minx, affectedRegion.getX(), Configuration.EPSILON); - assertEquals(miny, affectedRegion.getY(), Configuration.EPSILON); - assertEquals(maxx, affectedRegion.getX() + affectedRegion.getWidth(), Configuration.EPSILON); - assertEquals(maxy, affectedRegion.getY() + affectedRegion.getHeight(), Configuration.EPSILON); - - moveCommand.undo(); - - affectedRegion = moveCommand.getAffectedRegion(); - assertEquals(minx, affectedRegion.getX(), Configuration.EPSILON); - assertEquals(miny, affectedRegion.getY(), Configuration.EPSILON); - assertEquals(maxx, affectedRegion.getX() + affectedRegion.getWidth(), Configuration.EPSILON); - assertEquals(maxy, affectedRegion.getY() + affectedRegion.getHeight(), Configuration.EPSILON); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } + ModelComparator modelComparator = new ModelComparator(); + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testMoveAlias() throws Exception { + try { + Model model = getModelForFile("testFiles/spliting_test_Case.xml", false); + Model model2 = getModelForFile("testFiles/spliting_test_Case.xml", false); + Element alias = model.getElementByElementId("sa2"); + Element alias2 = model.getElementByElementId("sa1"); + double anotherAliasX = alias2.getX(); + double anotherAliasY = alias2.getY(); + + List<BioEntity> list = new ArrayList<>(); + list.add(alias); + double x = alias.getX(); + double y = alias.getY(); + + double dx = 10; + double dy = 2; + + // models should be equal before move + assertEquals(0, modelComparator.compare(model, model2)); + + MoveElementsCommand moveCommand = new MoveElementsCommand(model, list, dx, dy); + moveCommand.execute(); + + // after move models should be different + assertTrue(0 != modelComparator.compare(model, model2)); + + // check new coordinates + assertEquals(x + dx, alias.getX(), Configuration.EPSILON); + assertEquals(y + dy, alias.getY(), Configuration.EPSILON); + + // and check if another alias didn't change coordinates + assertEquals(anotherAliasX, alias2.getX(), Configuration.EPSILON); + assertEquals(anotherAliasY, alias2.getY(), Configuration.EPSILON); + + list = new ArrayList<>(); + list.add(model2.getElementByElementId("sa2")); + MoveElementsCommand moveCommand2 = new MoveElementsCommand(model2, list, dx, dy); + moveCommand2.execute(); + + // if we perfomr the same operator on the second model then they should be + // equal + assertEquals(0, modelComparator.compare(model, model2)); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testMoveSpecies() throws Exception { + try { + Model model = getModelForFile("testFiles/spliting_test_Case.xml", false); + List<BioEntity> list = new ArrayList<>(); + list.add(Mockito.mock(BioEntity.class)); + + MoveElementsCommand moveCommand = new MoveElementsCommand(model, list, 10, 10); + moveCommand.execute(); + fail("Exception expected"); + + } catch (InvalidArgumentException e) { + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testAliasWithReaction() throws Exception { + try { + Model model = getModelForFile("testFiles/spliting_test_Case.xml", false); + Model model2 = getModelForFile("testFiles/spliting_test_Case.xml", false); + Element alias = model.getElementByElementId("sa1"); + Element alias2 = model.getElementByElementId("sa2"); + double anotherAliasX = alias2.getX(); + double anotherAliasY = alias2.getY(); + Reaction reaction = model.getReactionByReactionId("re1"); + + List<BioEntity> list = new ArrayList<>(); + list.add(alias); + double x = reaction.getReactants().get(0).getLine().getBeginPoint().getX(); + double y = reaction.getReactants().get(0).getLine().getBeginPoint().getY(); + + double dx = 10; + double dy = 2; + + // models should be equal before move + assertEquals(0, modelComparator.compare(model, model2)); + + MoveElementsCommand moveCommand = new MoveElementsCommand(model, list, dx, dy); + moveCommand.execute(); + + // after move models should be different + assertTrue(0 != modelComparator.compare(model, model2)); + + // check new coordinates of reaction + assertEquals(x + dx, reaction.getReactants().get(0).getLine().getBeginPoint().getX(), Configuration.EPSILON); + assertEquals(y + dy, reaction.getReactants().get(0).getLine().getBeginPoint().getY(), Configuration.EPSILON); + + // and check if another alias didn't change coordinates + assertEquals(anotherAliasX, alias2.getX(), Configuration.EPSILON); + assertEquals(anotherAliasY, alias2.getY(), Configuration.EPSILON); + + list = new ArrayList<>(); + list.add(model2.getElementByElementId("sa1")); + MoveElementsCommand moveCommand2 = new MoveElementsCommand(model2, list, dx, dy); + moveCommand2.execute(); + + // if we perfomr the same operator on the second model then they should be + // equal + assertEquals(0, modelComparator.compare(model, model2)); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testMoveReaction() throws Exception { + try { + Model model = getModelForFile("testFiles/spliting_test_Case.xml", false); + Element alias2 = model.getElementByElementId("sa1"); + double anotherAliasX = alias2.getX(); + double anotherAliasY = alias2.getY(); + Reaction reaction = model.getReactionByReactionId("re1"); + + List<BioEntity> list = new ArrayList<>(); + list.add(reaction); + double x = reaction.getReactants().get(0).getLine().getBeginPoint().getX(); + double y = reaction.getReactants().get(0).getLine().getBeginPoint().getY(); + + double x2 = reaction.getReactants().get(0).getLine().getEndPoint().getX(); + double y2 = reaction.getReactants().get(0).getLine().getEndPoint().getY(); + + double dx = 10; + double dy = 2; + + MoveElementsCommand moveCommand = new MoveElementsCommand(model, list, dx, dy); + moveCommand.execute(); + + // check new coordinates of reaction (point attached to alias shouldn't + // move, the one not attached should move) + assertEquals(x, reaction.getReactants().get(0).getLine().getBeginPoint().getX(), Configuration.EPSILON); + assertEquals(y, reaction.getReactants().get(0).getLine().getBeginPoint().getY(), Configuration.EPSILON); + assertEquals(x2 + dx, reaction.getReactants().get(0).getLine().getEndPoint().getX(), Configuration.EPSILON); + assertEquals(y2 + dy, reaction.getReactants().get(0).getLine().getEndPoint().getY(), Configuration.EPSILON); + + // and check if another alias didn't change coordinates + assertEquals(anotherAliasX, alias2.getX(), Configuration.EPSILON); + assertEquals(anotherAliasY, alias2.getY(), Configuration.EPSILON); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testUndo() throws Exception { + try { + Model model = getModelForFile("testFiles/spliting_test_Case.xml", false); + Model model2 = getModelForFile("testFiles/spliting_test_Case.xml", false); + Element alias = model.getElementByElementId("sa2"); + + List<BioEntity> list = new ArrayList<>(); + list.add(alias); + + double dx = 10; + double dy = 2; + + // models should be equal before move + assertEquals(0, modelComparator.compare(model, model2)); + + MoveElementsCommand moveCommand = new MoveElementsCommand(model, list, dx, dy); + moveCommand.execute(); + + // after move models should be different + assertTrue(0 != modelComparator.compare(model, model2)); + + // undo command + moveCommand.undo(); + + // after undo they should be the same again + assertEquals(0, modelComparator.compare(model, model2)); + + moveCommand.redo(); + + // after redo they should be different again + assertTrue(0 != modelComparator.compare(model, model2)); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testGetAffectedRegion() throws Exception { + try { + Model model = getModelForFile("testFiles/spliting_test_Case.xml", false); + Element alias = model.getElementByElementId("sa2"); + + List<BioEntity> list = new ArrayList<>(); + list.add(alias); + double x = alias.getX(); + double y = alias.getY(); + + double dx = 10; + double dy = 2; + + double minx = x; + double maxx = alias.getWidth() + x + dx; + + double miny = y; + double maxy = alias.getHeight() + y + dy; + + MoveElementsCommand moveCommand = new MoveElementsCommand(model, list, dx, dy); + assertNull(moveCommand.getAffectedRegion()); + moveCommand.execute(); + assertNotNull(moveCommand.getAffectedRegion()); + Rectangle2D affectedRegion = moveCommand.getAffectedRegion(); + assertEquals(minx, affectedRegion.getX(), Configuration.EPSILON); + assertEquals(miny, affectedRegion.getY(), Configuration.EPSILON); + assertEquals(maxx, affectedRegion.getX() + affectedRegion.getWidth(), Configuration.EPSILON); + assertEquals(maxy, affectedRegion.getY() + affectedRegion.getHeight(), Configuration.EPSILON); + + moveCommand.undo(); + + affectedRegion = moveCommand.getAffectedRegion(); + assertEquals(minx, affectedRegion.getX(), Configuration.EPSILON); + assertEquals(miny, affectedRegion.getY(), Configuration.EPSILON); + assertEquals(maxx, affectedRegion.getX() + affectedRegion.getWidth(), Configuration.EPSILON); + assertEquals(maxy, affectedRegion.getY() + affectedRegion.getHeight(), Configuration.EPSILON); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } }