/*
 * Copyright 2010 Twitter, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.twitter.grabbyhands;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class JavaTest {
    public JavaTest() {
        System.out.println("java test");
    }

    public void run() {
        testCreate();
        testWriteRead();
    }

    protected void testCreate() {
        System.out.println("run testCreate");
        List<String> servers = Arrays.asList("localhost:22133");
        List<String> queues = Arrays.asList("grabby_test");

        Config config = new Config(servers);
        HashMap<String, ConfigQueue> queueConfigs = config.addQueues(queues);
        System.out.println("1");
        assert(queueConfigs.containsKey(queues.get(0)));

        config.setRecvNumConnections(4);
        config.setSendNumConnections(5);
        System.out.println("2");
        assert(config.recvNumConnections() == 4);
        assert(config.getRecvNumConnections() == 4);
        System.out.println("2a");
        assert(config.sendNumConnections() == 5);
        assert(config.getSendNumConnections() == 5);

        config.setMaxMessageBytes(100);
        System.out.println("2z");
        assert(config.getMaxMessageBytes() == 100);

        ConfigQueue configQueue = queueConfigs.get(queues.get(0));
        System.out.println("3");
        assert(configQueue.recvNumConnections() == 4);
        System.out.println("3a");
        assert(configQueue.getRecvNumConnections() == 4);

        System.out.println("4");
        assert(configQueue.recvQueueDepth() == 4);
        assert(configQueue.getRecvQueueDepth() == 4);

        System.out.println("5");
        assert(configQueue.sendNumConnections() == 5);
        assert(configQueue.getSendNumConnections() == 5);

        System.out.println("6");
        assert(configQueue.sendQueueDepth() == 5);
        assert(configQueue.getSendQueueDepth() == 5);

        GrabbyHands grabbyHands = new GrabbyHands(config);
        System.out.println("pass testCreate");
    }

    protected void testWriteRead() {
        System.out.println("run testWriteRead");
        // TODO: Implement
        System.exit(-1);
    }

    public static void main(String[] args) {
        JavaTest javaTest = new JavaTest();
        javaTest.run();
    }
}

@@ -641,19 +641,128 @@
 ava.
-util.Arrays
+nio.ByteBuffer;%0Aimport java.util.Arrays;%0Aimport java.util.concurrent.BlockingQueue;%0Aimport java.util.concurrent.TimeUnit
 ;%0Aim
@@ -897,32 +897,210 @@
  test%22);%0A    %7D%0A%0A
+    protected List%3CString%3E servers = Arrays.asList(%22localhost:22133%22);%0A    protected String queue = %22grabby_javatest%22;%0A    protected List%3CString%3E queues = Arrays.asList(queue);%0A%0A
     public void 
@@ -1244,133 +1244,8 @@
 e%22);
-%0A        List%3CString%3E servers = Arrays.asList(%22localhost:22133%22);%0A        List%3CString%3E queues = Arrays.asList(%22grabby_test%22);
 %0A%0A  
@@ -2795,77 +2795,930 @@
 n(%22r
-un testWriteRead%22);%0A        // TODO: Implement%0A        System.exit(-1
+w 1%22);%0A        Config config = new Config(servers);%0A        config.addQueues(queues);%0A        GrabbyHands grabbyHands = new GrabbyHands(config);%0A        BlockingQueue%3CWrite%3E send = grabbyHands.getSendQueue(queue);%0A        BlockingQueue%3CByteBuffer%3E recv = grabbyHands.getRecvQueue(queue);%0A        System.out.println(%22rw 2%22);%0A%0A        String sendText = %22text%22;%0A        Write write = new Write(sendText);%0A        assert(!write.written());%0A        assert(!write.cancelled());%0A        System.out.println(%22rw 3%22);%0A        try %7B%0A            send.put(write);%0A            ByteBuffer buffer = recv.poll(2, TimeUnit.SECONDS);%0A            assert(buffer != null);%0A%0A            String recvText = new String(buffer.array());%0A            assert(recvText.equals(sendText));%0A%0A            assert(write.written());%0A        %7D catch (InterruptedException e) %7B%0A            assert(false);%0A        %7D%0A        System.out.println(%22pass testWriteRead%22
 );%0A 

add round trip test in java.