未验证 提交 45d0cc11 编写于 作者: Y Yanjie Zhou 提交者: GitHub

add test cases (#7030)

* add test cases

* add test cases

* add test cases
上级 245c4072
/*
* 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 org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.bind.protocol;
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public final class PostgreSQLInt2BinaryProtocolValueTest {
@Mock
private PostgreSQLPacketPayload payload;
@Test
public void assertNewInstance() {
PostgreSQLInt2BinaryProtocolValue actual = new PostgreSQLInt2BinaryProtocolValue();
assertThat(actual.getColumnLength(null), equalTo(2));
when(payload.readInt2()).thenReturn(1);
assertThat(actual.read(payload), is(1));
actual.write(payload, 1);
verify(payload).writeInt2(1);
}
}
/*
* 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 org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.bind.protocol;
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public final class PostgreSQLInt4BinaryProtocolValueTest {
@Mock
private PostgreSQLPacketPayload payload;
@Test
public void assertNewInstance() {
PostgreSQLInt4BinaryProtocolValue actual = new PostgreSQLInt4BinaryProtocolValue();
assertThat(actual.getColumnLength(null), equalTo(4));
when(payload.readInt4()).thenReturn(1);
assertThat(actual.read(payload), is(1));
actual.write(payload, 1);
verify(payload).writeInt4(1);
}
}
/*
* 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 org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.bind.protocol;
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public final class PostgreSQLInt8BinaryProtocolValueTest {
@Mock
private PostgreSQLPacketPayload payload;
@Test
public void assertNewInstance() {
PostgreSQLInt8BinaryProtocolValue actual = new PostgreSQLInt8BinaryProtocolValue();
assertThat(actual.getColumnLength(null), equalTo(8));
when(payload.readInt8()).thenReturn(1L);
assertThat(actual.read(payload), is(1L));
actual.write(payload, 1L);
verify(payload).writeInt8(1L);
}
}
/*
* 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 org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.bind.protocol;
import io.netty.buffer.ByteBuf;
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public final class PostgreSQLStringBinaryProtocolValueTest {
@InjectMocks
private PostgreSQLPacketPayload payload;
@Mock
private ByteBuf byteBuf;
@Test
public void assertNewInstance() {
when(byteBuf.readerIndex()).thenReturn(8);
doAnswer(new Answer<ByteBuf>() {
@Override
public ByteBuf answer(final InvocationOnMock invocation) throws Throwable {
((byte[]) invocation.getArguments()[0])[0] = 97;
return byteBuf;
}
}).when(byteBuf).readBytes(any(byte[].class));
PostgreSQLStringBinaryProtocolValue actual = new PostgreSQLStringBinaryProtocolValue();
assertThat(actual.getColumnLength("str"), equalTo("str".length()));
when(payload.readInt4()).thenReturn(1);
assertThat(actual.read(payload), equalTo("a"));
verify(byteBuf).readerIndex(4);
actual.write(payload, "a");
verify(byteBuf).writeBytes("a".getBytes());
actual.write(payload, new byte[1]);
verify(byteBuf).writeBytes(new byte[1]);
}
}
/*
* 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 org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.bind.protocol;
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.sql.Timestamp;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public final class PostgreSQLTimeBinaryProtocolValueTest {
@Mock
private PostgreSQLPacketPayload payload;
@Test
public void assertNewInstance() {
PostgreSQLTimeBinaryProtocolValue actual = new PostgreSQLTimeBinaryProtocolValue();
assertThat(actual.getColumnLength(null), equalTo(8));
when(payload.readInt8()).thenReturn(1L);
assertThat(actual.read(payload), is(1L));
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
actual.write(payload, timestamp);
verify(payload).writeInt8(timestamp.getTime());
}
}
/*
* 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 org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.describe;
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class)
public final class PostgreSQLComDescribePacketTest {
@Mock
private PostgreSQLPacketPayload payload;
@Test
public void assertNewInstance() {
PostgreSQLComDescribePacket actual = new PostgreSQLComDescribePacket(payload);
actual.write(payload);
assertThat(actual.getMessageType(), is('D'));
verify(payload).readInt4();
verify(payload).readInt1();
verify(payload).readStringNul();
}
}
/*
* 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 org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.execute;
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class)
public final class PostgreSQLComExecutePacketTest {
@Mock
private PostgreSQLPacketPayload payload;
@Test
public void assertNewInstance() {
PostgreSQLComExecutePacket actual = new PostgreSQLComExecutePacket(payload);
verify(payload, atLeast(2)).readInt4();
verify(payload).readStringNul();
actual.write(payload);
assertThat(actual.getMessageType(), is('E'));
}
}
/*
* 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 org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.parse;
import org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLColumnType;
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.PostgreSQLBinaryStatementParameterType;
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.List;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public final class PostgreSQLComParsePacketTest {
@Mock
private PostgreSQLPacketPayload payload;
@Test
public void assertNewInstance() {
when(payload.readInt2()).thenReturn(1);
when(payload.readInt4()).thenReturn(0);
when(payload.readStringNul()).thenReturn("sql");
PostgreSQLComParsePacket actual = new PostgreSQLComParsePacket(payload);
actual.write(payload);
assertThat(actual.getMessageType(), is('P'));
assertThat(actual.getSql(), is("sql"));
assertThat(actual.getStatementId(), is("sql"));
List<PostgreSQLBinaryStatementParameterType> types = actual.getBinaryStatementParameterTypes();
assertNotNull(types);
assertThat(types.size(), equalTo(1));
assertThat(types.get(0).getColumnType(), is(PostgreSQLColumnType.POSTGRESQL_TYPE_UNSPECIFIED));
assertTrue(actual.toString().startsWith("PostgreSQLComParsePacket(statementId=sql, sql=sql, binaryStatementParameterTypes=["));
}
}
/*
* 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 org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.parse;
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
public final class PostgreSQLParseCompletePacketTest {
@Test
public void assertNewInstance() {
PostgreSQLParseCompletePacket actual = new PostgreSQLParseCompletePacket();
actual.write(mock(PostgreSQLPacketPayload.class));
assertThat(actual.getMessageType(), is('1'));
}
}
/*
* 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 org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.sync;
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class)
public final class PostgreSQLComSyncPacketTest {
@Mock
private PostgreSQLPacketPayload payload;
@Test
public void assertNewInstance() {
PostgreSQLComSyncPacket actual = new PostgreSQLComSyncPacket(payload);
actual.write(payload);
verify(payload).readInt4();
assertThat(actual.getMessageType(), is('S'));
}
}
/*
* 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 org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.text;
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public final class PostgreSQLComQueryPacketTest {
@Mock
private PostgreSQLPacketPayload payload;
@Test
public void assertNewInstance() {
when(payload.readStringNul()).thenReturn("sql");
PostgreSQLComQueryPacket actual = new PostgreSQLComQueryPacket(payload);
actual.write(payload);
verify(payload).readInt4();
assertThat(actual.getSql(), is("sql"));
assertThat(actual.getMessageType(), is('Q'));
assertThat(actual.toString(), is("PostgreSQLComQueryPacket(sql=sql)"));
}
}
/*
* 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 org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.text;
import lombok.SneakyThrows;
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.sql.SQLException;
import java.sql.SQLXML;
import java.util.Collections;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public final class PostgreSQLDataRowPacketTest {
@Mock
private PostgreSQLPacketPayload payload;
@Mock
private SQLXML sqlxml;
@Test
public void assertWriteWithNull() {
PostgreSQLDataRowPacket actual = new PostgreSQLDataRowPacket(Collections.singletonList(null));
actual.write(payload);
verify(payload).writeInt4(0xFFFFFFFF);
}
@Test
public void assertWriteWithBytes() {
PostgreSQLDataRowPacket actual = new PostgreSQLDataRowPacket(Collections.singletonList(new byte[]{'a'}));
actual.write(payload);
verify(payload).writeInt4(new byte[]{'a'}.length);
verify(payload).writeBytes(new byte[]{'a'});
}
@Test
@SneakyThrows
public void assertWriteWithSQLXML() {
when(sqlxml.getString()).thenReturn("string");
PostgreSQLDataRowPacket actual = new PostgreSQLDataRowPacket(Collections.singletonList(sqlxml));
actual.write(payload);
verify(payload).writeInt4("string".getBytes().length);
verify(payload).writeStringEOF("string");
}
@Test
public void assertWriteWithString() {
PostgreSQLDataRowPacket actual = new PostgreSQLDataRowPacket(Collections.singletonList("str"));
assertThat(actual.getData(), is(Collections.singletonList("str")));
actual.write(payload);
verify(payload).writeInt4("str".getBytes().length);
verify(payload).writeStringEOF("str");
}
@Test
@SneakyThrows
public void assertWriteWithSQLXML4Error() {
when(sqlxml.getString()).thenThrow(new SQLException("mock"));
PostgreSQLDataRowPacket actual = new PostgreSQLDataRowPacket(Collections.singletonList(sqlxml));
actual.write(payload);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册