Package greenfoot.junitUtils
Class WorldCreator
- java.lang.Object
-
- greenfoot.junitUtils.WorldCreator
-
public class WorldCreator extends java.lang.Object
It cleans queues used for testing. If we wants to verify the behaviour of any scenario, a world is needed and greenfoot objects (world and actors) must execute theirs acts. When a same test need many worlds or many act cycles, these worlds can be created by getWorld(...) methods and acts can be executed by runOnce(...) methods with empty queues. Also, some events (start, stop, delay, ...) of a tested scenario are simulated using others static methods.- Author:
- Francisco Guerra (francisco.guerra@ulpgc.es)
-
-
Constructor Summary
Constructors Constructor Description WorldCreator()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
getDelay()
It returns the delay was set by any object.static boolean
getDelay(int delay)
It verifies any object has set a delay.static int
getSpeed()
It return the speed of the animation that was set by any object.static boolean
getSpeed(int speed)
It verifies that any object has set this speed of the animation.static greenfoot.World
getWorld(int worldWidth, int worldHeight, int cellSize)
It creates and activates a generic world for testing.static <worldType>
worldTypegetWorld(java.lang.Class<? extends greenfoot.World> worldType, java.lang.Object... parameters)
It creates and activates a user world for testing.static boolean
isStart()
It verifies if any object has start the animation calling to Greenfoot.Start().static boolean
isStop()
It verifies if object has paused the animation calling to Greenfoot.Stop().static void
runOnce()
It executes one acts cycle (the act of world and the acts of actors added to current world of the scenario).static void
runOnce(greenfoot.Actor... actors)
It executes one act of the referred actors, that is, of the actors of the list that is passed as a parameter only when theirs world is the current world of the scenario.static void
runOnce(greenfoot.World world)
It executes one acts cycle (the act of world and the acts of actors added to world).static void
runOnce(greenfoot.World world, greenfoot.Actor... actors)
It executes an act cycle of the world and referenced actors that have been added to this world.static void
setMicLevel(int level)
It queues a microphone level in FIFO order for any next Greenfoot.getMicLevel() operation.
For the next actor behaviour:
-
-
-
Method Detail
-
getWorld
public static greenfoot.World getWorld(int worldWidth, int worldHeight, int cellSize) throws GreenfootRunnerExpected
It creates and activates a generic world for testing. It should be used instead of "new World ()" when the world of the stage has not yet been programmed, or when an empty world is needed (without actors) or when a world needs to be created and the data used in its creation should not affect the rest of the test. When more than one world is created in the same test, the first created world will be the world associated with the scenario, and otherWorld can be activated by calling the Greenfoot.setWorld(otherWorld). For the next actor behaviour:public class ExampleActorGetWorld extends Actor { @Override public void act(){ move(1); } }
import ... import greenfoot.TestRunnerState.WorldCreator; @RunWith(GreenfootRunner.class) public class GetWorldTest { World world1; World world2; ExampleActorGetWorld actor1; ExampleActorGetWorld actor2; @Before public void setUp() throws Exception { // GIVEN world1 = WorldCreator.getWorld(15, 15, 60); world2 = WorldCreator.getWorld(15, 15, 60); actor1 = new ExampleActorGetWorld(); world1.addObject(actor1, 3, 5); actor2 = new ExampleActorGetWorld(); world2.addObject(actor2, 3, 5); } @Test public void testGetWorld() throws Exception { // WHEN WorldCreator.runOnce(); // THEN assertEquals( 4, actor1.getX()); assertEquals( 3, actor2.getX()); } @Test public void testSetWorld() throws Exception { // WHEN Greenfoot.setWorld(world2); WorldCreator.runOnce(); // THEN assertEquals( 3, actor1.getX()); assertEquals( 4, actor2.getX()); } }
- Parameters:
worldWidth
- represents the world widthworldHeight
- represents the world heightcellSize
- represents the cell size- Returns:
- the created world
- Throws:
GreenfootRunnerExpected
- is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.
-
getWorld
public static <worldType> worldType getWorld(java.lang.Class<? extends greenfoot.World> worldType, java.lang.Object... parameters) throws GreenfootRunnerExpected, WorldConstructorNotFound
It creates and activates a user world for testing. It should be used instead of "new World ()" when a world needs to be created and the data used in its creation should not affect the rest of the test. When more than one world is created in the same test, the first created world will be the world associated with the scenario, and otherWorld can be activated by calling the Greenfoot.setWorld(otherWorld). For the next actor behaviour:public class ExampleWorld extends World { private int value = 0; public ExampleWorld(){ super(15, 15, 60); } public int getValue(){ return value; } @Override public void act(){ value = 1; } }
import ... import greenfoot.TestRunnerState.WorldCreator; @RunWith(GreenfootRunner.class) public class GetWorldTest { ExampleWorld world1; ExampleWorld world2; @Before public void setUp() throws Exception { // GIVEN world1 = WorldCreator.getWorld(ExampleWorld.class); world2 = WorldCreator.getWorld(ExampleWorld.class); } @Test public void testGetWorld() throws Exception { // WHEN WorldCreator.runOnce(); // THEN assertEquals( 1, world1.getValue()); assertEquals( 0, world2.getValue()); } @Test public void testSetWorld() throws Exception { // WHEN Greenfoot.setWorld(world2); WorldCreator.runOnce(); // THEN assertEquals( 0, world1.getValue()); assertEquals( 1, world2.getValue()); } }
- Type Parameters:
worldType
- represents the user world class- Parameters:
worldType
- represents the user world class.parameters
- is the parameters list of constructor.- Returns:
- the created world.
- Throws:
GreenfootRunnerExpected
- is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.WorldConstructorNotFound
- is raise when there is not any contructor of the world class that has a parameter list that matches with parameters.
-
runOnce
public static void runOnce() throws GreenfootRunnerExpected
It executes one acts cycle (the act of world and the acts of actors added to current world of the scenario). Delay, Speed and SoundPlayed events are deleted before acts are executed and Mic Level, Random and Ask events are deleted after acts are executed. Also, the keyboard and mouse buffers are reset after this operation. For the next actor behaviour:public class ExampleActorRunOnce extends Actor { @Override public void act(){ if (isTouching(ExampleActorRunOnce.class)){ move(4); } else { move(2); } } }
import ... import greenfoot.TestRunnerState.WorldCreator; @RunWith(GreenfootRunner.class) public class ExampleRunOnceTest { @Test public void testRunOnce() throws Exception { // GIVEN World world1 = WorldCreator.getWorld(15, 15, 60); World world2 = WorldCreator.getWorld(15, 15, 60); ExampleActorRunOnce actor_1_1 = new ExampleActorRunOnce(); ExampleActorRunOnce actor_1_2 = new ExampleActorRunOnce(); world1.addObject(actor_1_1, 3, 5); world1.addObject(actor_1_2, 5, 5); ExampleActorRunOnce actor_2_1 = new ExampleActorRunOnce(); ExampleActorRunOnce actor_2_2 = new ExampleActorRunOnce(); world2.addObject(actor_2_1, 3, 5); world2.addObject(actor_2_2, 5, 5); // WHEN WorldCreator.runOnce(); // THEN assertEquals( 5, actor_1_1.getX()); assertEquals( 9, actor_1_2.getX()); assertEquals( 3, actor_2_1.getX()); assertEquals( 5, actor_2_2.getX()); } }
- Throws:
GreenfootRunnerExpected
- is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.
-
runOnce
public static void runOnce(greenfoot.World world) throws GreenfootRunnerExpected
It executes one acts cycle (the act of world and the acts of actors added to world). If this world is not the current world of the scenario, it is executed Greenfoot.setWorld(world) before acts cycle. Delay, Speed and SoundPlayed events are deleted before acts are executed and Mic Level, Random and Ask events are deleted after acts are executed. Also, the keyboard and mouse buffers are reset after this operation. For the next actor behaviour:public class ExampleActorRunOnce extends Actor { @Override public void act(){ if (isTouching(ExampleActorRunOnce.class)){ move(4); } else { move(2); } } }
import ... import greenfoot.TestRunnerState.WorldCreator; @RunWith(GreenfootRunner.class) public class ExampleRunOnceTest { @Test public void testRunOnce() throws Exception { // GIVEN World world1 = WorldCreator.getWorld(15, 15, 60); World world2 = WorldCreator.getWorld(15, 15, 60); ExampleActorRunOnce actor_1_1 = new ExampleActorRunOnce(); ExampleActorRunOnce actor_1_2 = new ExampleActorRunOnce(); world1.addObject(actor_1_1, 3, 5); world1.addObject(actor_1_2, 5, 5); ExampleActorRunOnce actor_2_1 = new ExampleActorRunOnce(); ExampleActorRunOnce actor_2_2 = new ExampleActorRunOnce(); world2.addObject(actor_2_1, 3, 5); world2.addObject(actor_2_2, 5, 5); // WHEN WorldCreator.runOnce(world2); // THEN assertEquals(3, actor_1_1.getX()); assertEquals(5, actor_1_2.getX()); assertEquals(5, actor_2_1.getX()); assertEquals(9, actor_2_2.getX()); } }
- Parameters:
world
- where the actors were adding- Throws:
GreenfootRunnerExpected
- is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.
-
runOnce
public static void runOnce(greenfoot.Actor... actors) throws GreenfootRunnerExpected
It executes one act of the referred actors, that is, of the actors of the list that is passed as a parameter only when theirs world is the current world of the scenario. Delay, Speed and SoundPlayed events are deleted before acts are executed and Mic Level, Random and Ask events are deleted after acts are executed. Also, the keyboard and mouse buffers are reset after this operation.public class ExampleActorRunOnce extends Actor { @Override public void act(){ if (isTouching(ExampleActorRunOnce.class)){ move(4); } else { move(2); } } }
import ... import greenfoot.TestRunnerState.WorldCreator; @RunWith(GreenfootRunner.class) public class ExampleRunOnceTest { World world1; World world2; ExampleActorRunOnce actor_1_1; ExampleActorRunOnce actor_1_2; ExampleActorRunOnce actor_1_3; ExampleActorRunOnce actor_2_1; ExampleActorRunOnce actor_2_2; ExampleActorRunOnce actor_2_3; @Before public void setUp() throws Exception { // GIVEN world1 = WorldCreator.getWorld(15, 15, 60); world2 = WorldCreator.getWorld(15, 15, 60); actor_1_1 = new ExampleActorRunOnce(); actor_1_2 = new ExampleActorRunOnce(); actor_1_3 = new ExampleActorRunOnce(); world1.addObject(actor_1_1, 3, 5); world1.addObject(actor_1_2, 5, 5); world1.addObject(actor_1_3, 9, 5); actor_2_1 = new ExampleActorRunOnce(); actor_2_2 = new ExampleActorRunOnce(); actor_2_3 = new ExampleActorRunOnce(); world2.addObject(actor_2_1, 3, 5); world2.addObject(actor_2_2, 5, 5); world1.addObject(actor_2_3, 9, 5); } @Test public void testRunOnceOne() throws Exception { // WHEN WorldCreator.runOnce(actor_1_1); // THEN assertEquals(5, actor_1_1.getX()); assertEquals(5, actor_1_2.getX()); assertEquals(9, actor_1_3.getX()); assertEquals(3, actor_2_1.getX()); assertEquals(5, actor_2_2.getX()); assertEquals(9, actor_2_2.getX()); } @Test public void testRunOnceNone() throws Exception { // WHEN WorldCreator.runOnce(actor_2_1); // THEN assertEquals(3, actor_1_1.getX()); assertEquals(5, actor_1_2.getX()); assertEquals(9, actor_1_3.getX()); assertEquals(3, actor_2_1.getX()); assertEquals(5, actor_2_2.getX()); assertEquals(9, actor_2_2.getX()); } @Test public void testRunOnceMany() throws Exception { // WHEN WorldCreator.runOnce(actor_2_1, actor_1_2, actor_2_2, actor_1_1); // THEN assertEquals(5, actor_1_1.getX()); assertEquals(9, actor_1_2.getX()); assertEquals(9, actor_1_3.getX()); assertEquals(3, actor_2_1.getX()); assertEquals(5, actor_2_2.getX()); assertEquals(9, actor_2_2.getX()); } }
- Parameters:
actors
- list that may not have been added to any world- Throws:
GreenfootRunnerExpected
- is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.
-
runOnce
public static void runOnce(greenfoot.World world, greenfoot.Actor... actors) throws GreenfootRunnerExpected
It executes an act cycle of the world and referenced actors that have been added to this world. If this world is not the current world of the scenario, it is executed Greenfoot.setWorld(world) before acts cycle. Delay, Speed and SoundPlayed events are deleted before acts are executed and Mic Level, Random and Ask events are deleted after acts are executed. Also, the keyboard and mouse buffers are reset after this operation.public class ExampleActorRunOnce extends Actor { @Override public void act(){ if (isTouching(ExampleActorRunOnce.class)){ move(4); } else { move(2); } } }
import ... import greenfoot.TestRunnerState.WorldCreator; @RunWith(GreenfootRunner.class) public class ExampleRunOnceTest { World world1; World world2; ExampleActorRunOnce actor_1_1; ExampleActorRunOnce actor_1_2; ExampleActorRunOnce actor_1_3; ExampleActorRunOnce actor_2_1; ExampleActorRunOnce actor_2_2; ExampleActorRunOnce actor_2_3; @Before public void setUp() throws Exception { // GIVEN world1 = WorldCreator.getWorld(15, 15, 60); world2 = WorldCreator.getWorld(15, 15, 60); actor_1_1 = new ExampleActorRunOnce(); actor_1_2 = new ExampleActorRunOnce(); actor_1_3 = new ExampleActorRunOnce(); world1.addObject(actor_1_1, 3, 5); world1.addObject(actor_1_2, 5, 5); world1.addObject(actor_1_3, 9, 5); actor_2_1 = new ExampleActorRunOnce(); actor_2_2 = new ExampleActorRunOnce(); actor_2_3 = new ExampleActorRunOnce(); world2.addObject(actor_2_1, 3, 5); world2.addObject(actor_2_2, 5, 5); world1.addObject(actor_2_3, 9, 5); } @Test public void testRunOnceNone() throws Exception { // WHEN WorldCreator.runOnce(world2, actor_1_1); // THEN assertEquals(3, actor_1_1.getX()); assertEquals(5, actor_1_2.getX()); assertEquals(9, actor_1_3.getX()); assertEquals(3, actor_2_1.getX()); assertEquals(5, actor_2_2.getX()); assertEquals(9, actor_2_2.getX()); } @Test public void testRunOnceOne() throws Exception { // WHEN WorldCreator.runOnceOne(world2, actor_2_1); // THEN assertEquals(3, actor_1_1.getX()); assertEquals(5, actor_1_2.getX()); assertEquals(9, actor_1_3.getX()); assertEquals(5, actor_2_1.getX()); assertEquals(5, actor_2_2.getX()); assertEquals(9, actor_2_2.getX()); } @Test public void testRunOnceMany() throws Exception { // WHEN WorldCreator.runOnce(world2, actor_2_1, actor_1_2, actor_2_2, actor_1_1); // THEN assertEquals(3, actor_1_1.getX()); assertEquals(5, actor_1_2.getX()); assertEquals(9, actor_1_3.getX()); assertEquals(5, actor_2_1.getX()); assertEquals(9, actor_2_2.getX()); assertEquals(9, actor_2_2.getX()); } }
- Parameters:
world
- where the actors were addingactors
- list that must be had added to world- Throws:
GreenfootRunnerExpected
- is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.
-
isStop
public static boolean isStop() throws GreenfootRunnerExpected
It verifies if object has paused the animation calling to Greenfoot.Stop().
For the next actor behaviour:public class ExampleActorStop extends Actor { @Override public void act(){ Greenfoot.stop(); } }
import ... import greenfoot.TestRunnerState.WorldCreator; @RunWith(GreenfootRunner.class) public class ExampleActorStopTest { @Test public void testStop() throws Exception { // GIVEN World world = WorldCreator.getWorld(400, 300, 1); ExampleActorStop actor = new ExampleActorStop(); world.addObject(actor, 1, 1); // WHEN WorldCreator.runOnce(world); // THEN assertTrue(WorldCreator.isStop()); } }
- Returns:
- true when it is paused
- Throws:
GreenfootRunnerExpected
- is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.
-
isStart
public static boolean isStart() throws GreenfootRunnerExpected
It verifies if any object has start the animation calling to Greenfoot.Start().
For the next actor behaviour:public class ExampleActorStart extends Actor { @Override public void act(){ Greenfoot.start(); } }
import ... import greenfoot.TestRunnerState.WorldCreator; @RunWith(GreenfootRunner.class) public class ExampleActorStartTest { @Test public void testStart() throws Exception { // GIVEN World world = WorldCreator.getWorld(400, 300, 1); ExampleActorStart actor = new ExampleActorStart(); world.addObject(actor, 1, 1); // WHEN WorldCreator.runOnce(world); // THEN assertTrue(WorldCreator.isStart()); } }
- Returns:
- true when it is paused
- Throws:
GreenfootRunnerExpected
- is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.
-
setMicLevel
public static void setMicLevel(int level) throws GreenfootRunnerExpected
It queues a microphone level in FIFO order for any next Greenfoot.getMicLevel() operation.
For the next actor behaviour:public class ExampleActorMicLevel extends Actor { @Override public void act(){ if (Greenfoot.getMiLevel() == 90) { move(10); } } }
import ... import greenfoot.TestRunnerState.WorldCreator; @RunWith(GreenfootRunner.class) public class ExampleActorMicLevelTest { @Test public void testMicLevel() throws Exception { // GIVEN World world = WorldCreator.getWorld(400, 300, 1); ExampleActorMicLevel actor = new ExampleActorMicLevel(); world.addObject(actor, 1, 1); // WHEN WorldCreator.setMicLevel(90); WorldCreator.runOnce(world); // THEN assertEquals(11, WorldCreator.getX()); } }
- Parameters:
level
- is the value tested- Throws:
GreenfootRunnerExpected
- is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.
-
getSpeed
public static int getSpeed() throws GreenfootRunnerExpected
It return the speed of the animation that was set by any object.
For the next actor behaviour:public class ExampleActorSpeed extends Actor { @Override public void act(){ Greenfoot.setSpeed(100); } }
import ... import greenfoot.TestRunnerState.WorldCreator; @RunWith(GreenfootRunner.class) public class ExampleActorSpeedTest { @Test public void testSpeed() throws Exception { // GIVEN World world = WorldCreator.getWorld(400, 300, 1); ExampleActorSpeed actor = new ExampleActorSpeed(); world.addObject(actor, 1, 1); // WHEN WorldCreator.runOnce(world); // THEN assertEquals(100, WorldCreator.getSpeed()); } }
- Returns:
- next speed was done
- Throws:
GreenfootRunnerExpected
- is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.
-
getSpeed
public static boolean getSpeed(int speed) throws GreenfootRunnerExpected
It verifies that any object has set this speed of the animation.
For the next actor behaviour:public class ExampleActorSpeed extends Actor { @Override public void act(){ Greenfoot.setSpeed(100); } }
import ... import greenfoot.TestRunnerState.WorldCreator; @RunWith(GreenfootRunner.class) public class ExampleActorSpeedTest { @Test public void testSpeed() throws Exception { // GIVEN World world = WorldCreator.getWorld(400, 300, 1); ExampleActorSpeed actor = new ExampleActorSpeed(); world.addObject(actor, 1, 1); // WHEN WorldCreator.runOnce(world); // THEN assertTrue(WorldCreator.getSpeed(100)); } }
- Parameters:
speed
- is the value tested- Returns:
- true when any world or actor has set this speed
- Throws:
GreenfootRunnerExpected
- is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.
-
getDelay
public static int getDelay() throws GreenfootRunnerExpected
It returns the delay was set by any object.
For the next actor behaviour:public class ExampleActorDelay extends Actor { @Override public void act(){ Greenfoot.setDelay(12); } }
import ... import greenfoot.TestRunnerState.WorldCreator; @RunWith(GreenfootRunner.class) public class ExampleActorDelayTest { @Test public void testDelay() throws Exception { // GIVEN World world = WorldCreator.getWorld(400, 300, 1); ExampleActorDelay actor = new ExampleActorDelay(); world.addObject(actor, 1, 1); // WHEN WorldCreator.runOnce(world); // THEN assertEquals(12, WorldCreator.getDelay()); } }
- Returns:
- next delay that was done
- Throws:
GreenfootRunnerExpected
- is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.
-
getDelay
public static boolean getDelay(int delay) throws GreenfootRunnerExpected
It verifies any object has set a delay.
For the next actor behaviour:public class ExampleActorDelay extends Actor { @Override public void act(){ Greenfoot.setDelay(12); } }
import ... import greenfoot.TestRunnerState.WorldCreator; @RunWith(GreenfootRunner.class) public class ExampleActorDelayTest { @Test public void testDelay() throws Exception { // GIVEN World world = WorldCreator.getWorld(400, 300, 1); ExampleActorDelay actor = new ExampleActorDelay(); world.addObject(actor, 1, 1); // WHEN WorldCreator.runOnce(world); // THEN assertTrue(WorldCreator.getDelay()); } }
- Parameters:
delay
- is the value tested- Returns:
- true when any object set this delay
- Throws:
GreenfootRunnerExpected
- is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.
-
-