From b06e7e60f9152d0d323884818d8c9a9243310051 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 30 Jun 2023 14:38:31 +0100 Subject: [PATCH] Polish "Fix asymmetry of equals when element has trailing dashes" See gh-34804 --- .../source/ConfigurationPropertyName.java | 19 +++++++++++++++++-- .../ConfigurationPropertyNameTests.java | 15 +++++++-------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java index a1a3d6b7b1b0..6713e2f2a1eb 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -406,7 +406,7 @@ private boolean dashIgnoringElementEquals(Elements e1, Elements e2, int i) { int i2 = 0; while (i1 < l1) { if (i2 >= l2) { - return remainderIsNotAlphanumeric(e1, i, i1); + return remainderIsDashes(e1, i, i1); } char ch1 = e1.charAt(i, i1); char ch2 = e2.charAt(i, i2); @@ -487,6 +487,21 @@ private boolean remainderIsNotAlphanumeric(Elements elements, int element, int i return true; } + private boolean remainderIsDashes(Elements elements, int element, int index) { + if (elements.getType(element).isIndexed()) { + return false; + } + int length = elements.getLength(element); + do { + char c = Character.toLowerCase(elements.charAt(element, index++)); + if (c != '-') { + return false; + } + } + while (index < length); + return true; + } + @Override public int hashCode() { int hashCode = this.hashCode; diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertyNameTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertyNameTests.java index 2cf37b77a79d..5c29beedebfb 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertyNameTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertyNameTests.java @@ -693,21 +693,20 @@ void equalsWhenNameStartsTheSameUsingDashedCompare() { assertThat(n2).isNotEqualTo(n1); } - @Test - void equalsWhenAdaptedNameMatchesDueToRemovalOfTrailingCharacters() { - // gh-30317 + @Test // gh-30317 + void equalsWhenAdaptedNameMatchesDueToRemovalOfTrailingNonUniformCharacters() { ConfigurationPropertyName name1 = ConfigurationPropertyName.of("example.demo"); ConfigurationPropertyName name2 = ConfigurationPropertyName.adapt("example.demo$$", '.'); assertThat(name1).isEqualTo(name2); assertThat(name2).isEqualTo(name1); } - @Test - void equalsSymmetricWhenNameMatchesDueToRemovalOfTrailingDashes() { - ConfigurationPropertyName n1 = ConfigurationPropertyName.of("foobar"); - ConfigurationPropertyName n2 = ConfigurationPropertyName.of("foobar--"); - assertThat(n1).isEqualTo(n2); + @Test // gh-34804 + void equalsSymmetricWhenNameMatchesDueToIgnoredTrailingDashes() { + ConfigurationPropertyName n1 = ConfigurationPropertyName.of("example.demo"); + ConfigurationPropertyName n2 = ConfigurationPropertyName.of("example.demo--"); assertThat(n2).isEqualTo(n1); + assertThat(n1).isEqualTo(n2); } @Test