/*******************************************************************************
 * Copyright (c) 2015 IBM Corporation. All rights reserved.
 * This program and the accompanying materials are made available under the 
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 
 * which accompanies this distribution. 
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at 
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *     01/05/2015 Rick Curtis
 *       - 455683: Automatically detect target server
 ******************************************************************************/
package org.eclipse.persistence.jpa.test.server;

import javax.persistence.EntityManagerFactory;

import org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl;
import org.eclipse.persistence.jpa.test.basic.model.Employee;
import org.eclipse.persistence.jpa.test.framework.DDLGen;
import org.eclipse.persistence.jpa.test.framework.Emf;
import org.eclipse.persistence.jpa.test.framework.EmfRunner;
import org.eclipse.persistence.jpa.test.framework.Property;
import org.eclipse.persistence.platform.server.NoServerPlatform;
import org.eclipse.persistence.platform.server.ServerPlatform;
import org.eclipse.persistence.platform.server.was.WebSphere_Liberty_Platform;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(EmfRunner.class)
public class TestTargetServerDetector {
    @Emf(name = "libertyEmf", createTables = DDLGen.NONE, classes = { Employee.class }, properties = { @Property(
            name = "eclipselink.target-server", value = "WebSphere_Liberty") })
    private EntityManagerFactory libertyEmf;

    @Emf(name = "defaultEmf", createTables = DDLGen.NONE, classes = { Employee.class })
    private EntityManagerFactory defaultEmf;

    @Test
    public void test() {
        // Test that if a target-server property is set we honor that setting
        Assert.assertTrue(getPlatform(libertyEmf) instanceof WebSphere_Liberty_Platform);
        // Test that if no target-server property is set we get the default server
        Assert.assertTrue(getPlatform(defaultEmf) instanceof NoServerPlatform);
    }

    private ServerPlatform getPlatform(EntityManagerFactory emf) {
        return ((EntityManagerFactoryImpl) emf).getServerSession().getServerPlatform();
    }

}
