Skip to content

Commit f107667

Browse files
committed
Fix tests
1 parent 4b3bf0d commit f107667

2 files changed

Lines changed: 22 additions & 53 deletions

File tree

pom.xml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<groupId>com.contentful.java</groupId>
55
<artifactId>java-sdk</artifactId>
6-
<version>10.5.22-SNAPSHOT</version>
6+
<version>10.5.22</version>
77
<packaging>jar</packaging>
88

99
<name>${project.groupId}:${project.artifactId}</name>
@@ -20,7 +20,7 @@
2020
<url>http://github.com/contentful/contentful.java</url>
2121
<connection>scm:git:git://github.com/contentful/contentful.java.git</connection>
2222
<developerConnection>scm:git:ssh://git@github.com/contentful/contentful.java.git</developerConnection>
23-
<tag>HEAD</tag>
23+
<tag>java-sdk-10.5.22</tag>
2424
</scm>
2525

2626
<issueManagement>
@@ -127,7 +127,14 @@
127127
<dependency>
128128
<groupId>org.mockito</groupId>
129129
<artifactId>mockito-core</artifactId>
130-
<version>${mockito.version}</version>
130+
<version>3.4.0</version>
131+
<scope>test</scope>
132+
</dependency>
133+
134+
<dependency>
135+
<groupId>org.mockito</groupId>
136+
<artifactId>mockito-inline</artifactId>
137+
<version>3.4.0</version>
131138
<scope>test</scope>
132139
</dependency>
133140

@@ -191,7 +198,7 @@
191198
<id>simple-command</id>
192199
<phase>package</phase>
193200
<goals>
194-
<goal>attached</goal>
201+
<goal>single</goal>
195202
</goals>
196203
</execution>
197204
</executions>

src/test/java/com/contentful/java/cda/interceptor/ContentfulUserAgentHeaderInterceptorTest.java

Lines changed: 11 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
import com.contentful.java.cda.interceptor.ContentfulUserAgentHeaderInterceptor.Section.Version;
66

77
import org.junit.Test;
8-
9-
import java.lang.reflect.Field;
10-
import java.lang.reflect.Modifier;
118
import java.util.Locale;
9+
import java.lang.reflect.Field;
1210

1311
import static com.contentful.java.cda.interceptor.ContentfulUserAgentHeaderInterceptor.Section.OperatingSystem.Android;
1412
import static com.contentful.java.cda.interceptor.ContentfulUserAgentHeaderInterceptor.Section.OperatingSystem.Linux;
@@ -143,55 +141,19 @@ public void allZeroVersionGetsIgnored() {
143141

144142
@Test
145143
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
186147

187-
private void setFinalStatic(Field field, Object newValue) throws Exception {
188-
field.setAccessible(true);
148+
final Platform platform = Platform.get();
189149

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+
);
193154

194-
field.set(null, newValue);
155+
assertThat(os.getName()).isNotNull();
156+
assertThat(os.getVersion().toString()).isNotNull();
195157
}
196158

197159
@Test

0 commit comments

Comments
 (0)