package com.sun.ts.tests.jpa.core.override.table;

import com.sun.javatest.Status;
import com.sun.ts.lib.util.TestUtil;
import com.sun.ts.tests.jpa.common.PMClientBase;

import java.util.Properties;


public class Client extends PMClientBase {

    private static final Long ID = 1L;

    public Client() {
    }

    public static void main(String[] args) {
        Client theTests = new Client();
        Status s = theTests.run(args, System.out, System.err);
        s.exit();
    }

    public void setup(String[] args, Properties p) throws Fault {
        TestUtil.logTrace("setup");
        try {
            super.setup(args, p);
            removeTestData();
        } catch (Exception e) {
            TestUtil.logErr("Exception:test failed ",e);
        }
    }

    /*
     * @testName:       testNoTableAnnotation  
     * @assertion_ids:  PERSISTENCE:SPEC:1028; PERSISTENCE:SPEC:1028.1;
     * @test_Strategy:  Table and Entity are defined in orm.xml without 
     *                  using its annotation. The following test reads the 
     *                  entity and table names from the orm.xml and persists
     *                  the entity.
     */
    public void testNoTableAnnotation() throws Fault {
        NoTableAnnotation entity = new NoTableAnnotation();
        entity.setId(ID);
        try {
            getEntityTransaction().begin();
            TestUtil.logTrace("persisting entity" + entity);
            getEntityManager().persist(entity);
            TestUtil.logTrace("flushing");
            getEntityManager().flush();
            TestUtil.logTrace("Test Passed");
        } catch (Exception e) {
            TestUtil.logErr("test failed");
            throw new Fault(e);
        }
    }

    public void cleanup() throws Fault {
        TestUtil.logTrace("Cleanup data");
        removeTestData();
        TestUtil.logTrace("cleanup complete, calling super.cleanup");
        super.cleanup();
    }

    private void removeTestData() {
        TestUtil.logTrace("removeTestData");
        if (getEntityTransaction().isActive()) {
            getEntityTransaction().rollback();
        }
        try {
            getEntityTransaction().begin();
            getEntityManager().createNativeQuery("DELETE FROM NOENTITYLISTENER_TABLE").executeUpdate();
            getEntityTransaction().commit();
        } catch (Exception e) {
            TestUtil.logErr("Exception encountered while removing entities:",e);
        } finally {
            try {
                if (getEntityTransaction().isActive()) {
                    getEntityTransaction().rollback();
                }
            } catch (Exception re) {
                TestUtil.logErr("Unexpected Exception in removeTestData:", re);
            }
        }
    }

}

