/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.jtests.clients.entity;

import java.util.Collection;

import javax.ejb.FinderException;

import junit.framework.Assert;

import org.objectweb.jonas.jtests.beans.ebasic.Account;
import org.objectweb.jonas.jtests.beans.ebasic.AccountHome;
import org.objectweb.jonas.jtests.beans.ebasic.Person;
import org.objectweb.jonas.jtests.beans.ebasic.PersonHome;
import org.objectweb.jonas.jtests.util.JTestCase;

/**
 * This set of test are all tests common to CMP version 1 and CMP version 2
 * These are tests about the different cases of the primary key type:
 * - primary key that maps to a single field in the entity bean class (java.lang.Integer),
 * - unknown primary key class at the bean development phase,
 *   (defered primary key type specification to the deployment phase).
 *   Note that the compilation of this kind of bean is already a good test !!
 * The case of the primary key maps to multiple fields in the entity bean class
 * is already tested in the "inherit" test.
 * Beans used: ebasic/Account, ebasic/Person
 * @author Helene Joanin (jonas team)
 */
public abstract class A_VariousPKEC extends JTestCase {

    public A_VariousPKEC(String name) {
        super(name);
    }

    protected void setUp() {
        super.setUp();
        useBeans("ebasic", true);
    }

    /**
     * Return AccountHome, that can be either CMP version 1 or CMP version 2 bean.
     */
    abstract public AccountHome getAccountHome();

    /**
     * Return PersonHome, that can be either CMP version 1 or CMP version 2 bean.
     */
    abstract public PersonHome getPersonHome();


    /**
     * Creation of a bean with PK that maps a single field
     */
    public void testSpkCreate() throws Exception {
        int num = 1000;
        String customer = "Gertrude";

        Account acc = getAccountHome().create(num, customer);
        Assert.assertEquals("Number", num, acc.getNumberPrimitive());
        Assert.assertEquals("Customer", customer, acc.getCustomer());

        // cleaning
        acc.remove();
	
    }

    /**
     * Remove of a bean with PK that maps a single field
     */
    public void testSpkRemove() throws Exception {
        int num = 0;
        Account acc = null;

        getAccountHome().remove(new Integer(num));
        try {
            acc = getAccountHome().findByNumber(num);
            fail("bean already exists after a remove !!");
        } catch (FinderException e) {
        }

        // cleaning
        getAccountHome().create(num, "to be removed");
	
    }

    /**
     * findByPrimaryKey of a bean with PK that maps a single field
     */
    public void testSpkFindByPrimaryKey() throws Exception {
        int num = 10;

        Account acc = getAccountHome().findByPrimaryKey(new Integer(num));
        Assert.assertEquals("Number", num, acc.getNumberPrimitive());
	
    }

    /**
     * findAll beans with PK that maps a single field
     */
    public void testSpkFindAll() throws Exception {
        Collection col = getAccountHome().findAll();
    }

    /**
     * Creation of a bean with unknown PK class at the bean development phase
     */
    public void testUpkCreate() throws Exception {
        int num = 1000;
        String name = "Gertrude";

        Person prs = getPersonHome().create(num, name);
        Assert.assertEquals("Number", num, prs.getNumberPrimitive());
        Assert.assertEquals("Customer", name, prs.getName());

        // cleaning
        prs.remove();
	
    }

    /**
     * Remove of a bean with unknown PK class at the bean development phase
     */
    public void testUpkRemove() throws Exception {
        int num = 0;
        Person prs = null;

        getPersonHome().remove(new Integer(num));
        try {
            prs = getPersonHome().findByNumber(num);
            fail("bean already exists after a remove !!");
        } catch (FinderException e) {
        }

        // cleaning
        getPersonHome().create(num, "to be removed");
	
    }

    /**
     * findByPrimaryKey of a bean with unknown PK class at the bean development phase
     */
    public void testUpkFindByPrimaryKey() throws Exception {
        int num = 10;

        Person prs = getPersonHome().findByPrimaryKey(new Integer(num));
        Assert.assertEquals("Number", num, prs.getNumberPrimitive());
	
    }

    /**
     * findAll beans with unknown PK class at the bean development phase
     */
    public void testUpkFindAll() throws Exception {
        Collection col = getPersonHome().findAll();
    }

}
