package org.graphwalker.core.machine;

/*
 * #%L
 * GraphWalker Core
 * %%
 * Copyright (C) 2011 - 2014 GraphWalker
 * %%
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * #L%
 */

import org.graphwalker.core.condition.VertexCoverage;
import org.graphwalker.core.event.Observable;
import org.graphwalker.core.event.Observer;
import org.graphwalker.core.generator.RandomPath;
import org.graphwalker.core.model.Edge;
import org.graphwalker.core.model.Element;
import org.graphwalker.core.model.Model;
import org.graphwalker.core.model.Vertex;
import org.junit.Assert;
import org.junit.Test;

import static org.hamcrest.core.Is.is;

/**
 * @author Nils Olsson
 */
public class ObserverTest implements Observer<Element> {

    private int counter = 0;

    @Override
    public void update(Observable<Element> observable, Element object) {
        counter++;
    }

    @Test
    public void updateCounter() {
        Vertex vertex = new Vertex();
        Model model = new Model().addEdge(new Edge().setSourceVertex(vertex).setTargetVertex(new Vertex()));
        ExecutionContext context = new ExecutionContext(model, new RandomPath(new VertexCoverage(100)));
        context.setNextElement(vertex);
        Machine machine = new SimpleMachine(context);
        machine.addObserver(this);
        while (machine.hasNextStep()) {
            machine.getNextStep();
        }
        Assert.assertNotEquals(context.getProfiler().getTotalVisitCount(), 0);
        Assert.assertThat(counter, is(3));
    }
}

@@ -1395,16 +1395,166 @@
 omPath;%0A
+import org.graphwalker.core.machine.ExecutionContext;%0Aimport org.graphwalker.core.machine.Machine;%0Aimport org.graphwalker.core.machine.SimpleMachine;%0A
 import o
@@ -1581,24 +1581,24 @@
 model.Edge;%0A
-
 import org.g
@@ -2040,43 +2040,47 @@
     
-@Test%0A    public void updateCounter
+private ObservableMachine createMachine
 () %7B
@@ -2380,16 +2380,26 @@
         
+Observable
 Machine 
@@ -2448,32 +2448,1457 @@
     
-machine.addObserver(this
+Assert.assertTrue(Observer.class.isAssignableFrom(this.getClass()));%0A        Assert.assertTrue(Observable.class.isAssignableFrom(machine.getClass()));%0A        return machine;%0A    %7D%0A%0A    @Test(expected = NullPointerException.class)%0A    public void nullObserver() %7B%0A        ObservableMachine observable = createMachine();%0A        observable.addObserver(null);%0A    %7D%0A%0A    @Test%0A    public void changeObserver() %7B%0A        ObservableMachine observable = createMachine();%0A        Assert.assertThat(observable.hasChanged(), is(false));%0A        observable.notifyObservers();%0A        Assert.assertThat(observable.hasChanged(), is(false));%0A    %7D%0A%0A    @Test%0A    public void addRemoveObserver() %7B%0A        ObservableMachine observable = createMachine();%0A        observable.addObserver(this);%0A        Assert.assertThat(observable.countObservers(), is(1));%0A        observable.deleteObserver(this);%0A        Assert.assertThat(observable.countObservers(), is(0));%0A        observable.addObserver(this);%0A        Assert.assertThat(observable.countObservers(), is(1));%0A        observable.deleteObservers();%0A        Assert.assertThat(observable.countObservers(), is(0));%0A        observable.addObserver(this);%0A        Assert.assertThat(observable.countObservers(), is(1));%0A    %7D%0A%0A    @Test%0A    public void updateCounter() %7B%0A        Machine machine = createMachine();%0A        machine.addObserver(this);%0A        ExecutionContext context = machine.getExecutionContexts().get(0
 );%0A 

Add more tests