/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * 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.
 * </p>
 */

package io.shardingsphere.proxy.transport.mysql.packet.command.query.binary;

import org.junit.Test;

import java.lang.reflect.Field;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public final class BinaryStatementRegistryTest {
    
    private final String sql = "SELECT * FROM tbl WHERE id=?";
    
    @Test
    public void setUp() throws ReflectiveOperationException {
        Field statementIdAssignerField = BinaryStatementRegistry.class.getDeclaredField("statementIdAssigner");
        statementIdAssignerField.setAccessible(true);
        ((Map) statementIdAssignerField.get(BinaryStatementRegistry.getInstance())).clear();
        Field binaryStatementsField = BinaryStatementRegistry.class.getDeclaredField("binaryStatements");
        binaryStatementsField.setAccessible(true);
        ((Map) binaryStatementsField.get(BinaryStatementRegistry.getInstance())).clear();
        Field sequenceField = BinaryStatementRegistry.class.getDeclaredField("sequence");
        sequenceField.setAccessible(true);
        ((AtomicInteger) sequenceField.get(BinaryStatementRegistry.getInstance())).set(0);
    }
    
    @Test
    public void assertRegisterIfAbsent() {
        assertThat(BinaryStatementRegistry.getInstance().register(sql, 1), is(1));
        BinaryStatement actual = BinaryStatementRegistry.getInstance().getBinaryStatement(1);
        assertThat(actual.getSql(), is(sql));
        assertThat(actual.getParametersCount(), is(1));
    }
    
    @Test
    public void assertRegisterIfPresent() {
        assertThat(BinaryStatementRegistry.getInstance().register(sql, 1), is(1));
        assertThat(BinaryStatementRegistry.getInstance().register(sql, 1), is(1));
        BinaryStatement actual = BinaryStatementRegistry.getInstance().getBinaryStatement(1);
        assertThat(actual.getSql(), is(sql));
        assertThat(actual.getParametersCount(), is(1));
    }
}

@@ -691,16 +691,41 @@
 inary;%0A%0A
+import org.junit.Before;%0A
 import o
@@ -1052,36 +1052,38 @@
 d=?%22;%0A    %0A    @
-Test
+Before
 %0A    public void
@@ -1859,16 +1859,49 @@
 set(0);%0A
+        System.out.println(111);%0A
     %7D%0A  

add BinaryStatementRegistryTest