|
| 1 | +/* |
| 2 | + * Copyright 2026 Flamingock (https://www.flamingock.io) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.flamingock.internal.core.context; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.DisplayName; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import java.util.Optional; |
| 22 | + |
| 23 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 24 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 25 | + |
| 26 | +/** |
| 27 | + * Tests proving that {@link PriorityContextResolver} fails to delegate property resolution |
| 28 | + * to the base context when that context keeps dependencies and properties in separate stacks. |
| 29 | + * <p> |
| 30 | + * The bug: {@code getPropertyAs()} routes through {@code getDependencyValue(name, type)} which calls |
| 31 | + * {@code getDependency(name)}, never reaching the base context's {@code getProperty()}/{@code getPropertyAs()}. |
| 32 | + * This works for {@link SimpleContext} (which stores properties as dependencies) but breaks for any |
| 33 | + * {@code ContextResolver} that separates the two (like {@code SpringbootDependencyContext}). |
| 34 | + */ |
| 35 | +class PriorityContextResolverPropertyTest { |
| 36 | + |
| 37 | + @Test |
| 38 | + @DisplayName("Should resolve property from base context when property is in a separate stack (not a dependency)") |
| 39 | + void shouldResolvePropertyFromBaseContext_whenPropertyInSeparateStack() { |
| 40 | + // Given: a base context that stores properties separately from dependencies |
| 41 | + SplitStackContextResolver splitStackContext = new SplitStackContextResolver(); |
| 42 | + splitStackContext.addProperty("timeout", "5000"); |
| 43 | + |
| 44 | + // When: wrapped in PriorityContextResolver as the base context |
| 45 | + PriorityContextResolver priorityResolver = new PriorityContextResolver(new SimpleContext(), splitStackContext); |
| 46 | + |
| 47 | + // Then: the property should be resolvable |
| 48 | + Optional<String> result = priorityResolver.getProperty("timeout"); |
| 49 | + assertTrue(result.isPresent(), "Property 'timeout' should be present but was empty"); |
| 50 | + assertEquals("5000", result.get()); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + @DisplayName("Should resolve both dependency and property from base context when both exist in separate stacks") |
| 55 | + void shouldResolvePropertyFromBaseContext_whenDependencyAlsoExists() { |
| 56 | + // Given: a base context with both a dependency (bean) and a property in separate stacks |
| 57 | + SplitStackContextResolver splitStackContext = new SplitStackContextResolver(); |
| 58 | + splitStackContext.addDependency("myService", Runnable.class, (Runnable) () -> {}); |
| 59 | + splitStackContext.addProperty("timeout", "5000"); |
| 60 | + |
| 61 | + PriorityContextResolver priorityResolver = new PriorityContextResolver(new SimpleContext(), splitStackContext); |
| 62 | + |
| 63 | + // Sanity: the dependency IS resolvable through the priority context |
| 64 | + assertTrue(priorityResolver.getDependency(Runnable.class).isPresent(), |
| 65 | + "Dependency should be resolvable through PriorityContextResolver"); |
| 66 | + |
| 67 | + // Then: the property should also be resolvable |
| 68 | + Optional<String> result = priorityResolver.getProperty("timeout"); |
| 69 | + assertTrue(result.isPresent(), "Property 'timeout' should be present but was empty"); |
| 70 | + assertEquals("5000", result.get()); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + @DisplayName("Should resolve property from priority context when set via SimpleContext.setProperty (control test)") |
| 75 | + void shouldResolvePropertyFromPriorityContext_whenPropertySetAsProperty() { |
| 76 | + // Given: a SimpleContext (priority) with a property set directly |
| 77 | + // In SimpleContext, setProperty stores it as a dependency, so getDependencyValue works |
| 78 | + SimpleContext simpleContext = new SimpleContext(); |
| 79 | + simpleContext.setProperty("timeout", "5000"); |
| 80 | + |
| 81 | + PriorityContextResolver priorityResolver = new PriorityContextResolver(simpleContext, new SimpleContext()); |
| 82 | + |
| 83 | + // Then: this DOES work because SimpleContext stores properties as dependencies |
| 84 | + Optional<String> result = priorityResolver.getProperty("timeout"); |
| 85 | + assertTrue(result.isPresent(), "Property 'timeout' should be present"); |
| 86 | + assertEquals("5000", result.get()); |
| 87 | + } |
| 88 | +} |
0 commit comments