2929 */
3030package com .google .api .gax .core ;
3131
32- import static org .graalvm .nativeimage .ImageInfo .PROPERTY_IMAGE_CODE_KEY ;
33- import static org .graalvm .nativeimage .ImageInfo .PROPERTY_IMAGE_CODE_VALUE_RUNTIME ;
32+ import static org .junit .Assert .assertEquals ;
3433import static org .junit .Assert .assertTrue ;
3534
35+ import com .google .common .base .Strings ;
3636import java .util .regex .Pattern ;
37+ import org .junit .After ;
3738import org .junit .Test ;
3839import org .junit .runner .RunWith ;
3940import org .junit .runners .JUnit4 ;
@@ -57,10 +58,108 @@ public void testGaxVersion() {
5758 }
5859 }
5960
61+ private static String originalJavaVersion = System .getProperty ("java.version" );
62+ private static String originalJavaVendor = System .getProperty ("java.vendor" );
63+ private static String originalJavaVendorVersion = System .getProperty ("java.vendor.version" );
64+
65+ @ After
66+ public void cleanup () {
67+ if (Strings .isNullOrEmpty (originalJavaVersion )) {
68+ System .clearProperty ("java.version" );
69+ } else {
70+ System .setProperty ("java.version" , originalJavaVersion );
71+ }
72+
73+ if (Strings .isNullOrEmpty (originalJavaVendor )) {
74+ System .clearProperty ("java.vendor" );
75+ } else {
76+ System .setProperty ("java.vendor" , originalJavaVendor );
77+ }
78+
79+ if (Strings .isNullOrEmpty (originalJavaVendorVersion )) {
80+ System .clearProperty ("java.vendor.version" );
81+ } else {
82+ System .setProperty ("java.vendor.version" , originalJavaVendorVersion );
83+ }
84+ }
85+
86+ @ Test
87+ public void testGetJavaRuntimeInfo_graalVM () {
88+ // This case is one of major Java vendors
89+ System .setProperty ("java.version" , "17.0.3" );
90+ System .setProperty ("java.vendor" , "GraalVM Community" );
91+ System .setProperty ("java.vendor.version" , "GraalVM CE 22.1.0" );
92+
93+ String runtimeInfo = GaxProperties .getRuntimeVersion ();
94+ assertEquals ("17.0.3__GraalVM-Community__GraalVM-CE-22.1.0" , runtimeInfo );
95+ }
96+
97+ @ Test
98+ public void testGetJavaRuntimeInfo_temurin () {
99+ // This case is one of major Java vendors
100+ System .setProperty ("java.version" , "11.0.19" );
101+ System .setProperty ("java.vendor" , "Eclipse Adoptium" );
102+ System .setProperty ("java.vendor.version" , "Temurin-11.0.19+7" );
103+
104+ String runtimeInfo = GaxProperties .getRuntimeVersion ();
105+ assertEquals ("11.0.19__Eclipse-Adoptium__Temurin-11.0.19-7" , runtimeInfo );
106+ }
107+
108+ @ Test
109+ public void testGetJavaRuntimeInfo_coretto () {
110+ // This case is one of major Java vendors
111+ System .setProperty ("java.version" , "11.0.19" );
112+ System .setProperty ("java.vendor" , "Amazon.com Inc." );
113+ System .setProperty ("java.vendor.version" , "Corretto-11.0.19.7.1" );
114+
115+ String runtimeInfo = GaxProperties .getRuntimeVersion ();
116+ assertEquals ("11.0.19__Amazon.com-Inc.__Corretto-11.0.19.7.1" , runtimeInfo );
117+ }
118+
60119 @ Test
61- public void testGetVersion_nativeImage () {
62- System .setProperty (PROPERTY_IMAGE_CODE_KEY , PROPERTY_IMAGE_CODE_VALUE_RUNTIME );
63- String javaVersion = GaxProperties .getJavaVersion ();
64- assertTrue (javaVersion .endsWith ("-graalvm" ));
120+ public void testGetJavaRuntimeInfo_specialCharacters () {
121+ // testing for unsupported characters and spaces
122+ System .setProperty ("java.version" , "20%^.&0~.1#45`*" );
123+ System .setProperty ("java.vendor" , "A^!@#$*B()[]{} C ~%& D-E ?" );
124+ System .setProperty ("java.vendor.version" , "1!@%$@#.AB!346.9^" );
125+
126+ String runtimeInfo = GaxProperties .getRuntimeVersion ();
127+ assertEquals ("20--.-0-.1-45--__A------B-------C-----D-E--__1------.AB-346.9-" , runtimeInfo );
128+ }
129+
130+ @ Test
131+ public void testGetJavaRuntimeInfo_nullVendorVersion () {
132+ // testing for null java.vendor.version
133+ System .setProperty ("java.version" , "20.0.1" );
134+ System .setProperty ("java.vendor" , "Oracle" );
135+ System .clearProperty ("java.vendor.version" );
136+
137+ String runtimeInfo = GaxProperties .getRuntimeVersion ();
138+ assertEquals ("20.0.1__Oracle" , runtimeInfo );
139+ }
140+
141+ @ Test
142+ public void testGetJavaRuntimeInfo_nullVendorAndVendorVersion () {
143+ // testing for null java.vendor and java.vendor.version
144+ System .setProperty ("java.version" , "20.0.1" );
145+ System .clearProperty ("java.vendor" );
146+ System .clearProperty ("java.vendor.version" );
147+
148+ String runtimeInfo = GaxProperties .getRuntimeVersion ();
149+ assertEquals ("20.0.1" , runtimeInfo );
150+ }
151+
152+ @ Test
153+ public void testGetJavaRuntimeInfo_nullJavaVersion () {
154+ // testing for null java.version
155+ // We don't expect this case to happen, however we don't want the method to fail when it really
156+ // happens.
157+
158+ System .clearProperty ("java.version" );
159+ System .setProperty ("java.vendor" , "oracle" );
160+ System .setProperty ("java.vendor.version" , "20.0.1" );
161+
162+ String runtimeInfo = GaxProperties .getRuntimeVersion ();
163+ assertEquals ("null__oracle__20.0.1" , runtimeInfo );
65164 }
66165}
0 commit comments