Escape-sprowe / test / mastertest / release2 / ObserverTest.java
ObserverTest.java
Raw
/*******************************************************************************
 * This files was developed for CS4233: Object-Oriented Analysis & Design.
 * The course was taken at Worcester Polytechnic Institute.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Copyright ©2020 Gary F. Pollice
 *******************************************************************************/

package mastertest.release2;

import static org.junit.jupiter.api.Assertions.*;
import java.util.*;

import escape.initializers.EscapeGameBuilder;
import org.junit.jupiter.api.*;
import escape.*;
import escape.interfaces.Coordinate;

/**
 * Description
 */
class ObserverTest
{

	static EscapeGameManager manager;
	static String configFile;
	static MasterTestObserver observer;
	
	// Deduction amounts
	static int observerTestValue;
	
	// Deductions
	static int observerTestDeductions;
	
	@BeforeAll
	static void initialize()
	{
		configFile = "config/egc/R2Hex1.egc";
		observerTestDeductions = 0;
		observerTestValue = 3;
	}
	
	@AfterAll
	static void reportResults()
	{
		System.out.println("\n\n>>> Observer tests: " + -observerTestDeductions);
	}
	
	@BeforeEach
	void loadGame() throws Exception
	{
		EscapeGameBuilder egb = new EscapeGameBuilder(configFile);
		manager = egb.makeGameManager();
		assertNotNull(manager);
		observer = new MasterTestObserver();
        manager.addObserver(observer);
	}
	
	@Test
	void player1Wins()
	{
		observerTestDeductions += observerTestValue;
		List<Coordinate> coords = makeList(2, -2, -1, 1);
		makeMoves(coords);
		assertEquals(1, observer.messageCount());
		String msg = observer.nextMessage().getMessage().toLowerCase();
		assertTrue(msg.contains("player"));
		assertTrue(msg.contains("1"));
		assertTrue(msg.contains("wins"));
		observerTestDeductions -= observerTestValue;
	}
	
	@Test
	void badMove()
	{
		observerTestDeductions += observerTestValue;
		List<Coordinate> coords = 
			makeList(-2, 0, 10, 0);
		makeMoves(coords);
		assertEquals(1, observer.messageCount());
        System.err.println("\n\n>>>>>>>>>>>>>>>>>\n" 
		    + observer.nextMessage().getMessage()
		    + "\n>>>>>>>>>>>>>>>>\n");
		observerTestDeductions -= observerTestValue;
	}
	
	@Test
    void multipleMessages()
    {
        observerTestDeductions += observerTestValue;
        List<Coordinate> coords = 
            makeList(-2, 0, 10, 0, 2, -2, -1, 1);
        makeMoves(coords);
        assertEquals(2, observer.messageCount());
        System.err.println("\n\n>>>>>>>>>>>>>>>>>\n");
        do {
            System.err.println("\t> " + observer.nextMessage().getMessage());
        } while (observer.messageCount() > 0);
        System.err.println("\n>>>>>>>>>>>>>>>>\n");
        observerTestDeductions -= observerTestValue;
    }
	
	// helper
	void makeMoves(List<Coordinate>coords)
	{
	    do
        {
            Coordinate c1 = coords.remove(0);
            Coordinate c2 = coords.remove(0);
            manager.move(c1, c2);
        } while (!coords.isEmpty());
	}
	
	boolean textFound(String testName, String text)
	{
		System.out.println("Test: " + testName + " has " + observer.messageCount()
			+ " messages");
		Notification notification;
		while ((notification = observer.nextMessage()) != null) {
			if (notification.getMessage().toLowerCase().contains(text.toLowerCase())) {
				return true;
			}
		}
		return false;
	}
	
	// Make a list
	static List<Coordinate> makeList(int... ints)
	{
		int i = 0;
		List<Coordinate> coords = new LinkedList<Coordinate>();
		while (i < ints.length) {
			int x = ints[i++];
			int y = ints[i++];
			Coordinate c = manager.makeCoordinate(x, y);
			coords.add(c);
		}
		return coords;
	}
}