Skip to content

Commit

Permalink
Remove DeserializationPostInitialisable
Browse files Browse the repository at this point in the history
  • Loading branch information
elrodro83 committed Dec 12, 2024
1 parent f75b66e commit 515c0e5
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 249 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import static org.mule.runtime.api.scheduler.SchedulerConfig.config;
import static org.mule.runtime.api.store.ObjectStoreManager.BASE_IN_MEMORY_OBJECT_STORE_KEY;
import static org.mule.runtime.api.store.ObjectStoreManager.BASE_PERSISTENT_OBJECT_STORE_KEY;
import static org.mule.tck.SerializationTestUtils.addJavaSerializerToMockMuleContext;

import static java.lang.Thread.currentThread;
import static java.util.Optional.of;
Expand All @@ -24,12 +23,13 @@
import org.mule.runtime.api.artifact.Registry;
import org.mule.runtime.api.exception.MuleException;
import org.mule.runtime.api.lifecycle.InitialisationException;
import org.mule.runtime.api.serialization.ObjectSerializer;
import org.mule.runtime.api.store.ObjectStoreException;
import org.mule.runtime.api.store.ObjectStoreSettings;
import org.mule.runtime.api.store.PartitionableObjectStore;
import org.mule.runtime.core.api.MuleContext;
import org.mule.runtime.core.api.config.MuleConfiguration;
import org.mule.runtime.core.internal.context.MuleContextWithRegistry;
import org.mule.runtime.core.internal.serialization.JavaObjectSerializer;
import org.mule.runtime.core.internal.store.PartitionedInMemoryObjectStore;
import org.mule.runtime.core.internal.store.PartitionedPersistentObjectStore;
import org.mule.tck.SimpleUnitTestSupportSchedulerService;
Expand Down Expand Up @@ -77,13 +77,8 @@ public void setup() {
schedulerService = new SimpleUnitTestSupportSchedulerService();
muleContext = mock(MuleContextWithRegistry.class);
MuleConfiguration muleConfiguration = mock(MuleConfiguration.class);
when(muleConfiguration.getWorkingDirectory()).thenReturn(tempWorkDir.getRoot().getAbsolutePath());
when(muleContext.getConfiguration()).thenReturn(muleConfiguration);

when(muleContext.getExecutionClassLoader()).thenReturn(this.getClass().getClassLoader());

Registry registry = mock(Registry.class);
createRegistryAndBaseStore(muleContext, registry);
createRegistryAndBaseStore(muleConfiguration, new JavaObjectSerializer(this.getClass().getClassLoader()), registry);
when(muleContext.getSchedulerBaseConfig())
.thenReturn(config().withPrefix(MuleObjectStoreManagerTestCase.class.getName() + "#" + name.getMethodName()));

Expand Down Expand Up @@ -113,7 +108,6 @@ public void expireTwoStoresInParallel() throws ObjectStoreException, Initialisat
when(muleContext.isPrimaryPollingInstance()).thenReturn(true);
expireDelayLatch = new CountDownLatch(1);

addJavaSerializerToMockMuleContext(muleContext);
storeManager.initialise();

storeManager.createObjectStore(TEST_PARTITION_NAME + "_1", ObjectStoreSettings.builder()
Expand Down Expand Up @@ -142,7 +136,6 @@ public void expireInMemoryInSecondaryNode() throws InitialisationException {
when(muleContext.isPrimaryPollingInstance()).thenReturn(false);
expireDelayLatch = new CountDownLatch(1);

addJavaSerializerToMockMuleContext(muleContext);
storeManager.initialise();

storeManager.createObjectStore(TEST_PARTITION_NAME + "_1", ObjectStoreSettings.builder()
Expand Down Expand Up @@ -233,8 +226,6 @@ public String describeFailure() {

private ObjectStorePartition<Serializable> createStorePartition(String partitionName, boolean isPersistent)
throws InitialisationException {
addJavaSerializerToMockMuleContext(muleContext);

storeManager.initialise();

ObjectStorePartition<Serializable> store =
Expand All @@ -249,9 +240,9 @@ private ObjectStorePartition<Serializable> createStorePartition(String partition
return store;
}

private void createRegistryAndBaseStore(MuleContextWithRegistry muleContext, Registry registry) {
private void createRegistryAndBaseStore(MuleConfiguration muleConfiguration, ObjectSerializer serializer, Registry registry) {
when(registry.lookupByName(BASE_PERSISTENT_OBJECT_STORE_KEY))
.thenReturn(of(createPersistentPartitionableObjectStore(muleContext)));
.thenReturn(of(createPersistentPartitionableObjectStore(muleConfiguration, serializer)));
when(registry.lookupByName(BASE_IN_MEMORY_OBJECT_STORE_KEY)).thenReturn(of(createTransientPartitionableObjectStore()));
}

Expand All @@ -267,8 +258,9 @@ public void expire(long entryTTL, int maxEntries, String partitionName) throws O
};
}

private PartitionableObjectStore<?> createPersistentPartitionableObjectStore(MuleContext muleContext) {
return new PartitionedPersistentObjectStore(muleContext) {
private PartitionableObjectStore<?> createPersistentPartitionableObjectStore(MuleConfiguration muleConfiguration,
ObjectSerializer serializer) {
return new PartitionedPersistentObjectStore(muleConfiguration, serializer) {

@Override
public void expire(long entryTTL, int maxEntries, String partitionName) throws ObjectStoreException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@
*/
package org.mule.runtime.core.internal.util.store;

import static org.mule.runtime.core.internal.store.PartitionedPersistentObjectStore.OBJECT_STORE_DIR;

import static java.io.File.separator;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mule.runtime.core.internal.store.PartitionedPersistentObjectStore.OBJECT_STORE_DIR;
import static org.mule.tck.SerializationTestUtils.addJavaSerializerToMockMuleContext;

import org.mule.runtime.api.serialization.ObjectSerializer;
import org.mule.runtime.api.store.ObjectAlreadyExistsException;
import org.mule.runtime.api.store.ObjectStoreException;
import org.mule.runtime.core.api.MuleContext;
import org.mule.runtime.core.internal.store.DeserializationPostInitialisable;
import org.mule.runtime.core.api.config.MuleConfiguration;
import org.mule.runtime.core.internal.serialization.JavaObjectSerializer;
import org.mule.runtime.core.internal.store.PartitionedPersistentObjectStore;
import org.mule.tck.junit4.AbstractMuleTestCase;

Expand All @@ -28,10 +29,10 @@
import java.io.Serializable;

import org.apache.commons.io.FileUtils;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Answers;

/**
*
Expand All @@ -41,23 +42,22 @@ public class PartitionedPersistentObjectStoreTestCase extends AbstractMuleTestCa
private static final String OBJECT_KEY = "key";
private static final String OBJECT_BASE_VALUE = "value";

private MuleContext mockMuleContext = mock(MuleContext.class, Answers.RETURNS_DEEP_STUBS);
private MuleConfiguration muleConfiguration;
private JavaObjectSerializer javaObjectSerializer;
private PartitionedPersistentObjectStore<Serializable> os;
private int numberOfPartitions = 3;

@Before
public void setUpMockMuleContext() throws IOException {
numberOfPartitions = 3;
when(mockMuleContext.getConfiguration().getWorkingDirectory()).thenReturn(".");
when(mockMuleContext.getExecutionClassLoader()).thenReturn(Thread.currentThread().getContextClassLoader());
os = new TestPartitionedPersistentObjectStore(mockMuleContext);
muleConfiguration = mock(MuleConfiguration.class);
when(muleConfiguration.getWorkingDirectory()).thenReturn(".");
javaObjectSerializer = new JavaObjectSerializer(this.getClass().getClassLoader());
os = new TestPartitionedPersistentObjectStore(muleConfiguration, javaObjectSerializer);
File objectStorePersistDir = new File(PartitionedPersistentObjectStore.OBJECT_STORE_DIR);
if (objectStorePersistDir.exists()) {
FileUtils.deleteDirectory(objectStorePersistDir);
}

addJavaSerializerToMockMuleContext(mockMuleContext);
when(mockMuleContext.getExecutionClassLoader()).thenReturn(getClass().getClassLoader());
}

@Test
Expand Down Expand Up @@ -116,7 +116,8 @@ public void clearDoesntBreakPartition() throws ObjectStoreException {
os.clear(partitionName);
os.store(key, value, partitionName);

PartitionedPersistentObjectStore newOS = new PartitionedPersistentObjectStore<>(mockMuleContext);
PartitionedPersistentObjectStore newOS =
new PartitionedPersistentObjectStore<>(muleConfiguration, javaObjectSerializer);

newOS.open(partitionName);
Serializable retrieve = newOS.retrieve(key, partitionName);
Expand All @@ -137,7 +138,7 @@ public void disposePartitionRemovesPartitionDirectory() throws ObjectStoreExcept
}

private File getPartitionDirectory(String partitionName) {
String workingDirectory = mockMuleContext.getConfiguration().getWorkingDirectory();
String workingDirectory = muleConfiguration.getWorkingDirectory();
String path = workingDirectory + separator + OBJECT_STORE_DIR + separator + partitionName;
return new File(path);
}
Expand All @@ -147,14 +148,6 @@ public void allowsAnyPartitionName() throws Exception {
os.open("asdfsadfsa#$%@#$@#$@$%$#&8ASDFWER!!");
}

@Test
public void muleContextAwareValueGetsDeserialized() throws Exception {
os.open();
os.store("key", new DeserializableValue(mockMuleContext));
DeserializableValue value = (DeserializableValue) os.retrieve("key");
assertNotNull(value.getMuleContext());
}

private void closePartitions() throws ObjectStoreException {
for (int i = 0; i < numberOfPartitions; i++) {
os.close(getPartitionName(i));
Expand Down Expand Up @@ -217,27 +210,10 @@ private String getPartitionName(int i) {
return "partition" + i;
}

public static class DeserializableValue implements DeserializationPostInitialisable, Serializable {

private transient MuleContext muleContext;

public DeserializableValue(MuleContext muleContext) {
this.muleContext = muleContext;
}

public void initAfterDeserialisation(MuleContext muleContext) {
this.muleContext = muleContext;
}

public MuleContext getMuleContext() {
return muleContext;
}
}

private static class TestPartitionedPersistentObjectStore extends PartitionedPersistentObjectStore<Serializable> {

public TestPartitionedPersistentObjectStore(MuleContext mockMuleContext) {
super(mockMuleContext);
public TestPartitionedPersistentObjectStore(MuleConfiguration muleConfiguration, ObjectSerializer serializer) {
super(muleConfiguration, serializer);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@
*/
package org.mule.runtime.core.internal.util.store;

import static org.mule.runtime.core.api.util.FileUtils.openDirectory;

import static org.apache.commons.io.FileUtils.deleteDirectory;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.when;
import static org.mule.runtime.core.api.util.FileUtils.openDirectory;
import static org.mule.tck.SerializationTestUtils.addJavaSerializerToMockMuleContext;
import static org.mule.tck.util.MuleContextUtils.mockMuleContext;

import org.mule.runtime.api.store.ObjectDoesNotExistException;
import org.mule.runtime.api.store.ObjectStoreException;
import org.mule.runtime.core.api.MuleContext;
import org.mule.runtime.core.api.config.MuleConfiguration;
import org.mule.runtime.core.internal.serialization.JavaObjectSerializer;
import org.mule.tck.junit4.AbstractMuleTestCase;
import org.mule.tck.size.SmallTest;

Expand All @@ -30,18 +29,19 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;

import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

@SmallTest
@RunWith(MockitoJUnitRunner.class)
public class PersistentObjectStorePartitionTestCase extends AbstractMuleTestCase {

@Rule
public TemporaryFolder objectStoreFolder = new TemporaryFolder();
public MockitoRule rule = MockitoJUnit.rule();

private MuleContext muleContext = mockMuleContext();
@Rule
public TemporaryFolder objectStoreFolder = new TemporaryFolder();

@Mock
private MuleConfiguration muleConfiguration;
Expand All @@ -52,12 +52,10 @@ public class PersistentObjectStorePartitionTestCase extends AbstractMuleTestCase

@Before
public void setUp() throws Exception {
when(muleContext.getExecutionClassLoader()).thenReturn(getClass().getClassLoader());
when(muleContext.getConfiguration()).thenReturn(muleConfiguration);
workingDirectory = objectStoreFolder.getRoot().getParentFile();
when(muleConfiguration.getWorkingDirectory()).thenReturn(workingDirectory.getPath());
addJavaSerializerToMockMuleContext(muleContext);
partition = new PersistentObjectStorePartition(muleContext, "test", objectStoreFolder.getRoot());
partition = new PersistentObjectStorePartition(muleConfiguration, new JavaObjectSerializer(this.getClass().getClassLoader()),
"test", objectStoreFolder.getRoot());
partition.open();
}

Expand Down

This file was deleted.

Loading

0 comments on commit 515c0e5

Please sign in to comment.