2424import software .amazon .awssdk .codegen .poet .ClassSpec ;
2525import software .amazon .awssdk .codegen .poet .PoetUtils ;
2626
27-
2827public class VersionCompatibilityTestSpec implements ClassSpec {
2928 private final IntermediateModel model ;
3029
@@ -37,6 +36,7 @@ public TypeSpec poetSpec() {
3736 return PoetUtils .createClassBuilder (className ())
3837 .addModifiers (Modifier .PUBLIC )
3938 .addMethod (compatibilityTest ())
39+ .addMethod (isVersionCompatibleMethod ())
4040 .build ();
4141 }
4242
@@ -58,11 +58,42 @@ private MethodSpec compatibilityTest() {
5858 .addModifiers (Modifier .PUBLIC )
5959 .addAnnotation (Test .class )
6060 .returns (void .class )
61- .addStatement ("$T.assertThat($T.SDK_VERSION).isEqualTo($T.VERSION)" ,
62- assertions ,
63- versionInfo ,
64- serviceVersionInfo )
61+ .addStatement ("String coreVersion = $T.SDK_VERSION" , versionInfo )
62+ .addStatement ("String serviceVersion = $T.VERSION" , serviceVersionInfo )
63+ .addStatement ("$T.assertThat(isVersionCompatible(coreVersion, serviceVersion))" +
64+ ".withFailMessage(\" Core version %s must be equal to or newer than service version %s\" , " +
65+ "coreVersion, serviceVersion).isTrue()" ,
66+ assertions )
6567 .build ();
6668 }
6769
70+ private MethodSpec isVersionCompatibleMethod () {
71+ return MethodSpec .methodBuilder ("isVersionCompatible" )
72+ .addModifiers (Modifier .PRIVATE )
73+ .returns (boolean .class )
74+ .addParameter (String .class , "coreVersion" )
75+ .addParameter (String .class , "serviceVersion" )
76+ .addStatement ("String normalizedCore = coreVersion.replace(\" -SNAPSHOT\" , \" \" )" )
77+ .addStatement ("String normalizedService = serviceVersion.replace(\" -SNAPSHOT\" , \" \" )" )
78+ .addStatement ("String[] coreParts = normalizedCore.split(\" \\ \\ .\" )" )
79+ .addStatement ("String[] serviceParts = normalizedService.split(\" \\ \\ .\" )" )
80+ .addCode ("\n " )
81+ .addStatement ("int coreMajor = Integer.parseInt(coreParts[0])" )
82+ .addStatement ("int serviceMajor = Integer.parseInt(serviceParts[0])" )
83+ .beginControlFlow ("if (coreMajor != serviceMajor)" )
84+ .addStatement ("return coreMajor >= serviceMajor" )
85+ .endControlFlow ()
86+ .addCode ("\n " )
87+ .addStatement ("int coreMinor = Integer.parseInt(coreParts[1])" )
88+ .addStatement ("int serviceMinor = Integer.parseInt(serviceParts[1])" )
89+ .beginControlFlow ("if (coreMinor != serviceMinor)" )
90+ .addStatement ("return coreMinor >= serviceMinor" )
91+ .endControlFlow ()
92+ .addCode ("\n " )
93+ .addStatement ("int corePatch = Integer.parseInt(coreParts[2])" )
94+ .addStatement ("int servicePatch = Integer.parseInt(serviceParts[2])" )
95+ .addStatement ("return corePatch >= servicePatch" )
96+ .build ();
97+ }
6898}
99+
0 commit comments