/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 *
 */

/*
 * Test class for TCPClientDecorator utility methods.  
 *
 */
package org.apache.jmeter.protocol.tcp.sampler;

import junit.framework.TestCase;

public class TCPClientDecoratorTest extends TestCase {

    
    public void testIntToByteArray() throws Exception {
        byte[] ba;
        int len = 2;
        ba = TCPClientDecorator.intToByteArray(0, len);
        assertEquals(len, ba.length);
        assertEquals(0, ba[0]);
        assertEquals(0, ba[1]);

        ba = TCPClientDecorator.intToByteArray(15, len);
        assertEquals(len, ba.length);
        assertEquals(0, ba[0]);
        assertEquals(15, ba[1]);

        ba = TCPClientDecorator.intToByteArray(255, len);
        assertEquals(len, ba.length);
        assertEquals(0, ba[0]);
        assertEquals(-1, ba[1]);

        ba = TCPClientDecorator.intToByteArray(256, len);
        assertEquals(len, ba.length);
        assertEquals(1, ba[0]);
        assertEquals(0, ba[1]);

        ba = TCPClientDecorator.intToByteArray(-1, len);
        assertEquals(len, ba.length);
        assertEquals(-1, ba[0]);
        assertEquals(-1, ba[1]);

        ba = TCPClientDecorator.intToByteArray(TCPClientDecorator.SHORT_MAX_VALUE, len);
        assertEquals(len, ba.length);
        assertEquals(127, ba[0]);
        assertEquals(-1, ba[1]);
        
        ba = TCPClientDecorator.intToByteArray(TCPClientDecorator.SHORT_MIN_VALUE, len);
        assertEquals(len, ba.length);
        assertEquals(-128, ba[0]);
        assertEquals(0, ba[1]);
        
        try {
            ba = TCPClientDecorator.intToByteArray(TCPClientDecorator.SHORT_MIN_VALUE-1, len);
            fail();
        } catch (IllegalArgumentException iae) {
        }
        
        try {
            ba = TCPClientDecorator.intToByteArray(TCPClientDecorator.SHORT_MAX_VALUE+1, len);
            fail();
        } catch (IllegalArgumentException iae) {
        }

        len = 4;
        ba = TCPClientDecorator.intToByteArray(0, len);
        assertEquals(len, ba.length);
        assertEquals(0, ba[0]);
        assertEquals(0, ba[1]);
        assertEquals(0, ba[2]);
        assertEquals(0, ba[3]);

        ba = TCPClientDecorator.intToByteArray(15, len);
        assertEquals(len, ba.length);
        assertEquals(0, ba[0]);
        assertEquals(0, ba[1]);
        assertEquals(0, ba[2]);
        assertEquals(15, ba[3]);

        ba = TCPClientDecorator.intToByteArray(255, len);
        assertEquals(len, ba.length);
        assertEquals(0, ba[0]);
        assertEquals(0, ba[1]);
        assertEquals(0, ba[2]);
        assertEquals(-1, ba[3]);

        ba = TCPClientDecorator.intToByteArray(-1, len);
        assertEquals(len, ba.length);
        assertEquals(-1, ba[0]);
        assertEquals(-1, ba[1]);
        assertEquals(-1, ba[2]);
        assertEquals(-1, ba[3]);

        ba = TCPClientDecorator.intToByteArray(256, len);
        assertEquals(len, ba.length);
        assertEquals(0, ba[0]);
        assertEquals(0, ba[1]);
        assertEquals(1, ba[2]);
        assertEquals(0, ba[3]);

        ba = TCPClientDecorator.intToByteArray(65535, len);
        assertEquals(len, ba.length);
        assertEquals(0, ba[0]);
        assertEquals(0, ba[1]);
        assertEquals(-1, ba[2]);
        assertEquals(-1, ba[3]);

        ba = TCPClientDecorator.intToByteArray(65536, len);
        assertEquals(len, ba.length);
        assertEquals(0, ba[0]);
        assertEquals(1, ba[1]);
        assertEquals(0, ba[2]);
        assertEquals(0, ba[3]);

        ba = TCPClientDecorator.intToByteArray(Integer.MIN_VALUE, len);
        assertEquals(len, ba.length);
        assertEquals(-128, ba[0]);
        assertEquals(0, ba[1]);
        assertEquals(0, ba[2]);
        assertEquals(0, ba[3]);

        ba = TCPClientDecorator.intToByteArray(Integer.MAX_VALUE, len);
        assertEquals(len, ba.length);
        assertEquals(127, ba[0]);
        assertEquals(-1, ba[1]);
        assertEquals(-1, ba[2]);
        assertEquals(-1, ba[3]);

    }

    public void testByteArrayToInt() throws Exception {
        byte[] ba;

        ba = new byte[] { 0, 0 };
        assertEquals(0, TCPClientDecorator.byteArrayToInt(ba));

        ba = new byte[] { 0, 15 };
        assertEquals(15, TCPClientDecorator.byteArrayToInt(ba));

        ba = new byte[] { 0, -1 };
        assertEquals(255, TCPClientDecorator.byteArrayToInt(ba));

        ba = new byte[] { 1, 0 };
        assertEquals(256, TCPClientDecorator.byteArrayToInt(ba));

        ba = new byte[] { -1, -1 };
        assertEquals(-1, TCPClientDecorator.byteArrayToInt(ba));

        ba = new byte[] { 0, 0, -1, -1 };
        assertEquals(65535, TCPClientDecorator.byteArrayToInt(ba));

        ba = new byte[] { 0, 1, 0, 0 };
        assertEquals(65536, TCPClientDecorator.byteArrayToInt(ba));
        
        ba = new byte[] { 0, 0, 0, 0 };
        assertEquals(0, TCPClientDecorator.byteArrayToInt(ba));

        ba = new byte[] { -128, 0, 0, 0 };
        assertEquals(Integer.MIN_VALUE, TCPClientDecorator.byteArrayToInt(ba));

        ba = new byte[] { 127, -1, -1, -1 };
        assertEquals(Integer.MAX_VALUE, TCPClientDecorator.byteArrayToInt(ba));
    }

    
    public void testLoopBack() throws Exception {
        assertEquals(TCPClientDecorator.SHORT_MIN_VALUE, TCPClientDecorator.byteArrayToInt(TCPClientDecorator.intToByteArray(TCPClientDecorator.SHORT_MIN_VALUE, 2)));      
        assertEquals(TCPClientDecorator.SHORT_MAX_VALUE, TCPClientDecorator.byteArrayToInt(TCPClientDecorator.intToByteArray(TCPClientDecorator.SHORT_MAX_VALUE, 2)));      
        assertEquals(Integer.MIN_VALUE, TCPClientDecorator.byteArrayToInt(TCPClientDecorator.intToByteArray(Integer.MIN_VALUE, 4)));      
        assertEquals(Integer.MAX_VALUE, TCPClientDecorator.byteArrayToInt(TCPClientDecorator.intToByteArray(Integer.MAX_VALUE, 4)));      
    }

}

@@ -1959,33 +1959,14 @@
 ray(
-TCPClientDecorator.SHORT_
+Short.
 MAX_
@@ -2143,33 +2143,14 @@
 ray(
-TCPClientDecorator.SHORT_
+Short.
 MIN_
@@ -2345,33 +2345,14 @@
 ray(
-TCPClientDecorator.SHORT_
+Short.
 MIN_
@@ -2523,33 +2523,14 @@
 ray(
-TCPClientDecorator.SHORT_
+Short.
 MAX_
@@ -5967,33 +5967,14 @@
 als(
-TCPClientDecorator.SHORT_
+Short.
 MIN_
@@ -6052,33 +6052,14 @@
 ray(
-TCPClientDecorator.SHORT_
+Short.
 MIN_
@@ -6102,33 +6102,14 @@
 als(
-TCPClientDecorator.SHORT_
+Short.
 MAX_
@@ -6187,33 +6187,14 @@
 ray(
-TCPClientDecorator.SHORT_
+Short.
 MAX_

Replace local constants with ones from Short class