/*
 * Copyright 2014-2017 Real Logic Ltd.
 *
 * 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 io.aeron.samples;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.agrona.concurrent.UnsafeBuffer;

import java.util.Arrays;

@RunWith(Parameterized.class)
public class LogInspectorAsciiFormatBytesTest
{
    private static final String FORMAT_KEY = "aeron.log.inspector.data.format";
    private String originalDataFormatProperty;

    private byte buffer;
    private char expected;

    public LogInspectorAsciiFormatBytesTest(final int buffer, final int expected)
    {
        this.buffer = (byte)buffer;
        this.expected = (char)expected;
    }

    @Parameters(name = "{index}: ascii format[{0}]={1}")
    public static Iterable<Object[]> data()
    {
        return Arrays.asList(new Object[][]
            {
                { 0x17, 0x17 },
                { 0, 0 },
                { -1, 0 },
                { Byte.MAX_VALUE, Byte.MAX_VALUE },
                { Byte.MIN_VALUE, 0 },
            });
    }

    @Test
    public void shouldFormatBytesToAscii()
    {
        System.setProperty(FORMAT_KEY, "ascii");
        final char[] formattedBytes = LogInspector.formatBytes(new UnsafeBuffer(new byte[]{ buffer }), 0, 1);

        Assert.assertEquals(expected, formattedBytes[0]);
    }

    @Before
    public void setUp()
    {
        originalDataFormatProperty = System.getProperty(FORMAT_KEY);
    }

    @After
    public void tearDown()
    {
        if (null == originalDataFormatProperty)
        {
            System.clearProperty(FORMAT_KEY);
        }
        else
        {
            System.setProperty(FORMAT_KEY, originalDataFormatProperty);
        }
    }
}
@@ -1710,20 +1710,22 @@
 %7D%0A%0A    @
-Test
+Before
 %0A    pub
@@ -1737,299 +1737,14 @@
 oid 
-shouldFormatBytesToAscii()%0A    %7B%0A        System.setProperty(FORMAT_KEY, %22ascii%22);%0A        final char%5B%5D formattedBytes = LogInspector.formatBytes(new UnsafeBuffer(new byte%5B%5D%7B buffer %7D), 0, 1);%0A%0A        Assert.assertEquals(expected, formattedBytes%5B0%5D);%0A    %7D%0A%0A    @Before%0A    public void setUp
+before
 ()%0A 
@@ -1855,16 +1855,13 @@
 oid 
-tearDown
+after
 ()%0A 
@@ -2074,16 +2074,16 @@
 perty);%0A
-
         
@@ -2089,10 +2089,294 @@
  %7D%0A    %7D
+%0A%0A    @Test%0A    public void shouldFormatBytesToAscii()%0A    %7B%0A        System.setProperty(FORMAT_KEY, %22ascii%22);%0A        final char%5B%5D formattedBytes = LogInspector.formatBytes(new UnsafeBuffer(new byte%5B%5D%7B buffer %7D), 0, 1);%0A%0A        Assert.assertEquals(expected, formattedBytes%5B0%5D);%0A    %7D
 %0A%7D

Test consistency.