/**
 * 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.
 */
package io.hawtjms.stomp;

import static io.hawtjms.provider.stomp.StompConstants.CONNECTED;
import static io.hawtjms.provider.stomp.StompConstants.V1_0;
import static io.hawtjms.provider.stomp.StompConstants.V1_1;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import io.hawtjms.provider.stomp.StompCodec;
import io.hawtjms.provider.stomp.StompConstants;
import io.hawtjms.provider.stomp.StompFrame;

import java.io.IOException;
import java.nio.ByteBuffer;

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

/**
 * Tests for the STOMP Codec class to validate encode / decode integrity.
 */
public class StompCodecTest {

    private StompCodec codec;

    @Before
    public void setUp() {
        codec = new StompCodec();
    }

    @After
    public void tearDown() {
        codec = null;
    }

    @Test
    public void testContructor() {
        assertEquals(V1_0, codec.getVersion());
        assertEquals(20, codec.getMaxCommandLength());

        codec.setVersion(V1_1);
        assertEquals(V1_1, codec.getVersion());
    }

    @Test
    public void testSimpleCommandDecode() throws IOException {
        String input = "CONNECTED\n" +
                       "version:1.1\n" +
                       "server:activemq/5.9\n\n" +
                       StompConstants.NULL;

        ByteBuffer buffer = ByteBuffer.wrap(input.getBytes(StompConstants.UTF8));

        StompFrame frame = codec.decode(buffer);
        assertNotNull(frame);
        assertEquals(CONNECTED, frame.getCommand());
        assertEquals(V1_1, frame.getProperty("version"));
        assertEquals("activemq/5.9", frame.getProperty("server"));
    }

    @Test
    public void testSimpleCommandEncode() throws IOException {
        StompFrame frame = new StompFrame("CONNECTED");
        frame.setProperty("version", V1_1);
        frame.setProperty("server", "activemq/5.9");

        ByteBuffer encoded = codec.encode(frame);
        assertNotNull(encoded);
        assertTrue(encoded.position() == 0);
        assertTrue(encoded.limit() > 0);
    }

    @Test
    public void testSimpleCommandEncodeAndDecode() throws IOException {
        StompFrame frame = new StompFrame("CONNECTED");
        frame.setProperty("version", V1_1);
        frame.setProperty("server", "activemq/5.9");

        ByteBuffer encoded = codec.encode(frame);
        assertNotNull(encoded);
        assertTrue(encoded.position() == 0);
        assertTrue(encoded.limit() > 0);

        frame = codec.decode(encoded);
        assertNotNull(frame);
        assertEquals(CONNECTED, frame.getCommand());
        assertEquals(V1_1, frame.getProperty("version"));
        assertEquals("activemq/5.9", frame.getProperty("server"));
    }
}

@@ -890,16 +890,80 @@
 NECTED;%0A
+import static io.hawtjms.provider.stomp.StompConstants.MESSAGE;%0A
 import s
@@ -2596,32 +2596,732 @@
 rver%22));%0A    %7D%0A%0A
+    @Test%0A    public void testDecodeTextMessage() throws IOException %7B%0A        final String body = %22This is a test%22;%0A%0A        String input = %22MESSAGE%5Cn%22 +%0A                       %22version:1.1%5Cn%22 +%0A                       %22server:activemq/5.9%5Cn%5Cn%22 +%0A                       body + StompConstants.NULL;%0A%0A        ByteBuffer buffer = ByteBuffer.wrap(input.getBytes(StompConstants.UTF8));%0A%0A        StompFrame frame = codec.decode(buffer);%0A        assertNotNull(frame);%0A        assertEquals(MESSAGE, frame.getCommand());%0A        assertEquals(V1_1, frame.getProperty(%22version%22));%0A        assertEquals(%22activemq/5.9%22, frame.getProperty(%22server%22));%0A        assertEquals(body, frame.getContentAsString());%0A    %7D%0A%0A
     @Test%0A    pu

Test for body encoding.