/*
 * 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 java.util.Iterator;
import javax.ejb.FinderException;
import junit.framework.Assert;

import org.objectweb.jonas.jtests.beans.etype.bytearray.ByteArray;
import org.objectweb.jonas.jtests.beans.etype.bytearray.ByteArrayHome;
import org.objectweb.jonas.jtests.util.JTestCase;

/**
 * This set of test are common to CMP version 1 and CMP version 2
 * These are tests about byte array field of entity bean .
 * Beans used: etype/bytearray
 * @author Helene Joanin
 */
public abstract class A_EtypeByteArray extends JTestCase {

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

    protected boolean isInit = false;

    protected void setUp() {
        super.setUp();
        useBeans("bytearray", true);
        if (!isInit) {
            ByteArrayHome home = getHome();
            // check if tables have been initialized
            try {
                home.findByPrimaryKey("pk1");
            } catch (Exception e) { 
                // init here tables
                try {
                    home.create("pk1", new byte[] {1});
                    home.create("pk2", new byte[] {2, 2});
                    home.create("pk3", new byte[] {3, 3, 3});
                    home.create("pk4", new byte[] {4, 4, 4, 4});
                    home.create("pk5", new byte[] {5, 5, 5, 5, 5});
                    home.create("pk5bis", new byte[] {5, 5, 5, 5, 5});
                    home.create("pktoremove", new byte[] {100});
                } catch (Exception i) {
                    fail("InitialState creation problem: "+i);
                }
            }
            isInit = true;
        }
    }

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

    /**
     * findByPrimaryKey() test
     */
    public void testByteArrayFindByPk() throws Exception {
        String pk = "pk1";
        ByteArray bean = getHome().findByPrimaryKey(pk);
        Assert.assertEquals("Pk", pk, bean.getPk());
    }

    /**
     * findByF1() test
     */
    public void testByteArrayFindByF1() throws Exception {
        byte[] f1 = new byte[] {5, 5, 5, 5, 5};
        Collection cBeans = getHome().findByF1(f1);
        int nb = 0;
        Iterator iBeans = cBeans.iterator();
        while (iBeans.hasNext()) {
            ByteArray bean = (ByteArray) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
                                                                               ByteArray.class);
            Assert.assertEquals("F1", 5, bean.getF1().length);
            nb++;
        }
        assertEquals("Beans number: ", 2, nb);
    }

    /**
     * getF1() test
     */
    public void testByteArrayGetF1() throws Exception {
        String pk = "pk2";
        ByteArray bean = getHome().findByPrimaryKey(pk);
        byte[] f1 = bean.getF1();
        Assert.assertEquals("Pk", pk, bean.getPk());
        Assert.assertEquals("F1", 2, f1.length);
    }

    /**
     * setF1() test
     */
    public void testByteArraySetF1() throws Exception {
        String pk = "pk3";
        ByteArray bean = getHome().findByPrimaryKey(pk);
        int nb = 30;
        byte[] f1 = new byte[nb];
        for (int i=0; i<nb; i++) {
            f1[i] = 3;
        }
        bean.setF1(f1);
        Assert.assertEquals("Pk", pk, bean.getPk());
        Assert.assertEquals("F1", nb, bean.getF1().length);
    }

    /**
     * create() test
     */
    public void testByteArrayCreate() throws Exception {
        String pk = "pkcreated";
        byte[] f1 = new byte[] {9, 9, 9, 9, 9, 9, 9, 9, 9};
        ByteArray bean = getHome().create(pk, f1);
        Assert.assertEquals("Pk", pk, bean.getPk());
        Assert.assertEquals("F1", 9, bean.getF1().length);
        bean = getHome().findByPrimaryKey(pk);
        // cleaning
        bean.remove();
    }

    /**
     * remove() test
     */
    public void testByteArrayRemove() throws Exception {
        String pk = "pktoremove";
        ByteArray bean = getHome().findByPrimaryKey(pk);
        byte[] f1 = bean.getF1();
        bean.remove();
        try {
            getHome().findByPrimaryKey(pk);
            fail("not removed");
        } catch (FinderException e) {
            // ok
        }
        // cleaning
        getHome().create(pk, f1);
    }

}
