/*******************************************************************************
 * Copyright (c) 1998, 2008 Oracle. 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:
 *     Oracle - initial API and implementation from Oracle TopLink
 ******************************************************************************/
package org.eclipse.persistence.testing.tests.jpa.lob;

import java.lang.Integer;
import java.lang.reflect.Field;

import javax.persistence.EntityManager;
import junit.framework.*;

import org.eclipse.persistence.internal.helper.Helper;
import org.eclipse.persistence.internal.weaving.PersistenceWeaved;
import org.eclipse.persistence.testing.models.jpa.lob.Image;
import org.eclipse.persistence.testing.models.jpa.lob.ImageSimulator;
import org.eclipse.persistence.testing.models.jpa.lob.LobTableCreator;
import org.eclipse.persistence.testing.framework.junit.JUnitTestCase;

/**
 * JUnit test case(s) for the TopLink EntityMappingsXMLProcessor.
 */
public class LobJUnitTestCase extends JUnitTestCase {

    private static Integer imageId;
    private static Image originalImage;

    public LobJUnitTestCase() {
        super();
    }

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

    public static Test suite() {
        TestSuite suite = new TestSuite("Lob Model");
        suite.addTest(new LobJUnitTestCase("testSetup"));
        suite.addTest(new LobJUnitTestCase("testCreate"));
        suite.addTest(new LobJUnitTestCase("testRead"));
        suite.addTest(new LobJUnitTestCase("testUpdate"));
        suite.addTest(new LobJUnitTestCase("testDelete"));
        
        return suite;
    }
    
    /**
     * The setup is done as a test, both to record its failure, and to allow execution in the server.
     */
    public void testSetup() {
        new LobTableCreator().replaceTables(JUnitTestCase.getServerSession());
        clearCache();
    }
    
    public void testCreate() {
        EntityManager em = createEntityManager();
        beginTransaction(em);
        try {
            Image image = ImageSimulator.generateImage(4800, 4500);
            originalImage = image;
            em.persist(image);
            imageId = image.getId();
            commitTransaction(em);
        } catch (RuntimeException e) {
            if (isTransactionActive(em)) {
                rollbackTransaction(em);
            }
            closeEntityManager(em);
            throw e;
        }
    }

    public void testDelete() {
        EntityManager em = createEntityManager();
        beginTransaction(em);
        try {
            em.remove(em.find(Image.class, imageId));
            commitTransaction(em);
        } catch (RuntimeException e) {
            if (isTransactionActive(em)) {
                rollbackTransaction(em);
            }
            closeEntityManager(em);
            throw e;
        }
        assertTrue("Error deleting Image", em.find(Image.class, imageId) == null);
    }

    public void testRead() throws Exception {
        clearCache();
        EntityManager em = createEntityManager();
        Image image = em.find(Image.class, imageId);
        assertTrue(image.getId() == imageId);
        // Check lazy loading of lob, if agent was used.
        if (image instanceof PersistenceWeaved) {
            Field field = image.getClass().getDeclaredField("audio");
            field.setAccessible(true);
            if (field.get(image) != null) {
                fail("Lazy basic should not be fetched.");
            }
        }
        assertTrue("byte-arrays do not match", Helper.compareByteArrays(image.getAudio(), originalImage.getAudio()));
        assertTrue("char-arrays do not match", Helper.compareCharArrays(image.getCommentary(), originalImage.getCommentary()));
        assertTrue("Byte-arrays do not match", Helper.compareArrays(image.getPicture(), originalImage.getPicture()));
        assertTrue(image.getScript().equals(originalImage.getScript()));
    }

    public void testUpdate() {
        EntityManager em = createEntityManager();
        beginTransaction(em);
        try {
            Image image = em.find(Image.class, imageId);
            image.setAudio(null);
            image.setCommentary(null);
            image.setPicture(null);
            image.setScript(null);
            em.merge(image);
            commitTransaction(em);
        } catch (RuntimeException e) {
            if (isTransactionActive(em)) {
                rollbackTransaction(em);
            }
            closeEntityManager(em);
            throw e;
        }
        Image image = em.find(Image.class, imageId);
        assertNull(image.getAudio());
        assertNull(image.getCommentary());
        assertNull(image.getPicture());
        assertNull(image.getScript());
    }

    public static void main(String[] args) {
        junit.textui.TestRunner.run(LobJUnitTestCase.suite());
    }
}

@@ -2900,24 +2900,57 @@
 %0A        %7D%0D%0A
+        closeEntityManager(em);%0D%0A
     %7D%0D%0A%0D%0A   
@@ -3457,24 +3457,57 @@
  == null);%0D%0A
+        closeEntityManager(em);%0D%0A
     %7D%0D%0A%0D%0A   
@@ -3616,32 +3616,63 @@
 tityManager();%0D%0A
+        beginTransaction(em);%0D%0A
         Image im
@@ -3701,32 +3701,52 @@
 ass, imageId);%0D%0A
+        try %7B%0D%0A     
         assertTr
@@ -3784,16 +3784,21 @@
         
+     
 // Check
@@ -3839,16 +3839,21 @@
  used.%0D%0A
+     
         
@@ -3907,16 +3907,21 @@
         
+     
 Field fi
@@ -3983,16 +3983,21 @@
         
+    
+ 
 field.se
@@ -4012,24 +4012,29 @@
 ble(true);%0D%0A
+     
             
@@ -4082,16 +4082,21 @@
         
+    
+ 
 fail(%22La
@@ -4135,33 +4135,290 @@
 );%0D%0A            
-%7D
+     %7D%0D%0A            %7D%0D%0A             commitTransaction(em);%0D%0A        %7D catch (RuntimeException e) %7B%0D%0A            if (isTransactionActive(em)) %7B%0D%0A                rollbackTransaction(em);%0D%0A            %7D%0D%0A            closeEntityManager(em);%0D%0A            throw e;
 %0D%0A        %7D%0D%0A   
@@ -4406,32 +4406,34 @@
  e;%0D%0A        %7D%0D%0A
+%0D%0A
         assertTr
@@ -4853,24 +4853,57 @@
 cript()));%0D%0A
+        closeEntityManager(em);%0D%0A
     %7D%0D%0A%0D%0A   
@@ -5725,24 +5725,57 @@
 Script());%0D%0A
+        closeEntityManager(em);%0D%0A
     %7D%0D%0A%0D%0A   

Modify test case to add beginTransaction(em) in order to get fetched object out of transaction inside Container.