package forklift.consumer;

import java.util.ArrayList;
import java.util.List;

import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;

import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import forklift.connectors.ConnectorException;
import forklift.connectors.ForkliftConnectorI;
import forklift.connectors.ForkliftMessage;

@Component
@Scope("prototype")
public class MockConnector implements ForkliftConnectorI {
    List<Message> msgs = new ArrayList<Message>();

    private ForkliftConnectorI mock;

    public MockConnector() throws ConnectorException, JMSException {
        final MessageConsumer consumer = Mockito.mock(MessageConsumer.class);

        final Answer<Message> answer = new Answer<Message>() {
            @Override
            public Message answer(InvocationOnMock invocation) throws Throwable {
                if (msgs.size() == 0)
                    return null;

                return msgs.remove(0);
            }

        };

        Mockito.when(consumer.receive()).thenAnswer(answer);
        Mockito.when(consumer.receive(Mockito.anyLong())).thenAnswer(answer);

        mock = Mockito.mock(ForkliftConnectorI.class);
        Mockito.when(mock.getQueue(Mockito.anyString())).thenReturn(consumer);
    }

    @Override
    public void start() throws ConnectorException {
        mock.start();
    }

    @Override
    public void stop() throws ConnectorException {
        mock.stop();
    }

    @Override
    public Connection getConnection()
      throws ConnectorException {
        return mock.getConnection();
    }

    @Override
    public MessageConsumer getQueue(String name)
      throws ConnectorException {
        return mock.getQueue(name);
    }

    @Override
    public MessageConsumer getTopic(String name)
      throws ConnectorException {
        return mock.getTopic(name);
    }

    @Override
    public ForkliftMessage jmsToForklift(Message m) {
        ForkliftMessage msg = new ForkliftMessage();
        msg.setMsg("TEST MSG");
        return msg;
    }

    public void addMsg() {
        msgs.add(Mockito.mock(Message.class));
    }
}

@@ -310,61 +310,8 @@
 er;%0A
-import org.springframework.context.annotation.Scope;%0A
 impo
@@ -510,28 +510,8 @@
 ent%0A
-@Scope(%22prototype%22)%0A
 publ
@@ -573,38 +573,80 @@
     
-List%3CMessage%3E msgs = new Array
+private static ThreadLocal%3CList%3CMessage%3E%3E threadLocal = new ThreadLocal%3C
 List
@@ -654,16 +654,17 @@
 Message%3E
+%3E
 ();%0A%0A   
@@ -759,32 +759,84 @@
  JMSException %7B%0A
+        threadLocal.set(new ArrayList%3CMessage%3E());%0A%0A
         final Me
@@ -1085,20 +1085,33 @@
     if (
-msgs
+threadLocal.get()
 .size() 
@@ -1173,20 +1173,33 @@
  return 
-msgs
+threadLocal.get()
 .remove(
@@ -2327,12 +2327,25 @@
     
-msgs
+threadLocal.get()
 .add

Use a threadlocal so that multithreaded tests stop failing.