|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2019 the original author or authors. |
| 2 | + * Copyright 2002-2020 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
@@ -288,10 +288,72 @@ void malformedExpressions() {
|
288 | 288 |
|
289 | 289 | @Test
|
290 | 290 | void sensibleToString() {
|
291 |
| - assertThat(Profiles.of("spring & framework", "java | kotlin").toString()).isEqualTo("spring & framework or java | kotlin"); |
| 291 | + assertThat(Profiles.of("spring")).hasToString("spring"); |
| 292 | + assertThat(Profiles.of("(spring & framework) | (spring & java)")).hasToString("(spring & framework) | (spring & java)"); |
| 293 | + assertThat(Profiles.of("(spring&framework)|(spring&java)")).hasToString("(spring&framework)|(spring&java)"); |
| 294 | + assertThat(Profiles.of("spring & framework", "java | kotlin")).hasToString("spring & framework or java | kotlin"); |
| 295 | + assertThat(Profiles.of("java | kotlin", "spring & framework")).hasToString("java | kotlin or spring & framework"); |
292 | 296 | }
|
293 | 297 |
|
294 |
| - private void assertMalformed(Supplier<Profiles> supplier) { |
| 298 | + @Test |
| 299 | + void sensibleEquals() { |
| 300 | + assertEqual("(spring & framework) | (spring & java)"); |
| 301 | + assertEqual("(spring&framework)|(spring&java)"); |
| 302 | + assertEqual("spring & framework", "java | kotlin"); |
| 303 | + |
| 304 | + // Ensure order of individual expressions does not affect equals(). |
| 305 | + String expression1 = "A | B"; |
| 306 | + String expression2 = "C & (D | E)"; |
| 307 | + Profiles profiles1 = Profiles.of(expression1, expression2); |
| 308 | + Profiles profiles2 = Profiles.of(expression2, expression1); |
| 309 | + assertThat(profiles1).isEqualTo(profiles2); |
| 310 | + assertThat(profiles2).isEqualTo(profiles1); |
| 311 | + } |
| 312 | + |
| 313 | + private void assertEqual(String... expressions) { |
| 314 | + Profiles profiles1 = Profiles.of(expressions); |
| 315 | + Profiles profiles2 = Profiles.of(expressions); |
| 316 | + assertThat(profiles1).isEqualTo(profiles2); |
| 317 | + assertThat(profiles2).isEqualTo(profiles1); |
| 318 | + } |
| 319 | + |
| 320 | + @Test |
| 321 | + void sensibleHashCode() { |
| 322 | + assertHashCode("(spring & framework) | (spring & java)"); |
| 323 | + assertHashCode("(spring&framework)|(spring&java)"); |
| 324 | + assertHashCode("spring & framework", "java | kotlin"); |
| 325 | + |
| 326 | + // Ensure order of individual expressions does not affect hashCode(). |
| 327 | + String expression1 = "A | B"; |
| 328 | + String expression2 = "C & (D | E)"; |
| 329 | + Profiles profiles1 = Profiles.of(expression1, expression2); |
| 330 | + Profiles profiles2 = Profiles.of(expression2, expression1); |
| 331 | + assertThat(profiles1).hasSameHashCodeAs(profiles2); |
| 332 | + } |
| 333 | + |
| 334 | + private void assertHashCode(String... expressions) { |
| 335 | + Profiles profiles1 = Profiles.of(expressions); |
| 336 | + Profiles profiles2 = Profiles.of(expressions); |
| 337 | + assertThat(profiles1).hasSameHashCodeAs(profiles2); |
| 338 | + } |
| 339 | + |
| 340 | + @Test |
| 341 | + void equalsAndHashCodeAreNotBasedOnLogicalStructureOfNodesWithinExpressionTree() { |
| 342 | + Profiles profiles1 = Profiles.of("A | B"); |
| 343 | + Profiles profiles2 = Profiles.of("B | A"); |
| 344 | + |
| 345 | + assertThat(profiles1.matches(activeProfiles("A"))).isTrue(); |
| 346 | + assertThat(profiles1.matches(activeProfiles("B"))).isTrue(); |
| 347 | + assertThat(profiles2.matches(activeProfiles("A"))).isTrue(); |
| 348 | + assertThat(profiles2.matches(activeProfiles("B"))).isTrue(); |
| 349 | + |
| 350 | + assertThat(profiles1).isNotEqualTo(profiles2); |
| 351 | + assertThat(profiles2).isNotEqualTo(profiles1); |
| 352 | + assertThat(profiles1.hashCode()).isNotEqualTo(profiles2.hashCode()); |
| 353 | + } |
| 354 | + |
| 355 | + |
| 356 | + private static void assertMalformed(Supplier<Profiles> supplier) { |
295 | 357 | assertThatIllegalArgumentException().isThrownBy(
|
296 | 358 | supplier::get)
|
297 | 359 | .withMessageContaining("Malformed");
|
|
0 commit comments