OPENJPA-1348 Add testcase to demonstrate WriteBehind cache flush operation failure using embeddable

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@825000 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Tim McConnell 2009-10-14 02:44:20 +00:00
parent f1fc076840
commit 52fdddcf38
4 changed files with 595 additions and 0 deletions

View File

@ -0,0 +1,251 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.openjpa.jdbc.writebehind.crud;
import java.util.Collection;
import org.apache.openjpa.jdbc.writebehind.entities.Embeddable01;
import org.apache.openjpa.jdbc.writebehind.entities.Entity01;
import org.apache.openjpa.jdbc.writebehind.entities.Entity02;
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
import org.apache.openjpa.writebehind.WriteBehindCache;
import org.apache.openjpa.writebehind.WriteBehindCacheManager;
import org.apache.openjpa.writebehind.WriteBehindCallback;
public class TestEmbeddable extends SingleEMFTestCase {
private OpenJPAEntityManagerSPI em = null;
private OpenJPAEntityManagerFactorySPI emf_noCache = null;
private OpenJPAEntityManagerSPI em_noCache = null;
private WriteBehindCache wbCache = null;
private WriteBehindCacheManager wbcm = null;
private WriteBehindCallback wbCallback = null;
private static Object[] cacheProps =
new Object[] {
Entity01.class, Embeddable01.class, Entity02.class, CLEAR_TABLES, RETAIN_DATA,
"openjpa.DataCache", "true",
"openjpa.RemoteCommitProvider", "sjvm",
"openjpa.WriteBehindCache", "true",
"openjpa.WriteBehindCallback", "true(sleepTime=15000)",
"openjpa.RuntimeUnenhancedClasses", "unsupported",
"openjpa.Log", "DefaultLevel=WARN"
};
private static Object [] noCacheProps =
new Object[] {
Entity01.class, Embeddable01.class, Entity02.class, CLEAR_TABLES, RETAIN_DATA,
"openjpa.RuntimeUnenhancedClasses", "unsupported",
"openjpa.Log", "DefaultLevel=WARN"
};
public void setUp() throws Exception {
//
// Create and verify the necessary WriteBehind cache objects
//
emf = createEMF(cacheProps);
assertNotNull(emf);
em = emf.createEntityManager();
assertNotNull(em);
wbcm = emf.getConfiguration().getWriteBehindCacheManagerInstance();
assertNotNull(wbcm);
wbCache = wbcm.getSystemWriteBehindCache();
assertNotNull(wbCache);
wbCallback = emf.getConfiguration().getWriteBehindCallbackInstance();
assertNotNull(wbCallback);
//
// Create and verify the non-WriteBehind cache objects
//
emf_noCache = createEMF(noCacheProps);
assertNotNull(emf_noCache);
em_noCache = emf_noCache.createEntityManager();
assertNotNull(em_noCache);
//
// Clear the persistence contexts
//
em.clear();
em_noCache.clear();
//
// Clear the WriteBehind cache
//
wbCache.clear();
assertTrue(wbCache.getSize() == 0);
assertTrue(wbCache.isEmpty());
}
public void tearDown() {
//
// WriteBehind cache EMF/EM
//
if (em != null) {
em.close();
}
if (emf != null) {
emf.close();
}
//
// Non-WriteBehind cache EMF/EF
//
if (em_noCache != null) {
em_noCache.close();
}
if (emf_noCache != null) {
emf_noCache.close();
}
}
/**
* Insert Entity01/Embeddable01 with manual flush of WriteBehind cache
*
* @exception Exception
*/
/* public void testEntity01ManualFlush() {
//
// Create a new instance of the entity/embeddable class
//
Entity01 newEntity01 = new Entity01();
newEntity01.setId(1);
newEntity01.setEnt01_str01("AA");
newEntity01.setEnt01_str02("BBBB");
newEntity01.setEnt01_str03("CCCCCCCC");
newEntity01.setEnt01_int01(1);
newEntity01.setEnt01_int02(2);
newEntity01.setEnt01_int03(3);
newEntity01.setEmb01_int01(4);
newEntity01.setEmb01_int02(5);
newEntity01.setEmb01_int03(6);
//
// Persist the new entity/embeddable in the WriteBehind cache
//
em.getTransaction().begin();
em.persist(newEntity01);
em.getTransaction().commit();
//
// Verify the entity was saved in the WriteBehind cache
//
assertTrue(wbCache.getSize() > 0);
assertFalse(wbCache.isEmpty());
assertTrue(wbCache.contains(newEntity01));
//
// Verify the entity has not yet been saved in the database
//
Entity01 findEntity01 = em_noCache.find(Entity01.class, 1);
assertNull(findEntity01);
//
// Flush the WriteBehind cache
//
Collection<Exception> exceptions = wbCallback.flush();
assertTrue(exceptions.size() == 0);
assertTrue(wbCache.getSize() == 0);
assertTrue(wbCache.isEmpty());
//
// Verify the entity/embeddable has now been saved in the database
//
em_noCache.clear();
findEntity01 = em_noCache.find(Entity01.class, 1);
assertNotNull(findEntity01);
assertEquals(findEntity01.getId(), 1);
assertEquals(findEntity01.getEnt01_str01(), "AA");
assertEquals(findEntity01.getEnt01_str02(), "BBBB");
assertEquals(findEntity01.getEnt01_str03(), "CCCCCCCC");
assertEquals(findEntity01.getEnt01_int01(), 1);
assertEquals(findEntity01.getEnt01_int02(), 2);
assertEquals(findEntity01.getEnt01_int03(), 3);
assertEquals(fndEntity01.getEmb01_int01(), 4);
assertEquals(findEntity01.getEmb01_int02(), 5);
assertEquals(findEntity01.getEmb01_int03(), 6);
} */
/**
* Insert Entity02 with manual flush of WriteBehind cache
*
* @exception Exception
*/
public void testEntity02ManualFlush() {
//
// Create a new instance of the entity class
//
Entity02 newEntity02 = new Entity02();
newEntity02.setId(2);
newEntity02.setEnt02_str01("DD");
newEntity02.setEnt02_str02("EEEE");
newEntity02.setEnt02_str03("FFFFFFFF");
newEntity02.setEnt02_int01(7);
newEntity02.setEnt02_int02(8);
newEntity02.setEnt02_int03(9);
//
// Persist the new entity in the WriteBehind cache
//
em.getTransaction().begin();
em.persist(newEntity02);
em.getTransaction().commit();
//
// Verify the entity was saved in the WriteBehind cache
//
assertTrue(wbCache.getSize() > 0);
assertFalse(wbCache.isEmpty());
assertTrue(wbCache.contains(newEntity02));
//
// Verify the entity has not yet been saved in the database
//
Entity02 findEntity02 = em_noCache.find(Entity02.class, 2);
assertNull(findEntity02);
//
// Flush the WriteBehind cache
//
Collection<Exception> exceptions = wbCallback.flush();
assertTrue(exceptions.size() == 0);
assertTrue(wbCache.getSize() == 0);
assertTrue(wbCache.isEmpty());
//
// Verify the entity has now been saved in the database
//
em_noCache.clear();
findEntity02 = em_noCache.find(Entity02.class, 2);
assertNotNull(findEntity02);
assertEquals(findEntity02.getId(), 2);
assertEquals(findEntity02.getEnt02_str01(), "DD");
assertEquals(findEntity02.getEnt02_str02(), "EEEE");
assertEquals(findEntity02.getEnt02_str03(), "FFFFFFFF");
assertEquals(findEntity02.getEnt02_int01(), 7);
assertEquals(findEntity02.getEnt02_int02(), 8);
assertEquals(findEntity02.getEnt02_int03(), 9);
}
}

View File

@ -0,0 +1,70 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.openjpa.jdbc.writebehind.entities;
import javax.persistence.Embeddable;
@Embeddable
public class Embeddable01 {
private int emb01_int01;
private int emb01_int02;
private int emb01_int03;
public Embeddable01() {
}
public Embeddable01(int emb01_int01,
int emb01_int02,
int emb01_int03) {
this.emb01_int01 = emb01_int01;
this.emb01_int02 = emb01_int02;
this.emb01_int03 = emb01_int03;
}
public String toString() {
return( "Embeddable01: " +
" emb01_int01: " + getEmb01_int01() +
" emb01_int02: " + getEmb01_int02() +
" emb01_int03: " + getEmb01_int03() );
}
//----------------------------------------------------------------------------------------------
// Embeddable01 fields
//----------------------------------------------------------------------------------------------
public int getEmb01_int01() {
return emb01_int01;
}
public void setEmb01_int01(int ii) {
this.emb01_int01 = ii;
}
public int getEmb01_int02() {
return emb01_int02;
}
public void setEmb01_int02(int ii) {
this.emb01_int02 = ii;
}
public int getEmb01_int03() {
return emb01_int03;
}
public void setEmb01_int03(int ii) {
this.emb01_int03 = ii;
}
}

View File

@ -0,0 +1,155 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.openjpa.jdbc.writebehind.entities;
import java.io.Serializable;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="WB_Entity01")
public class Entity01 implements Serializable {
private static final long serialVersionUID = 2961572787273807912L;
@Id
private int id;
private String ent01_str01;
private String ent01_str02;
private String ent01_str03;
private int ent01_int01;
private int ent01_int02;
private int ent01_int03;
@Embedded
private Embeddable01 embeddable01;
public Entity01() {
embeddable01 = new Embeddable01();
}
public Entity01(String ent01_str01,
String ent01_str02,
String ent01_str03,
int ent01_int01,
int ent01_int02,
int ent01_int03,
Embeddable01 embeddable01) {
this.ent01_str01 = ent01_str01;
this.ent01_str02 = ent01_str02;
this.ent01_str02 = ent01_str03;
this.ent01_int01 = ent01_int01;
this.ent01_int02 = ent01_int02;
this.ent01_int03 = ent01_int03;
this.embeddable01 = embeddable01;
}
public String toString() {
return( "Entity01: id: " + getId() +
" ent01_str01: " + getEnt01_str01() +
" ent01_str02: " + getEnt01_str02() +
" ent01_str03: " + getEnt01_str03() +
" ent01_int01: " + getEnt01_int01() +
" ent01_int02: " + getEnt01_int02() +
" ent01_int03: " + getEnt01_int03() +
" embeddable01: " + getEmbeddable01() );
}
//----------------------------------------------------------------------------------------------
// Entity01 fields
//----------------------------------------------------------------------------------------------
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEnt01_str01() {
return ent01_str01;
}
public void setEnt01_str01(String str) {
this.ent01_str01 = str;
}
public String getEnt01_str02() {
return ent01_str02;
}
public void setEnt01_str02(String str) {
this.ent01_str02 = str;
}
public String getEnt01_str03() {
return ent01_str03;
}
public void setEnt01_str03(String str) {
this.ent01_str03 = str;
}
public int getEnt01_int01() {
return ent01_int01;
}
public void setEnt01_int01(int ii) {
this.ent01_int01 = ii;
}
public int getEnt01_int02() {
return ent01_int02;
}
public void setEnt01_int02(int ii) {
this.ent01_int02 = ii;
}
public int getEnt01_int03() {
return ent01_int03;
}
public void setEnt01_int03(int ii) {
this.ent01_int03 = ii;
}
public Embeddable01 getEmbeddable01() {
return embeddable01;
}
public void setEmbeddable01(Embeddable01 embeddable01) {
this.embeddable01 = embeddable01;
}
//----------------------------------------------------------------------------------------------
// Embeddable01 fields
//----------------------------------------------------------------------------------------------
public int getEmb01_int01() {
return embeddable01.getEmb01_int01();
}
public void setEmb01_int01(int ii) {
embeddable01.setEmb01_int01(ii);
}
public int getEmb01_int02() {
return embeddable01.getEmb01_int02();
}
public void setEmb01_int02(int ii) {
embeddable01.setEmb01_int02(ii);
}
public int getEmb01_int03() {
return embeddable01.getEmb01_int03();
}
public void setEmb01_int03(int ii) {
embeddable01.setEmb01_int03(ii);
}
}

View File

@ -0,0 +1,119 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.openjpa.jdbc.writebehind.entities;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="WB_Entity02")
public class Entity02 implements Serializable {
private static final long serialVersionUID = 2961572787273807912L;
@Id
private int id;
private String ent02_str01;
private String ent02_str02;
private String ent02_str03;
private int ent02_int01;
private int ent02_int02;
private int ent02_int03;
public Entity02() {
}
public Entity02(String ent02_str01,
String ent02_str02,
String ent02_str03,
int ent02_int01,
int ent02_int02,
int ent02_int03) {
this.ent02_str01 = ent02_str01;
this.ent02_str02 = ent02_str02;
this.ent02_str02 = ent02_str03;
this.ent02_int01 = ent02_int01;
this.ent02_int02 = ent02_int02;
this.ent02_int03 = ent02_int03;
}
public String toString() {
return( "Entity02: id: " + getId() +
" ent02_str01: " + getEnt02_str01() +
" ent02_str02: " + getEnt02_str02() +
" ent02_str03: " + getEnt02_str03() +
" ent02_int01: " + getEnt02_int01() +
" ent02_int02: " + getEnt02_int02() +
" ent02_int03: " + getEnt02_int03() );
}
//----------------------------------------------------------------------------------------------
// Entity02 fields
//----------------------------------------------------------------------------------------------
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEnt02_str01() {
return ent02_str01;
}
public void setEnt02_str01(String str) {
this.ent02_str01 = str;
}
public String getEnt02_str02() {
return ent02_str02;
}
public void setEnt02_str02(String str) {
this.ent02_str02 = str;
}
public String getEnt02_str03() {
return ent02_str03;
}
public void setEnt02_str03(String str) {
this.ent02_str03 = str;
}
public int getEnt02_int01() {
return ent02_int01;
}
public void setEnt02_int01(int ii) {
this.ent02_int01 = ii;
}
public int getEnt02_int02() {
return ent02_int02;
}
public void setEnt02_int02(int ii) {
this.ent02_int02 = ii;
}
public int getEnt02_int03() {
return ent02_int03;
}
public void setEnt02_int03(int ii) {
this.ent02_int03 = ii;
}
}