diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/identifier/ShardingSphereIdentifier.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/identifier/ShardingSphereIdentifier.java index 7f54a7bad7eb6..f06d08f6e6595 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/identifier/ShardingSphereIdentifier.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/identifier/ShardingSphereIdentifier.java @@ -38,8 +38,7 @@ public ShardingSphereIdentifier(final String value) { } public ShardingSphereIdentifier(final String value, final DatabaseType databaseType) { - isCaseSensitive = DatabaseTypedSPILoader.findService(DatabaseDialectIdentifierHandler.class, databaseType) - .map(DatabaseDialectIdentifierHandler::isCaseSensitive).orElse(false); + isCaseSensitive = DatabaseTypedSPILoader.findService(DatabaseDialectIdentifierHandler.class, databaseType).map(DatabaseDialectIdentifierHandler::isCaseSensitive).orElse(false); this.value = new CaseInsensitiveString(value); } @@ -68,12 +67,15 @@ public boolean equals(final Object obj) { if (!(obj instanceof ShardingSphereIdentifier)) { return false; } - return isCaseSensitive ? value.toString().equals(((ShardingSphereIdentifier) obj).value.toString()) : value.equals(((ShardingSphereIdentifier) obj).value); + if (null == getValue() && null == ((ShardingSphereIdentifier) obj).getValue()) { + return true; + } + return isCaseSensitive ? String.valueOf(getValue()).equals(((ShardingSphereIdentifier) obj).getValue()) : value.equals(((ShardingSphereIdentifier) obj).value); } @Override public int hashCode() { - return isCaseSensitive ? value.toString().hashCode() : value.hashCode(); + return isCaseSensitive ? String.valueOf(getValue()).hashCode() : value.hashCode(); } @Override diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/identifier/ShardingSphereIdentifierTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/identifier/ShardingSphereIdentifierTest.java new file mode 100644 index 0000000000000..791194b6728a8 --- /dev/null +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/identifier/ShardingSphereIdentifierTest.java @@ -0,0 +1,72 @@ +/* + * 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.infra.metadata.identifier; + +import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.MatcherAssert.assertThat; + +class ShardingSphereIdentifierTest { + + @Test + void assertEqualsWithNotShardingSphereIdentifier() { + assertThat(new ShardingSphereIdentifier("foo"), not(new Object())); + } + + @Test + void assertEqualsWithNullValue() { + assertThat(new ShardingSphereIdentifier("foo"), not(new ShardingSphereIdentifier((String) null))); + assertThat(new ShardingSphereIdentifier((String) null), not(new ShardingSphereIdentifier("foo"))); + assertThat(new ShardingSphereIdentifier((String) null), is(new ShardingSphereIdentifier((String) null))); + } + + @Test + void assertEqualsWithCaseSensitive() { + assertThat(new ShardingSphereIdentifier(new IdentifierValue("`foo`")), is(new ShardingSphereIdentifier(new IdentifierValue("`foo`")))); + assertThat(new ShardingSphereIdentifier(new IdentifierValue("`foo`")), is(new ShardingSphereIdentifier(new IdentifierValue("foo")))); + assertThat(new ShardingSphereIdentifier(new IdentifierValue("`foo`")), not(new ShardingSphereIdentifier("FOO"))); + } + + @Test + void assertEqualsWithCaseInsensitive() { + assertThat(new ShardingSphereIdentifier("foo"), is(new ShardingSphereIdentifier("foo"))); + assertThat(new ShardingSphereIdentifier("foo"), is(new ShardingSphereIdentifier("FOO"))); + } + + @Test + void assertHashCodeWithCaseSensitive() { + assertThat(new ShardingSphereIdentifier(new IdentifierValue("`foo`")).hashCode(), is(new ShardingSphereIdentifier(new IdentifierValue("`foo`")).hashCode())); + assertThat(new ShardingSphereIdentifier(new IdentifierValue("`foo`")).hashCode(), not(new ShardingSphereIdentifier(new IdentifierValue("`FOO`")).hashCode())); + } + + @Test + void assertHashCodeWithCaseInsensitive() { + assertThat(new ShardingSphereIdentifier("foo").hashCode(), is(new ShardingSphereIdentifier("foo").hashCode())); + assertThat(new ShardingSphereIdentifier("foo").hashCode(), is(new ShardingSphereIdentifier("FOO").hashCode())); + } + + @Test + void assertToString() { + assertThat(new ShardingSphereIdentifier("foo").toString(), is("foo")); + assertThat(new ShardingSphereIdentifier("FOO").toString(), is("FOO")); + assertThat(new ShardingSphereIdentifier(new IdentifierValue("`foo`")).toString(), is("foo")); + } +}