/*
 * The MIT License
 *
 * Copyright 2017 michael.
 *
 * 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.
 */
package il.ac.bgu.cs.bp.bpjs.model;

import il.ac.bgu.cs.bp.bpjs.execution.listeners.BProgramRunnerListener;
import il.ac.bgu.cs.bp.bpjs.internal.ExecutorServiceMaker;
import il.ac.bgu.cs.bp.bpjs.model.eventselection.EventSelectionResult;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;

import static org.junit.Assert.*;

/**
 *
 * @author michael
 */
public class BThreadSyncSnapshotTest {
    
    /**
     * Test default naming.
     */
    @Test
    public void testDefaultNaming() {
        BThreadSyncSnapshot sut = new BThreadSyncSnapshot();
        assertEquals( BThreadSyncSnapshot.class.getName(), sut.getName() );
    }

    @Rule
    public final ExpectedException exception = ExpectedException.none();

    private final List<BProgramRunnerListener> listeners = new ArrayList<>();

    @Test
    public void testJSVarState() throws InterruptedException {
        BProgram bprog = new SingleResourceBProgram("SnapshotTests/ABCDTrace.js");
        BProgramSyncSnapshot setup = bprog.setup();

        ExecutorService execSvc = ExecutorServiceMaker.makeWithName("BProgramSnapshotEqualityTest");
        List<BProgramSyncSnapshot> snapshots = new ArrayList<>();
        BProgramSyncSnapshot step = setup.start(execSvc);
        snapshots.add(step);
        //Iteration 1,starts already at request state A
        for (int i =0; i< 4; i++) {
            Set<BEvent> possibleEvents_a = bprog.getEventSelectionStrategy().selectableEvents(step.getStatements(), step.getExternalEvents());
            EventSelectionResult event_a = bprog.getEventSelectionStrategy().select(step.getStatements(), step.getExternalEvents(), possibleEvents_a).get();
            step = step.triggerEvent(event_a.getEvent(), execSvc, listeners);
        }
        snapshots.add(step);
        for (int i =0; i< 4; i++) {
            Set<BEvent> possibleEvents_a = bprog.getEventSelectionStrategy().selectableEvents(step.getStatements(), step.getExternalEvents());
            EventSelectionResult event_a = bprog.getEventSelectionStrategy().select(step.getStatements(), step.getExternalEvents(), possibleEvents_a).get();
            step = step.triggerEvent(event_a.getEvent(), execSvc, listeners);
        }
        snapshots.add(step);
        //these two objects (by debugging) share program counter and frame index and should differ on variables only
        assertNotEquals(snapshots.get(1).getBThreadSnapshots(),snapshots.get(2).getBThreadSnapshots());
    }


    
}


@@ -3732,17 +3732,1247 @@
 %0A    %7D%0A%0A
-%0A
     
+@Test%0A    public void testJavaVarState() throws InterruptedException %7B%0A        BProgram bprog = new StringBProgram(%22bp.registerBThread(function()%7B%5Cn%22 +%0A                %22        let a = new java.lang.Integer(7);%5Cn%22 +%0A                %22        while (true) %7B%22 +%0A                %22        bp.sync(%7Brequest:bp.Event(%5C%22A%5C%22)%7D);%5Cn%22 +%0A                %22        a = java.lang.Integer.reverse(a);%5Cn%22 +%0A                %22        %7D%5Cn%22 +%0A                %22%7D);%22);%0A        Integer a = new java.lang.Integer(7);%0A        a = java.lang.Integer.reverse(a);%0A        BProgramSyncSnapshot setup = bprog.setup();%0A        ExecutorService execSvcA = ExecutorServiceMaker.makeWithName(%22BProgramSnapshotTriggerTest%22);%0A        BProgramSyncSnapshot stepa = setup.start(execSvcA);%0A        Set%3CBEvent%3E possibleEvents_a = bprog.getEventSelectionStrategy().selectableEvents(stepa.getStatements(), stepa.getExternalEvents());%0A        EventSelectionResult event_a = bprog.getEventSelectionStrategy().select(stepa.getStatements(), stepa.getExternalEvents(), possibleEvents_a).get();%0A        BProgramSyncSnapshot stepb = stepa.triggerEvent(event_a.getEvent(), execSvcA, listeners);%0A        assertNotEquals(stepa.getBThreadSnapshots(),stepb.getBThreadSnapshots());%0A%0A    %7D%0A%0A%0A
 %0A%7D%0A%0A

Add another failing Java test