package edu.hawaii.ics.yucheng; import java.util.ArrayList; import junit.framework.TestCase; /** * Tests the GraphSolver class. */ public class GraphSolverTest extends TestCase { class TestSolver extends GraphSolver { @Override public GraphSolution solve(final Graph g) { return null; } public void testProgress() { super.publish(null); } } /** * Tests the addGraphListener method. * * @throws Exception */ public void testAddGraphListener1() throws Exception { final TestSolver s = new TestSolver(); try { s.addGraphListener(null); fail(); } catch (final NullPointerException e) { // This is expected. } } /** * Tests the addGraphListener method. * * @throws Exception */ public void testAddGraphListener2() throws Exception { final ArrayList<Integer> list = new ArrayList<Integer>(); final TestSolver s = new TestSolver(); s.startAnimation(1, 0); s.addGraphListener(new GraphListener() { public void progress(final GraphSolution solution) { list.add(Integer.valueOf(1)); } }); s.testProgress(); assertEquals(1, list.size()); } /** * Tests the constructor. * * @throws Exception */ public void testConstructor() throws Exception { new TestSolver(); } /** * Tests the isAnimating method. * * @throws Exception */ public void testIsAnimating() throws Exception { final TestSolver s = new TestSolver(); assertFalse(s.isAnimating()); s.startAnimation(1); assertTrue(s.isAnimating()); } /** * Tests the publish method. * * @throws Exception */ public void testPublish1() throws Exception { final ArrayList<Integer> list = new ArrayList<Integer>(); final TestSolver s = new TestSolver(); s.addGraphListener(new GraphListener() { public void progress(final GraphSolution solution) { list.add(Integer.valueOf(1)); } }); s.testProgress(); assertEquals(0, list.size()); } /** * Tests the publish method. * * @throws Exception */ public void testPublish2() throws Exception { final ArrayList<Integer> list = new ArrayList<Integer>(); final TestSolver s = new TestSolver(); s.startAnimation(1000, 0); s.addGraphListener(new GraphListener() { public void progress(final GraphSolution solution) { list.add(Integer.valueOf(1)); } }); s.testProgress(); s.testProgress(); assertEquals(1, list.size()); } /** * Tests the publish method. * * @throws Exception */ public void testPublish3() throws Exception { final ArrayList<Integer> list = new ArrayList<Integer>(); final TestSolver s = new TestSolver(); s.startAnimation(1); s.addGraphListener(new GraphListener() { public void progress(final GraphSolution solution) { list.add(Integer.valueOf(1)); } }); s.testProgress(); assertEquals(1, list.size()); } /** * Tests the removeGraphListener method. * * @throws Exception */ public void testRemoveGraphListener1() throws Exception { final TestSolver s = new TestSolver(); try { s.removeGraphListener(null); fail(); } catch (final NullPointerException e) { // This is expected. } } /** * Tests the removeGraphListener method. * * @throws Exception */ public void testRemoveGraphListener2() throws Exception { final ArrayList<Integer> list = new ArrayList<Integer>(); final TestSolver s = new TestSolver(); s.startAnimation(1, 0); final GraphListener listener = new GraphListener() { public void progress(final GraphSolution solution) { list.add(Integer.valueOf(1)); } }; s.addGraphListener(listener); s.testProgress(); assertEquals(1, list.size()); s.removeGraphListener(listener); s.testProgress(); assertEquals(1, list.size()); } /** * Tests the startAnimation method. * * @throws Exception */ public void testStartAnimation1() throws Exception { final TestSolver s = new TestSolver(); s.startAnimation(1); assertTrue(s.isAnimating()); } /** * Tests the startAnimation method. * * @throws Exception */ public void testStartAnimation2a() throws Exception { final TestSolver s = new TestSolver(); try { s.startAnimation(-1, 0); fail(); } catch (final IllegalArgumentException e) { // This is expected. } } /** * Tests the startAnimation method. * * @throws Exception */ public void testStartAnimation2b() throws Exception { final TestSolver s = new TestSolver(); try { s.startAnimation(0, -1); fail(); } catch (final IllegalArgumentException e) { // This is expected. } } /** * Tests the startAnimation method. * * @throws Exception */ public void testStartAnimation2c() throws Exception { final TestSolver s = new TestSolver(); s.startAnimation(1, 1); assertTrue(s.isAnimating()); } /** * Tests the stopAnimation method. * * @throws Exception */ public void testStopAnimation() throws Exception { final TestSolver s = new TestSolver(); s.startAnimation(1, 1); assertTrue(s.isAnimating()); s.stopAnimation(); assertFalse(s.isAnimating()); } }