Polishing.

Original pull request #1901
This commit is contained in:
Jens Schauder
2024-09-30 11:56:57 +02:00
parent 08daf47842
commit 4d5a382a73
2 changed files with 20 additions and 11 deletions

View File

@@ -24,7 +24,6 @@ import java.util.Map;
import java.util.Objects;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
@@ -60,15 +59,15 @@ public final class Identifier {
/**
* Creates an {@link Identifier} from {@code name}, {@code value}, and a {@link Class target type}.
*
* @param name must not be {@literal null} or empty.
* @param value must not be null
* @param name must not be {@literal null}.
* @param value must not be {@literal null}.
* @param targetType must not be {@literal null}.
* @return the {@link Identifier} for {@code name}, {@code value}, and a {@link Class target type}.
*/
public static Identifier of(SqlIdentifier name, Object value, Class<?> targetType) {
Assert.notNull(name, "Name must not be empty");
Assert.notNull(value, "Value must not be empty");
Assert.notNull(name, "Name must not be null");
Assert.notNull(value, "Value must not be null");
Assert.notNull(targetType, "Target type must not be null");
return new Identifier(Collections.singletonList(new SingleIdentifierValue(name, value, targetType)));
@@ -92,7 +91,8 @@ public final class Identifier {
map.forEach((k, v) -> {
Assert.notNull(v, "The source map for identifier cannot contain null values");
Assert.notNull(v, "The source map for identifier must not contain null values");
values.add(new SingleIdentifierValue(k, v, ClassUtils.getUserClass(v)));
});

View File

@@ -47,19 +47,28 @@ public class IdentifierUnitTests {
}
@Test // DATAJDBC-326
public void parametersWithStringKeysUseObjectAsTypeForNull() {
public void typeIsCalculatedCorrectly() {
HashMap<SqlIdentifier, Object> parameters = new HashMap<>();
Object value = new Object();
Object objectValue = new Object();
Object stringValue = "text";
Object intValue = 23;
Object integerValue = 42;
parameters.put(unquoted("one"), value);
parameters.put(unquoted("one"), objectValue);
parameters.put(unquoted("two"), stringValue);
parameters.put(unquoted("three"), intValue);
parameters.put(unquoted("four"), integerValue);
Identifier identifier = Identifier.from(parameters);
assertThat(identifier.getParts()) //
.extracting("name", "value", "targetType") //
.containsExactly( //
Assertions.tuple(unquoted("one"), value, Object.class) //
.containsExactlyInAnyOrder( //
Assertions.tuple(unquoted("one"), objectValue, Object.class), //
Assertions.tuple(unquoted("two"), stringValue, String.class), //
Assertions.tuple(unquoted("three"), intValue, Integer.class), //
Assertions.tuple(unquoted("four"), integerValue, Integer.class) //
);
}