Skip to content

Commit

Permalink
add OrderItem test case for #557
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Jan 17, 2018
1 parent 87a54b0 commit e9935ba
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,17 @@ public boolean equals(final Object obj) {
return type == orderItem.getType() && (columnLabelEquals(orderItem) || qualifiedNameEquals(orderItem) || indexEquals(orderItem));
}

private boolean columnLabelEquals(final OrderItem orderItem) {
String columnLabel = getColumnLabel();
return null != columnLabel && columnLabel.equalsIgnoreCase(orderItem.getColumnLabel());
}

private boolean qualifiedNameEquals(final OrderItem orderItem) {
Optional<String> thisQualifiedName = getQualifiedName();
Optional<String> thatQualifiedName = orderItem.getQualifiedName();
return thisQualifiedName.isPresent() && thatQualifiedName.isPresent() && thisQualifiedName.get().equalsIgnoreCase(thatQualifiedName.get());
}

private boolean columnLabelEquals(final OrderItem orderItem) {
String columnLabel = getColumnLabel();
return null != columnLabel && columnLabel.equalsIgnoreCase(orderItem.getColumnLabel());
}

private boolean indexEquals(final OrderItem orderItem) {
return -1 != index && index == orderItem.getIndex();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import io.shardingjdbc.core.parsing.lexer.AllLexerTests;
import io.shardingjdbc.core.parsing.lexer.analyzer.TokenizerTest;
import io.shardingjdbc.core.parsing.parser.context.OrderItemTest;
import io.shardingjdbc.core.parsing.parser.sql.AllStatementParserTests;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
Expand All @@ -30,7 +31,8 @@
AllStatementParserTests.class,
SQLParsingEngineTest.class,
UnsupportedSQLParsingEngineTest.class,
SQLJudgeEngineTest.class
SQLJudgeEngineTest.class,
OrderItemTest.class
})
public class AllParsingTests {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright 1999-2015 dangdang.com.
* <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.shardingjdbc.core.parsing.parser.context;

import com.google.common.base.Optional;
import io.shardingjdbc.core.constant.OrderType;
import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public final class OrderItemTest {

@Test
public void assertGetColumnLabelWithoutAlias() {
OrderItem actualOrderItem = new OrderItem("column_name", OrderType.ASC, OrderType.ASC, Optional.<String>absent());
assertThat(actualOrderItem.getColumnLabel(), is("column_name"));
}

@Test
public void assertGetColumnLabelWithAlias() {
OrderItem actualOrderItem = new OrderItem("column_name", OrderType.ASC, OrderType.ASC, Optional.of("column_alias"));
assertThat(actualOrderItem.getColumnLabel(), is("column_alias"));
}

@Test
public void assertGetColumnLabelWithIndex() {
OrderItem actualOrderItem = new OrderItem(1, OrderType.ASC, OrderType.ASC);
assertNull(actualOrderItem.getColumnLabel());
}

@Test
public void assertGetQualifiedNameWithoutName() {
OrderItem actualOrderItem = new OrderItem(1, OrderType.ASC, OrderType.ASC);
assertNull(actualOrderItem.getQualifiedName().orNull());
}

@Test
public void assertGetColumnLabelWithOutOwner() {
OrderItem actualOrderItem = new OrderItem("column_name", OrderType.ASC, OrderType.ASC, Optional.of("column_alias"));
assertThat(actualOrderItem.getQualifiedName().get(), is("column_name"));
}

@Test
public void assertGetColumnLabelWithOwner() {
OrderItem actualOrderItem = new OrderItem("tbl", "column_name", OrderType.ASC, OrderType.ASC, Optional.of("column_alias"));
assertThat(actualOrderItem.getQualifiedName().get(), is("tbl.column_name"));
}

@SuppressWarnings("ObjectEqualsNull")
@Test
public void assertEqualsWithNull() {
assertFalse(new OrderItem(1, OrderType.ASC, OrderType.ASC).equals(null));
}

@Test
public void assertEqualsWithOtherObject() {
assertFalse(new OrderItem(1, OrderType.ASC, OrderType.ASC).equals(new Object()));
}

@Test
public void assertEqualsWithDifferentOrderType() {
assertFalse(new OrderItem(1, OrderType.ASC, OrderType.ASC).equals(new OrderItem(1, OrderType.DESC, OrderType.ASC)));
}

@Test
public void assertEqualsWithSameColumnLabel() {
assertTrue(new OrderItem("column_name", OrderType.ASC, OrderType.ASC, Optional.of("column_alias"))
.equals(new OrderItem("tbl", "column_name", OrderType.ASC, OrderType.ASC, Optional.of("column_alias"))));
}

@Test
public void assertEqualsWithSameQualifiedName() {
assertTrue(new OrderItem("tbl", "column_name", OrderType.ASC, OrderType.ASC, Optional.of("column_alias"))
.equals(new OrderItem("tbl", "column_name", OrderType.ASC, OrderType.ASC, Optional.<String>absent())));
}

@Test
public void assertEqualsWithSameIndex() {
assertTrue(new OrderItem(1, OrderType.ASC, OrderType.ASC).equals(new OrderItem(1, OrderType.ASC, OrderType.ASC)));
}

@Test
public void assertNotEquals() {
assertFalse(new OrderItem("tbl", "column_name", OrderType.ASC, OrderType.ASC, Optional.of("column_alias"))
.equals(new OrderItem("column_name", OrderType.ASC, OrderType.ASC, Optional.<String>absent())));
}
}

0 comments on commit e9935ba

Please sign in to comment.