package com.openxc.remote.sources;

import java.lang.InterruptedException;

import java.net.MalformedURLException;
import java.net.URL;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TraceVehicleDataSourceTest {
    TraceVehicleDataSource source;
    VehicleDataSourceCallbackInterface callback;
    boolean receivedNumericalCallback;
    boolean receivedStateCallback;
    final URL filename = this.getClass().getResource("/trace.json");
    final URL malformedFilename = this.getClass().getResource("/trace.txt");

    @Before
    public void setUp() {
        callback = new VehicleDataSourceCallbackInterface() {
            public void receive(String name, double value) {
                receivedNumericalCallback = true;
            }

            public void receive(String name, String value) {
                receivedStateCallback = true;
            }
        };
    }

    @After
    public void tearDown() {
    }

    @Test
    public void testSetFile() {
    }

    private void runSourceToCompletion(VehicleDataSourceInterface source)
            throws InterruptedException {
        Thread thread = new Thread(source);
        thread.run();
        thread.join();
    }

    @Test
    public void testPlaybackFile() throws InterruptedException,
            VehicleDataSourceException {
        receivedNumericalCallback = false;
        receivedStateCallback = false;
        source = new TraceVehicleDataSource(callback, filename);
        runSourceToCompletion(source);
        assert(receivedNumericalCallback);
        assert(receivedStateCallback);
    }

    @Test
    public void testMalformedJson() throws InterruptedException ,
            VehicleDataSourceException {
        receivedNumericalCallback = false;
        receivedStateCallback = false;
        source = new TraceVehicleDataSource(callback, malformedFilename);
        runSourceToCompletion(source);
        assert(!receivedNumericalCallback);
    }

    @Test
    public void testMissingFile() throws MalformedURLException,
            InterruptedException, VehicleDataSourceException {
        receivedNumericalCallback = false;
        receivedStateCallback = false;
        source = new TraceVehicleDataSource(callback, new URL("file://foo"));
        runSourceToCompletion(source);
        assert(!receivedNumericalCallback);
    }

    @Test
    public void testConstructWithCallbackAndFile()
            throws VehicleDataSourceException {
        source = new TraceVehicleDataSource(callback, filename);
    }
}

@@ -245,16 +245,162 @@
 eTest %7B%0A
+    final URL filename = this.getClass().getResource(%22/trace.json%22);%0A    final URL malformedFilename = this.getClass().getResource(%22/trace.txt%22);%0A
     Trac
@@ -557,148 +557,55 @@
     
-final URL filename = this.getClass().getResource(%22/trace.json%22);%0A    final URL malformedFilename = this.getClass().getResource(%22/trace.txt%22)
+double receivedNumber;%0A    String receivedState
 ;%0A%0A 
@@ -806,32 +806,72 @@
 allback = true;%0A
+                receivedNumber = value;%0A
             %7D%0A%0A 
@@ -968,32 +968,71 @@
 allback = true;%0A
+                receivedState = value;%0A
             %7D%0A  
@@ -1730,32 +1730,122 @@
 StateCallback);%0A
+        assertEquals(receivedNumber, 42);%0A        assertEquals(receivedState, %22MyState%22);%0A
     %7D%0A%0A    @Test

Check the values returned by the data source.