Skip to content

Commit

Permalink
W-16584126: PolyMule fixes (mulesoft#13817)
Browse files Browse the repository at this point in the history
* W-16584126: PolyMule fixes

* Fix `equals`

* Allowing lambda serialization

* Serialization test

* Review.
  • Loading branch information
rbourbonnavarro authored Sep 6, 2024
1 parent 05e9951 commit 0ec0360
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2023 Salesforce, Inc. All rights reserved.
* The software in this package is published under the terms of the CPAL v1.0
* license, a copy of which has been included with this distribution in the
* LICENSE.txt file.
*/
package org.mule.runtime.core.internal.transformer.type;

import static org.mule.runtime.api.util.classloader.MuleImplementationLoaderUtils.isResolveMuleImplementationLoadersDynamically;

import static org.apache.commons.lang3.SerializationUtils.deserialize;
import static org.apache.commons.lang3.SerializationUtils.serialize;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.mockStatic;

import org.mule.runtime.api.metadata.DataType;
import org.mule.runtime.api.metadata.DataType.DynamicDelegateDataType;
import org.mule.runtime.api.util.classloader.MuleImplementationLoaderUtils;
import org.mule.runtime.core.privileged.metadata.DefaultDataTypeBuilder;
import org.mule.tck.junit4.AbstractMuleTestCase;
import org.mule.tck.size.SmallTest;

import java.io.Serializable;
import java.util.function.Supplier;

import io.qameta.allure.Issue;
import org.junit.Test;
import org.mockito.MockedStatic;

@SmallTest
@Issue("W-16584126")
public class DynamicDelegateDataTypeSerializationTestCase extends AbstractMuleTestCase {

@Test
public void serializeDynamicDelegateDataType() {
DataType delegate = new DefaultDataTypeBuilder().build();
DynamicDelegateDataType original = new DynamicDelegateDataType((Supplier<DataType> & Serializable) () -> delegate);
byte[] serialized = serialize(original);
DynamicDelegateDataType deserialized = deserialize(serialized);
assertThat(deserialized, is(original));

try (final MockedStatic<MuleImplementationLoaderUtils> mockedLoaderUtils = mockStatic(MuleImplementationLoaderUtils.class)) {
mockedLoaderUtils.when(MuleImplementationLoaderUtils::isResolveMuleImplementationLoadersDynamically).thenReturn(true);
mockedLoaderUtils.when(MuleImplementationLoaderUtils::getMuleImplementationsLoader)
.thenReturn(this.getClass().getClassLoader());
assertThat(isResolveMuleImplementationLoadersDynamically(), is(true));

assertThat(deserialized.getDelegate(), is(original.getDelegate()));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public DataType getItemDataType() {

@Override
public boolean isCompatibleWith(DataType dataType) {
if (dataType instanceof DynamicDelegateDataType) {
dataType = ((DynamicDelegateDataType) dataType).getDelegate();
}
if (!(dataType instanceof DefaultCollectionDataType)) {
return false;
}
Expand All @@ -54,6 +57,9 @@ public boolean isCompatibleWith(DataType dataType) {

@Override
public boolean equals(Object o) {
if (o instanceof DynamicDelegateDataType) {
o = ((DynamicDelegateDataType) o).getDelegate();
}
if (this == o) {
return true;
}
Expand All @@ -70,6 +76,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
// No need to consider `DynamicDelegateDataType` for hashcode calculation as we're only interested in its delegate
return Objects.hash(getType(), getItemDataType(), getMediaType());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ protected DefaultMapDataType(Class<?> type, DataType keyType, DataType valueType

@Override
public boolean isCompatibleWith(DataType dataType) {
if (dataType instanceof DynamicDelegateDataType) {
dataType = ((DynamicDelegateDataType) dataType).getDelegate();
}
if (!(dataType instanceof DefaultMapDataType)) {
return false;
}
Expand All @@ -59,6 +62,9 @@ public DataType getValueDataType() {

@Override
public boolean equals(Object o) {
if (o instanceof DynamicDelegateDataType) {
o = ((DynamicDelegateDataType) o).getDelegate();
}
if (this == o) {
return true;
}
Expand All @@ -82,6 +88,7 @@ protected boolean equalsCheckClass(Object o) {

@Override
public int hashCode() {
// No need to consider `DynamicDelegateDataType` for hashcode calculation as we're only interested in its delegate
return Objects.hash(getType(), getKeyDataType(), getValueDataType(), getMediaType());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ private Class<?> getPrimitiveWrapper(Class<?> primitiveType) {

@Override
public boolean equals(Object o) {
if (o instanceof DynamicDelegateDataType) {
o = ((DynamicDelegateDataType) o).getDelegate();
}
if (this == o) {
return true;
}
Expand All @@ -96,6 +99,9 @@ protected boolean equalsCheckClass(Object o) {

@Override
public boolean isCompatibleWith(DataType dataType) {
if (dataType instanceof DynamicDelegateDataType) {
dataType = ((DynamicDelegateDataType) dataType).getDelegate();
}
if (this == dataType) {
return true;
}
Expand Down Expand Up @@ -132,6 +138,7 @@ private boolean mediaTypesMatch(DataType other) {

@Override
public int hashCode() {
// No need to consider `DynamicDelegateDataType` for hashcode calculation as we're only interested in its delegate
return Objects.hash(getType(), getMediaType());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ private JpmsUtils() {
"--add-opens=java.base/java.lang=org.mule.runtime.jpms.utils";
private static final String REQUIRED_ADD_OPENS_JAVA_LANG_REFLECT =
"--add-opens=java.base/java.lang.reflect=org.mule.runtime.jpms.utils";
private static final String REQUIRED_ADD_OPENS_JAVA_LANG_INVOKE =
"--add-opens=java.base/java.lang.invoke=org.mule.runtime.jpms.utils";
private static final String REQUIRED_ADD_OPENS_JDK_INTERNAL_REF =
"--add-opens=java.base/jdk.internal.ref=org.mule.runtime.jpms.utils";
private static final String REQUIRED_ADD_OPENS_JAVA_NIO =
Expand All @@ -92,6 +94,7 @@ private JpmsUtils() {

private static final List<String> REQUIRED_ADD_OPENS = asList(REQUIRED_ADD_OPENS_JAVA_LANG,
REQUIRED_ADD_OPENS_JAVA_LANG_REFLECT,
REQUIRED_ADD_OPENS_JAVA_LANG_INVOKE,
REQUIRED_ADD_OPENS_JDK_INTERNAL_REF,
REQUIRED_ADD_OPENS_JAVA_NIO,
REQUIRED_ADD_OPENS_SUN_NIO_CH,
Expand Down Expand Up @@ -261,9 +264,9 @@ private static void openPackages(ModuleLayer layer) {
openToModule(layer, "org.mule.runtime.launcher", "org.mule.boot.api",
singletonList("org.mule.runtime.module.boot.internal"));
openToModule(layer, "kryo.shaded", "java.base",
asList("java.lang", "java.lang.reflect"));
asList("java.lang", "java.lang.reflect", "java.lang.invoke"));
openToModule(layer, "org.mule.runtime.jpms.utils", "java.base",
asList("java.lang", "java.lang.reflect"));
asList("java.lang", "java.lang.reflect", "java.lang.invoke"));

// To avoid a performance-related warning from Hazelcast according to
// https://docs.hazelcast.com/hazelcast/5.2/getting-started/install-hazelcast#using-modular-java
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@
-XX:+IgnoreUnrecognizedVMOptions
--add-opens=java.base/java.lang=ALL-UNNAMED
--add-opens=java.base/java.lang.reflect=ALL-UNNAMED
--add-opens=java.base/java.lang.invoke=ALL-UNNAMED
${surefire.args.base}
</surefire.test.unit.open.args>
<!-- Emulate the behaviour in standalone, where slf4j is loaded in the boot layer -->
Expand Down

0 comments on commit 0ec0360

Please sign in to comment.