Skip to content

Commit

Permalink
TRUNK-6296: Replace PowerMock Usage Due to Limited Support in Modern …
Browse files Browse the repository at this point in the history
…Java Versions
  • Loading branch information
wikumChamith committed Feb 1, 2025
1 parent 636538f commit e2e016e
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 65 deletions.
12 changes: 0 additions & 12 deletions NOTICE.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,18 +321,6 @@
<td>war</td>
<td><a class="externalLink" href="http://openmrs.org/license">Mozilla Public License 2.0 with Healthcare Disclaimer</a></td></tr>
<tr class="a">
<td>org.powermock</td>
<td><a class="externalLink" href="http://www.powermock.org/powermock-api/powermock-api-mockito">powermock-api-mockito</a></td>
<td>1.6.6</td>
<td>jar</td>
<td><a class="externalLink" href="http://www.apache.org/licenses/LICENSE-2.0.txt">The Apache Software License, Version 2.0</a></td></tr>
<tr class="b">
<td>org.powermock</td>
<td><a class="externalLink" href="http://www.powermock.org">powermock-module-junit4</a></td>
<td>1.6.6</td>
<td>jar</td>
<td><a class="externalLink" href="http://www.apache.org/licenses/LICENSE-2.0.txt">The Apache Software License, Version 2.0</a></td></tr>
<tr class="a">
<td>org.slf4j</td>
<td><a class="externalLink" href="http://www.slf4j.org">jcl-over-slf4j</a></td>
<td>1.6.0</td>
Expand Down
16 changes: 0 additions & 16 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,6 @@
<artifactId>openmrs-test</artifactId>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
</exclusion>
<exclusion>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-reflect</artifactId>
<version>2.0.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
*/
package org.openmrs.liquibase;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.openmrs.util.DatabaseIT;
import org.powermock.reflect.Whitebox;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -48,7 +48,9 @@ public void shouldGetInitialLiquibaseSnapshotVersion() throws Exception {
public void shouldReturnDefaultSnapshotVersion() throws Exception {
ChangeLogDetective changeLogDetective = ChangeLogDetective.getInstance();

Whitebox.setInternalState(changeLogDetective, "initialSnapshotVersion", (Object)null);
Field field = ChangeLogDetective.class.getDeclaredField("initialSnapshotVersion");
field.setAccessible(true);
field.set(changeLogDetective, null);

String expected = VERSION_1_9_X;
String actual = changeLogDetective.getInitialLiquibaseSnapshotVersion("some context", this);
Expand Down
20 changes: 17 additions & 3 deletions api/src/test/java/org/openmrs/module/ModuleUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
Expand All @@ -42,7 +44,6 @@
import org.openmrs.api.context.Context;
import org.openmrs.test.jupiter.BaseContextSensitiveTest;
import org.openmrs.util.OpenmrsConstants;
import org.powermock.reflect.Whitebox;

/**
* Tests methods on the {@link org.openmrs.module.ModuleUtil} class
Expand Down Expand Up @@ -120,7 +121,7 @@ public void isOpenmrsVersionInVersions_shouldReturnTrueIfCurrentOpenmrsVersionMa
throws Exception {

final String currentVersion = "1.9.8";
Whitebox.setInternalState(OpenmrsConstants.class, "OPENMRS_VERSION_SHORT", currentVersion);
setFinalStaticField(OpenmrsConstants.class, "OPENMRS_VERSION_SHORT", currentVersion);
assertTrue(ModuleUtil.isOpenmrsVersionInVersions( currentVersion, "1.10.*"));
}

Expand All @@ -131,7 +132,7 @@ public void isOpenmrsVersionInVersions_shouldReturnTrueIfCurrentOpenmrsVersionMa
public void isOpenmrsVersionInVersions_shouldReturnFalseIfCurrentOpenmrsVersionDoesNotMatchAnyElementInVersions()
throws Exception {

Whitebox.setInternalState(OpenmrsConstants.class, "OPENMRS_VERSION_SHORT", "1.9.8");
setFinalStaticField(OpenmrsConstants.class, "OPENMRS_VERSION_SHORT", "1.9.8");
assertFalse(ModuleUtil.isOpenmrsVersionInVersions("1.11.*", "2.1.0"));
}

Expand Down Expand Up @@ -814,4 +815,17 @@ protected File getEmptyJarDestinationFolder() throws IOException {
}
return destinationFolder;
}

private static void setFinalStaticField(Class<?> clazz, String fieldName, Object value) throws Exception {
Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
Field unsafeField = unsafeClass.getDeclaredField("theUnsafe");
unsafeField.setAccessible(true);
Object unsafe = unsafeField.get(null);

Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);

unsafeClass.getMethod("putObjectVolatile", Object.class, long.class, Object.class)
.invoke(unsafe, clazz, unsafeClass.getMethod("staticFieldOffset", Field.class).invoke(unsafe, field), value);
}
}
4 changes: 4 additions & 0 deletions api/src/test/java/org/openmrs/test/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.sql.Connection;
import java.text.ParseException;
import java.util.Collection;
Expand Down Expand Up @@ -229,4 +231,6 @@ public static void waitForClockTick() {
public static Date createDateTime(String dateTimeString) throws ParseException {
return DateUtils.parseDate(dateTimeString, "yyyy-MM-dd hh:mm:ss.SSS", "yyyy-MM-dd hh:mm:ss", "yyyy-MM-dd");
}


}
15 changes: 11 additions & 4 deletions api/src/test/java/org/openmrs/util/DatabaseUpdaterDatabaseIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import org.junit.jupiter.api.Test;
import org.openmrs.liquibase.ChangeLogDetective;
import org.openmrs.liquibase.ChangeLogVersionFinder;
import org.powermock.reflect.Whitebox;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -48,9 +48,16 @@ public void tearDown() {

@Test
public void should() throws Exception {

Whitebox.setInternalState(ChangeLogDetective.getInstance(), "initialSnapshotVersion", (Object)null);
Whitebox.setInternalState(ChangeLogDetective.getInstance(), "unrunLiquibaseUpdates", (Object)null);
{
Field field = ChangeLogDetective.class.getDeclaredField("initialSnapshotVersion");
field.setAccessible(true);
field.set(ChangeLogDetective.getInstance(), null);
}
{
Field field = ChangeLogDetective.class.getDeclaredField("initialSnapshotVersion");
field.setAccessible(true);
field.set(ChangeLogDetective.getInstance(), null);
}

ChangeLogVersionFinder changeLogVersionFinder = new ChangeLogVersionFinder();
Map<String, List<String>> snapshotCombinations = changeLogVersionFinder.getSnapshotCombinations();
Expand Down
10 changes: 0 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -480,16 +480,6 @@
<artifactId>mockito-core</artifactId>
<version>${mockitoVersion}</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.9</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.9</version>
</dependency>
<!--hamcrest-core is needed to update JUnit 4 transitive dependency on hamcrest-core as explained in
http://hamcrest.org/JavaHamcrest/distributables#upgrading-from-hamcrest-1x and
https://github.com/hamcrest/JavaHamcrest/issues/224#issuecomment-449760045
Expand Down
8 changes: 0 additions & 8 deletions test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,6 @@
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
</dependency>
<!--hamcrest-core is needed to update JUnit 4 transitive dependency on hamcrest-core as explained in
http://hamcrest.org/JavaHamcrest/distributables#upgrading-from-hamcrest-1x and
https://github.com/hamcrest/JavaHamcrest/issues/224#issuecomment-449760045
Expand Down
10 changes: 0 additions & 10 deletions web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,6 @@
<artifactId>openmrs-test</artifactId>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
</exclusion>
<exclusion>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Test-jar for sharing api test resources in web tests -->
<dependency>
Expand Down

0 comments on commit e2e016e

Please sign in to comment.