Skip to content

Commit

Permalink
Fix Enterprise REST Tests by removing hardcoded cluster version (#970)
Browse files Browse the repository at this point in the history
[`JobCancelledAfterUpgradeTest` is
failing](https://github.com/hazelcast/hazelcast-mono/pull/968#issuecomment-1973254145)
because the expected Hazelcast version is hardcoded.

Changes:
- Make expected versions dynamic
- Use reflection to populate `CURRENT_CLUSTER_VERSION` &
`PREVIOUS_CLUSTER_VERSION` so they don't need to be incremented in
future
- Convert `VersionsTest` to JUnit5
GitOrigin-RevId: d540246e0f204e5fc34ddbdf2bc69049a8b8e71c
  • Loading branch information
JackPGreen authored and actions-user committed Mar 4, 2024
1 parent ce3527f commit b6da12d
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@

package com.hazelcast.internal.cluster;

import com.hazelcast.instance.GeneratedBuildProperties;
import com.hazelcast.version.Version;

import javax.annotation.Nonnull;

import java.lang.reflect.Field;
import java.text.MessageFormat;
import java.util.Objects;

public final class Versions {

/**
Expand Down Expand Up @@ -65,8 +72,50 @@ public final class Versions {
*/
public static final Version V5_5 = Version.of(5, 5);

public static final Version PREVIOUS_CLUSTER_VERSION = V5_4;
public static final Version CURRENT_CLUSTER_VERSION = V5_5;
@Nonnull
public static final Version PREVIOUS_CLUSTER_VERSION;
@Nonnull
public static final Version CURRENT_CLUSTER_VERSION;

static {
// Dynamically set PREVIOUS_CLUSTER_VERSION & CURRENT_CLUSTER_VERSION by reflection

Version buildPropertiesVersion = Version.of(GeneratedBuildProperties.VERSION);

// Find an equivalent version in the existing constant pool
Version currentClusterVersionConstant = null;

// The previous version is assumed to be the declared version lexicographically before CURRENT_CLUSTER_VERSION
Version previousHighest = null;

for (Field field : Versions.class.getFields()) {
if (field.getType()
.equals(Version.class)) {
try {
Version version = (Version) field.get(null);

if (version != null) {
int versionCompareToBuildPropertiesVersion = version.compareTo(buildPropertiesVersion);

if (versionCompareToBuildPropertiesVersion == 0) {
currentClusterVersionConstant = version;
} else if (versionCompareToBuildPropertiesVersion < 0
&& (previousHighest == null || version.compareTo(previousHighest) > 0)) {
previousHighest = version;
}
}
} catch (ReflectiveOperationException e) {
throw new ExceptionInInitializerError(e);
}
}
}

CURRENT_CLUSTER_VERSION = Objects.requireNonNull(currentClusterVersionConstant,
() -> MessageFormat.format("Failed to find matching constant for version {0}", buildPropertiesVersion));

PREVIOUS_CLUSTER_VERSION = Objects.requireNonNull(previousHighest,
() -> MessageFormat.format("Failed to find version preceeding {0}", CURRENT_CLUSTER_VERSION));
}

private Versions() {
}
Expand Down
4 changes: 2 additions & 2 deletions hazelcast/src/main/java/com/hazelcast/version/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
int result = (int) major;
result = 31 * result + (int) minor;
int result = major;
result = 31 * result + minor;
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,43 @@

package com.hazelcast.internal.cluster;

import com.hazelcast.test.HazelcastParallelClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.annotation.ParallelJVMTest;
import com.hazelcast.test.annotation.QuickTest;
import com.hazelcast.version.Version;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import static org.junit.Assert.assertEquals;

@RunWith(HazelcastParallelClassRunner.class)
@Category({QuickTest.class, ParallelJVMTest.class})
public class VersionsTest extends HazelcastTestSupport {
import static com.hazelcast.test.HazelcastTestSupport.assertUtilityConstructor;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@Tag("com.hazelcast.test.annotation.QuickTest")
class VersionsTest {
@Test
public void testConstructor() {
void testConstructor() {
assertUtilityConstructor(Versions.class);
}

@Test
public void version_4_0() {
void version_4_0() {
assertEquals(Versions.V4_0, Version.of(4, 0));
}

@Test
public void version_4_1() {
void version_4_1() {
assertEquals(Versions.V4_1, Version.of(4, 1));
}

@Test
void testParse() {
Version version = Versions.CURRENT_CLUSTER_VERSION;
assertEquals(version, Version.of(version.toString()));
}

@Test
void testCurrentVersion() {
assertNotNull(Versions.CURRENT_CLUSTER_VERSION);
assertNotNull(Versions.PREVIOUS_CLUSTER_VERSION);

assertNotEquals(Versions.PREVIOUS_CLUSTER_VERSION, Versions.CURRENT_CLUSTER_VERSION);
}
}

0 comments on commit b6da12d

Please sign in to comment.