mirror of https://github.com/apache/openjpa.git
OpenJPA-1610: Changed two methods (one name change, one signature change) to allow applications using OpenJPA 1.x to run with the JPA 2.0 jar on the classpath after recompiling the applications and correcting any use of the two methods that have changed.
git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/1.1.x@930357 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
e910221a63
commit
e6104b82b9
File diff suppressed because it is too large
Load Diff
|
@ -1,148 +1,148 @@
|
|||
/*
|
||||
* 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.persistence.jdbc.annotations;
|
||||
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
import org.apache.openjpa.jdbc.meta.strats.*;
|
||||
import org.apache.openjpa.persistence.*;
|
||||
import org.apache.openjpa.persistence.jdbc.*;
|
||||
|
||||
|
||||
@Entity
|
||||
@Table(name = "NONSTD_ENTITY")
|
||||
@DataStoreIdColumn(name = "OID")
|
||||
@DiscriminatorStrategy(ClassNameDiscriminatorStrategy.ALIAS)
|
||||
@DiscriminatorColumn(name = "DISCRIM", length = 128)
|
||||
@XMappingOverride(name = "superCollection",
|
||||
containerTable = @ContainerTable(name = "SUP_COLL",
|
||||
joinColumns = @XJoinColumn(name = "OWNER")),
|
||||
elementColumns = @ElementColumn(name = "SUP_ELEM"))
|
||||
public class NonstandardMappingEntity
|
||||
extends NonstandardMappingMappedSuper {
|
||||
|
||||
@Persistent(fetch = FetchType.LAZY)
|
||||
@Strategy("org.apache.openjpa.persistence.jdbc.annotations.PointHandler")
|
||||
@Columns({
|
||||
@Column(name = "X_COL"),
|
||||
@Column(name = "Y_COL")
|
||||
})
|
||||
@Index(name = "PNT_IDX")
|
||||
private Point custom;
|
||||
|
||||
@PersistentCollection(elementType = String.class)
|
||||
@ContainerTable(name = "STRINGS_COLL",
|
||||
joinColumns = @XJoinColumn(name = "OWNER"),
|
||||
joinIndex = @Index(enabled = false))
|
||||
@ElementColumn(name = "STR_ELEM", length = 127)
|
||||
@OrderColumn(name = "ORDER_COL")
|
||||
@ElementIndex
|
||||
private List stringCollection = new ArrayList();
|
||||
|
||||
@PersistentCollection
|
||||
@ContainerTable(name = "JOIN_COLL",
|
||||
joinColumns = @XJoinColumn(name = "OWNER"),
|
||||
joinForeignKey = @ForeignKey)
|
||||
@ElementJoinColumn(name = "JOIN_ELEM")
|
||||
@ElementForeignKey
|
||||
@ElementNonpolymorphic(NonpolymorphicType.JOINABLE)
|
||||
private List<NonstandardMappingEntity> joinCollection =
|
||||
new ArrayList<NonstandardMappingEntity>();
|
||||
|
||||
@PersistentMap(keyType = String.class, elementType = String.class)
|
||||
@ContainerTable(name = "STRINGS_MAP",
|
||||
joinColumns = @XJoinColumn(name = "OWNER"),
|
||||
joinIndex = @Index(enabled = false))
|
||||
@KeyColumn(name = "STR_KEY", length = 127)
|
||||
@ElementColumn(name = "STR_VAL", length = 127)
|
||||
@KeyIndex
|
||||
@ElementIndex
|
||||
private Map stringMap = new HashMap();
|
||||
|
||||
@PersistentMap
|
||||
@ContainerTable(name = "JOIN_MAP",
|
||||
joinColumns = @XJoinColumn(name = "OWNER"),
|
||||
joinForeignKey = @ForeignKey)
|
||||
@KeyJoinColumn(name = "JOIN_KEY")
|
||||
@KeyForeignKey
|
||||
@KeyNonpolymorphic
|
||||
@ElementJoinColumn(name = "JOIN_VAL")
|
||||
@ElementForeignKey
|
||||
@ElementNonpolymorphic
|
||||
private Map<NonstandardMappingEntity, NonstandardMappingEntity> joinMap =
|
||||
new HashMap<NonstandardMappingEntity, NonstandardMappingEntity>();
|
||||
|
||||
@Embedded
|
||||
@EmbeddedMapping(nullIndicatorAttributeName = "uuid", overrides = {
|
||||
@MappingOverride(name = "rel",
|
||||
joinColumns = @XJoinColumn(name = "EM_REL_ID")),
|
||||
@MappingOverride(name = "eager",
|
||||
containerTable = @ContainerTable(name = "EM_EAGER"),
|
||||
elementJoinColumns = @ElementJoinColumn(name = "ELEM_EAGER_ID"))
|
||||
})
|
||||
private ExtensionsEntity embed;
|
||||
|
||||
@PersistentCollection(elementEmbedded = true)
|
||||
@ContainerTable(name = "EMBED_COLL")
|
||||
@ElementEmbeddedMapping(overrides = {
|
||||
@XMappingOverride(name = "basic", columns = @Column(name = "EM_BASIC"))
|
||||
})
|
||||
private List<EmbedValue2> embedCollection = new ArrayList<EmbedValue2>();
|
||||
|
||||
public Point getCustom() {
|
||||
return this.custom;
|
||||
}
|
||||
|
||||
public void setCustom(Point custom) {
|
||||
this.custom = custom;
|
||||
}
|
||||
|
||||
public List getStringCollection() {
|
||||
return this.stringCollection;
|
||||
}
|
||||
|
||||
public List<NonstandardMappingEntity> getJoinCollection() {
|
||||
return this.joinCollection;
|
||||
}
|
||||
|
||||
public Map getStringMap() {
|
||||
return this.stringMap;
|
||||
}
|
||||
|
||||
public Map<NonstandardMappingEntity,NonstandardMappingEntity> getJoinMap() {
|
||||
return this.joinMap;
|
||||
}
|
||||
|
||||
public ExtensionsEntity getEmbed() {
|
||||
return this.embed;
|
||||
}
|
||||
|
||||
public void setEmbed(ExtensionsEntity embed) {
|
||||
this.embed = embed;
|
||||
}
|
||||
|
||||
public List<EmbedValue2> getEmbedCollection() {
|
||||
return this.embedCollection;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* 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.persistence.jdbc.annotations;
|
||||
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
import org.apache.openjpa.jdbc.meta.strats.*;
|
||||
import org.apache.openjpa.persistence.*;
|
||||
import org.apache.openjpa.persistence.jdbc.*;
|
||||
|
||||
|
||||
@Entity
|
||||
@Table(name = "NONSTD_ENTITY")
|
||||
@DataStoreIdColumn(name = "OID")
|
||||
@DiscriminatorStrategy(ClassNameDiscriminatorStrategy.ALIAS)
|
||||
@DiscriminatorColumn(name = "DISCRIM", length = 128)
|
||||
@XMappingOverride(name = "superCollection",
|
||||
containerTable = @ContainerTable(name = "SUP_COLL",
|
||||
joinColumns = @XJoinColumn(name = "OWNER")),
|
||||
elementColumns = @ElementColumn(name = "SUP_ELEM"))
|
||||
public class NonstandardMappingEntity
|
||||
extends NonstandardMappingMappedSuper {
|
||||
|
||||
@Persistent(fetch = FetchType.LAZY)
|
||||
@Strategy("org.apache.openjpa.persistence.jdbc.annotations.PointHandler")
|
||||
@Columns({
|
||||
@Column(name = "X_COL"),
|
||||
@Column(name = "Y_COL")
|
||||
})
|
||||
@Index(name = "PNT_IDX")
|
||||
private Point custom;
|
||||
|
||||
@PersistentCollection(elementType = String.class)
|
||||
@ContainerTable(name = "STRINGS_COLL",
|
||||
joinColumns = @XJoinColumn(name = "OWNER"),
|
||||
joinIndex = @Index(enabled = false))
|
||||
@ElementColumn(name = "STR_ELEM", length = 127)
|
||||
@org.apache.openjpa.persistence.jdbc.OrderColumn(name = "ORDER_COL")
|
||||
@ElementIndex
|
||||
private List stringCollection = new ArrayList();
|
||||
|
||||
@PersistentCollection
|
||||
@ContainerTable(name = "JOIN_COLL",
|
||||
joinColumns = @XJoinColumn(name = "OWNER"),
|
||||
joinForeignKey = @ForeignKey)
|
||||
@ElementJoinColumn(name = "JOIN_ELEM")
|
||||
@ElementForeignKey
|
||||
@ElementNonpolymorphic(NonpolymorphicType.JOINABLE)
|
||||
private List<NonstandardMappingEntity> joinCollection =
|
||||
new ArrayList<NonstandardMappingEntity>();
|
||||
|
||||
@PersistentMap(keyType = String.class, elementType = String.class)
|
||||
@ContainerTable(name = "STRINGS_MAP",
|
||||
joinColumns = @XJoinColumn(name = "OWNER"),
|
||||
joinIndex = @Index(enabled = false))
|
||||
@KeyColumn(name = "STR_KEY", length = 127)
|
||||
@ElementColumn(name = "STR_VAL", length = 127)
|
||||
@KeyIndex
|
||||
@ElementIndex
|
||||
private Map stringMap = new HashMap();
|
||||
|
||||
@PersistentMap
|
||||
@ContainerTable(name = "JOIN_MAP",
|
||||
joinColumns = @XJoinColumn(name = "OWNER"),
|
||||
joinForeignKey = @ForeignKey)
|
||||
@KeyJoinColumn(name = "JOIN_KEY")
|
||||
@KeyForeignKey
|
||||
@KeyNonpolymorphic
|
||||
@ElementJoinColumn(name = "JOIN_VAL")
|
||||
@ElementForeignKey
|
||||
@ElementNonpolymorphic
|
||||
private Map<NonstandardMappingEntity, NonstandardMappingEntity> joinMap =
|
||||
new HashMap<NonstandardMappingEntity, NonstandardMappingEntity>();
|
||||
|
||||
@Embedded
|
||||
@EmbeddedMapping(nullIndicatorAttributeName = "uuid", overrides = {
|
||||
@MappingOverride(name = "rel",
|
||||
joinColumns = @XJoinColumn(name = "EM_REL_ID")),
|
||||
@MappingOverride(name = "eager",
|
||||
containerTable = @ContainerTable(name = "EM_EAGER"),
|
||||
elementJoinColumns = @ElementJoinColumn(name = "ELEM_EAGER_ID"))
|
||||
})
|
||||
private ExtensionsEntity embed;
|
||||
|
||||
@PersistentCollection(elementEmbedded = true)
|
||||
@ContainerTable(name = "EMBED_COLL")
|
||||
@ElementEmbeddedMapping(overrides = {
|
||||
@XMappingOverride(name = "basic", columns = @Column(name = "EM_BASIC"))
|
||||
})
|
||||
private List<EmbedValue2> embedCollection = new ArrayList<EmbedValue2>();
|
||||
|
||||
public Point getCustom() {
|
||||
return this.custom;
|
||||
}
|
||||
|
||||
public void setCustom(Point custom) {
|
||||
this.custom = custom;
|
||||
}
|
||||
|
||||
public List getStringCollection() {
|
||||
return this.stringCollection;
|
||||
}
|
||||
|
||||
public List<NonstandardMappingEntity> getJoinCollection() {
|
||||
return this.joinCollection;
|
||||
}
|
||||
|
||||
public Map getStringMap() {
|
||||
return this.stringMap;
|
||||
}
|
||||
|
||||
public Map<NonstandardMappingEntity,NonstandardMappingEntity> getJoinMap() {
|
||||
return this.joinMap;
|
||||
}
|
||||
|
||||
public ExtensionsEntity getEmbed() {
|
||||
return this.embed;
|
||||
}
|
||||
|
||||
public void setEmbed(ExtensionsEntity embed) {
|
||||
this.embed = embed;
|
||||
}
|
||||
|
||||
public List<EmbedValue2> getEmbedCollection() {
|
||||
return this.embedCollection;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,242 +1,242 @@
|
|||
/*
|
||||
* TestCalendarFields.java
|
||||
*
|
||||
* Created on October 9, 2006, 6:12 PM
|
||||
*
|
||||
* To change this template, choose Tools | Template Manager
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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.persistence.kernel;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
|
||||
|
||||
import org.apache.openjpa.persistence.kernel.common.apps.CalendarFields;
|
||||
|
||||
import org.apache.openjpa.kernel.OpenJPAStateManager;
|
||||
import org.apache.openjpa.lib.util.JavaVersions;
|
||||
import org.apache.openjpa.persistence.OpenJPAEntityManager;
|
||||
|
||||
public class TestCalendarFields extends BaseKernelTest {
|
||||
|
||||
/**
|
||||
* Creates a new instance of TestCalendarFields
|
||||
*/
|
||||
public TestCalendarFields(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public void setUp() {
|
||||
deleteAll(CalendarFields.class);
|
||||
}
|
||||
|
||||
public void testFieldDefaultTimeZone() {
|
||||
CalendarFields cal = new CalendarFields();
|
||||
|
||||
OpenJPAEntityManager pm;
|
||||
|
||||
pm = getPM();
|
||||
startTx(pm);
|
||||
cal.setSingapore(Calendar.
|
||||
getInstance(TimeZone.getTimeZone("America/New_York")));
|
||||
pm.persist(cal);
|
||||
int id = cal.getId();
|
||||
endTx(pm);
|
||||
endEm(pm);
|
||||
|
||||
pm = getPM();
|
||||
cal = (CalendarFields) pm.find(CalendarFields.class, id);
|
||||
assertEquals(TimeZone.getTimeZone("Asia/Singapore"),
|
||||
cal.getSingapore().getTimeZone());
|
||||
endEm(pm);
|
||||
}
|
||||
|
||||
public void testTimeZoneEquals() {
|
||||
CalendarFields c1 = new CalendarFields();
|
||||
CalendarFields c2 = new CalendarFields();
|
||||
assertTimeZonesEquals(c1, c2);
|
||||
|
||||
OpenJPAEntityManager pm;
|
||||
|
||||
pm = getPM();
|
||||
startTx(pm);
|
||||
pm.persist(c2);
|
||||
int id2 = c2.getId();
|
||||
assertTimeZonesEquals(c1, c2);
|
||||
endTx(pm);
|
||||
assertTimeZonesEquals(c1, c2);
|
||||
endEm(pm);
|
||||
|
||||
pm = getPM();
|
||||
c2 = (CalendarFields) pm.find(CalendarFields.class, id2);
|
||||
assertTimeZonesEquals(c1, c2);
|
||||
assertTimeZonesEquals(c1, (CalendarFields) pm.detach(c2));
|
||||
endEm(pm);
|
||||
}
|
||||
|
||||
public void testCalendarQuery() {
|
||||
long time = 1136660560572L;
|
||||
|
||||
CalendarFields cal = new CalendarFields();
|
||||
|
||||
OpenJPAEntityManager pm;
|
||||
|
||||
pm = getPM();
|
||||
startTx(pm);
|
||||
cal.getSingapore().setTime(new Date(time));
|
||||
pm.persist(cal);
|
||||
int id = cal.getId();
|
||||
endTx(pm);
|
||||
endEm(pm);
|
||||
|
||||
pm = getPM();
|
||||
|
||||
//FIXME jthomas
|
||||
Date date = new Date(time + 100000);
|
||||
Calendar cals = Calendar.getInstance();
|
||||
cals.setTime(date);
|
||||
|
||||
String query =
|
||||
"SELECT o FROM CalendarFields o WHERE o.singapore < :cal";
|
||||
int size = pm.createQuery(query).setParameter("cal", cals).
|
||||
getResultList().size();
|
||||
|
||||
assertEquals(1, size);
|
||||
|
||||
// assertSize(0, pm.newQuery(CalendarFields.class, "singapore < :date").execute(new Date(time - 100000)));
|
||||
//
|
||||
// assertSize(0, pm.newQuery(CalendarFields.class, "singapore > :date").execute(new Date(time + 100000)));
|
||||
// assertSize(1, pm.newQuery(CalendarFields.class, "singapore > :date").execute(new Date(time - 100000)));
|
||||
//
|
||||
// assertSize(1, pm.newQuery(CalendarFields.class, "singapore < :date").execute(newCalendar(new Date(time + 100000), null)));
|
||||
// assertSize(0, pm.newQuery(CalendarFields.class, "singapore < :date").execute(newCalendar(new Date(time - 100000), null)));
|
||||
//
|
||||
// assertSize(0, pm.newQuery(CalendarFields.class, "singapore > :date").execute(newCalendar(new Date(time + 100000), null)));
|
||||
// assertSize(1, pm.newQuery(CalendarFields.class, "singapore > :date").execute(newCalendar(new Date(time - 100000), null)));
|
||||
|
||||
endEm(pm);
|
||||
}
|
||||
|
||||
private static Calendar newCalendar(Date date, String tz) {
|
||||
Calendar cal = Calendar.getInstance(
|
||||
tz == null ? TimeZone.getDefault() : TimeZone.getTimeZone(tz));
|
||||
cal.setTime(date);
|
||||
return cal;
|
||||
}
|
||||
|
||||
public void testMutateCalendarDirties() {
|
||||
CalendarFields c1 = new CalendarFields();
|
||||
|
||||
OpenJPAEntityManager pm;
|
||||
|
||||
pm = getPM();
|
||||
startTx(pm);
|
||||
pm.persist(c1);
|
||||
int id = c1.getId();
|
||||
endTx(pm);
|
||||
endEm(pm);
|
||||
|
||||
pm = getPM();
|
||||
c1 = pm.find(CalendarFields.class, id);
|
||||
|
||||
startTx(pm);
|
||||
|
||||
OpenJPAStateManager sm = getStateManager(c1, pm);
|
||||
|
||||
assertFalse(sm.getDirty().get(sm.getMetaData().
|
||||
getField("gmt").getIndex()));
|
||||
|
||||
// test setting to same value doesn't dirty the field
|
||||
/*
|
||||
setTimeInMillis(c1.getGmtc1.getGmt().getTime().getTime());
|
||||
c1.getGmt().setTime(c1.getGmt().getTime());
|
||||
assertFalse(sm.getDirty().get(sm.getMetaData().
|
||||
getField("gmt").getIndex()));
|
||||
*/
|
||||
|
||||
// test changing time
|
||||
setTimeInMillis(c1.getGmt(), 12345);
|
||||
assertTrue(sm.getDirty().get(sm.getMetaData().
|
||||
getField("gmt").getIndex()));
|
||||
|
||||
assertFalse(sm.getDirty().get(sm.getMetaData().
|
||||
getField("newYork").getIndex()));
|
||||
// test mutate via "add()" method
|
||||
c1.getNewYork().add(Calendar.SECOND, -1);
|
||||
assertTrue(sm.getDirty().get(sm.getMetaData().
|
||||
getField("newYork").getIndex()));
|
||||
|
||||
assertFalse(sm.getDirty().get(sm.getMetaData().
|
||||
getField("berlin").getIndex()));
|
||||
// test mutate via "setTimeZone()" method
|
||||
c1.getBerlin().setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||
assertTrue(sm.getDirty().get(sm.getMetaData().
|
||||
getField("berlin").getIndex()));
|
||||
|
||||
// Calendar.set can only be subclassed in JDK 1.4+ (it is final in
|
||||
// 1.3), so we only run this test in JDK 1.4+
|
||||
if (JavaVersions.VERSION >= 4) {
|
||||
assertFalse(sm.getDirty().get(sm.getMetaData().
|
||||
getField("singapore").getIndex()));
|
||||
// test mutate via "set()" method
|
||||
c1.getSingapore().set(Calendar.YEAR, 1998);
|
||||
assertTrue(sm.getDirty().get(sm.getMetaData().
|
||||
getField("singapore").getIndex()));
|
||||
}
|
||||
|
||||
assertFalse(sm.getDirty().get(sm.getMetaData().
|
||||
getField("pacific").getIndex()));
|
||||
// test mutate via "roll()" method
|
||||
c1.getPacific().roll(Calendar.YEAR, 5);
|
||||
assertTrue(sm.getDirty().get(sm.getMetaData().
|
||||
getField("pacific").getIndex()));
|
||||
|
||||
endTx(pm);
|
||||
endEm(pm);
|
||||
}
|
||||
|
||||
private static void setTimeInMillis(Calendar cal, long millis) {
|
||||
// "setTimeInMillis" is protected in JDK 1.3, put public in 1.4 & 1.5
|
||||
try {
|
||||
// Equivalent to: cal.setTimeInMillis (millis);
|
||||
cal.getClass().getMethod("setTimeInMillis",
|
||||
new Class[]{ long.class }).invoke(cal,
|
||||
new Object[]{ new Long(millis) });
|
||||
} catch (Exception e) {
|
||||
cal.setTime(new Date(millis));
|
||||
}
|
||||
}
|
||||
|
||||
private void assertTimeZonesEquals(CalendarFields c1, CalendarFields c2) {
|
||||
for (int i = 0; i < c1.getCalendars().size(); i++) {
|
||||
Calendar cal1 = (Calendar) c1.getCalendars().get(i);
|
||||
Calendar cal2 = (Calendar) c2.getCalendars().get(i);
|
||||
|
||||
if (cal1 != null && cal2 != null)
|
||||
assertEquals(cal1.getTimeZone().getID(),
|
||||
cal2.getTimeZone().getID());
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
* TestCalendarFields.java
|
||||
*
|
||||
* Created on October 9, 2006, 6:12 PM
|
||||
*
|
||||
* To change this template, choose Tools | Template Manager
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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.persistence.kernel;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
|
||||
|
||||
import org.apache.openjpa.persistence.kernel.common.apps.CalendarFields;
|
||||
|
||||
import org.apache.openjpa.kernel.OpenJPAStateManager;
|
||||
import org.apache.openjpa.lib.util.JavaVersions;
|
||||
import org.apache.openjpa.persistence.OpenJPAEntityManager;
|
||||
|
||||
public class TestCalendarFields extends BaseKernelTest {
|
||||
|
||||
/**
|
||||
* Creates a new instance of TestCalendarFields
|
||||
*/
|
||||
public TestCalendarFields(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public void setUp() {
|
||||
deleteAll(CalendarFields.class);
|
||||
}
|
||||
|
||||
public void testFieldDefaultTimeZone() {
|
||||
CalendarFields cal = new CalendarFields();
|
||||
|
||||
OpenJPAEntityManager pm;
|
||||
|
||||
pm = getPM();
|
||||
startTx(pm);
|
||||
cal.setSingapore(Calendar.
|
||||
getInstance(TimeZone.getTimeZone("America/New_York")));
|
||||
pm.persist(cal);
|
||||
int id = cal.getId();
|
||||
endTx(pm);
|
||||
endEm(pm);
|
||||
|
||||
pm = getPM();
|
||||
cal = (CalendarFields) pm.find(CalendarFields.class, id);
|
||||
assertEquals(TimeZone.getTimeZone("Asia/Singapore"),
|
||||
cal.getSingapore().getTimeZone());
|
||||
endEm(pm);
|
||||
}
|
||||
|
||||
public void testTimeZoneEquals() {
|
||||
CalendarFields c1 = new CalendarFields();
|
||||
CalendarFields c2 = new CalendarFields();
|
||||
assertTimeZonesEquals(c1, c2);
|
||||
|
||||
OpenJPAEntityManager pm;
|
||||
|
||||
pm = getPM();
|
||||
startTx(pm);
|
||||
pm.persist(c2);
|
||||
int id2 = c2.getId();
|
||||
assertTimeZonesEquals(c1, c2);
|
||||
endTx(pm);
|
||||
assertTimeZonesEquals(c1, c2);
|
||||
endEm(pm);
|
||||
|
||||
pm = getPM();
|
||||
c2 = (CalendarFields) pm.find(CalendarFields.class, id2);
|
||||
assertTimeZonesEquals(c1, c2);
|
||||
assertTimeZonesEquals(c1, (CalendarFields) pm.detachCopy(c2));
|
||||
endEm(pm);
|
||||
}
|
||||
|
||||
public void testCalendarQuery() {
|
||||
long time = 1136660560572L;
|
||||
|
||||
CalendarFields cal = new CalendarFields();
|
||||
|
||||
OpenJPAEntityManager pm;
|
||||
|
||||
pm = getPM();
|
||||
startTx(pm);
|
||||
cal.getSingapore().setTime(new Date(time));
|
||||
pm.persist(cal);
|
||||
int id = cal.getId();
|
||||
endTx(pm);
|
||||
endEm(pm);
|
||||
|
||||
pm = getPM();
|
||||
|
||||
//FIXME jthomas
|
||||
Date date = new Date(time + 100000);
|
||||
Calendar cals = Calendar.getInstance();
|
||||
cals.setTime(date);
|
||||
|
||||
String query =
|
||||
"SELECT o FROM CalendarFields o WHERE o.singapore < :cal";
|
||||
int size = pm.createQuery(query).setParameter("cal", cals).
|
||||
getResultList().size();
|
||||
|
||||
assertEquals(1, size);
|
||||
|
||||
// assertSize(0, pm.newQuery(CalendarFields.class, "singapore < :date").execute(new Date(time - 100000)));
|
||||
//
|
||||
// assertSize(0, pm.newQuery(CalendarFields.class, "singapore > :date").execute(new Date(time + 100000)));
|
||||
// assertSize(1, pm.newQuery(CalendarFields.class, "singapore > :date").execute(new Date(time - 100000)));
|
||||
//
|
||||
// assertSize(1, pm.newQuery(CalendarFields.class, "singapore < :date").execute(newCalendar(new Date(time + 100000), null)));
|
||||
// assertSize(0, pm.newQuery(CalendarFields.class, "singapore < :date").execute(newCalendar(new Date(time - 100000), null)));
|
||||
//
|
||||
// assertSize(0, pm.newQuery(CalendarFields.class, "singapore > :date").execute(newCalendar(new Date(time + 100000), null)));
|
||||
// assertSize(1, pm.newQuery(CalendarFields.class, "singapore > :date").execute(newCalendar(new Date(time - 100000), null)));
|
||||
|
||||
endEm(pm);
|
||||
}
|
||||
|
||||
private static Calendar newCalendar(Date date, String tz) {
|
||||
Calendar cal = Calendar.getInstance(
|
||||
tz == null ? TimeZone.getDefault() : TimeZone.getTimeZone(tz));
|
||||
cal.setTime(date);
|
||||
return cal;
|
||||
}
|
||||
|
||||
public void testMutateCalendarDirties() {
|
||||
CalendarFields c1 = new CalendarFields();
|
||||
|
||||
OpenJPAEntityManager pm;
|
||||
|
||||
pm = getPM();
|
||||
startTx(pm);
|
||||
pm.persist(c1);
|
||||
int id = c1.getId();
|
||||
endTx(pm);
|
||||
endEm(pm);
|
||||
|
||||
pm = getPM();
|
||||
c1 = pm.find(CalendarFields.class, id);
|
||||
|
||||
startTx(pm);
|
||||
|
||||
OpenJPAStateManager sm = getStateManager(c1, pm);
|
||||
|
||||
assertFalse(sm.getDirty().get(sm.getMetaData().
|
||||
getField("gmt").getIndex()));
|
||||
|
||||
// test setting to same value doesn't dirty the field
|
||||
/*
|
||||
setTimeInMillis(c1.getGmtc1.getGmt().getTime().getTime());
|
||||
c1.getGmt().setTime(c1.getGmt().getTime());
|
||||
assertFalse(sm.getDirty().get(sm.getMetaData().
|
||||
getField("gmt").getIndex()));
|
||||
*/
|
||||
|
||||
// test changing time
|
||||
setTimeInMillis(c1.getGmt(), 12345);
|
||||
assertTrue(sm.getDirty().get(sm.getMetaData().
|
||||
getField("gmt").getIndex()));
|
||||
|
||||
assertFalse(sm.getDirty().get(sm.getMetaData().
|
||||
getField("newYork").getIndex()));
|
||||
// test mutate via "add()" method
|
||||
c1.getNewYork().add(Calendar.SECOND, -1);
|
||||
assertTrue(sm.getDirty().get(sm.getMetaData().
|
||||
getField("newYork").getIndex()));
|
||||
|
||||
assertFalse(sm.getDirty().get(sm.getMetaData().
|
||||
getField("berlin").getIndex()));
|
||||
// test mutate via "setTimeZone()" method
|
||||
c1.getBerlin().setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||
assertTrue(sm.getDirty().get(sm.getMetaData().
|
||||
getField("berlin").getIndex()));
|
||||
|
||||
// Calendar.set can only be subclassed in JDK 1.4+ (it is final in
|
||||
// 1.3), so we only run this test in JDK 1.4+
|
||||
if (JavaVersions.VERSION >= 4) {
|
||||
assertFalse(sm.getDirty().get(sm.getMetaData().
|
||||
getField("singapore").getIndex()));
|
||||
// test mutate via "set()" method
|
||||
c1.getSingapore().set(Calendar.YEAR, 1998);
|
||||
assertTrue(sm.getDirty().get(sm.getMetaData().
|
||||
getField("singapore").getIndex()));
|
||||
}
|
||||
|
||||
assertFalse(sm.getDirty().get(sm.getMetaData().
|
||||
getField("pacific").getIndex()));
|
||||
// test mutate via "roll()" method
|
||||
c1.getPacific().roll(Calendar.YEAR, 5);
|
||||
assertTrue(sm.getDirty().get(sm.getMetaData().
|
||||
getField("pacific").getIndex()));
|
||||
|
||||
endTx(pm);
|
||||
endEm(pm);
|
||||
}
|
||||
|
||||
private static void setTimeInMillis(Calendar cal, long millis) {
|
||||
// "setTimeInMillis" is protected in JDK 1.3, put public in 1.4 & 1.5
|
||||
try {
|
||||
// Equivalent to: cal.setTimeInMillis (millis);
|
||||
cal.getClass().getMethod("setTimeInMillis",
|
||||
new Class[]{ long.class }).invoke(cal,
|
||||
new Object[]{ new Long(millis) });
|
||||
} catch (Exception e) {
|
||||
cal.setTime(new Date(millis));
|
||||
}
|
||||
}
|
||||
|
||||
private void assertTimeZonesEquals(CalendarFields c1, CalendarFields c2) {
|
||||
for (int i = 0; i < c1.getCalendars().size(); i++) {
|
||||
Calendar cal1 = (Calendar) c1.getCalendars().get(i);
|
||||
Calendar cal2 = (Calendar) c2.getCalendars().get(i);
|
||||
|
||||
if (cal1 != null && cal2 != null)
|
||||
assertEquals(cal1.getTimeZone().getID(),
|
||||
cal2.getTimeZone().getID());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -1,358 +1,358 @@
|
|||
/*
|
||||
* TestOptimisticLockGroups.java
|
||||
*
|
||||
* Created on October 12, 2006, 2:57 PM
|
||||
*
|
||||
* To change this template, choose Tools | Template Manager
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
/*
|
||||
* 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.persistence.kernel;
|
||||
|
||||
|
||||
|
||||
import org.apache.openjpa.persistence.kernel.common.apps.LockGroupPC;
|
||||
import org.apache.openjpa.persistence.kernel.common.apps.LockGroupPC2;
|
||||
import org.apache.openjpa.persistence.kernel.common.apps.RuntimeTest1;
|
||||
|
||||
import org.apache.openjpa.persistence.OpenJPAEntityManager;
|
||||
|
||||
public class TestOptimisticLockGroups extends BaseKernelTest {
|
||||
|
||||
private Object oid;
|
||||
|
||||
/**
|
||||
* Creates a new instance of TestOptimisticLockGroups
|
||||
*/
|
||||
public TestOptimisticLockGroups() {
|
||||
}
|
||||
|
||||
public TestOptimisticLockGroups(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public void setUp() {
|
||||
deleteAll(LockGroupPC.class);
|
||||
|
||||
OpenJPAEntityManager pm = getPM(false, false);
|
||||
startTx(pm);
|
||||
Object o = new LockGroupPC();
|
||||
pm.persist(o);
|
||||
endTx(pm);
|
||||
oid = pm.getObjectId(o);
|
||||
endEm(pm);
|
||||
}
|
||||
|
||||
public void testDefaultLockGroupFailure1() {
|
||||
OpenJPAEntityManager pm1 = getPM(false, false);
|
||||
startTx(pm1);
|
||||
LockGroupPC pc1 = (LockGroupPC) pm1.find(LockGroupPC.class, oid);
|
||||
pc1.setDefaultLockGroupStringField("pm1 value");
|
||||
|
||||
OpenJPAEntityManager pm2 = getPM(false, false);
|
||||
startTx(pm2);
|
||||
LockGroupPC pc2 = (LockGroupPC) pm2.find(LockGroupPC.class, oid);
|
||||
pc2.setDefaultLockGroupStringField("pm2 value");
|
||||
|
||||
endTx(pm1);
|
||||
endEm(pm1);
|
||||
|
||||
try {
|
||||
endTx(pm2);
|
||||
fail("should not be able to commit change to same value");
|
||||
} catch (Exception e) {
|
||||
assertEquals(pc2, getFailedObject(e));
|
||||
}
|
||||
endEm(pm2);
|
||||
}
|
||||
|
||||
public void testDefaultLockGroupFailure2() {
|
||||
OpenJPAEntityManager pm1 = getPM(false, false);
|
||||
startTx(pm1);
|
||||
LockGroupPC pc1 = (LockGroupPC) pm1.find(LockGroupPC.class, oid);
|
||||
pc1.setDefaultLockGroupStringField("pm1 value");
|
||||
|
||||
OpenJPAEntityManager pm2 = getPM(false, false);
|
||||
startTx(pm2);
|
||||
LockGroupPC pc2 = (LockGroupPC) pm2.find(LockGroupPC.class, oid);
|
||||
pc2.setExplicitDefaultLockGroupIntField(2);
|
||||
|
||||
endTx(pm1);
|
||||
endEm(pm1);
|
||||
|
||||
try {
|
||||
endTx(pm2);
|
||||
fail("should not be able to commit change to same value");
|
||||
} catch (Exception e) {
|
||||
assertEquals(pc2, getFailedObject(e));
|
||||
}
|
||||
endEm(pm2);
|
||||
}
|
||||
|
||||
public void testNonDefaultLockGroupFailure1() {
|
||||
OpenJPAEntityManager pm1 = getPM(false, false);
|
||||
startTx(pm1);
|
||||
LockGroupPC pc1 = (LockGroupPC) pm1.find(LockGroupPC.class, oid);
|
||||
pc1.setLockGroup0IntField(1);
|
||||
|
||||
OpenJPAEntityManager pm2 = getPM(false, false);
|
||||
startTx(pm2);
|
||||
LockGroupPC pc2 = (LockGroupPC) pm2.find(LockGroupPC.class, oid);
|
||||
pc2.setLockGroup0IntField(2);
|
||||
|
||||
endTx(pm1);
|
||||
endEm(pm1);
|
||||
|
||||
try {
|
||||
endTx(pm2);
|
||||
fail("should not be able to commit change to same value");
|
||||
} catch (Exception e) {
|
||||
assertEquals(pc2, getFailedObject(e));
|
||||
}
|
||||
endEm(pm2);
|
||||
}
|
||||
|
||||
public void testNonDefaultLockGroupFailure2() {
|
||||
OpenJPAEntityManager pm1 = getPM(false, false);
|
||||
startTx(pm1);
|
||||
LockGroupPC pc1 = (LockGroupPC) pm1.find(LockGroupPC.class, oid);
|
||||
pc1.setLockGroup0IntField(1);
|
||||
|
||||
OpenJPAEntityManager pm2 = getPM(false, false);
|
||||
startTx(pm2);
|
||||
LockGroupPC pc2 = (LockGroupPC) pm2.find(LockGroupPC.class, oid);
|
||||
pc2.setLockGroup0StringField("pm2");
|
||||
|
||||
endTx(pm1);
|
||||
endEm(pm1);
|
||||
|
||||
try {
|
||||
endTx(pm2);
|
||||
fail("should not be able to commit change to same value");
|
||||
} catch (Exception e) {
|
||||
assertEquals(pc2, getFailedObject(e));
|
||||
}
|
||||
endEm(pm2);
|
||||
}
|
||||
|
||||
public void testMultipleLockGroupSuccess1() {
|
||||
OpenJPAEntityManager pm1 = getPM(false, false);
|
||||
startTx(pm1);
|
||||
LockGroupPC pc1 = (LockGroupPC) pm1.find(LockGroupPC.class, oid);
|
||||
pc1.setDefaultLockGroupStringField("pm1 value");
|
||||
pc1.setExplicitDefaultLockGroupIntField(1);
|
||||
|
||||
OpenJPAEntityManager pm2 = getPM(false, false);
|
||||
startTx(pm2);
|
||||
LockGroupPC pc2 = (LockGroupPC) pm2.find(LockGroupPC.class, oid);
|
||||
pc2.setLockGroup0IntField(2);
|
||||
|
||||
OpenJPAEntityManager pm3 = getPM(false, false);
|
||||
startTx(pm3);
|
||||
LockGroupPC pc3 = (LockGroupPC) pm3.find(LockGroupPC.class, oid);
|
||||
pc3.setLockGroup1RelationField(new RuntimeTest1());
|
||||
|
||||
endTx(pm1);
|
||||
endEm(pm1);
|
||||
|
||||
endTx(pm2);
|
||||
endEm(pm2);
|
||||
|
||||
endTx(pm3);
|
||||
endEm(pm3);
|
||||
}
|
||||
|
||||
public void testMultipleLockGroupSuccess2() {
|
||||
OpenJPAEntityManager pm1 = getPM(false, false);
|
||||
startTx(pm1);
|
||||
LockGroupPC pc1 = (LockGroupPC) pm1.find(LockGroupPC.class, oid);
|
||||
pc1.setDefaultLockGroupStringField("pm1 value");
|
||||
pc1.setLockGroup0IntField(1);
|
||||
|
||||
OpenJPAEntityManager pm2 = getPM(false, false);
|
||||
startTx(pm2);
|
||||
LockGroupPC pc2 = (LockGroupPC) pm2.find(LockGroupPC.class, oid);
|
||||
pc2.setLockGroup1RelationField(new RuntimeTest1());
|
||||
|
||||
endTx(pm2);
|
||||
endEm(pm2);
|
||||
|
||||
endTx(pm1);
|
||||
endEm(pm1);
|
||||
}
|
||||
|
||||
// FIX ME: aokeke - test is taking so much resource and causing subsequent test to fail
|
||||
// public void testNoLockGroupSuccess() {
|
||||
// OpenJPAEntityManager pm1 = getPM(false, false);
|
||||
// startTx(pm1,());
|
||||
// LockGroupPC pc1 = (LockGroupPC) pm1.find(LockGroupPC.class,oid);
|
||||
// pc1.setDefaultLockGroupStringField("pm1 value");
|
||||
// pc1.setLockGroup0IntField(1);
|
||||
// pc1.setUnlockedStringField("pm1 value");
|
||||
//
|
||||
// OpenJPAEntityManager pm2 = getPM(false, false);
|
||||
// startTx(pm2,());
|
||||
// LockGroupPC pc2 = (LockGroupPC) pm2.find(LockGroupPC.class,oid);
|
||||
// pc2.setLockGroup1RelationField(new RuntimeTest1());
|
||||
// pc2.setUnlockedStringField("pm2 value");
|
||||
//
|
||||
// endTx(pm1,());
|
||||
// endEm(pm1);
|
||||
//
|
||||
// endTx(pm2,());
|
||||
// endEm(pm2);
|
||||
// }
|
||||
|
||||
public void testAttachDetachSuccess()
|
||||
throws Exception {
|
||||
OpenJPAEntityManager pm1 = getPM(false, false);
|
||||
LockGroupPC pc1 = (LockGroupPC) pm1.find(LockGroupPC.class, oid);
|
||||
Object detached = pm1.detach(pc1);
|
||||
startTx(pm1);
|
||||
pc1.setLockGroup0IntField(1);
|
||||
pc1.setUnlockedStringField("pm1 changed value");
|
||||
endTx(pm1);
|
||||
endEm(pm1);
|
||||
|
||||
/*
|
||||
// won't work without non-transient detached state
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
|
||||
ObjectOutputStream oos = new ObjectOutputStream (baos);
|
||||
oos.writeObject (detached);
|
||||
oos.close ();
|
||||
baos.close ();
|
||||
|
||||
ByteArrayInputStream bais =
|
||||
new ByteArrayInputStream (baos.toByteArray ());
|
||||
ObjectInputStream ois = new ObjectInputStream (bais);
|
||||
LockGroupPC clone = (LockGroupPC) ois.readObject ();
|
||||
*/
|
||||
LockGroupPC clone = (LockGroupPC) detached;
|
||||
clone.setLockGroup1IntField(2);
|
||||
clone.setUnlockedStringField("pm2 value");
|
||||
|
||||
OpenJPAEntityManager pm2 = getPM(false, false);
|
||||
startTx(pm2);
|
||||
pm2.merge(clone);
|
||||
endTx(pm2);
|
||||
endEm(pm2);
|
||||
}
|
||||
|
||||
public void testAttachDetachFailure()
|
||||
throws Exception {
|
||||
OpenJPAEntityManager pm1 = getPM(false, false);
|
||||
LockGroupPC pc1 = (LockGroupPC) pm1.find(LockGroupPC.class, oid);
|
||||
Object detached = pm1.detach(pc1);
|
||||
startTx(pm1);
|
||||
pc1.setLockGroup0IntField(1);
|
||||
endTx(pm1);
|
||||
endEm(pm1);
|
||||
|
||||
/*
|
||||
// won't work without non-transient detached state
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
|
||||
ObjectOutputStream oos = new ObjectOutputStream (baos);
|
||||
oos.writeObject (detached);
|
||||
oos.close ();
|
||||
baos.close ();
|
||||
|
||||
ByteArrayInputStream bais =
|
||||
new ByteArrayInputStream (baos.toByteArray ());
|
||||
ObjectInputStream ois = new ObjectInputStream (bais);
|
||||
LockGroupPC clone = (LockGroupPC) ois.readObject ();
|
||||
*/
|
||||
LockGroupPC clone = (LockGroupPC) detached;
|
||||
clone.setLockGroup0IntField(2);
|
||||
|
||||
OpenJPAEntityManager pm2 = getPM(false, false);
|
||||
startTx(pm2);
|
||||
boolean failed = false;
|
||||
try {
|
||||
pm2.merge(clone);
|
||||
} catch (Exception e) {
|
||||
failed = true;
|
||||
}
|
||||
|
||||
if (failed)
|
||||
rollbackTx(pm2);
|
||||
else {
|
||||
try {
|
||||
endTx(pm2);
|
||||
fail("Allowed conflicting changes");
|
||||
} catch (Exception jve) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testLockGroupNone() {
|
||||
OpenJPAEntityManager pm = getPM(false, false);
|
||||
// pm.begin();
|
||||
deleteAll(LockGroupPC2.class, pm);
|
||||
startTx(pm);
|
||||
LockGroupPC2 pc = new LockGroupPC2();
|
||||
pc.setName("pc");
|
||||
pm.persist(pc);
|
||||
endTx(pm);
|
||||
Object oid = pm.getObjectId(pc);
|
||||
endEm(pm);
|
||||
|
||||
pm = getPM(false, false);
|
||||
pc = (LockGroupPC2) pm.find(LockGroupPC2.class, oid);
|
||||
startTx(pm);
|
||||
pc.getList().add("foo");
|
||||
|
||||
OpenJPAEntityManager pm2 = getPM(false, false);
|
||||
LockGroupPC2 pc2 = (LockGroupPC2) pm2.find(LockGroupPC2.class, oid);
|
||||
startTx(pm2);
|
||||
pc2.getList().add("bar");
|
||||
endTx(pm2);
|
||||
endEm(pm2);
|
||||
|
||||
endTx(pm);
|
||||
endEm(pm);
|
||||
|
||||
pm = getPM(false, false);
|
||||
pc = (LockGroupPC2) pm.find(LockGroupPC2.class, oid);
|
||||
assertEquals(2, pc.getList().size());
|
||||
endEm(pm);
|
||||
}
|
||||
|
||||
// public void testKnownSubclass() {
|
||||
// OpenJPAEntityManager pm = getPM(false, false);
|
||||
// LockGroupPCKnownSubclass pc = new LockGroupPCKnownSubclass();
|
||||
// pc.setDefaultLockGroupStringField("pc");
|
||||
// startTx(pm,());
|
||||
// pm.persist(pc);
|
||||
// endTx(pm,());
|
||||
//
|
||||
// startTx(pm,());
|
||||
// pc.setKnownSubclassStringField("foo");
|
||||
// endTx(pm,());
|
||||
// }
|
||||
|
||||
//FIXME jthomas - what do we need to substitute for JDOException ?
|
||||
// private Object getFailedObject(JDOException e) {
|
||||
// return ((JDOException) e.getNestedExceptions()[0]).getFailedObject();
|
||||
// }
|
||||
|
||||
private Object getFailedObject(Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* TestOptimisticLockGroups.java
|
||||
*
|
||||
* Created on October 12, 2006, 2:57 PM
|
||||
*
|
||||
* To change this template, choose Tools | Template Manager
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
/*
|
||||
* 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.persistence.kernel;
|
||||
|
||||
|
||||
|
||||
import org.apache.openjpa.persistence.kernel.common.apps.LockGroupPC;
|
||||
import org.apache.openjpa.persistence.kernel.common.apps.LockGroupPC2;
|
||||
import org.apache.openjpa.persistence.kernel.common.apps.RuntimeTest1;
|
||||
|
||||
import org.apache.openjpa.persistence.OpenJPAEntityManager;
|
||||
|
||||
public class TestOptimisticLockGroups extends BaseKernelTest {
|
||||
|
||||
private Object oid;
|
||||
|
||||
/**
|
||||
* Creates a new instance of TestOptimisticLockGroups
|
||||
*/
|
||||
public TestOptimisticLockGroups() {
|
||||
}
|
||||
|
||||
public TestOptimisticLockGroups(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public void setUp() {
|
||||
deleteAll(LockGroupPC.class);
|
||||
|
||||
OpenJPAEntityManager pm = getPM(false, false);
|
||||
startTx(pm);
|
||||
Object o = new LockGroupPC();
|
||||
pm.persist(o);
|
||||
endTx(pm);
|
||||
oid = pm.getObjectId(o);
|
||||
endEm(pm);
|
||||
}
|
||||
|
||||
public void testDefaultLockGroupFailure1() {
|
||||
OpenJPAEntityManager pm1 = getPM(false, false);
|
||||
startTx(pm1);
|
||||
LockGroupPC pc1 = (LockGroupPC) pm1.find(LockGroupPC.class, oid);
|
||||
pc1.setDefaultLockGroupStringField("pm1 value");
|
||||
|
||||
OpenJPAEntityManager pm2 = getPM(false, false);
|
||||
startTx(pm2);
|
||||
LockGroupPC pc2 = (LockGroupPC) pm2.find(LockGroupPC.class, oid);
|
||||
pc2.setDefaultLockGroupStringField("pm2 value");
|
||||
|
||||
endTx(pm1);
|
||||
endEm(pm1);
|
||||
|
||||
try {
|
||||
endTx(pm2);
|
||||
fail("should not be able to commit change to same value");
|
||||
} catch (Exception e) {
|
||||
assertEquals(pc2, getFailedObject(e));
|
||||
}
|
||||
endEm(pm2);
|
||||
}
|
||||
|
||||
public void testDefaultLockGroupFailure2() {
|
||||
OpenJPAEntityManager pm1 = getPM(false, false);
|
||||
startTx(pm1);
|
||||
LockGroupPC pc1 = (LockGroupPC) pm1.find(LockGroupPC.class, oid);
|
||||
pc1.setDefaultLockGroupStringField("pm1 value");
|
||||
|
||||
OpenJPAEntityManager pm2 = getPM(false, false);
|
||||
startTx(pm2);
|
||||
LockGroupPC pc2 = (LockGroupPC) pm2.find(LockGroupPC.class, oid);
|
||||
pc2.setExplicitDefaultLockGroupIntField(2);
|
||||
|
||||
endTx(pm1);
|
||||
endEm(pm1);
|
||||
|
||||
try {
|
||||
endTx(pm2);
|
||||
fail("should not be able to commit change to same value");
|
||||
} catch (Exception e) {
|
||||
assertEquals(pc2, getFailedObject(e));
|
||||
}
|
||||
endEm(pm2);
|
||||
}
|
||||
|
||||
public void testNonDefaultLockGroupFailure1() {
|
||||
OpenJPAEntityManager pm1 = getPM(false, false);
|
||||
startTx(pm1);
|
||||
LockGroupPC pc1 = (LockGroupPC) pm1.find(LockGroupPC.class, oid);
|
||||
pc1.setLockGroup0IntField(1);
|
||||
|
||||
OpenJPAEntityManager pm2 = getPM(false, false);
|
||||
startTx(pm2);
|
||||
LockGroupPC pc2 = (LockGroupPC) pm2.find(LockGroupPC.class, oid);
|
||||
pc2.setLockGroup0IntField(2);
|
||||
|
||||
endTx(pm1);
|
||||
endEm(pm1);
|
||||
|
||||
try {
|
||||
endTx(pm2);
|
||||
fail("should not be able to commit change to same value");
|
||||
} catch (Exception e) {
|
||||
assertEquals(pc2, getFailedObject(e));
|
||||
}
|
||||
endEm(pm2);
|
||||
}
|
||||
|
||||
public void testNonDefaultLockGroupFailure2() {
|
||||
OpenJPAEntityManager pm1 = getPM(false, false);
|
||||
startTx(pm1);
|
||||
LockGroupPC pc1 = (LockGroupPC) pm1.find(LockGroupPC.class, oid);
|
||||
pc1.setLockGroup0IntField(1);
|
||||
|
||||
OpenJPAEntityManager pm2 = getPM(false, false);
|
||||
startTx(pm2);
|
||||
LockGroupPC pc2 = (LockGroupPC) pm2.find(LockGroupPC.class, oid);
|
||||
pc2.setLockGroup0StringField("pm2");
|
||||
|
||||
endTx(pm1);
|
||||
endEm(pm1);
|
||||
|
||||
try {
|
||||
endTx(pm2);
|
||||
fail("should not be able to commit change to same value");
|
||||
} catch (Exception e) {
|
||||
assertEquals(pc2, getFailedObject(e));
|
||||
}
|
||||
endEm(pm2);
|
||||
}
|
||||
|
||||
public void testMultipleLockGroupSuccess1() {
|
||||
OpenJPAEntityManager pm1 = getPM(false, false);
|
||||
startTx(pm1);
|
||||
LockGroupPC pc1 = (LockGroupPC) pm1.find(LockGroupPC.class, oid);
|
||||
pc1.setDefaultLockGroupStringField("pm1 value");
|
||||
pc1.setExplicitDefaultLockGroupIntField(1);
|
||||
|
||||
OpenJPAEntityManager pm2 = getPM(false, false);
|
||||
startTx(pm2);
|
||||
LockGroupPC pc2 = (LockGroupPC) pm2.find(LockGroupPC.class, oid);
|
||||
pc2.setLockGroup0IntField(2);
|
||||
|
||||
OpenJPAEntityManager pm3 = getPM(false, false);
|
||||
startTx(pm3);
|
||||
LockGroupPC pc3 = (LockGroupPC) pm3.find(LockGroupPC.class, oid);
|
||||
pc3.setLockGroup1RelationField(new RuntimeTest1());
|
||||
|
||||
endTx(pm1);
|
||||
endEm(pm1);
|
||||
|
||||
endTx(pm2);
|
||||
endEm(pm2);
|
||||
|
||||
endTx(pm3);
|
||||
endEm(pm3);
|
||||
}
|
||||
|
||||
public void testMultipleLockGroupSuccess2() {
|
||||
OpenJPAEntityManager pm1 = getPM(false, false);
|
||||
startTx(pm1);
|
||||
LockGroupPC pc1 = (LockGroupPC) pm1.find(LockGroupPC.class, oid);
|
||||
pc1.setDefaultLockGroupStringField("pm1 value");
|
||||
pc1.setLockGroup0IntField(1);
|
||||
|
||||
OpenJPAEntityManager pm2 = getPM(false, false);
|
||||
startTx(pm2);
|
||||
LockGroupPC pc2 = (LockGroupPC) pm2.find(LockGroupPC.class, oid);
|
||||
pc2.setLockGroup1RelationField(new RuntimeTest1());
|
||||
|
||||
endTx(pm2);
|
||||
endEm(pm2);
|
||||
|
||||
endTx(pm1);
|
||||
endEm(pm1);
|
||||
}
|
||||
|
||||
// FIX ME: aokeke - test is taking so much resource and causing subsequent test to fail
|
||||
// public void testNoLockGroupSuccess() {
|
||||
// OpenJPAEntityManager pm1 = getPM(false, false);
|
||||
// startTx(pm1,());
|
||||
// LockGroupPC pc1 = (LockGroupPC) pm1.find(LockGroupPC.class,oid);
|
||||
// pc1.setDefaultLockGroupStringField("pm1 value");
|
||||
// pc1.setLockGroup0IntField(1);
|
||||
// pc1.setUnlockedStringField("pm1 value");
|
||||
//
|
||||
// OpenJPAEntityManager pm2 = getPM(false, false);
|
||||
// startTx(pm2,());
|
||||
// LockGroupPC pc2 = (LockGroupPC) pm2.find(LockGroupPC.class,oid);
|
||||
// pc2.setLockGroup1RelationField(new RuntimeTest1());
|
||||
// pc2.setUnlockedStringField("pm2 value");
|
||||
//
|
||||
// endTx(pm1,());
|
||||
// endEm(pm1);
|
||||
//
|
||||
// endTx(pm2,());
|
||||
// endEm(pm2);
|
||||
// }
|
||||
|
||||
public void testAttachDetachSuccess()
|
||||
throws Exception {
|
||||
OpenJPAEntityManager pm1 = getPM(false, false);
|
||||
LockGroupPC pc1 = (LockGroupPC) pm1.find(LockGroupPC.class, oid);
|
||||
Object detached = pm1.detachCopy(pc1);
|
||||
startTx(pm1);
|
||||
pc1.setLockGroup0IntField(1);
|
||||
pc1.setUnlockedStringField("pm1 changed value");
|
||||
endTx(pm1);
|
||||
endEm(pm1);
|
||||
|
||||
/*
|
||||
// won't work without non-transient detached state
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
|
||||
ObjectOutputStream oos = new ObjectOutputStream (baos);
|
||||
oos.writeObject (detached);
|
||||
oos.close ();
|
||||
baos.close ();
|
||||
|
||||
ByteArrayInputStream bais =
|
||||
new ByteArrayInputStream (baos.toByteArray ());
|
||||
ObjectInputStream ois = new ObjectInputStream (bais);
|
||||
LockGroupPC clone = (LockGroupPC) ois.readObject ();
|
||||
*/
|
||||
LockGroupPC clone = (LockGroupPC) detached;
|
||||
clone.setLockGroup1IntField(2);
|
||||
clone.setUnlockedStringField("pm2 value");
|
||||
|
||||
OpenJPAEntityManager pm2 = getPM(false, false);
|
||||
startTx(pm2);
|
||||
pm2.merge(clone);
|
||||
endTx(pm2);
|
||||
endEm(pm2);
|
||||
}
|
||||
|
||||
public void testAttachDetachFailure()
|
||||
throws Exception {
|
||||
OpenJPAEntityManager pm1 = getPM(false, false);
|
||||
LockGroupPC pc1 = (LockGroupPC) pm1.find(LockGroupPC.class, oid);
|
||||
Object detached = pm1.detachCopy(pc1);
|
||||
startTx(pm1);
|
||||
pc1.setLockGroup0IntField(1);
|
||||
endTx(pm1);
|
||||
endEm(pm1);
|
||||
|
||||
/*
|
||||
// won't work without non-transient detached state
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
|
||||
ObjectOutputStream oos = new ObjectOutputStream (baos);
|
||||
oos.writeObject (detached);
|
||||
oos.close ();
|
||||
baos.close ();
|
||||
|
||||
ByteArrayInputStream bais =
|
||||
new ByteArrayInputStream (baos.toByteArray ());
|
||||
ObjectInputStream ois = new ObjectInputStream (bais);
|
||||
LockGroupPC clone = (LockGroupPC) ois.readObject ();
|
||||
*/
|
||||
LockGroupPC clone = (LockGroupPC) detached;
|
||||
clone.setLockGroup0IntField(2);
|
||||
|
||||
OpenJPAEntityManager pm2 = getPM(false, false);
|
||||
startTx(pm2);
|
||||
boolean failed = false;
|
||||
try {
|
||||
pm2.merge(clone);
|
||||
} catch (Exception e) {
|
||||
failed = true;
|
||||
}
|
||||
|
||||
if (failed)
|
||||
rollbackTx(pm2);
|
||||
else {
|
||||
try {
|
||||
endTx(pm2);
|
||||
fail("Allowed conflicting changes");
|
||||
} catch (Exception jve) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testLockGroupNone() {
|
||||
OpenJPAEntityManager pm = getPM(false, false);
|
||||
// pm.begin();
|
||||
deleteAll(LockGroupPC2.class, pm);
|
||||
startTx(pm);
|
||||
LockGroupPC2 pc = new LockGroupPC2();
|
||||
pc.setName("pc");
|
||||
pm.persist(pc);
|
||||
endTx(pm);
|
||||
Object oid = pm.getObjectId(pc);
|
||||
endEm(pm);
|
||||
|
||||
pm = getPM(false, false);
|
||||
pc = (LockGroupPC2) pm.find(LockGroupPC2.class, oid);
|
||||
startTx(pm);
|
||||
pc.getList().add("foo");
|
||||
|
||||
OpenJPAEntityManager pm2 = getPM(false, false);
|
||||
LockGroupPC2 pc2 = (LockGroupPC2) pm2.find(LockGroupPC2.class, oid);
|
||||
startTx(pm2);
|
||||
pc2.getList().add("bar");
|
||||
endTx(pm2);
|
||||
endEm(pm2);
|
||||
|
||||
endTx(pm);
|
||||
endEm(pm);
|
||||
|
||||
pm = getPM(false, false);
|
||||
pc = (LockGroupPC2) pm.find(LockGroupPC2.class, oid);
|
||||
assertEquals(2, pc.getList().size());
|
||||
endEm(pm);
|
||||
}
|
||||
|
||||
// public void testKnownSubclass() {
|
||||
// OpenJPAEntityManager pm = getPM(false, false);
|
||||
// LockGroupPCKnownSubclass pc = new LockGroupPCKnownSubclass();
|
||||
// pc.setDefaultLockGroupStringField("pc");
|
||||
// startTx(pm,());
|
||||
// pm.persist(pc);
|
||||
// endTx(pm,());
|
||||
//
|
||||
// startTx(pm,());
|
||||
// pc.setKnownSubclassStringField("foo");
|
||||
// endTx(pm,());
|
||||
// }
|
||||
|
||||
//FIXME jthomas - what do we need to substitute for JDOException ?
|
||||
// private Object getFailedObject(JDOException e) {
|
||||
// return ((JDOException) e.getNestedExceptions()[0]).getFailedObject();
|
||||
// }
|
||||
|
||||
private Object getFailedObject(Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,229 +1,229 @@
|
|||
/*
|
||||
* TestOpenJPAEntityManagerFactoryImpl.java
|
||||
*
|
||||
* Created on October 13, 2006, 10:54 AM
|
||||
*
|
||||
* To change this template, choose Tools | Template Manager
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
/*
|
||||
* 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.persistence.kernel;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.openjpa.persistence.kernel.common.apps.RuntimeTest1;
|
||||
import org.apache.openjpa.persistence.kernel.common.apps.RuntimeTest4;
|
||||
import org.apache.openjpa.conf.OpenJPAConfiguration;
|
||||
import org.apache.openjpa.conf.OpenJPAConfigurationImpl;
|
||||
import org.apache.openjpa.event.LifecycleEvent;
|
||||
import org.apache.openjpa.event.LoadListener;
|
||||
import org.apache.openjpa.kernel.AbstractBrokerFactory;
|
||||
import org.apache.openjpa.kernel.BrokerImpl;
|
||||
import org.apache.openjpa.kernel.StoreManager;
|
||||
import org.apache.openjpa.lib.conf.ConfigurationProvider;
|
||||
import org.apache.openjpa.meta.MetaDataRepository;
|
||||
import org.apache.openjpa.persistence.JPAFacadeHelper;
|
||||
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
|
||||
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
|
||||
import org.apache.openjpa.persistence.OpenJPAPersistence;
|
||||
|
||||
public class TestPersistenceManagerFactoryImpl extends BaseKernelTest {
|
||||
|
||||
private OpenJPAConfiguration _conf = null;
|
||||
|
||||
/**
|
||||
* Creates a new instance of TestOpenJPAEntityManagerFactoryImpl
|
||||
*/
|
||||
public TestPersistenceManagerFactoryImpl() {
|
||||
}
|
||||
|
||||
public TestPersistenceManagerFactoryImpl(String test) {
|
||||
super(test);
|
||||
}
|
||||
|
||||
public void setUp() {
|
||||
_conf = new OpenJPAConfigurationImpl();
|
||||
_conf.setConnection2UserName("user");
|
||||
_conf.setConnection2Password("pass");
|
||||
_conf.setConnection2URL("url");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that configuration is frozen after retrieving a factory.
|
||||
*
|
||||
* This test case is for kodo persistencemanagerfactories and not openjpaentitymanagers.
|
||||
* therefore its been commented out.
|
||||
* FIX ME: aokeke
|
||||
*/
|
||||
/* public void testConfigurationFreeze()
|
||||
{
|
||||
OpenJPAEntityManagerFactory pmf = getOpenJPAEntityManagerFactory(_conf.toProperties(false));
|
||||
assertEquals("user", pmf.getConfiguration().getConnection2UserName());
|
||||
assertEquals("url", pmf.getConfiguration().getConnection2URL());
|
||||
try
|
||||
{
|
||||
pmf.getConfiguration().setConnection2URL("url2");
|
||||
fail("Allowed configuration change.");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
}
|
||||
}*/
|
||||
|
||||
/**
|
||||
* Test that persistence manager factories are being pooled.
|
||||
*
|
||||
* This test case is for kodo persistencemanagerfactories. It doesnt apply to
|
||||
* openjpaentitymanagerfactories therefore it will be commented out.
|
||||
* FIX ME: aokeke
|
||||
*/
|
||||
/*public void testFactoryPooling() {
|
||||
Properties props = new Properties();
|
||||
props.putAll(_conf.toProperties(false));
|
||||
OpenJPAEntityManagerFactory pmf1 = getOpenJPAEntityManagerFactory(props);
|
||||
|
||||
props = new Properties();
|
||||
props.putAll(_conf.toProperties(false));
|
||||
OpenJPAEntityManagerFactory pmf2 = getOpenJPAEntityManagerFactory(props);
|
||||
|
||||
props = new Properties();
|
||||
props.putAll(_conf.toProperties(false));
|
||||
OpenJPAEntityManagerFactory pmf3 = getOpenJPAEntityManagerFactory(props);
|
||||
|
||||
_conf.setConnectionURL("url2");
|
||||
props = new Properties();
|
||||
props.putAll(_conf.toProperties(false));
|
||||
OpenJPAEntityManagerFactory pmf4 = getOpenJPAEntityManagerFactory
|
||||
(_conf.toProperties(false));
|
||||
|
||||
props = new Properties();
|
||||
props.putAll(_conf.toProperties(false));
|
||||
OpenJPAEntityManagerFactory pmf5 = getOpenJPAEntityManagerFactory(_conf.toProperties(false));
|
||||
|
||||
assertTrue(JPAFacadeHelper.toBrokerFactory(pmf1) == JPAFacadeHelper.toBrokerFactory(pmf2));
|
||||
assertTrue(JPAFacadeHelper.toBrokerFactory(pmf1) == JPAFacadeHelper.toBrokerFactory(pmf3));
|
||||
assertTrue(JPAFacadeHelper.toBrokerFactory(pmf1) != JPAFacadeHelper.toBrokerFactory(pmf4));
|
||||
assertTrue(JPAFacadeHelper.toBrokerFactory(pmf4) == JPAFacadeHelper.toBrokerFactory(pmf5));
|
||||
}*/
|
||||
|
||||
/**
|
||||
* Tests that lifecycle listeners are transferred from factory to
|
||||
* persistence managers.
|
||||
*/
|
||||
public void testFactoryLifecycleListeners() {
|
||||
OpenJPAEntityManagerFactory pmf =
|
||||
(OpenJPAEntityManagerFactory) getEmf();
|
||||
OpenJPAEntityManagerFactorySPI pmfSPI =
|
||||
((OpenJPAEntityManagerFactorySPI) OpenJPAPersistence.cast(pmf));
|
||||
|
||||
//FIXME jthomas
|
||||
LoadListener listener = new LoadListener() {
|
||||
public void afterLoad(LifecycleEvent ev) {
|
||||
}
|
||||
|
||||
public void afterRefresh(LifecycleEvent ev) {
|
||||
}
|
||||
};
|
||||
|
||||
pmfSPI
|
||||
.addLifecycleListener(listener, new Class[]{ RuntimeTest4.class });
|
||||
|
||||
try {
|
||||
BrokerImpl broker = (BrokerImpl) JPAFacadeHelper.toBroker
|
||||
(pmf.createEntityManager());
|
||||
MetaDataRepository repos = broker.getConfiguration().
|
||||
getMetaDataRepositoryInstance();
|
||||
assertTrue("no listeners defined added to Runtimetest4",
|
||||
broker.getLifecycleEventManager().hasLoadListeners(
|
||||
new RuntimeTest4("foo"),
|
||||
repos.getMetaData(RuntimeTest4.class, null, true)));
|
||||
assertFalse("there should be listeners def for runtimetest1",
|
||||
broker.getLifecycleEventManager().hasLoadListeners
|
||||
(new RuntimeTest1(), repos.getMetaData
|
||||
(RuntimeTest1.class, null, true)));
|
||||
broker.close();
|
||||
} finally {
|
||||
pmfSPI.removeLifecycleListener(listener);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that pooling is maintained on deserialization.
|
||||
* This test case is for kodo persistencemanagerfactories. It doesnt apply to
|
||||
* openjpaentitymanagerfactories therefore it will be commented out.
|
||||
*/
|
||||
/*
|
||||
public void testFactorySerialization()
|
||||
throws Exception {
|
||||
OpenJPAEntityManagerFactory pmf1 = getOpenJPAEntityManagerFactory(_conf.toProperties(false));
|
||||
Object pmf2 = roundtrip(pmf1, true);
|
||||
assertEquals(pmf1, pmf2);
|
||||
assertTrue(JPAFacadeHelper.toBrokerFactory(pmf1) ==
|
||||
JPAFacadeHelper.toBrokerFactory((OpenJPAEntityManagerFactory) pmf2));
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Tests that the <code>Platform</code> property is set by the
|
||||
* concrete PMF implementation.
|
||||
*/
|
||||
public void testPlatform() {
|
||||
OpenJPAEntityManagerFactory pmf =
|
||||
(OpenJPAEntityManagerFactory) getEmf();
|
||||
assertNotNull(pmf.getProperties().getProperty("Platform"));
|
||||
}
|
||||
|
||||
protected OpenJPAEntityManagerFactory getEmf(Map props) {
|
||||
props.put("openjpa.BrokerFactory", BrokerFactoryTest.class.getName());
|
||||
return (OpenJPAEntityManagerFactory) super.getEmf(props);
|
||||
}
|
||||
|
||||
public static class BrokerFactoryTest extends AbstractBrokerFactory {
|
||||
|
||||
// standard brokerfactory getter implemented by subclasses
|
||||
public static synchronized BrokerFactoryTest getInstance(
|
||||
ConfigurationProvider cp) {
|
||||
Object key = toPoolKey(cp.getProperties());
|
||||
BrokerFactoryTest factory =
|
||||
(BrokerFactoryTest) getPooledFactoryForKey(key);
|
||||
if (factory != null)
|
||||
return factory;
|
||||
|
||||
factory = newInstance(cp);
|
||||
pool(key, factory);
|
||||
return factory;
|
||||
}
|
||||
|
||||
// standard brokerfactory getter implemented by subclasses
|
||||
public static BrokerFactoryTest newInstance(ConfigurationProvider cp) {
|
||||
OpenJPAConfigurationImpl conf = new OpenJPAConfigurationImpl();
|
||||
cp.setInto(conf);
|
||||
return new BrokerFactoryTest(conf);
|
||||
}
|
||||
|
||||
protected BrokerFactoryTest(OpenJPAConfiguration conf) {
|
||||
super(conf);
|
||||
}
|
||||
|
||||
protected StoreManager newStoreManager() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
* TestOpenJPAEntityManagerFactoryImpl.java
|
||||
*
|
||||
* Created on October 13, 2006, 10:54 AM
|
||||
*
|
||||
* To change this template, choose Tools | Template Manager
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
/*
|
||||
* 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.persistence.kernel;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.openjpa.persistence.kernel.common.apps.RuntimeTest1;
|
||||
import org.apache.openjpa.persistence.kernel.common.apps.RuntimeTest4;
|
||||
import org.apache.openjpa.conf.OpenJPAConfiguration;
|
||||
import org.apache.openjpa.conf.OpenJPAConfigurationImpl;
|
||||
import org.apache.openjpa.event.LifecycleEvent;
|
||||
import org.apache.openjpa.event.LoadListener;
|
||||
import org.apache.openjpa.kernel.AbstractBrokerFactory;
|
||||
import org.apache.openjpa.kernel.BrokerImpl;
|
||||
import org.apache.openjpa.kernel.StoreManager;
|
||||
import org.apache.openjpa.lib.conf.ConfigurationProvider;
|
||||
import org.apache.openjpa.meta.MetaDataRepository;
|
||||
import org.apache.openjpa.persistence.JPAFacadeHelper;
|
||||
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
|
||||
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
|
||||
import org.apache.openjpa.persistence.OpenJPAPersistence;
|
||||
|
||||
public class TestPersistenceManagerFactoryImpl extends BaseKernelTest {
|
||||
|
||||
private OpenJPAConfiguration _conf = null;
|
||||
|
||||
/**
|
||||
* Creates a new instance of TestOpenJPAEntityManagerFactoryImpl
|
||||
*/
|
||||
public TestPersistenceManagerFactoryImpl() {
|
||||
}
|
||||
|
||||
public TestPersistenceManagerFactoryImpl(String test) {
|
||||
super(test);
|
||||
}
|
||||
|
||||
public void setUp() {
|
||||
_conf = new OpenJPAConfigurationImpl();
|
||||
_conf.setConnection2UserName("user");
|
||||
_conf.setConnection2Password("pass");
|
||||
_conf.setConnection2URL("url");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that configuration is frozen after retrieving a factory.
|
||||
*
|
||||
* This test case is for kodo persistencemanagerfactories and not openjpaentitymanagers.
|
||||
* therefore its been commented out.
|
||||
* FIX ME: aokeke
|
||||
*/
|
||||
/* public void testConfigurationFreeze()
|
||||
{
|
||||
OpenJPAEntityManagerFactory pmf = getOpenJPAEntityManagerFactory(_conf.toProperties(false));
|
||||
assertEquals("user", pmf.getConfiguration().getConnection2UserName());
|
||||
assertEquals("url", pmf.getConfiguration().getConnection2URL());
|
||||
try
|
||||
{
|
||||
pmf.getConfiguration().setConnection2URL("url2");
|
||||
fail("Allowed configuration change.");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
}
|
||||
}*/
|
||||
|
||||
/**
|
||||
* Test that persistence manager factories are being pooled.
|
||||
*
|
||||
* This test case is for kodo persistencemanagerfactories. It doesnt apply to
|
||||
* openjpaentitymanagerfactories therefore it will be commented out.
|
||||
* FIX ME: aokeke
|
||||
*/
|
||||
/*public void testFactoryPooling() {
|
||||
Properties props = new Properties();
|
||||
props.putAll(_conf.toProperties(false));
|
||||
OpenJPAEntityManagerFactory pmf1 = getOpenJPAEntityManagerFactory(props);
|
||||
|
||||
props = new Properties();
|
||||
props.putAll(_conf.toProperties(false));
|
||||
OpenJPAEntityManagerFactory pmf2 = getOpenJPAEntityManagerFactory(props);
|
||||
|
||||
props = new Properties();
|
||||
props.putAll(_conf.toProperties(false));
|
||||
OpenJPAEntityManagerFactory pmf3 = getOpenJPAEntityManagerFactory(props);
|
||||
|
||||
_conf.setConnectionURL("url2");
|
||||
props = new Properties();
|
||||
props.putAll(_conf.toProperties(false));
|
||||
OpenJPAEntityManagerFactory pmf4 = getOpenJPAEntityManagerFactory
|
||||
(_conf.toProperties(false));
|
||||
|
||||
props = new Properties();
|
||||
props.putAll(_conf.toProperties(false));
|
||||
OpenJPAEntityManagerFactory pmf5 = getOpenJPAEntityManagerFactory(_conf.toProperties(false));
|
||||
|
||||
assertTrue(JPAFacadeHelper.toBrokerFactory(pmf1) == JPAFacadeHelper.toBrokerFactory(pmf2));
|
||||
assertTrue(JPAFacadeHelper.toBrokerFactory(pmf1) == JPAFacadeHelper.toBrokerFactory(pmf3));
|
||||
assertTrue(JPAFacadeHelper.toBrokerFactory(pmf1) != JPAFacadeHelper.toBrokerFactory(pmf4));
|
||||
assertTrue(JPAFacadeHelper.toBrokerFactory(pmf4) == JPAFacadeHelper.toBrokerFactory(pmf5));
|
||||
}*/
|
||||
|
||||
/**
|
||||
* Tests that lifecycle listeners are transferred from factory to
|
||||
* persistence managers.
|
||||
*/
|
||||
public void testFactoryLifecycleListeners() {
|
||||
OpenJPAEntityManagerFactory pmf =
|
||||
(OpenJPAEntityManagerFactory) getEmf();
|
||||
OpenJPAEntityManagerFactorySPI pmfSPI =
|
||||
((OpenJPAEntityManagerFactorySPI) OpenJPAPersistence.cast(pmf));
|
||||
|
||||
//FIXME jthomas
|
||||
LoadListener listener = new LoadListener() {
|
||||
public void afterLoad(LifecycleEvent ev) {
|
||||
}
|
||||
|
||||
public void afterRefresh(LifecycleEvent ev) {
|
||||
}
|
||||
};
|
||||
|
||||
pmfSPI
|
||||
.addLifecycleListener(listener, new Class[]{ RuntimeTest4.class });
|
||||
|
||||
try {
|
||||
BrokerImpl broker = (BrokerImpl) JPAFacadeHelper.toBroker
|
||||
(pmf.createEntityManager());
|
||||
MetaDataRepository repos = broker.getConfiguration().
|
||||
getMetaDataRepositoryInstance();
|
||||
assertTrue("no listeners defined added to Runtimetest4",
|
||||
broker.getLifecycleEventManager().hasLoadListeners(
|
||||
new RuntimeTest4("foo"),
|
||||
repos.getMetaData(RuntimeTest4.class, null, true)));
|
||||
assertFalse("there should be listeners def for runtimetest1",
|
||||
broker.getLifecycleEventManager().hasLoadListeners
|
||||
(new RuntimeTest1(), repos.getMetaData
|
||||
(RuntimeTest1.class, null, true)));
|
||||
broker.close();
|
||||
} finally {
|
||||
pmfSPI.removeLifecycleListener(listener);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that pooling is maintained on deserialization.
|
||||
* This test case is for kodo persistencemanagerfactories. It doesnt apply to
|
||||
* openjpaentitymanagerfactories therefore it will be commented out.
|
||||
*/
|
||||
/*
|
||||
public void testFactorySerialization()
|
||||
throws Exception {
|
||||
OpenJPAEntityManagerFactory pmf1 = getOpenJPAEntityManagerFactory(_conf.toProperties(false));
|
||||
Object pmf2 = roundtrip(pmf1, true);
|
||||
assertEquals(pmf1, pmf2);
|
||||
assertTrue(JPAFacadeHelper.toBrokerFactory(pmf1) ==
|
||||
JPAFacadeHelper.toBrokerFactory((OpenJPAEntityManagerFactory) pmf2));
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Tests that the <code>Platform</code> property is set by the
|
||||
* concrete PMF implementation.
|
||||
*/
|
||||
public void testPlatform() {
|
||||
OpenJPAEntityManagerFactory pmf =
|
||||
(OpenJPAEntityManagerFactory) getEmf();
|
||||
assertNotNull(pmf.getProperties().get("Platform"));
|
||||
}
|
||||
|
||||
protected OpenJPAEntityManagerFactory getEmf(Map props) {
|
||||
props.put("openjpa.BrokerFactory", BrokerFactoryTest.class.getName());
|
||||
return (OpenJPAEntityManagerFactory) super.getEmf(props);
|
||||
}
|
||||
|
||||
public static class BrokerFactoryTest extends AbstractBrokerFactory {
|
||||
|
||||
// standard brokerfactory getter implemented by subclasses
|
||||
public static synchronized BrokerFactoryTest getInstance(
|
||||
ConfigurationProvider cp) {
|
||||
Object key = toPoolKey(cp.getProperties());
|
||||
BrokerFactoryTest factory =
|
||||
(BrokerFactoryTest) getPooledFactoryForKey(key);
|
||||
if (factory != null)
|
||||
return factory;
|
||||
|
||||
factory = newInstance(cp);
|
||||
pool(key, factory);
|
||||
return factory;
|
||||
}
|
||||
|
||||
// standard brokerfactory getter implemented by subclasses
|
||||
public static BrokerFactoryTest newInstance(ConfigurationProvider cp) {
|
||||
OpenJPAConfigurationImpl conf = new OpenJPAConfigurationImpl();
|
||||
cp.setInto(conf);
|
||||
return new BrokerFactoryTest(conf);
|
||||
}
|
||||
|
||||
protected BrokerFactoryTest(OpenJPAConfiguration conf) {
|
||||
super(conf);
|
||||
}
|
||||
|
||||
protected StoreManager newStoreManager() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,463 +1,463 @@
|
|||
/*
|
||||
* 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.persistence.managedinterface;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.HashSet;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import javax.persistence.Query;
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
|
||||
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
|
||||
import org.apache.openjpa.persistence.OpenJPAEntityManager;
|
||||
import org.apache.openjpa.persistence.JPAFacadeHelper;
|
||||
import org.apache.openjpa.persistence.Extent;
|
||||
import org.apache.openjpa.persistence.query.SimpleEntity;
|
||||
import org.apache.openjpa.meta.ClassMetaData;
|
||||
import org.apache.openjpa.persistence.PersistenceException;
|
||||
|
||||
public class TestManagedInterfaces extends SingleEMFTestCase {
|
||||
|
||||
@Override
|
||||
public void setUp() {
|
||||
super.setUp(SimpleEntity.class, ManagedInterfaceEmbed.class,
|
||||
ManagedInterfaceSup.class, ManagedIface.class,
|
||||
ManagedInterfaceOwner.class, MixedInterface.class,
|
||||
MixedInterfaceImpl.class, NonMappedInterfaceImpl.class,
|
||||
CLEAR_TABLES);
|
||||
}
|
||||
|
||||
public void testEmbeddedMetaData() {
|
||||
emf.createEntityManager().close();
|
||||
ClassMetaData ownerMeta = JPAFacadeHelper.getMetaData(emf,
|
||||
ManagedIface.class);
|
||||
ClassMetaData embeddedMeta = ownerMeta.getField("embed")
|
||||
.getDefiningMetaData();
|
||||
assertTrue(embeddedMeta.isManagedInterface());
|
||||
assertTrue(embeddedMeta.isIntercepting());
|
||||
|
||||
ClassMetaData embeddableMeta = JPAFacadeHelper.getMetaData(emf,
|
||||
ManagedInterfaceEmbed.class);
|
||||
assertTrue(embeddableMeta.isManagedInterface());
|
||||
assertTrue(embeddableMeta.isIntercepting());
|
||||
}
|
||||
|
||||
public void testManagedInterface() throws Exception {
|
||||
OpenJPAEntityManager em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
ManagedIface pc = em.createInstance(ManagedIface.class);
|
||||
pc.setIntFieldSup(3);
|
||||
pc.setIntField(4);
|
||||
pc.setEmbed(em.createInstance(ManagedInterfaceEmbed.class));
|
||||
|
||||
pc.getEmbed().setEmbedIntField(5);
|
||||
assertEquals(5, pc.getEmbed().getEmbedIntField());
|
||||
em.persist(pc);
|
||||
Object oid = em.getObjectId(pc);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
pc = em.find(ManagedIface.class, oid);
|
||||
assertEquals(3, pc.getIntFieldSup());
|
||||
assertEquals(4, pc.getIntField());
|
||||
assertEquals(5, pc.getEmbed().getEmbedIntField());
|
||||
em.getTransaction().begin();
|
||||
pc.setIntField(14);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager ();
|
||||
em.getTransaction().begin();
|
||||
Query query = em.createQuery("select o from ManagedIface o " +
|
||||
"where o.intField = 14");
|
||||
pc = (ManagedIface) query.getSingleResult();
|
||||
assertEquals(14, pc.getIntField());
|
||||
em.remove(pc);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
try {
|
||||
assertNull(em.find(ManagedIface.class, oid));
|
||||
} catch (EntityNotFoundException onfe) {}
|
||||
|
||||
em.close();
|
||||
}
|
||||
|
||||
public void testInterfaceOwner() {
|
||||
OpenJPAEntityManager em = emf.createEntityManager();
|
||||
ManagedInterfaceOwner pc = new ManagedInterfaceOwner();
|
||||
pc.setIFace(em.createInstance(ManagedInterfaceSup.class));
|
||||
pc.setEmbed(em.createInstance(ManagedInterfaceEmbed.class));
|
||||
pc.getIFace().setIntFieldSup(3);
|
||||
pc.getEmbed().setEmbedIntField(5);
|
||||
|
||||
em.getTransaction().begin();
|
||||
em.persist(pc);
|
||||
Object oid = em.getObjectId(pc);
|
||||
em.getTransaction().commit();
|
||||
pc = em.find(ManagedInterfaceOwner.class, oid);
|
||||
assertEquals(3, pc.getIFace().getIntFieldSup());
|
||||
assertEquals(5, pc.getEmbed().getEmbedIntField());
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
pc = em.find(ManagedInterfaceOwner.class, oid);
|
||||
assertEquals(3, pc.getIFace().getIntFieldSup());
|
||||
assertEquals(5, pc.getEmbed().getEmbedIntField());
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
Query q = em.createQuery("select o from ManagedInterfaceOwner o " +
|
||||
"where o.iface.intFieldSup = 3 and o.embed.embedIntField = 5");
|
||||
pc = (ManagedInterfaceOwner) q.getSingleResult();
|
||||
assertEquals(3, pc.getIFace().getIntFieldSup());
|
||||
assertEquals(5, pc.getEmbed().getEmbedIntField());
|
||||
|
||||
pc.getIFace().setIntFieldSup(13);
|
||||
pc.getEmbed().setEmbedIntField(15);
|
||||
assertEquals(13, pc.getIFace().getIntFieldSup());
|
||||
assertEquals(15, pc.getEmbed().getEmbedIntField());
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
pc = em.find(ManagedInterfaceOwner.class, oid);
|
||||
assertEquals(13, pc.getIFace().getIntFieldSup());
|
||||
assertEquals(15, pc.getEmbed().getEmbedIntField());
|
||||
em.close();
|
||||
}
|
||||
|
||||
public void testCollection() {
|
||||
OpenJPAEntityManager em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
ManagedIface pc = em.createInstance(ManagedIface.class);
|
||||
Set set = new HashSet();
|
||||
set.add(new Integer(3));
|
||||
set.add(new Integer(4));
|
||||
set.add(new Integer(5));
|
||||
pc.setSetInteger(set);
|
||||
em.persist(pc);
|
||||
Object oid = em.getObjectId(pc);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
pc = em.find(ManagedIface.class, oid);
|
||||
set = pc.getSetInteger();
|
||||
assertEquals(3, set.size());
|
||||
assertTrue(set.contains(new Integer(3)));
|
||||
assertTrue(set.contains(new Integer(4)));
|
||||
assertTrue(set.contains(new Integer(5)));
|
||||
em.getTransaction().begin();
|
||||
set.remove(new Integer(4));
|
||||
set.add(new Integer(14));
|
||||
set.add(new Integer(15));
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
pc = em.find(ManagedIface.class, oid);
|
||||
set = pc.getSetInteger();
|
||||
assertEquals(4, set.size());
|
||||
assertTrue(set.contains(new Integer(3)));
|
||||
assertTrue(set.contains(new Integer(5)));
|
||||
assertTrue(set.contains(new Integer(14)));
|
||||
assertTrue(set.contains(new Integer(15)));
|
||||
em.getTransaction().begin();
|
||||
pc.setSetInteger(null);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
pc = em.find(ManagedIface.class, oid);
|
||||
set = pc.getSetInteger();
|
||||
assertTrue (set == null || set.size() == 0);
|
||||
em.close();
|
||||
}
|
||||
|
||||
public void testCollectionPC() {
|
||||
OpenJPAEntityManager em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
ManagedIface pc = em.createInstance(ManagedIface.class);
|
||||
Set set = new HashSet();
|
||||
set.add(new SimpleEntity("a", "3"));
|
||||
set.add(new SimpleEntity("b", "4"));
|
||||
set.add(new SimpleEntity("c", "5"));
|
||||
pc.setSetPC(set);
|
||||
em.persist(pc);
|
||||
Object oid = em.getObjectId(pc);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
pc = em.find(ManagedIface.class, oid);
|
||||
set = pc.getSetPC();
|
||||
assertEquals(3, set.size());
|
||||
Collection seen = new ArrayList();
|
||||
SimpleEntity rel;
|
||||
SimpleEntity toRem = null;
|
||||
for (Iterator it = set.iterator(); it.hasNext();) {
|
||||
rel = (SimpleEntity) it.next();
|
||||
seen.add(rel.getName());
|
||||
if (rel.getValue().equals("4"))
|
||||
toRem = rel;
|
||||
}
|
||||
assertEquals(3, seen.size());
|
||||
assertTrue(seen.contains("a"));
|
||||
assertTrue(seen.contains("b"));
|
||||
assertTrue(seen.contains("c"));
|
||||
em.getTransaction().begin();
|
||||
assertNotNull(toRem);
|
||||
set.remove(toRem);
|
||||
set.add(new SimpleEntity("x", "14"));
|
||||
set.add(new SimpleEntity("y", "15"));
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
pc = em.find(ManagedIface.class, oid);
|
||||
set = pc.getSetPC();
|
||||
assertEquals(4, set.size());
|
||||
seen.clear();
|
||||
for (Iterator it = set.iterator(); it.hasNext();) {
|
||||
rel = (SimpleEntity) it.next();
|
||||
seen.add(rel.getName());
|
||||
}
|
||||
assertEquals(4, seen.size());
|
||||
assertTrue(seen.contains("a"));
|
||||
assertTrue(seen.contains("c"));
|
||||
assertTrue(seen.contains("x"));
|
||||
assertTrue(seen.contains("y"));
|
||||
em.getTransaction().begin();
|
||||
pc.setSetPC(null);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
pc = em.find(ManagedIface.class, oid);
|
||||
set = pc.getSetPC();
|
||||
assertTrue (set == null || set.size() == 0);
|
||||
em.close();
|
||||
}
|
||||
|
||||
public void testCollectionInterfaces() {
|
||||
OpenJPAEntityManager em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
ManagedIface pc = em.createInstance(ManagedIface.class);
|
||||
Set set = new HashSet();
|
||||
set.add(createInstance(em, 3));
|
||||
set.add(createInstance(em, 4));
|
||||
set.add(createInstance(em, 5));
|
||||
pc.setSetI(set);
|
||||
em.persist(pc);
|
||||
Object oid = em.getObjectId(pc);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
pc = em.find(ManagedIface.class, oid);
|
||||
set = pc.getSetI();
|
||||
assertEquals(3, set.size());
|
||||
Collection seen = new ArrayList();
|
||||
ManagedIface rel = null;
|
||||
ManagedIface toRem = null;
|
||||
for (Iterator it = set.iterator(); it.hasNext();) {
|
||||
rel = (ManagedIface) it.next();
|
||||
seen.add(new Integer(rel.getIntField()));
|
||||
if (rel.getIntField() == 4)
|
||||
toRem = rel;
|
||||
}
|
||||
assertEquals(3, seen.size());
|
||||
assertTrue(seen.contains(new Integer(3)));
|
||||
assertTrue(seen.contains(new Integer(4)));
|
||||
assertTrue(seen.contains(new Integer(5)));
|
||||
em.getTransaction().begin();
|
||||
assertNotNull(toRem);
|
||||
set.remove(toRem);
|
||||
set.add(createInstance(em, 14));
|
||||
set.add(createInstance(em, 15));
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
pc = em.find(ManagedIface.class, oid);
|
||||
set = pc.getSetI();
|
||||
assertEquals(4, set.size());
|
||||
seen.clear();
|
||||
for (Iterator it = set.iterator(); it.hasNext();) {
|
||||
rel = (ManagedIface) it.next();
|
||||
seen.add(new Integer(rel.getIntField()));
|
||||
}
|
||||
assertEquals(4, seen.size());
|
||||
assertTrue(seen.contains(new Integer(3)));
|
||||
assertTrue(seen.contains(new Integer(5)));
|
||||
assertTrue(seen.contains(new Integer(14)));
|
||||
assertTrue(seen.contains(new Integer(15)));
|
||||
em.getTransaction().begin();
|
||||
pc.setSetPC(null);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
pc = em.find(ManagedIface.class, oid);
|
||||
set = pc.getSetPC();
|
||||
assertTrue (set == null || set.size() == 0);
|
||||
em.close();
|
||||
}
|
||||
|
||||
public void testMixedQuery() {
|
||||
createMixed();
|
||||
|
||||
OpenJPAEntityManager em = emf.createEntityManager();
|
||||
Query q = em.createQuery("select o from MixedInterface o " +
|
||||
"where o.intField = 4");
|
||||
Collection c = q.getResultList();
|
||||
Set seen = new HashSet();
|
||||
assertEquals(2, c.size());
|
||||
MixedInterface pc;
|
||||
for (Iterator it = c.iterator(); it.hasNext();) {
|
||||
pc = (MixedInterface) it.next();
|
||||
assertEquals(4, pc.getIntField());
|
||||
seen.add(pc.getClass());
|
||||
}
|
||||
assertEquals(2, seen.size());
|
||||
|
||||
// Changes of OPENJPA-485 had the positive (but unintended) consequence
|
||||
// of making this case pass, which was failing before as reported in
|
||||
// OPENJPA-481
|
||||
}
|
||||
|
||||
public void testQueryForMixedInterfaceImpls() {
|
||||
createMixed();
|
||||
|
||||
OpenJPAEntityManager em = emf.createEntityManager();
|
||||
Query q = em.createQuery("select o from MixedInterfaceImpl o " +
|
||||
"where o.intField = 4");
|
||||
MixedInterface pc = (MixedInterface) q.getSingleResult();
|
||||
assertEquals(4, pc.getIntField());
|
||||
assertTrue(pc instanceof MixedInterfaceImpl);
|
||||
em.close();
|
||||
}
|
||||
|
||||
public void testMixedExtent() {
|
||||
createMixed();
|
||||
|
||||
OpenJPAEntityManager em = emf.createEntityManager();
|
||||
Extent e = em.createExtent(MixedInterface.class, true);
|
||||
Set seen = new HashSet();
|
||||
int size = 0;
|
||||
for (Iterator it = e.iterator(); it.hasNext();) {
|
||||
seen.add(it.next().getClass());
|
||||
size++;
|
||||
}
|
||||
assertEquals(3, size);
|
||||
assertEquals(2, seen.size());
|
||||
|
||||
e = em.createExtent(MixedInterface.class, false);
|
||||
seen = new HashSet();
|
||||
size = 0;
|
||||
for (Iterator it = e.iterator(); it.hasNext();) {
|
||||
seen.add(it.next().getClass());
|
||||
size++;
|
||||
}
|
||||
assertEquals(1, size);
|
||||
assertNotEquals(MixedInterfaceImpl.class, seen.iterator().next());
|
||||
em.close();
|
||||
}
|
||||
|
||||
private void createMixed() {
|
||||
OpenJPAEntityManager em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
MixedInterface pc = em.createInstance(MixedInterface.class);
|
||||
pc.setIntField(4);
|
||||
em.persist(pc);
|
||||
pc = new MixedInterfaceImpl();
|
||||
pc.setIntField(4);
|
||||
em.persist(pc);
|
||||
pc = new MixedInterfaceImpl();
|
||||
pc.setIntField(8);
|
||||
em.persist(pc);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
}
|
||||
|
||||
public void testUnimplementedThrowsException() {
|
||||
OpenJPAEntityManager em = emf.createEntityManager();
|
||||
ManagedIface pc = createInstance(em, 1);
|
||||
try {
|
||||
pc.unimplemented();
|
||||
fail("Exception expected.");
|
||||
} catch (UnsupportedOperationException uoe) {} // good
|
||||
em.close();
|
||||
}
|
||||
|
||||
public void testNonMappedCreateInstanceException() {
|
||||
// OpenJPA's support of non-mapped interfaces differs from JDO support;
|
||||
// there is no special query or relation support for non-mapped
|
||||
// interfaces in OpenJPA at this time.
|
||||
OpenJPAEntityManager em = null;
|
||||
try {
|
||||
em = emf.createEntityManager();
|
||||
em.createInstance(NonMappedInterface.class);
|
||||
fail("IllegalArgumentException expected");
|
||||
} catch (IllegalArgumentException e) {} // good
|
||||
if (em != null)
|
||||
em.close();
|
||||
}
|
||||
|
||||
public void testDetach() {
|
||||
OpenJPAEntityManager em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
ManagedIface pc = createInstance(em, 4);
|
||||
em.persist(pc);
|
||||
Object oid = em.getObjectId(pc);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
ManagedIface pcx = em.find(ManagedIface.class, oid);
|
||||
pc = em.detach(pcx);
|
||||
em.close();
|
||||
|
||||
assertTrue(em.isDetached(pc));
|
||||
pc.setIntField(7);
|
||||
|
||||
em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
em.merge(pc);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
pc = em.find(ManagedIface.class, oid);
|
||||
assertEquals(7, pc.getIntField());
|
||||
em.close();
|
||||
}
|
||||
|
||||
private ManagedIface createInstance(OpenJPAEntityManager em, int i) {
|
||||
ManagedIface pc = em.createInstance(ManagedIface.class);
|
||||
pc.setIntField(i);
|
||||
return pc;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* 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.persistence.managedinterface;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.HashSet;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import javax.persistence.Query;
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
|
||||
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
|
||||
import org.apache.openjpa.persistence.OpenJPAEntityManager;
|
||||
import org.apache.openjpa.persistence.JPAFacadeHelper;
|
||||
import org.apache.openjpa.persistence.Extent;
|
||||
import org.apache.openjpa.persistence.query.SimpleEntity;
|
||||
import org.apache.openjpa.meta.ClassMetaData;
|
||||
import org.apache.openjpa.persistence.PersistenceException;
|
||||
|
||||
public class TestManagedInterfaces extends SingleEMFTestCase {
|
||||
|
||||
@Override
|
||||
public void setUp() {
|
||||
super.setUp(SimpleEntity.class, ManagedInterfaceEmbed.class,
|
||||
ManagedInterfaceSup.class, ManagedIface.class,
|
||||
ManagedInterfaceOwner.class, MixedInterface.class,
|
||||
MixedInterfaceImpl.class, NonMappedInterfaceImpl.class,
|
||||
CLEAR_TABLES);
|
||||
}
|
||||
|
||||
public void testEmbeddedMetaData() {
|
||||
emf.createEntityManager().close();
|
||||
ClassMetaData ownerMeta = JPAFacadeHelper.getMetaData(emf,
|
||||
ManagedIface.class);
|
||||
ClassMetaData embeddedMeta = ownerMeta.getField("embed")
|
||||
.getDefiningMetaData();
|
||||
assertTrue(embeddedMeta.isManagedInterface());
|
||||
assertTrue(embeddedMeta.isIntercepting());
|
||||
|
||||
ClassMetaData embeddableMeta = JPAFacadeHelper.getMetaData(emf,
|
||||
ManagedInterfaceEmbed.class);
|
||||
assertTrue(embeddableMeta.isManagedInterface());
|
||||
assertTrue(embeddableMeta.isIntercepting());
|
||||
}
|
||||
|
||||
public void testManagedInterface() throws Exception {
|
||||
OpenJPAEntityManager em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
ManagedIface pc = em.createInstance(ManagedIface.class);
|
||||
pc.setIntFieldSup(3);
|
||||
pc.setIntField(4);
|
||||
pc.setEmbed(em.createInstance(ManagedInterfaceEmbed.class));
|
||||
|
||||
pc.getEmbed().setEmbedIntField(5);
|
||||
assertEquals(5, pc.getEmbed().getEmbedIntField());
|
||||
em.persist(pc);
|
||||
Object oid = em.getObjectId(pc);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
pc = em.find(ManagedIface.class, oid);
|
||||
assertEquals(3, pc.getIntFieldSup());
|
||||
assertEquals(4, pc.getIntField());
|
||||
assertEquals(5, pc.getEmbed().getEmbedIntField());
|
||||
em.getTransaction().begin();
|
||||
pc.setIntField(14);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager ();
|
||||
em.getTransaction().begin();
|
||||
Query query = em.createQuery("select o from ManagedIface o " +
|
||||
"where o.intField = 14");
|
||||
pc = (ManagedIface) query.getSingleResult();
|
||||
assertEquals(14, pc.getIntField());
|
||||
em.remove(pc);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
try {
|
||||
assertNull(em.find(ManagedIface.class, oid));
|
||||
} catch (EntityNotFoundException onfe) {}
|
||||
|
||||
em.close();
|
||||
}
|
||||
|
||||
public void testInterfaceOwner() {
|
||||
OpenJPAEntityManager em = emf.createEntityManager();
|
||||
ManagedInterfaceOwner pc = new ManagedInterfaceOwner();
|
||||
pc.setIFace(em.createInstance(ManagedInterfaceSup.class));
|
||||
pc.setEmbed(em.createInstance(ManagedInterfaceEmbed.class));
|
||||
pc.getIFace().setIntFieldSup(3);
|
||||
pc.getEmbed().setEmbedIntField(5);
|
||||
|
||||
em.getTransaction().begin();
|
||||
em.persist(pc);
|
||||
Object oid = em.getObjectId(pc);
|
||||
em.getTransaction().commit();
|
||||
pc = em.find(ManagedInterfaceOwner.class, oid);
|
||||
assertEquals(3, pc.getIFace().getIntFieldSup());
|
||||
assertEquals(5, pc.getEmbed().getEmbedIntField());
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
pc = em.find(ManagedInterfaceOwner.class, oid);
|
||||
assertEquals(3, pc.getIFace().getIntFieldSup());
|
||||
assertEquals(5, pc.getEmbed().getEmbedIntField());
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
Query q = em.createQuery("select o from ManagedInterfaceOwner o " +
|
||||
"where o.iface.intFieldSup = 3 and o.embed.embedIntField = 5");
|
||||
pc = (ManagedInterfaceOwner) q.getSingleResult();
|
||||
assertEquals(3, pc.getIFace().getIntFieldSup());
|
||||
assertEquals(5, pc.getEmbed().getEmbedIntField());
|
||||
|
||||
pc.getIFace().setIntFieldSup(13);
|
||||
pc.getEmbed().setEmbedIntField(15);
|
||||
assertEquals(13, pc.getIFace().getIntFieldSup());
|
||||
assertEquals(15, pc.getEmbed().getEmbedIntField());
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
pc = em.find(ManagedInterfaceOwner.class, oid);
|
||||
assertEquals(13, pc.getIFace().getIntFieldSup());
|
||||
assertEquals(15, pc.getEmbed().getEmbedIntField());
|
||||
em.close();
|
||||
}
|
||||
|
||||
public void testCollection() {
|
||||
OpenJPAEntityManager em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
ManagedIface pc = em.createInstance(ManagedIface.class);
|
||||
Set set = new HashSet();
|
||||
set.add(new Integer(3));
|
||||
set.add(new Integer(4));
|
||||
set.add(new Integer(5));
|
||||
pc.setSetInteger(set);
|
||||
em.persist(pc);
|
||||
Object oid = em.getObjectId(pc);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
pc = em.find(ManagedIface.class, oid);
|
||||
set = pc.getSetInteger();
|
||||
assertEquals(3, set.size());
|
||||
assertTrue(set.contains(new Integer(3)));
|
||||
assertTrue(set.contains(new Integer(4)));
|
||||
assertTrue(set.contains(new Integer(5)));
|
||||
em.getTransaction().begin();
|
||||
set.remove(new Integer(4));
|
||||
set.add(new Integer(14));
|
||||
set.add(new Integer(15));
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
pc = em.find(ManagedIface.class, oid);
|
||||
set = pc.getSetInteger();
|
||||
assertEquals(4, set.size());
|
||||
assertTrue(set.contains(new Integer(3)));
|
||||
assertTrue(set.contains(new Integer(5)));
|
||||
assertTrue(set.contains(new Integer(14)));
|
||||
assertTrue(set.contains(new Integer(15)));
|
||||
em.getTransaction().begin();
|
||||
pc.setSetInteger(null);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
pc = em.find(ManagedIface.class, oid);
|
||||
set = pc.getSetInteger();
|
||||
assertTrue (set == null || set.size() == 0);
|
||||
em.close();
|
||||
}
|
||||
|
||||
public void testCollectionPC() {
|
||||
OpenJPAEntityManager em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
ManagedIface pc = em.createInstance(ManagedIface.class);
|
||||
Set set = new HashSet();
|
||||
set.add(new SimpleEntity("a", "3"));
|
||||
set.add(new SimpleEntity("b", "4"));
|
||||
set.add(new SimpleEntity("c", "5"));
|
||||
pc.setSetPC(set);
|
||||
em.persist(pc);
|
||||
Object oid = em.getObjectId(pc);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
pc = em.find(ManagedIface.class, oid);
|
||||
set = pc.getSetPC();
|
||||
assertEquals(3, set.size());
|
||||
Collection seen = new ArrayList();
|
||||
SimpleEntity rel;
|
||||
SimpleEntity toRem = null;
|
||||
for (Iterator it = set.iterator(); it.hasNext();) {
|
||||
rel = (SimpleEntity) it.next();
|
||||
seen.add(rel.getName());
|
||||
if (rel.getValue().equals("4"))
|
||||
toRem = rel;
|
||||
}
|
||||
assertEquals(3, seen.size());
|
||||
assertTrue(seen.contains("a"));
|
||||
assertTrue(seen.contains("b"));
|
||||
assertTrue(seen.contains("c"));
|
||||
em.getTransaction().begin();
|
||||
assertNotNull(toRem);
|
||||
set.remove(toRem);
|
||||
set.add(new SimpleEntity("x", "14"));
|
||||
set.add(new SimpleEntity("y", "15"));
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
pc = em.find(ManagedIface.class, oid);
|
||||
set = pc.getSetPC();
|
||||
assertEquals(4, set.size());
|
||||
seen.clear();
|
||||
for (Iterator it = set.iterator(); it.hasNext();) {
|
||||
rel = (SimpleEntity) it.next();
|
||||
seen.add(rel.getName());
|
||||
}
|
||||
assertEquals(4, seen.size());
|
||||
assertTrue(seen.contains("a"));
|
||||
assertTrue(seen.contains("c"));
|
||||
assertTrue(seen.contains("x"));
|
||||
assertTrue(seen.contains("y"));
|
||||
em.getTransaction().begin();
|
||||
pc.setSetPC(null);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
pc = em.find(ManagedIface.class, oid);
|
||||
set = pc.getSetPC();
|
||||
assertTrue (set == null || set.size() == 0);
|
||||
em.close();
|
||||
}
|
||||
|
||||
public void testCollectionInterfaces() {
|
||||
OpenJPAEntityManager em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
ManagedIface pc = em.createInstance(ManagedIface.class);
|
||||
Set set = new HashSet();
|
||||
set.add(createInstance(em, 3));
|
||||
set.add(createInstance(em, 4));
|
||||
set.add(createInstance(em, 5));
|
||||
pc.setSetI(set);
|
||||
em.persist(pc);
|
||||
Object oid = em.getObjectId(pc);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
pc = em.find(ManagedIface.class, oid);
|
||||
set = pc.getSetI();
|
||||
assertEquals(3, set.size());
|
||||
Collection seen = new ArrayList();
|
||||
ManagedIface rel = null;
|
||||
ManagedIface toRem = null;
|
||||
for (Iterator it = set.iterator(); it.hasNext();) {
|
||||
rel = (ManagedIface) it.next();
|
||||
seen.add(new Integer(rel.getIntField()));
|
||||
if (rel.getIntField() == 4)
|
||||
toRem = rel;
|
||||
}
|
||||
assertEquals(3, seen.size());
|
||||
assertTrue(seen.contains(new Integer(3)));
|
||||
assertTrue(seen.contains(new Integer(4)));
|
||||
assertTrue(seen.contains(new Integer(5)));
|
||||
em.getTransaction().begin();
|
||||
assertNotNull(toRem);
|
||||
set.remove(toRem);
|
||||
set.add(createInstance(em, 14));
|
||||
set.add(createInstance(em, 15));
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
pc = em.find(ManagedIface.class, oid);
|
||||
set = pc.getSetI();
|
||||
assertEquals(4, set.size());
|
||||
seen.clear();
|
||||
for (Iterator it = set.iterator(); it.hasNext();) {
|
||||
rel = (ManagedIface) it.next();
|
||||
seen.add(new Integer(rel.getIntField()));
|
||||
}
|
||||
assertEquals(4, seen.size());
|
||||
assertTrue(seen.contains(new Integer(3)));
|
||||
assertTrue(seen.contains(new Integer(5)));
|
||||
assertTrue(seen.contains(new Integer(14)));
|
||||
assertTrue(seen.contains(new Integer(15)));
|
||||
em.getTransaction().begin();
|
||||
pc.setSetPC(null);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
pc = em.find(ManagedIface.class, oid);
|
||||
set = pc.getSetPC();
|
||||
assertTrue (set == null || set.size() == 0);
|
||||
em.close();
|
||||
}
|
||||
|
||||
public void testMixedQuery() {
|
||||
createMixed();
|
||||
|
||||
OpenJPAEntityManager em = emf.createEntityManager();
|
||||
Query q = em.createQuery("select o from MixedInterface o " +
|
||||
"where o.intField = 4");
|
||||
Collection c = q.getResultList();
|
||||
Set seen = new HashSet();
|
||||
assertEquals(2, c.size());
|
||||
MixedInterface pc;
|
||||
for (Iterator it = c.iterator(); it.hasNext();) {
|
||||
pc = (MixedInterface) it.next();
|
||||
assertEquals(4, pc.getIntField());
|
||||
seen.add(pc.getClass());
|
||||
}
|
||||
assertEquals(2, seen.size());
|
||||
|
||||
// Changes of OPENJPA-485 had the positive (but unintended) consequence
|
||||
// of making this case pass, which was failing before as reported in
|
||||
// OPENJPA-481
|
||||
}
|
||||
|
||||
public void testQueryForMixedInterfaceImpls() {
|
||||
createMixed();
|
||||
|
||||
OpenJPAEntityManager em = emf.createEntityManager();
|
||||
Query q = em.createQuery("select o from MixedInterfaceImpl o " +
|
||||
"where o.intField = 4");
|
||||
MixedInterface pc = (MixedInterface) q.getSingleResult();
|
||||
assertEquals(4, pc.getIntField());
|
||||
assertTrue(pc instanceof MixedInterfaceImpl);
|
||||
em.close();
|
||||
}
|
||||
|
||||
public void testMixedExtent() {
|
||||
createMixed();
|
||||
|
||||
OpenJPAEntityManager em = emf.createEntityManager();
|
||||
Extent e = em.createExtent(MixedInterface.class, true);
|
||||
Set seen = new HashSet();
|
||||
int size = 0;
|
||||
for (Iterator it = e.iterator(); it.hasNext();) {
|
||||
seen.add(it.next().getClass());
|
||||
size++;
|
||||
}
|
||||
assertEquals(3, size);
|
||||
assertEquals(2, seen.size());
|
||||
|
||||
e = em.createExtent(MixedInterface.class, false);
|
||||
seen = new HashSet();
|
||||
size = 0;
|
||||
for (Iterator it = e.iterator(); it.hasNext();) {
|
||||
seen.add(it.next().getClass());
|
||||
size++;
|
||||
}
|
||||
assertEquals(1, size);
|
||||
assertNotEquals(MixedInterfaceImpl.class, seen.iterator().next());
|
||||
em.close();
|
||||
}
|
||||
|
||||
private void createMixed() {
|
||||
OpenJPAEntityManager em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
MixedInterface pc = em.createInstance(MixedInterface.class);
|
||||
pc.setIntField(4);
|
||||
em.persist(pc);
|
||||
pc = new MixedInterfaceImpl();
|
||||
pc.setIntField(4);
|
||||
em.persist(pc);
|
||||
pc = new MixedInterfaceImpl();
|
||||
pc.setIntField(8);
|
||||
em.persist(pc);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
}
|
||||
|
||||
public void testUnimplementedThrowsException() {
|
||||
OpenJPAEntityManager em = emf.createEntityManager();
|
||||
ManagedIface pc = createInstance(em, 1);
|
||||
try {
|
||||
pc.unimplemented();
|
||||
fail("Exception expected.");
|
||||
} catch (UnsupportedOperationException uoe) {} // good
|
||||
em.close();
|
||||
}
|
||||
|
||||
public void testNonMappedCreateInstanceException() {
|
||||
// OpenJPA's support of non-mapped interfaces differs from JDO support;
|
||||
// there is no special query or relation support for non-mapped
|
||||
// interfaces in OpenJPA at this time.
|
||||
OpenJPAEntityManager em = null;
|
||||
try {
|
||||
em = emf.createEntityManager();
|
||||
em.createInstance(NonMappedInterface.class);
|
||||
fail("IllegalArgumentException expected");
|
||||
} catch (IllegalArgumentException e) {} // good
|
||||
if (em != null)
|
||||
em.close();
|
||||
}
|
||||
|
||||
public void testDetach() {
|
||||
OpenJPAEntityManager em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
ManagedIface pc = createInstance(em, 4);
|
||||
em.persist(pc);
|
||||
Object oid = em.getObjectId(pc);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
ManagedIface pcx = em.find(ManagedIface.class, oid);
|
||||
pc = em.detachCopy(pcx);
|
||||
em.close();
|
||||
|
||||
assertTrue(em.isDetached(pc));
|
||||
pc.setIntField(7);
|
||||
|
||||
em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
em.merge(pc);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
em = emf.createEntityManager();
|
||||
pc = em.find(ManagedIface.class, oid);
|
||||
assertEquals(7, pc.getIntField());
|
||||
em.close();
|
||||
}
|
||||
|
||||
private ManagedIface createInstance(OpenJPAEntityManager em, int i) {
|
||||
ManagedIface pc = em.createInstance(ManagedIface.class);
|
||||
pc.setIntField(i);
|
||||
return pc;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,404 +1,404 @@
|
|||
/*
|
||||
* 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.persistence.meta;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
|
||||
|
||||
import org.apache.openjpa.persistence.meta.common.apps.ValueStrategyPC;
|
||||
import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
|
||||
import org.apache.openjpa.meta.ClassMetaData;
|
||||
import org.apache.openjpa.persistence.OpenJPAEntityManager;
|
||||
import org.apache.openjpa.util.OpenJPAException;
|
||||
|
||||
/**
|
||||
* <p>Test value and update strategies. Also tests version fields, which
|
||||
* are represented in JDO as a value strategy.</p>
|
||||
*
|
||||
* @author Abe White
|
||||
*/
|
||||
public class TestValueStrategies
|
||||
extends AbstractTestCase {
|
||||
|
||||
public TestValueStrategies(String test) {
|
||||
super(test, "metacactusapp");
|
||||
}
|
||||
|
||||
public void setUp() {
|
||||
deleteAll(ValueStrategyPC.class);
|
||||
}
|
||||
|
||||
public void testIgnoreUpdate() {
|
||||
ValueStrategyPC pc = new ValueStrategyPC();
|
||||
pc.setName("pc");
|
||||
pc.setIgnoreUpdate(10);
|
||||
|
||||
OpenJPAEntityManager pm =
|
||||
(OpenJPAEntityManager) currentEntityManager();
|
||||
startTx(pm);
|
||||
pm.persist(pc);
|
||||
assertEquals(10, pc.getIgnoreUpdate());
|
||||
endTx(pm);
|
||||
Object oid = pm.getObjectId(pc);
|
||||
endEm(pm);
|
||||
|
||||
//pm = getPM(false, false);
|
||||
pm = (OpenJPAEntityManager) currentEntityManager();
|
||||
pc = (ValueStrategyPC) pm.find(ValueStrategyPC.class, oid);
|
||||
assertNotNull(pc);
|
||||
assertEquals(10, pc.getIgnoreUpdate());
|
||||
startTx(pm);
|
||||
pc.setIgnoreUpdate(100);
|
||||
assertFalse(pm.isDirty(pc));
|
||||
pm.transactional(pc, false);
|
||||
endTx(pm);
|
||||
assertEquals(10, pc.getIgnoreUpdate());
|
||||
endEm(pm);
|
||||
|
||||
pm = (OpenJPAEntityManager) currentEntityManager();
|
||||
pc = (ValueStrategyPC) pm.find(ValueStrategyPC.class, oid);
|
||||
assertNotNull(pc);
|
||||
assertEquals(10, pc.getIgnoreUpdate());
|
||||
endEm(pm);
|
||||
}
|
||||
|
||||
public void testRestrictUpdate() {
|
||||
ValueStrategyPC pc = new ValueStrategyPC();
|
||||
pc.setName("pc");
|
||||
pc.setRestrictUpdate(10);
|
||||
|
||||
OpenJPAEntityManager pm =
|
||||
(OpenJPAEntityManager) currentEntityManager();
|
||||
startTx(pm);
|
||||
pm.persist(pc);
|
||||
assertEquals(10, pc.getRestrictUpdate());
|
||||
endTx(pm);
|
||||
Object oid = pm.getObjectId(pc);
|
||||
endEm(pm);
|
||||
|
||||
//pm = getPM(false, false);
|
||||
pm = (OpenJPAEntityManager) currentEntityManager();
|
||||
pc = (ValueStrategyPC) pm.find(ValueStrategyPC.class, oid);
|
||||
assertNotNull(pc);
|
||||
assertEquals(10, pc.getRestrictUpdate());
|
||||
startTx(pm);
|
||||
try {
|
||||
pc.setRestrictUpdate(100);
|
||||
fail("Allowed update of restricted field.");
|
||||
} catch (RuntimeException re) {
|
||||
}
|
||||
endTx(pm);
|
||||
assertEquals(10, pc.getRestrictUpdate());
|
||||
endEm(pm);
|
||||
}
|
||||
|
||||
public void testUUID() {
|
||||
ValueStrategyPC pc = new ValueStrategyPC();
|
||||
ValueStrategyPC pc2 = new ValueStrategyPC();
|
||||
pc.setName("pc");
|
||||
pc2.setName("pc2");
|
||||
assertNull(pc.getUUID());
|
||||
assertNull(pc2.getUUID());
|
||||
assertNull(pc.getUUIDHex());
|
||||
assertNull(pc2.getUUIDHex());
|
||||
|
||||
OpenJPAEntityManager pm =
|
||||
(OpenJPAEntityManager) currentEntityManager();
|
||||
pm.setOptimistic(true);
|
||||
startTx(pm);
|
||||
pm.persist(pc);
|
||||
pm.persist(pc2);
|
||||
String str = pc.getUUID();
|
||||
String hex = pc.getUUIDHex();
|
||||
assertTrue(!pm.isStoreActive()); // no flush needed
|
||||
endTx(pm);
|
||||
String str2 = pc2.getUUID();
|
||||
String hex2 = pc2.getUUIDHex();
|
||||
Object oid = pm.getObjectId(pc);
|
||||
Object oid2 = pm.getObjectId(pc2);
|
||||
endEm(pm);
|
||||
|
||||
assertNotNull(str);
|
||||
assertNotNull(str2);
|
||||
assertTrue(!str.equals(str2));
|
||||
assertNotNull(hex);
|
||||
assertNotNull(hex2);
|
||||
assertTrue(!hex.equals(hex2));
|
||||
|
||||
pm = (OpenJPAEntityManager) currentEntityManager();
|
||||
pc = (ValueStrategyPC) pm.find(ValueStrategyPC.class, oid);
|
||||
pc2 = (ValueStrategyPC) pm.find(ValueStrategyPC.class, oid2);
|
||||
assertEquals(str, pc.getUUID());
|
||||
assertEquals(str2, pc2.getUUID());
|
||||
assertEquals(hex, pc.getUUIDHex());
|
||||
assertEquals(hex2, pc2.getUUIDHex());
|
||||
startTx(pm);
|
||||
pc.setUUIDHex("foo");
|
||||
pc2.setUUIDHex("bar");
|
||||
endTx(pm);
|
||||
endEm(pm);
|
||||
|
||||
pm = (OpenJPAEntityManager) currentEntityManager();
|
||||
pc = (ValueStrategyPC) pm.find(ValueStrategyPC.class, oid);
|
||||
pc2 = (ValueStrategyPC) pm.find(ValueStrategyPC.class, oid2);
|
||||
assertEquals("foo", pc.getUUIDHex());
|
||||
assertEquals("bar", pc2.getUUIDHex());
|
||||
endEm(pm);
|
||||
}
|
||||
|
||||
public void testSequence() {
|
||||
ValueStrategyPC pc = new ValueStrategyPC();
|
||||
ValueStrategyPC pc2 = new ValueStrategyPC();
|
||||
pc.setName("pc");
|
||||
pc2.setName("pc2");
|
||||
assertEquals(0, pc.getSequence());
|
||||
assertEquals(0, pc2.getSequence());
|
||||
|
||||
OpenJPAEntityManager pm =
|
||||
(OpenJPAEntityManager) currentEntityManager();
|
||||
pm.setOptimistic(true);
|
||||
startTx(pm);
|
||||
pm.persist(pc);
|
||||
pm.persist(pc2);
|
||||
int seq = pc.getSequence();
|
||||
assertTrue(!pm.isStoreActive()); // no flush needed
|
||||
endTx(pm);
|
||||
int seq2 = pc2.getSequence();
|
||||
Object oid = pm.getObjectId(pc);
|
||||
Object oid2 = pm.getObjectId(pc2);
|
||||
endEm(pm);
|
||||
|
||||
assertTrue(seq > 0);
|
||||
assertTrue(seq2 > 0);
|
||||
assertTrue(seq != seq2);
|
||||
|
||||
pm = (OpenJPAEntityManager) currentEntityManager();
|
||||
pc = (ValueStrategyPC) pm.find(ValueStrategyPC.class, oid);
|
||||
pc2 = (ValueStrategyPC) pm.find(ValueStrategyPC.class, oid2);
|
||||
assertEquals(seq, pc.getSequence());
|
||||
assertEquals(seq2, pc2.getSequence());
|
||||
startTx(pm);
|
||||
pc.setSequence(99);
|
||||
pc2.setSequence(100);
|
||||
endTx(pm);
|
||||
endEm(pm);
|
||||
|
||||
pm = (OpenJPAEntityManager) currentEntityManager();
|
||||
pc = (ValueStrategyPC) pm.find(ValueStrategyPC.class, oid);
|
||||
pc2 = (ValueStrategyPC) pm.find(ValueStrategyPC.class, oid2);
|
||||
assertEquals(99, pc.getSequence());
|
||||
assertEquals(100, pc2.getSequence());
|
||||
endEm(pm);
|
||||
}
|
||||
|
||||
public void testVersion() {
|
||||
versionTest(getEmf());
|
||||
}
|
||||
|
||||
public void testVersionDataCache() {
|
||||
|
||||
Map map = new HashMap();
|
||||
map.put("openjpa.DataCache", "true");
|
||||
map.put("openjpa.RemoteCommitProvider", "sjvm");
|
||||
versionTest(getEmf(map));
|
||||
}
|
||||
|
||||
private void versionTest(EntityManagerFactory pmf) {
|
||||
ValueStrategyPC pc = new ValueStrategyPC();
|
||||
pc.setName("pc");
|
||||
|
||||
OpenJPAEntityManager pm = (OpenJPAEntityManager)
|
||||
pmf.createEntityManager();
|
||||
startTx(pm);
|
||||
pm.persist(pc);
|
||||
ClassMetaData meta = getConfiguration()
|
||||
.getMetaDataRepositoryInstance().
|
||||
getMetaData(pc.getClass(), null, false);
|
||||
assertNotNull(meta.getVersionField());
|
||||
assertEquals("version", meta.getVersionField().getName());
|
||||
assertEquals(0, pc.getVersion());
|
||||
endTx(pm);
|
||||
assertEquals(1, pc.getVersion());
|
||||
Object oid = pm.getObjectId(pc);
|
||||
endEm(pm);
|
||||
|
||||
// do no-op commit
|
||||
pm = (OpenJPAEntityManager) pmf.createEntityManager();
|
||||
pc = (ValueStrategyPC) pm.find(ValueStrategyPC.class, oid);
|
||||
assertNotNull(pc);
|
||||
assertEquals(1, pc.getVersion());
|
||||
startTx(pm);
|
||||
try {
|
||||
pc.setVersion(10);
|
||||
fail("Allowed change to version field.");
|
||||
} catch (RuntimeException re) {
|
||||
}
|
||||
endTx(pm);
|
||||
assertEquals(1, pc.getVersion());
|
||||
endEm(pm);
|
||||
|
||||
// do real commit
|
||||
pm = (OpenJPAEntityManager) pmf.createEntityManager();
|
||||
pc = (ValueStrategyPC) pm.find(ValueStrategyPC.class, oid);
|
||||
assertNotNull(pc);
|
||||
assertEquals(1, pc.getVersion());
|
||||
startTx(pm);
|
||||
pc.setName("changed");
|
||||
pm.flush();
|
||||
assertEquals(1, pc.getVersion());
|
||||
endTx(pm);
|
||||
assertEquals("changed", pc.getName());
|
||||
assertEquals(2, pc.getVersion());
|
||||
endEm(pm);
|
||||
|
||||
// rollback
|
||||
pm = (OpenJPAEntityManager) pmf.createEntityManager();
|
||||
pc = (ValueStrategyPC) pm.find(ValueStrategyPC.class, oid);
|
||||
assertNotNull(pc);
|
||||
assertEquals(2, pc.getVersion());
|
||||
startTx(pm);
|
||||
pc.setName("changed2");
|
||||
pm.flush();
|
||||
assertEquals(2, pc.getVersion());
|
||||
rollbackTx(pm);
|
||||
assertEquals(2, pc.getVersion());
|
||||
endEm(pm);
|
||||
}
|
||||
|
||||
public void testVersionDetach() {
|
||||
ValueStrategyPC pc = new ValueStrategyPC();
|
||||
ValueStrategyPC pc2 = new ValueStrategyPC();
|
||||
pc.setName("pc");
|
||||
pc2.setName("pc2");
|
||||
|
||||
OpenJPAEntityManager pm =
|
||||
(OpenJPAEntityManager) currentEntityManager();
|
||||
startTx(pm);
|
||||
pm.persist(pc);
|
||||
pm.persist(pc2);
|
||||
endTx(pm);
|
||||
startTx(pm);
|
||||
pc.setName("changed");
|
||||
pc2.setName("changed2");
|
||||
endTx(pm);
|
||||
assertEquals(2, pc.getVersion());
|
||||
assertEquals(2, pc2.getVersion());
|
||||
ValueStrategyPC detached = (ValueStrategyPC) pm.detach(pc);
|
||||
ValueStrategyPC detached2 = (ValueStrategyPC) pm.detach(pc2);
|
||||
endEm(pm);
|
||||
|
||||
// clean attach
|
||||
pm = (OpenJPAEntityManager) currentEntityManager();
|
||||
startTx(pm);
|
||||
pc = (ValueStrategyPC) pm.merge(detached);
|
||||
assertEquals(2, pc.getVersion());
|
||||
endTx(pm);
|
||||
assertEquals(2, pc.getVersion());
|
||||
endEm(pm);
|
||||
|
||||
// dirty attach
|
||||
detached.setName("changed-detached");
|
||||
pm = (OpenJPAEntityManager) currentEntityManager();
|
||||
startTx(pm);
|
||||
pc = (ValueStrategyPC) pm.merge(detached);
|
||||
assertEquals(2, pc.getVersion());
|
||||
endTx(pm);
|
||||
assertEquals(3, pc.getVersion());
|
||||
endEm(pm);
|
||||
|
||||
// stale attach
|
||||
detached.setName("stale");
|
||||
pm = (OpenJPAEntityManager) currentEntityManager();
|
||||
startTx(pm);
|
||||
try {
|
||||
pm.merge(detached);
|
||||
endTx(pm);
|
||||
fail("Committed stale version.");
|
||||
} catch (OpenJPAException je) {
|
||||
}
|
||||
if (isActiveTx(pm))
|
||||
rollbackTx(pm);
|
||||
endEm(pm);
|
||||
|
||||
// modify version field in detached; allow either exception or
|
||||
// allow the update to be ignored
|
||||
detached2.setName("changed2-detached");
|
||||
detached2.setVersion(99);
|
||||
pm = (OpenJPAEntityManager) currentEntityManager();
|
||||
startTx(pm);
|
||||
try {
|
||||
pc2 = (ValueStrategyPC) pm.merge(detached2);
|
||||
assertEquals(2, pc2.getVersion());
|
||||
endTx(pm);
|
||||
assertEquals(3, pc2.getVersion());
|
||||
} catch (OpenJPAException je) {
|
||||
}
|
||||
if (isActiveTx(pm))
|
||||
rollbackTx(pm);
|
||||
;
|
||||
endEm(pm);
|
||||
}
|
||||
|
||||
public void testVersionRefresh() {
|
||||
ValueStrategyPC pc = new ValueStrategyPC();
|
||||
pc.setName("pc");
|
||||
|
||||
OpenJPAEntityManager pm =
|
||||
(OpenJPAEntityManager) currentEntityManager();
|
||||
startTx(pm);
|
||||
pm.persist(pc);
|
||||
endTx(pm);
|
||||
startTx(pm);
|
||||
pc.setName("changed");
|
||||
endTx(pm);
|
||||
assertEquals(2, pc.getVersion());
|
||||
|
||||
// clean refresh
|
||||
startTx(pm);
|
||||
pm.refresh(pc);
|
||||
assertEquals(2, pc.getVersion());
|
||||
|
||||
// concurrent mod
|
||||
OpenJPAEntityManager pm2 =
|
||||
(OpenJPAEntityManager) currentEntityManager();
|
||||
startTx(pm2);
|
||||
ValueStrategyPC pc2 = (ValueStrategyPC) pm2.find
|
||||
(ValueStrategyPC.class, pm2.getObjectId(pc));
|
||||
pc2.setName("changed2");
|
||||
endTx(pm2);
|
||||
assertEquals(3, pc2.getVersion());
|
||||
endEm(pm2);
|
||||
|
||||
// stale refresh
|
||||
pm.refresh(pc);
|
||||
assertEquals(3, pc.getVersion());
|
||||
|
||||
// dirty refresh
|
||||
pc.setName("changed-1");
|
||||
pm.refresh(pc);
|
||||
assertEquals(3, pc.getVersion());
|
||||
|
||||
pc.setName("changed-2");
|
||||
endTx(pm);
|
||||
assertEquals(4, pc.getVersion());
|
||||
endEm(pm);
|
||||
}
|
||||
}
|
||||
/*
|
||||
* 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.persistence.meta;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
|
||||
|
||||
import org.apache.openjpa.persistence.meta.common.apps.ValueStrategyPC;
|
||||
import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
|
||||
import org.apache.openjpa.meta.ClassMetaData;
|
||||
import org.apache.openjpa.persistence.OpenJPAEntityManager;
|
||||
import org.apache.openjpa.util.OpenJPAException;
|
||||
|
||||
/**
|
||||
* <p>Test value and update strategies. Also tests version fields, which
|
||||
* are represented in JDO as a value strategy.</p>
|
||||
*
|
||||
* @author Abe White
|
||||
*/
|
||||
public class TestValueStrategies
|
||||
extends AbstractTestCase {
|
||||
|
||||
public TestValueStrategies(String test) {
|
||||
super(test, "metacactusapp");
|
||||
}
|
||||
|
||||
public void setUp() {
|
||||
deleteAll(ValueStrategyPC.class);
|
||||
}
|
||||
|
||||
public void testIgnoreUpdate() {
|
||||
ValueStrategyPC pc = new ValueStrategyPC();
|
||||
pc.setName("pc");
|
||||
pc.setIgnoreUpdate(10);
|
||||
|
||||
OpenJPAEntityManager pm =
|
||||
(OpenJPAEntityManager) currentEntityManager();
|
||||
startTx(pm);
|
||||
pm.persist(pc);
|
||||
assertEquals(10, pc.getIgnoreUpdate());
|
||||
endTx(pm);
|
||||
Object oid = pm.getObjectId(pc);
|
||||
endEm(pm);
|
||||
|
||||
//pm = getPM(false, false);
|
||||
pm = (OpenJPAEntityManager) currentEntityManager();
|
||||
pc = (ValueStrategyPC) pm.find(ValueStrategyPC.class, oid);
|
||||
assertNotNull(pc);
|
||||
assertEquals(10, pc.getIgnoreUpdate());
|
||||
startTx(pm);
|
||||
pc.setIgnoreUpdate(100);
|
||||
assertFalse(pm.isDirty(pc));
|
||||
pm.transactional(pc, false);
|
||||
endTx(pm);
|
||||
assertEquals(10, pc.getIgnoreUpdate());
|
||||
endEm(pm);
|
||||
|
||||
pm = (OpenJPAEntityManager) currentEntityManager();
|
||||
pc = (ValueStrategyPC) pm.find(ValueStrategyPC.class, oid);
|
||||
assertNotNull(pc);
|
||||
assertEquals(10, pc.getIgnoreUpdate());
|
||||
endEm(pm);
|
||||
}
|
||||
|
||||
public void testRestrictUpdate() {
|
||||
ValueStrategyPC pc = new ValueStrategyPC();
|
||||
pc.setName("pc");
|
||||
pc.setRestrictUpdate(10);
|
||||
|
||||
OpenJPAEntityManager pm =
|
||||
(OpenJPAEntityManager) currentEntityManager();
|
||||
startTx(pm);
|
||||
pm.persist(pc);
|
||||
assertEquals(10, pc.getRestrictUpdate());
|
||||
endTx(pm);
|
||||
Object oid = pm.getObjectId(pc);
|
||||
endEm(pm);
|
||||
|
||||
//pm = getPM(false, false);
|
||||
pm = (OpenJPAEntityManager) currentEntityManager();
|
||||
pc = (ValueStrategyPC) pm.find(ValueStrategyPC.class, oid);
|
||||
assertNotNull(pc);
|
||||
assertEquals(10, pc.getRestrictUpdate());
|
||||
startTx(pm);
|
||||
try {
|
||||
pc.setRestrictUpdate(100);
|
||||
fail("Allowed update of restricted field.");
|
||||
} catch (RuntimeException re) {
|
||||
}
|
||||
endTx(pm);
|
||||
assertEquals(10, pc.getRestrictUpdate());
|
||||
endEm(pm);
|
||||
}
|
||||
|
||||
public void testUUID() {
|
||||
ValueStrategyPC pc = new ValueStrategyPC();
|
||||
ValueStrategyPC pc2 = new ValueStrategyPC();
|
||||
pc.setName("pc");
|
||||
pc2.setName("pc2");
|
||||
assertNull(pc.getUUID());
|
||||
assertNull(pc2.getUUID());
|
||||
assertNull(pc.getUUIDHex());
|
||||
assertNull(pc2.getUUIDHex());
|
||||
|
||||
OpenJPAEntityManager pm =
|
||||
(OpenJPAEntityManager) currentEntityManager();
|
||||
pm.setOptimistic(true);
|
||||
startTx(pm);
|
||||
pm.persist(pc);
|
||||
pm.persist(pc2);
|
||||
String str = pc.getUUID();
|
||||
String hex = pc.getUUIDHex();
|
||||
assertTrue(!pm.isStoreActive()); // no flush needed
|
||||
endTx(pm);
|
||||
String str2 = pc2.getUUID();
|
||||
String hex2 = pc2.getUUIDHex();
|
||||
Object oid = pm.getObjectId(pc);
|
||||
Object oid2 = pm.getObjectId(pc2);
|
||||
endEm(pm);
|
||||
|
||||
assertNotNull(str);
|
||||
assertNotNull(str2);
|
||||
assertTrue(!str.equals(str2));
|
||||
assertNotNull(hex);
|
||||
assertNotNull(hex2);
|
||||
assertTrue(!hex.equals(hex2));
|
||||
|
||||
pm = (OpenJPAEntityManager) currentEntityManager();
|
||||
pc = (ValueStrategyPC) pm.find(ValueStrategyPC.class, oid);
|
||||
pc2 = (ValueStrategyPC) pm.find(ValueStrategyPC.class, oid2);
|
||||
assertEquals(str, pc.getUUID());
|
||||
assertEquals(str2, pc2.getUUID());
|
||||
assertEquals(hex, pc.getUUIDHex());
|
||||
assertEquals(hex2, pc2.getUUIDHex());
|
||||
startTx(pm);
|
||||
pc.setUUIDHex("foo");
|
||||
pc2.setUUIDHex("bar");
|
||||
endTx(pm);
|
||||
endEm(pm);
|
||||
|
||||
pm = (OpenJPAEntityManager) currentEntityManager();
|
||||
pc = (ValueStrategyPC) pm.find(ValueStrategyPC.class, oid);
|
||||
pc2 = (ValueStrategyPC) pm.find(ValueStrategyPC.class, oid2);
|
||||
assertEquals("foo", pc.getUUIDHex());
|
||||
assertEquals("bar", pc2.getUUIDHex());
|
||||
endEm(pm);
|
||||
}
|
||||
|
||||
public void testSequence() {
|
||||
ValueStrategyPC pc = new ValueStrategyPC();
|
||||
ValueStrategyPC pc2 = new ValueStrategyPC();
|
||||
pc.setName("pc");
|
||||
pc2.setName("pc2");
|
||||
assertEquals(0, pc.getSequence());
|
||||
assertEquals(0, pc2.getSequence());
|
||||
|
||||
OpenJPAEntityManager pm =
|
||||
(OpenJPAEntityManager) currentEntityManager();
|
||||
pm.setOptimistic(true);
|
||||
startTx(pm);
|
||||
pm.persist(pc);
|
||||
pm.persist(pc2);
|
||||
int seq = pc.getSequence();
|
||||
assertTrue(!pm.isStoreActive()); // no flush needed
|
||||
endTx(pm);
|
||||
int seq2 = pc2.getSequence();
|
||||
Object oid = pm.getObjectId(pc);
|
||||
Object oid2 = pm.getObjectId(pc2);
|
||||
endEm(pm);
|
||||
|
||||
assertTrue(seq > 0);
|
||||
assertTrue(seq2 > 0);
|
||||
assertTrue(seq != seq2);
|
||||
|
||||
pm = (OpenJPAEntityManager) currentEntityManager();
|
||||
pc = (ValueStrategyPC) pm.find(ValueStrategyPC.class, oid);
|
||||
pc2 = (ValueStrategyPC) pm.find(ValueStrategyPC.class, oid2);
|
||||
assertEquals(seq, pc.getSequence());
|
||||
assertEquals(seq2, pc2.getSequence());
|
||||
startTx(pm);
|
||||
pc.setSequence(99);
|
||||
pc2.setSequence(100);
|
||||
endTx(pm);
|
||||
endEm(pm);
|
||||
|
||||
pm = (OpenJPAEntityManager) currentEntityManager();
|
||||
pc = (ValueStrategyPC) pm.find(ValueStrategyPC.class, oid);
|
||||
pc2 = (ValueStrategyPC) pm.find(ValueStrategyPC.class, oid2);
|
||||
assertEquals(99, pc.getSequence());
|
||||
assertEquals(100, pc2.getSequence());
|
||||
endEm(pm);
|
||||
}
|
||||
|
||||
public void testVersion() {
|
||||
versionTest(getEmf());
|
||||
}
|
||||
|
||||
public void testVersionDataCache() {
|
||||
|
||||
Map map = new HashMap();
|
||||
map.put("openjpa.DataCache", "true");
|
||||
map.put("openjpa.RemoteCommitProvider", "sjvm");
|
||||
versionTest(getEmf(map));
|
||||
}
|
||||
|
||||
private void versionTest(EntityManagerFactory pmf) {
|
||||
ValueStrategyPC pc = new ValueStrategyPC();
|
||||
pc.setName("pc");
|
||||
|
||||
OpenJPAEntityManager pm = (OpenJPAEntityManager)
|
||||
pmf.createEntityManager();
|
||||
startTx(pm);
|
||||
pm.persist(pc);
|
||||
ClassMetaData meta = getConfiguration()
|
||||
.getMetaDataRepositoryInstance().
|
||||
getMetaData(pc.getClass(), null, false);
|
||||
assertNotNull(meta.getVersionField());
|
||||
assertEquals("version", meta.getVersionField().getName());
|
||||
assertEquals(0, pc.getVersion());
|
||||
endTx(pm);
|
||||
assertEquals(1, pc.getVersion());
|
||||
Object oid = pm.getObjectId(pc);
|
||||
endEm(pm);
|
||||
|
||||
// do no-op commit
|
||||
pm = (OpenJPAEntityManager) pmf.createEntityManager();
|
||||
pc = (ValueStrategyPC) pm.find(ValueStrategyPC.class, oid);
|
||||
assertNotNull(pc);
|
||||
assertEquals(1, pc.getVersion());
|
||||
startTx(pm);
|
||||
try {
|
||||
pc.setVersion(10);
|
||||
fail("Allowed change to version field.");
|
||||
} catch (RuntimeException re) {
|
||||
}
|
||||
endTx(pm);
|
||||
assertEquals(1, pc.getVersion());
|
||||
endEm(pm);
|
||||
|
||||
// do real commit
|
||||
pm = (OpenJPAEntityManager) pmf.createEntityManager();
|
||||
pc = (ValueStrategyPC) pm.find(ValueStrategyPC.class, oid);
|
||||
assertNotNull(pc);
|
||||
assertEquals(1, pc.getVersion());
|
||||
startTx(pm);
|
||||
pc.setName("changed");
|
||||
pm.flush();
|
||||
assertEquals(1, pc.getVersion());
|
||||
endTx(pm);
|
||||
assertEquals("changed", pc.getName());
|
||||
assertEquals(2, pc.getVersion());
|
||||
endEm(pm);
|
||||
|
||||
// rollback
|
||||
pm = (OpenJPAEntityManager) pmf.createEntityManager();
|
||||
pc = (ValueStrategyPC) pm.find(ValueStrategyPC.class, oid);
|
||||
assertNotNull(pc);
|
||||
assertEquals(2, pc.getVersion());
|
||||
startTx(pm);
|
||||
pc.setName("changed2");
|
||||
pm.flush();
|
||||
assertEquals(2, pc.getVersion());
|
||||
rollbackTx(pm);
|
||||
assertEquals(2, pc.getVersion());
|
||||
endEm(pm);
|
||||
}
|
||||
|
||||
public void testVersionDetach() {
|
||||
ValueStrategyPC pc = new ValueStrategyPC();
|
||||
ValueStrategyPC pc2 = new ValueStrategyPC();
|
||||
pc.setName("pc");
|
||||
pc2.setName("pc2");
|
||||
|
||||
OpenJPAEntityManager pm =
|
||||
(OpenJPAEntityManager) currentEntityManager();
|
||||
startTx(pm);
|
||||
pm.persist(pc);
|
||||
pm.persist(pc2);
|
||||
endTx(pm);
|
||||
startTx(pm);
|
||||
pc.setName("changed");
|
||||
pc2.setName("changed2");
|
||||
endTx(pm);
|
||||
assertEquals(2, pc.getVersion());
|
||||
assertEquals(2, pc2.getVersion());
|
||||
ValueStrategyPC detached = (ValueStrategyPC) pm.detachCopy(pc);
|
||||
ValueStrategyPC detached2 = (ValueStrategyPC) pm.detachCopy(pc2);
|
||||
endEm(pm);
|
||||
|
||||
// clean attach
|
||||
pm = (OpenJPAEntityManager) currentEntityManager();
|
||||
startTx(pm);
|
||||
pc = (ValueStrategyPC) pm.merge(detached);
|
||||
assertEquals(2, pc.getVersion());
|
||||
endTx(pm);
|
||||
assertEquals(2, pc.getVersion());
|
||||
endEm(pm);
|
||||
|
||||
// dirty attach
|
||||
detached.setName("changed-detached");
|
||||
pm = (OpenJPAEntityManager) currentEntityManager();
|
||||
startTx(pm);
|
||||
pc = (ValueStrategyPC) pm.merge(detached);
|
||||
assertEquals(2, pc.getVersion());
|
||||
endTx(pm);
|
||||
assertEquals(3, pc.getVersion());
|
||||
endEm(pm);
|
||||
|
||||
// stale attach
|
||||
detached.setName("stale");
|
||||
pm = (OpenJPAEntityManager) currentEntityManager();
|
||||
startTx(pm);
|
||||
try {
|
||||
pm.merge(detached);
|
||||
endTx(pm);
|
||||
fail("Committed stale version.");
|
||||
} catch (OpenJPAException je) {
|
||||
}
|
||||
if (isActiveTx(pm))
|
||||
rollbackTx(pm);
|
||||
endEm(pm);
|
||||
|
||||
// modify version field in detached; allow either exception or
|
||||
// allow the update to be ignored
|
||||
detached2.setName("changed2-detached");
|
||||
detached2.setVersion(99);
|
||||
pm = (OpenJPAEntityManager) currentEntityManager();
|
||||
startTx(pm);
|
||||
try {
|
||||
pc2 = (ValueStrategyPC) pm.merge(detached2);
|
||||
assertEquals(2, pc2.getVersion());
|
||||
endTx(pm);
|
||||
assertEquals(3, pc2.getVersion());
|
||||
} catch (OpenJPAException je) {
|
||||
}
|
||||
if (isActiveTx(pm))
|
||||
rollbackTx(pm);
|
||||
;
|
||||
endEm(pm);
|
||||
}
|
||||
|
||||
public void testVersionRefresh() {
|
||||
ValueStrategyPC pc = new ValueStrategyPC();
|
||||
pc.setName("pc");
|
||||
|
||||
OpenJPAEntityManager pm =
|
||||
(OpenJPAEntityManager) currentEntityManager();
|
||||
startTx(pm);
|
||||
pm.persist(pc);
|
||||
endTx(pm);
|
||||
startTx(pm);
|
||||
pc.setName("changed");
|
||||
endTx(pm);
|
||||
assertEquals(2, pc.getVersion());
|
||||
|
||||
// clean refresh
|
||||
startTx(pm);
|
||||
pm.refresh(pc);
|
||||
assertEquals(2, pc.getVersion());
|
||||
|
||||
// concurrent mod
|
||||
OpenJPAEntityManager pm2 =
|
||||
(OpenJPAEntityManager) currentEntityManager();
|
||||
startTx(pm2);
|
||||
ValueStrategyPC pc2 = (ValueStrategyPC) pm2.find
|
||||
(ValueStrategyPC.class, pm2.getObjectId(pc));
|
||||
pc2.setName("changed2");
|
||||
endTx(pm2);
|
||||
assertEquals(3, pc2.getVersion());
|
||||
endEm(pm2);
|
||||
|
||||
// stale refresh
|
||||
pm.refresh(pc);
|
||||
assertEquals(3, pc.getVersion());
|
||||
|
||||
// dirty refresh
|
||||
pc.setName("changed-1");
|
||||
pm.refresh(pc);
|
||||
assertEquals(3, pc.getVersion());
|
||||
|
||||
pc.setName("changed-2");
|
||||
endTx(pm);
|
||||
assertEquals(4, pc.getVersion());
|
||||
endEm(pm);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -79,7 +79,7 @@ public class TestLRS
|
|||
OpenJPAEntityManager em = emf.createEntityManager();
|
||||
LRSEntity lrs = em.find(LRSEntity.class, id);
|
||||
assertLRS(lrs, "lrs");
|
||||
lrs = em.detach(lrs);
|
||||
lrs = em.detachCopy(lrs);
|
||||
assertEquals("lrs", lrs.getName());
|
||||
assertNull(lrs.getLRSList());
|
||||
em.close();
|
||||
|
|
|
@ -73,7 +73,7 @@ public class TestFlushBeforeDetach extends SQLListenerTestCase {
|
|||
|
||||
i.setItemData("EFGH");
|
||||
|
||||
OpenJPAPersistence.cast(em).detach(i);
|
||||
OpenJPAPersistence.cast(em).detachCopy(i);
|
||||
em.getTransaction().rollback();
|
||||
assertNotSQL("UPDATE ITEM SET.*");
|
||||
em.close();
|
||||
|
|
|
@ -97,10 +97,17 @@ public class EntityManagerFactoryImpl
|
|||
return _factory.getConfiguration();
|
||||
}
|
||||
|
||||
public Properties getProperties() {
|
||||
public Properties getPropertiesAsProperties() {
|
||||
return _factory.getProperties();
|
||||
}
|
||||
|
||||
public Map<String, Object> getProperties() {
|
||||
@SuppressWarnings("unchecked")
|
||||
HashMap<String, Object> retv =
|
||||
new HashMap<String, Object>((Map) getPropertiesAsProperties());
|
||||
return retv;
|
||||
}
|
||||
|
||||
public Object putUserObject(Object key, Object val) {
|
||||
return _factory.putUserObject(key, val);
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -534,11 +534,13 @@ public interface OpenJPAEntityManager
|
|||
|
||||
/**
|
||||
* Detach the specified object from the entity manager.
|
||||
*
|
||||
* Note: renamed from detach to avoid conflict with JPA 2.0 EntityManager.detach method
|
||||
*
|
||||
* @param pc the instance to detach
|
||||
* @return the detached instance
|
||||
*/
|
||||
public <T> T detach(T pc);
|
||||
public <T> T detachCopy(T pc);
|
||||
|
||||
/**
|
||||
* Detach the specified objects from the entity manager.
|
||||
|
|
|
@ -35,8 +35,16 @@ public interface OpenJPAEntityManagerFactory
|
|||
|
||||
/**
|
||||
* Return properties describing this runtime.
|
||||
* Although the name has changed, this method
|
||||
* returns the same type as the old getProperties method.
|
||||
*/
|
||||
public Properties getProperties();
|
||||
public Properties getPropertiesAsProperties();
|
||||
|
||||
/**
|
||||
* Returns properties describing this runtime
|
||||
* using new signature to avoid conflict with JPA 2.0 API
|
||||
*/
|
||||
public Map<String, Object> getProperties();
|
||||
|
||||
/**
|
||||
* Put the specified key-value pair into the map of user objects.
|
||||
|
|
Loading…
Reference in New Issue