1515 */
1616package io .flamingock .springboot ;
1717
18+ import com .github .cloudyrock .mongock .ChangeSet ;
1819import io .flamingock .api .annotations .Apply ;
1920import io .flamingock .api .annotations .Change ;
2021import io .flamingock .internal .core .change .loaded .CodeLoadedChange ;
@@ -92,6 +93,92 @@ private CodeLoadedChange getCodeLoadedChange(Class<?> sourceClass) {
9293 return LoadedChangeBuilder .getCodeBuilderInstance (sourceClass ).build ();
9394 }
9495
96+ private CodeLoadedChange getLegacyCodeLoadedChange (Class <?> sourceClass ) {
97+ return LoadedChangeBuilder .getCodeBuilderInstance (sourceClass ).setLegacy (true ).build ();
98+ }
99+
100+ // -----------------------------------------------------------------------
101+ // Legacy method @Profile tests — @ChangeSet vs ChangeUnit distinction
102+ // -----------------------------------------------------------------------
103+
104+ // _010__LegacyWithMethodProfile: legacy change setLegacy(true), method has @Profile but
105+ // does NOT carry @ChangeSet (simulates a ChangeUnit/@Execution-style change).
106+ // Method-level @Profile must be IGNORED — fall through to class-level (no class @Profile → unfiltered).
107+
108+ @ Test
109+ @ DisplayName ("SHOULD return true WHEN legacy non-@ChangeSet method has @Profile(P1) and activeProfiles=[P1] — method @Profile is IGNORED" )
110+ void legacyMethodProfileIgnoredWhenActiveProfileMatches () {
111+ assertTrue (new SpringbootProfileFilter ("P1" ).filter (getLegacyCodeLoadedChange (_010__LegacyWithMethodProfile .class )));
112+ }
113+
114+ @ Test
115+ @ DisplayName ("SHOULD return true WHEN legacy non-@ChangeSet method has @Profile(P1) and activeProfiles=[P2] — method @Profile is IGNORED" )
116+ void legacyMethodProfileIgnoredWhenActiveProfileDoesNotMatch () {
117+ // Method-level @Profile is not honored for non-@ChangeSet methods (ChangeUnit-style),
118+ // so fall through to class-level: no class @Profile → unfiltered → true.
119+ assertTrue (new SpringbootProfileFilter ("P2" ).filter (getLegacyCodeLoadedChange (_010__LegacyWithMethodProfile .class )));
120+ }
121+
122+ @ Test
123+ @ DisplayName ("SHOULD return true WHEN legacy class has @Profile(P1) and method has no @Profile, activeProfiles=[P1]" )
124+ void legacyClassProfileFallbackWhenNoMethodProfile () {
125+ assertTrue (new SpringbootProfileFilter ("P1" ).filter (getLegacyCodeLoadedChange (_011__LegacyClassProfileNoMethod .class )));
126+ }
127+
128+ @ Test
129+ @ DisplayName ("SHOULD return false WHEN legacy class has @Profile(P1) and method has no @Profile, activeProfiles=[P2]" )
130+ void legacyClassProfileFallbackWhenNoMethodProfileNotMatching () {
131+ assertFalse (new SpringbootProfileFilter ("P2" ).filter (getLegacyCodeLoadedChange (_011__LegacyClassProfileNoMethod .class )));
132+ }
133+
134+ // -----------------------------------------------------------------------
135+ // Legacy @ChangeSet method @Profile tests — @Profile must be HONORED
136+ // -----------------------------------------------------------------------
137+
138+ @ Test
139+ @ DisplayName ("SHOULD return true WHEN legacy @ChangeSet method has @Profile(P1) and activeProfiles=[P1]" )
140+ void changeSetMethodProfileHonoredWhenMatches () {
141+ assertTrue (new SpringbootProfileFilter ("P1" ).filter (getLegacyCodeLoadedChange (_013__ChangeSetWithMethodProfile .class )));
142+ }
143+
144+ @ Test
145+ @ DisplayName ("SHOULD return false WHEN legacy @ChangeSet method has @Profile(P1) and activeProfiles=[P2]" )
146+ void changeSetMethodProfileNotHonoredWhenNotMatches () {
147+ assertFalse (new SpringbootProfileFilter ("P2" ).filter (getLegacyCodeLoadedChange (_013__ChangeSetWithMethodProfile .class )));
148+ }
149+
150+ // -----------------------------------------------------------------------
151+ // Native method @Profile tests — method-level @Profile must be IGNORED
152+ // -----------------------------------------------------------------------
153+
154+ @ Test
155+ @ DisplayName ("SHOULD return true WHEN native @Apply method has @Profile(P1) but no class @Profile, activeProfiles=[]" )
156+ void nativeMethodProfileNotHonoredWhenActiveProfileEmpty () {
157+ // No class-level @Profile means the change is always unfiltered.
158+ // Method-level @Profile on native @Apply is NOT honored.
159+ assertTrue (new SpringbootProfileFilter ().filter (getCodeLoadedChange (_012__NativeWithMethodProfile .class )));
160+ }
161+
162+ @ Test
163+ @ DisplayName ("SHOULD return true WHEN native @Apply method has @Profile(P1) but no class @Profile, activeProfiles=[P1]" )
164+ void nativeMethodProfileNotHonoredWhenActiveProfileMatches () {
165+ // Even when active profiles happen to match, the method-level @Profile is
166+ // not checked — it must not gate execution for native changes.
167+ assertTrue (new SpringbootProfileFilter ("P1" ).filter (getCodeLoadedChange (_012__NativeWithMethodProfile .class )));
168+ }
169+
170+ @ Test
171+ @ DisplayName ("SHOULD return true WHEN native @Apply method has @Profile(P1) but no class @Profile, activeProfiles=[P2]" )
172+ void nativeMethodProfileNotHonoredWhenActiveProfileDoesNotMatch () {
173+ // Method-level @Profile must be entirely ignored for native changes.
174+ // Without class-level @Profile the change is unfiltered even when P2 is active.
175+ assertTrue (new SpringbootProfileFilter ("P2" ).filter (getCodeLoadedChange (_012__NativeWithMethodProfile .class )));
176+ }
177+
178+ // =======================================================================
179+ // Test change classes
180+ // =======================================================================
181+
95182 @ Change (id ="not-annotated" , author = "aperezdieppa" )
96183 public static class _000__NotAnnotated {
97184 @ Apply
@@ -126,4 +213,46 @@ public void apply() {
126213 // testing purpose
127214 }
128215 }
216+
217+ // Legacy-style change: method-level @Profile on the apply method
218+ @ Change (id ="legacy-method-profile" , author = "test" )
219+ public static class _010__LegacyWithMethodProfile {
220+ @ Apply
221+ @ Profile ("P1" )
222+ public void apply () {
223+ // testing purpose
224+ }
225+ }
226+
227+ // Legacy-style change: only class-level @Profile, method has none
228+ @ Profile ("P1" )
229+ @ Change (id ="legacy-class-fallback" , author = "test" )
230+ public static class _011__LegacyClassProfileNoMethod {
231+ @ Apply
232+ public void apply () {
233+ // testing purpose
234+ }
235+ }
236+
237+ // Native Flamingock change: method-level @Profile should be IGNORED by the filter
238+ @ Change (id ="native-method-profile" , author = "test" )
239+ public static class _012__NativeWithMethodProfile {
240+ @ Apply
241+ @ Profile ("P1" )
242+ public void apply () {
243+ // testing purpose
244+ }
245+ }
246+
247+ // Legacy @ChangeSet change: method-level @Profile should be HONORED by the filter
248+ @ SuppressWarnings ("deprecation" )
249+ @ Change (id ="legacy-changeset-method-profile" , author = "test" )
250+ public static class _013__ChangeSetWithMethodProfile {
251+ @ Apply
252+ @ ChangeSet (author = "test" , id = "test-id" , order = "1" )
253+ @ Profile ("P1" )
254+ public void apply () {
255+ // testing purpose
256+ }
257+ }
129258}
0 commit comments