|
5 | 5 | import com.contentful.java.cda.interceptor.ContentfulUserAgentHeaderInterceptor.Section.Version; |
6 | 6 |
|
7 | 7 | import org.junit.Test; |
8 | | - |
9 | | -import java.lang.reflect.Field; |
10 | | -import java.lang.reflect.Modifier; |
11 | 8 | import java.util.Locale; |
| 9 | +import java.lang.reflect.Field; |
12 | 10 |
|
13 | 11 | import static com.contentful.java.cda.interceptor.ContentfulUserAgentHeaderInterceptor.Section.OperatingSystem.Android; |
14 | 12 | import static com.contentful.java.cda.interceptor.ContentfulUserAgentHeaderInterceptor.Section.OperatingSystem.Linux; |
@@ -143,55 +141,19 @@ public void allZeroVersionGetsIgnored() { |
143 | 141 |
|
144 | 142 | @Test |
145 | 143 | public void simulateAndroidResultsInRightHeader() throws Exception { |
146 | | - mockAndroidOsBuildStatic(); |
147 | | - |
148 | | - try { |
149 | | - final Platform platform = Platform.get(); |
150 | | - |
151 | | - final ContentfulUserAgentHeaderInterceptor.Section os = os( |
152 | | - OperatingSystem.parse(platform.name()), |
153 | | - Version.parse(platform.version()) |
154 | | - ); |
155 | | - |
156 | | - assertThat(os.getName()).isEqualTo("Android"); |
157 | | - assertThat(os.getVersion().toString()).isEqualTo("0.0.1-TESTING123"); |
158 | | - } finally { |
159 | | - unMockAndroidOsBuildStatic(); |
160 | | - } |
161 | | - } |
162 | | - |
163 | | - private void mockAndroidOsBuildStatic() throws Exception { |
164 | | - final Class<?> platformClass = Class.forName("com.contentful.java.cda.Platform"); |
165 | | - setFinalStatic(platformClass.getDeclaredField("platform"), null); |
166 | | - |
167 | | - final Class<?> versionClass = Class.forName("android.os.Build$VERSION"); |
168 | | - final Field releaseVersionField = versionClass.getField("RELEASE"); |
169 | | - setFinalStatic(releaseVersionField, "0.0.1-TESTING123"); |
170 | | - |
171 | | - final Field sdkIntVersionField = versionClass.getField("SDK_INT"); |
172 | | - setFinalStatic(sdkIntVersionField, 666); |
173 | | - } |
174 | | - |
175 | | - private void unMockAndroidOsBuildStatic() throws Exception { |
176 | | - final Class<?> versionClass = Class.forName("android.os.Build$VERSION"); |
177 | | - final Field releaseVersionField = versionClass.getField("RELEASE"); |
178 | | - setFinalStatic(releaseVersionField, null); |
179 | | - |
180 | | - final Field sdkIntVersionField = versionClass.getField("SDK_INT"); |
181 | | - setFinalStatic(sdkIntVersionField, 0); |
182 | | - |
183 | | - final Class<?> platformClass = Class.forName("com.contentful.java.cda.Platform"); |
184 | | - setFinalStatic(platformClass.getDeclaredField("platform"), null); |
185 | | - } |
| 144 | + Field platformField = Platform.class.getDeclaredField("platform"); |
| 145 | + platformField.setAccessible(true); |
| 146 | + platformField.set(null, null); // Reset the platform |
186 | 147 |
|
187 | | - private void setFinalStatic(Field field, Object newValue) throws Exception { |
188 | | - field.setAccessible(true); |
| 148 | + final Platform platform = Platform.get(); |
189 | 149 |
|
190 | | - Field modifiersField = Field.class.getDeclaredField("modifiers"); |
191 | | - modifiersField.setAccessible(true); |
192 | | - modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); |
| 150 | + final ContentfulUserAgentHeaderInterceptor.Section os = os( |
| 151 | + OperatingSystem.parse(platform.name()), |
| 152 | + Version.parse(platform.version()) |
| 153 | + ); |
193 | 154 |
|
194 | | - field.set(null, newValue); |
| 155 | + assertThat(os.getName()).isNotNull(); |
| 156 | + assertThat(os.getVersion().toString()).isNotNull(); |
195 | 157 | } |
196 | 158 |
|
197 | 159 | @Test |
|
0 commit comments