/*
 * Copyright (c) 1998, 2021 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0,
 * or the Eclipse Distribution License v. 1.0 which is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
 */

// Contributors:
//     Oracle - initial API and implementation from Oracle TopLink
package org.eclipse.persistence.testing.tests.transparentindirection;

import org.eclipse.persistence.sessions.UnitOfWork;
import org.eclipse.persistence.testing.framework.AutoVerifyTestCase;
import org.eclipse.persistence.testing.framework.TestErrorException;
import org.eclipse.persistence.testing.models.transparentindirection.Player;
import org.eclipse.persistence.testing.models.transparentindirection.Team;

/**
 * BUG - 4082205 Bi-directional relationship maintenance does not work with
 * transparent maps.
 *
 * @author Guy Pelletier
 * @version 1.0
 */
public class BidirectionalRelationshipMaintenanceTest extends AutoVerifyTestCase {
    boolean m_exceptionCaught;

    public BidirectionalRelationshipMaintenanceTest() {
        setDescription("Test bidrectional relationship maintenance on an indirect transparent map.");
    }

    @Override
    public void reset() {
        rollbackTransaction();
        getSession().getIdentityMapAccessor().initializeIdentityMaps();
    }

    @Override
    public void setup() throws Exception {
        m_exceptionCaught = false;

        beginTransaction();
        getSession().getIdentityMapAccessor().initializeAllIdentityMaps();
    }

    @Override
    public void test() {
        UnitOfWork uow = getSession().acquireUnitOfWork();
        Team team = (Team)uow.registerObject(new Team());
        Player player = (Player)uow.registerObject(new Player());
        player.setTeam(team);

        try {
            uow.commit();
        } catch (Exception e) {
            m_exceptionCaught = true;
        }
    }

    @Override
    public void verify() throws Exception {
        if (m_exceptionCaught) {
            throw new TestErrorException("Relationship maintenance failed.");
        }
    }
}
