Skip to content

Commit

Permalink
DoValueMigrationDataObjectVisitor: Do not ignore result from super call
Browse files Browse the repository at this point in the history
Method replaceOrVisit must not ignore the result from the super call,
if values are replaced by the super call, e.g. through a
DataObjectVisitorExtension, the replaced value must not be ignored.

346741
  • Loading branch information
LukasHuser authored and stephan-merkli committed Apr 14, 2023
1 parent c21abf9 commit 15f06f3
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 2 deletions.
Expand Up @@ -21,6 +21,7 @@
import org.eclipse.scout.rt.dataobject.DataObjectHelper;
import org.eclipse.scout.rt.dataobject.DoEntityBuilder;
import org.eclipse.scout.rt.dataobject.DoEntityHolder;
import org.eclipse.scout.rt.dataobject.IDataObjectVisitorExtension;
import org.eclipse.scout.rt.dataobject.IDoEntity;
import org.eclipse.scout.rt.dataobject.migration.DataObjectMigrator.DataObjectMigratorResult;
import org.eclipse.scout.rt.dataobject.migration.fixture.house.HouseFixtureDo;
Expand All @@ -34,6 +35,9 @@
import org.eclipse.scout.rt.dataobject.migration.fixture.house.PetFixtureDoValueMigrationHandler_3;
import org.eclipse.scout.rt.dataobject.migration.fixture.house.RoomFixtureDo;
import org.eclipse.scout.rt.dataobject.migration.fixture.house.RoomSizeFixtureDoValueMigrationHandler_2;
import org.eclipse.scout.rt.dataobject.migration.fixture.house.RoomTypeCompositeFixture;
import org.eclipse.scout.rt.dataobject.migration.fixture.house.RoomTypeCompositeFixtureDataObjectVisitorExtension;
import org.eclipse.scout.rt.dataobject.migration.fixture.house.RoomTypeCompositeFixtureDo;
import org.eclipse.scout.rt.dataobject.migration.fixture.house.RoomTypeFixtureDoValueMigrationHandler_2;
import org.eclipse.scout.rt.dataobject.migration.fixture.house.RoomTypeFixtureStringId;
import org.eclipse.scout.rt.dataobject.migration.fixture.house.RoomTypesCollectionFixtureDo;
Expand Down Expand Up @@ -489,4 +493,29 @@ public void testMapValueMigration() {
String roomName = resultMap.get(RoomTypesFixture.ROOM).getName();
assertTrue("room".equals(roomName) || "standard room".equals(roomName) || "small room".equals(roomName));
}

/**
* Tests value migration for objects handled by an {@link IDataObjectVisitorExtension}.
*
* @see RoomTypeCompositeFixture
* @see RoomTypeCompositeFixtureDataObjectVisitorExtension
*/
@Test
public void testValueMigrationWithDataObjectVisitorExtension() {
RoomTypeCompositeFixtureDo original = BEANS.get(RoomTypeCompositeFixtureDo.class)
.withRoomTypeComposite(new RoomTypeCompositeFixture(
ROOM_TYPE_STANDARD_ROOM, // 'standard-room' will be migrated to 'room' by RoomTypeFixtureDoValueMigrationHandler_2
ROOM_TYPE_SMALL_ROOM)); // 'small-room' will be migrated to 'room' by RoomTypeFixtureDoValueMigrationHandler_2

DataObjectMigratorResult<RoomTypeCompositeFixtureDo> result = s_migrator.applyValueMigration(s_migrationContext, original);

assertTrue(result.isChanged());

RoomTypeCompositeFixtureDo expected = BEANS.get(RoomTypeCompositeFixtureDo.class)
.withRoomTypeComposite(new RoomTypeCompositeFixture(
RoomTypesFixture.ROOM,
RoomTypesFixture.ROOM));

assertEqualsWithComparisonFailure(expected, result.getDataObject());
}
}
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2010-2023 BSI Business Systems Integration AG.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* BSI Business Systems Integration AG - initial API and implementation
*/
package org.eclipse.scout.rt.dataobject.migration.fixture.house;

import static org.eclipse.scout.rt.platform.util.Assertions.assertNotNull;

import java.util.Objects;

/**
* Immutable composite object used as value in {@link RoomTypeCompositeFixtureDo}.
*
* @see RoomTypeCompositeFixtureDataObjectVisitorExtension
*/
public class RoomTypeCompositeFixture {

private final RoomTypeFixtureStringId m_primaryRoomType;
private final RoomTypeFixtureStringId m_secondaryRoomType;

public RoomTypeCompositeFixture(RoomTypeFixtureStringId primaryRoomType, RoomTypeFixtureStringId secondaryRoomType) {
m_primaryRoomType = assertNotNull(primaryRoomType);
m_secondaryRoomType = assertNotNull(secondaryRoomType);
}

public RoomTypeFixtureStringId getPrimaryRoomType() {
return m_primaryRoomType;
}

public RoomTypeFixtureStringId getSecondaryRoomType() {
return m_secondaryRoomType;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
RoomTypeCompositeFixture that = (RoomTypeCompositeFixture) o;
return m_primaryRoomType.equals(that.m_primaryRoomType) && m_secondaryRoomType.equals(that.m_secondaryRoomType);
}

@Override
public int hashCode() {
return Objects.hash(m_primaryRoomType, m_secondaryRoomType);
}
}
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2010-2023 BSI Business Systems Integration AG.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* BSI Business Systems Integration AG - initial API and implementation
*/
package org.eclipse.scout.rt.dataobject.migration.fixture.house;

import java.util.function.Consumer;
import java.util.function.UnaryOperator;

import org.eclipse.scout.rt.dataobject.AbstractDataObjectVisitorExtension;
import org.eclipse.scout.rt.platform.util.ObjectUtility;

/**
* Handles the components within a {@link RoomTypeCompositeFixture}.
*/
public class RoomTypeCompositeFixtureDataObjectVisitorExtension extends AbstractDataObjectVisitorExtension<RoomTypeCompositeFixture> {

@Override
public void visit(RoomTypeCompositeFixture value, Consumer<Object> chain) {
chain.accept(value.getPrimaryRoomType());
chain.accept(value.getSecondaryRoomType());
}

@Override
public RoomTypeCompositeFixture replaceOrVisit(RoomTypeCompositeFixture value, UnaryOperator<Object> chain) {
RoomTypeFixtureStringId migratedPrimaryRoomType = (RoomTypeFixtureStringId) chain.apply(value.getPrimaryRoomType());
RoomTypeFixtureStringId migratedSecondaryRoomType = (RoomTypeFixtureStringId) chain.apply(value.getSecondaryRoomType());

if (ObjectUtility.equals(migratedPrimaryRoomType, value.getPrimaryRoomType()) && ObjectUtility.equals(migratedSecondaryRoomType, value.getSecondaryRoomType())) {
// no changes detected: return value itself
return value;
}

// create a new instance of the immutable composite object with the migrated components
return new RoomTypeCompositeFixture(migratedPrimaryRoomType, migratedSecondaryRoomType);
}
}
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2010-2021 BSI Business Systems Integration AG.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* BSI Business Systems Integration AG - initial API and implementation
*/
package org.eclipse.scout.rt.dataobject.migration.fixture.house;

import javax.annotation.Generated;

import org.eclipse.scout.rt.dataobject.DoEntity;
import org.eclipse.scout.rt.dataobject.DoValue;
import org.eclipse.scout.rt.dataobject.TypeName;
import org.eclipse.scout.rt.dataobject.TypeVersion;
import org.eclipse.scout.rt.dataobject.migration.fixture.version.CharlieFixtureTypeVersions.CharlieFixture_1;

/**
* Used for value migration tests for composite objects handled by
* {@link RoomTypeCompositeFixtureDataObjectVisitorExtension}.
*/
@TypeName("charlieFixture.RoomTypeCompositeFixture")
@TypeVersion(CharlieFixture_1.class)
public class RoomTypeCompositeFixtureDo extends DoEntity {

public DoValue<RoomTypeCompositeFixture> roomTypeComposite() {
return doValue("roomTypeComposite");
}

/* **************************************************************************
* GENERATED CONVENIENCE METHODS
* *************************************************************************/

@Generated("DoConvenienceMethodsGenerator")
public RoomTypeCompositeFixtureDo withRoomTypeComposite(RoomTypeCompositeFixture roomTypeComposite) {
roomTypeComposite().set(roomTypeComposite);
return this;
}

@Generated("DoConvenienceMethodsGenerator")
public RoomTypeCompositeFixture getRoomTypeComposite() {
return roomTypeComposite().get();
}
}
Expand Up @@ -73,7 +73,6 @@ protected <T> T replaceOrVisit(T oldValue) {
}
}
// recursively visit migrated value
super.replaceOrVisit(currentValue);
return currentValue;
return super.replaceOrVisit(currentValue);
}
}

0 comments on commit 15f06f3

Please sign in to comment.