mirror of https://github.com/apache/openjpa.git
OPENJPA-1077 Validation-mode element support added to persistence.xml and to createEMF properties Map
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@774930 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
d7ddfb1055
commit
cb115bcd16
|
@ -1645,4 +1645,20 @@ public interface OpenJPAConfiguration
|
|||
* @since 2.0.0
|
||||
*/
|
||||
public void setFinderCache(String cache);
|
||||
|
||||
/**
|
||||
* The bean validation mode to use for managed classes.
|
||||
* Defaults to <code>AUTO</code>.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public String getValidationMode();
|
||||
|
||||
/**
|
||||
* Set the bean validation mode to use for managed classes.
|
||||
* If not set, defaults to <code>AUTO</code>.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public void setValidationMode(String mode);
|
||||
}
|
||||
|
|
|
@ -152,6 +152,7 @@ public class OpenJPAConfigurationImpl
|
|||
public PluginValue preparedQueryCachePlugin;
|
||||
public PluginValue finderCachePlugin;
|
||||
public ObjectValue specification;
|
||||
public StringValue validationMode;
|
||||
|
||||
// custom values
|
||||
public BrokerFactoryValue brokerFactoryPlugin;
|
||||
|
@ -551,6 +552,20 @@ public class OpenJPAConfigurationImpl
|
|||
queryTimeout.setDefault("-1");
|
||||
queryTimeout.setDynamic(true);
|
||||
|
||||
// kernel can't access javax.persistence.ValidationMode enums here
|
||||
validationMode = addString("javax.persistence.validation.mode");
|
||||
aliases =
|
||||
new String[] {
|
||||
"AUTO", "auto",
|
||||
"CALLBACK", "callback",
|
||||
"NONE", "none"
|
||||
};
|
||||
validationMode.setAliases(aliases);
|
||||
validationMode.setAliasListComprehensive(true);
|
||||
validationMode.setDefault(aliases[0]);
|
||||
validationMode.set(aliases[0]);
|
||||
validationMode.setDynamic(true);
|
||||
|
||||
// initialize supported options that some runtimes may not support
|
||||
supportedOptions.add(OPTION_NONTRANS_READ);
|
||||
supportedOptions.add(OPTION_OPTIMISTIC);
|
||||
|
@ -1509,6 +1524,14 @@ public class OpenJPAConfigurationImpl
|
|||
eagerInitialization.set(retry);
|
||||
}
|
||||
|
||||
public void setValidationMode(String mode) {
|
||||
validationMode.setString(mode);
|
||||
}
|
||||
|
||||
public String getValidationMode() {
|
||||
return validationMode.getString();
|
||||
}
|
||||
|
||||
public void instantiateAll() {
|
||||
super.instantiateAll();
|
||||
getMetaDataRepositoryInstance();
|
||||
|
|
|
@ -0,0 +1,226 @@
|
|||
/*
|
||||
* 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.validation;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.persistence.ValidationMode;
|
||||
|
||||
import org.apache.openjpa.conf.OpenJPAConfiguration;
|
||||
import org.apache.openjpa.lib.log.Log;
|
||||
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
|
||||
import org.apache.openjpa.persistence.OpenJPAPersistence;
|
||||
import org.apache.openjpa.persistence.query.SimpleEntity;
|
||||
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
|
||||
|
||||
/**
|
||||
* Tests the new Bean Validation Mode support in the JPA 2.0 spec.
|
||||
* Basic (no provider) Validation scenarios being tested:
|
||||
* 1) By default, validation mode is AUTO
|
||||
* 2) Validation mode of AUTO in persistence.xml is the same as default
|
||||
* 3) Validation mode of NONE in persistence.xml overrides default AUTO
|
||||
* 4) Validation mode of CALLBACK in persistence.xml overrides default AUTO
|
||||
* 5) Validation mode in createEMF(Map props) overrides no persistence.xml
|
||||
* 6) Validation mode in createEMF(Map props) overrides persistence.xml
|
||||
* 7) Validation mode in createEMF(Map props) can be a ValidationMode enum
|
||||
*
|
||||
* @version $Rev$ $Date$
|
||||
*/
|
||||
public class TestValidationMode extends SingleEMFTestCase {
|
||||
|
||||
@Override
|
||||
public void setUp() {
|
||||
super.setUp(CLEAR_TABLES, SimpleEntity.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scenario being tested:
|
||||
* 1) By default, validation mode == AUTO
|
||||
*/
|
||||
public void testValidationMode1() {
|
||||
getLog().trace("testValidationMode1() - Default mode is AUTO");
|
||||
OpenJPAEntityManagerFactory emf = null;
|
||||
|
||||
// create our EMF
|
||||
emf = OpenJPAPersistence.createEntityManagerFactory(
|
||||
"simple",
|
||||
"org/apache/openjpa/persistence/validation/persistence.xml");
|
||||
assertNotNull(emf);
|
||||
// verify default validation mode
|
||||
OpenJPAConfiguration conf = emf.getConfiguration();
|
||||
assertNotNull(conf);
|
||||
assertEquals("Default validation mode",
|
||||
String.valueOf(ValidationMode.AUTO),
|
||||
conf.getValidationMode());
|
||||
}
|
||||
|
||||
/**
|
||||
* Scenario being tested:
|
||||
* 2) Validation mode of AUTO in persistence.xml is the same as default
|
||||
*/
|
||||
public void testValidationMode2() {
|
||||
getLog().trace("testValidationMode1() - AUTO in persistence.xml");
|
||||
OpenJPAEntityManagerFactory emf = null;
|
||||
|
||||
// create our EMF
|
||||
emf = OpenJPAPersistence.createEntityManagerFactory(
|
||||
"simple-auto-mode",
|
||||
"org/apache/openjpa/persistence/validation/persistence.xml");
|
||||
assertNotNull(emf);
|
||||
// verify expected validation mode
|
||||
OpenJPAConfiguration conf = emf.getConfiguration();
|
||||
assertNotNull(conf);
|
||||
assertEquals("Validation mode",
|
||||
String.valueOf(ValidationMode.AUTO),
|
||||
conf.getValidationMode());
|
||||
}
|
||||
|
||||
/**
|
||||
* Scenario being tested:
|
||||
* 3) Validation mode of NONE in persistence.xml overrides default
|
||||
*/
|
||||
public void testValidationMode3() {
|
||||
getLog().trace("testValidationMode3() - persistence.xml overrides " +
|
||||
"Default");
|
||||
OpenJPAEntityManagerFactory emf = null;
|
||||
|
||||
// create our EMF
|
||||
emf = OpenJPAPersistence.createEntityManagerFactory(
|
||||
"simple-none-mode",
|
||||
"org/apache/openjpa/persistence/validation/persistence.xml");
|
||||
assertNotNull(emf);
|
||||
// verify validation mode
|
||||
OpenJPAConfiguration conf = emf.getConfiguration();
|
||||
assertNotNull(conf);
|
||||
assertEquals("Validation mode",
|
||||
String.valueOf(ValidationMode.NONE),
|
||||
conf.getValidationMode());
|
||||
}
|
||||
|
||||
/**
|
||||
* Scenario being tested:
|
||||
* 4) Validation mode of CALLBACK in persistence.xml overrides default
|
||||
*/
|
||||
public void testValidationMode4() {
|
||||
getLog().trace("testValidationMode4() - persistence.xml overrides " +
|
||||
"Default");
|
||||
OpenJPAEntityManagerFactory emf = null;
|
||||
|
||||
// create our EMF
|
||||
emf = OpenJPAPersistence.createEntityManagerFactory(
|
||||
"simple-callback-mode",
|
||||
"org/apache/openjpa/persistence/validation/persistence.xml");
|
||||
assertNotNull(emf);
|
||||
// verify validation mode
|
||||
OpenJPAConfiguration conf = emf.getConfiguration();
|
||||
assertNotNull(conf);
|
||||
assertEquals("Validation mode",
|
||||
String.valueOf(ValidationMode.CALLBACK),
|
||||
conf.getValidationMode());
|
||||
}
|
||||
|
||||
/**
|
||||
* Scenario being tested:
|
||||
* 5) Validation mode in createEMF(Map props) overrides no persistence.xml
|
||||
*/
|
||||
public void testValidationMode5() {
|
||||
getLog().trace("testValidationMode5() - Map(NONE) overrides default");
|
||||
OpenJPAEntityManagerFactory emf = null;
|
||||
|
||||
// create the Map to test overrides
|
||||
Map<String,String> props = new HashMap<String,String>();
|
||||
props.put("javax.persistence.validation.mode",
|
||||
String.valueOf(ValidationMode.NONE));
|
||||
|
||||
// create our EMF
|
||||
emf = OpenJPAPersistence.createEntityManagerFactory(
|
||||
"simple",
|
||||
"org/apache/openjpa/persistence/validation/persistence.xml",
|
||||
props);
|
||||
assertNotNull(emf);
|
||||
// verify validation mode
|
||||
OpenJPAConfiguration conf = emf.getConfiguration();
|
||||
assertNotNull(conf);
|
||||
assertEquals("Validation mode",
|
||||
String.valueOf(ValidationMode.NONE),
|
||||
conf.getValidationMode());
|
||||
}
|
||||
|
||||
/**
|
||||
* Scenario being tested:
|
||||
* 6) Validation mode in createEMF(Map props) overrides persistence.xml
|
||||
*/
|
||||
public void testValidationMode6() {
|
||||
getLog().trace("testValidationMode6() - Map(NONE) overrides PU " +
|
||||
"provided mode=callback");
|
||||
OpenJPAEntityManagerFactory emf = null;
|
||||
|
||||
// create the Map to test overrides
|
||||
Map<String,String> props = new HashMap<String,String>();
|
||||
props.put("javax.persistence.validation.mode",
|
||||
String.valueOf(ValidationMode.NONE));
|
||||
|
||||
// create our EMF
|
||||
emf = OpenJPAPersistence.createEntityManagerFactory(
|
||||
"simple-callback-mode",
|
||||
"org/apache/openjpa/persistence/validation/persistence.xml",
|
||||
props);
|
||||
assertNotNull(emf);
|
||||
// verify validation mode
|
||||
OpenJPAConfiguration conf = emf.getConfiguration();
|
||||
assertNotNull(conf);
|
||||
assertEquals("Validation mode",
|
||||
String.valueOf(ValidationMode.NONE),
|
||||
conf.getValidationMode());
|
||||
}
|
||||
|
||||
/**
|
||||
* Scenario being tested:
|
||||
* 7) Validation mode in createEMF(Map props) can be a ValidationMode enum
|
||||
*/
|
||||
public void testValidationMode7() {
|
||||
getLog().trace("testValidationMode7() - Map(ValidationMode.NONE) " +
|
||||
"overrides PU provided mode=callback");
|
||||
OpenJPAEntityManagerFactory emf = null;
|
||||
|
||||
// create the Map to test overrides
|
||||
Map<String,Object> props = new HashMap<String,Object>();
|
||||
props.put("javax.persistence.validation.mode",
|
||||
ValidationMode.NONE);
|
||||
|
||||
// create our EMF
|
||||
emf = OpenJPAPersistence.createEntityManagerFactory(
|
||||
"simple-callback-mode",
|
||||
"org/apache/openjpa/persistence/validation/persistence.xml",
|
||||
props);
|
||||
assertNotNull(emf);
|
||||
// verify validation mode
|
||||
OpenJPAConfiguration conf = emf.getConfiguration();
|
||||
assertNotNull(conf);
|
||||
assertEquals("Validation mode",
|
||||
String.valueOf(ValidationMode.NONE),
|
||||
conf.getValidationMode());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Internal convenience method for getting the OpenJPA logger
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private Log getLog() {
|
||||
return emf.getConfiguration().getLog("Tests");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
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.
|
||||
-->
|
||||
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
version="2.0">
|
||||
|
||||
<!-- Following PUs are used by TestValidationMode -->
|
||||
<persistence-unit name="simple">
|
||||
<class>org.apache.openjpa.persistence.query.SimpleEntity</class>
|
||||
</persistence-unit>
|
||||
|
||||
<persistence-unit name="simple-auto-mode">
|
||||
<validation-mode>AUTO</validation-mode>
|
||||
<class>org.apache.openjpa.persistence.query.SimpleEntity</class>
|
||||
</persistence-unit>
|
||||
|
||||
<persistence-unit name="simple-callback-mode" validation-mode="CALLBACK">
|
||||
<validation-mode>CALLBACK</validation-mode>
|
||||
<class>org.apache.openjpa.persistence.query.SimpleEntity</class>
|
||||
</persistence-unit>
|
||||
|
||||
<persistence-unit name="simple-none-mode" validation-mode="NONE">
|
||||
<validation-mode>NONE</validation-mode>
|
||||
<class>org.apache.openjpa.persistence.query.SimpleEntity</class>
|
||||
</persistence-unit>
|
||||
|
||||
</persistence>
|
|
@ -34,6 +34,7 @@ import java.util.Map;
|
|||
import java.util.MissingResourceException;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.persistence.ValidationMode;
|
||||
import javax.persistence.spi.PersistenceUnitInfo;
|
||||
import javax.persistence.spi.PersistenceUnitTransactionType;
|
||||
|
||||
|
@ -43,6 +44,7 @@ import org.apache.openjpa.conf.OpenJPAConfiguration;
|
|||
import org.apache.openjpa.conf.OpenJPAConfigurationImpl;
|
||||
import org.apache.openjpa.conf.OpenJPAProductDerivation;
|
||||
import org.apache.openjpa.conf.Specification;
|
||||
import org.apache.openjpa.kernel.LockLevels;
|
||||
import org.apache.openjpa.kernel.MixedLockLevels;
|
||||
import org.apache.openjpa.lib.conf.AbstractProductDerivation;
|
||||
import org.apache.openjpa.lib.conf.Configuration;
|
||||
|
@ -686,7 +688,12 @@ public class PersistenceProductDerivation
|
|||
// startPersistenceUnit()
|
||||
// case 'property' for 'properties' is handled in startElement()
|
||||
case 'c': // class
|
||||
if ("class".equals(name))
|
||||
_info.addManagedClassName(currentText());
|
||||
else // FIXME - caching
|
||||
throw new javax.persistence.PersistenceException(
|
||||
"Not implemented yet");
|
||||
break;
|
||||
case 'e': // exclude-unlisted-classes
|
||||
_info.setExcludeUnlistedClasses("true".equalsIgnoreCase
|
||||
(currentText()));
|
||||
|
@ -713,6 +720,10 @@ public class PersistenceProductDerivation
|
|||
if ("provider".equals(name))
|
||||
_info.setPersistenceProviderClassName(currentText());
|
||||
break;
|
||||
case 'v': // validation-mode
|
||||
_info.setValidationMode(Enum.valueOf(ValidationMode.class,
|
||||
currentText()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -60,6 +60,8 @@ public class PersistenceUnitInfoImpl
|
|||
implements PersistenceUnitInfo, SourceTracker {
|
||||
|
||||
public static final String KEY_PROVIDER = "javax.persistence.provider";
|
||||
public static final String VALIDATION_MODE =
|
||||
"javax.persistence.validation.mode";
|
||||
|
||||
private static final Localizer s_loc = Localizer.forPackage
|
||||
(PersistenceUnitInfoImpl.class);
|
||||
|
@ -80,6 +82,7 @@ public class PersistenceUnitInfoImpl
|
|||
private boolean _excludeUnlisted;
|
||||
private URL _persistenceXmlFile;
|
||||
private String _schemaVersion = "1.0";
|
||||
private ValidationMode _validationMode;
|
||||
|
||||
// A persistence unit is defined by a persistence.xml file. The jar
|
||||
// file or directory whose META-INF directory contains the
|
||||
|
@ -311,6 +314,11 @@ public class PersistenceUnitInfoImpl
|
|||
setNonJtaDataSourceName((String) val);
|
||||
else
|
||||
setNonJtaDataSource((DataSource) val);
|
||||
} else if (VALIDATION_MODE.equals(key)) {
|
||||
if (val instanceof String)
|
||||
setValidationMode((String) val);
|
||||
else
|
||||
setValidationMode((ValidationMode) val);
|
||||
} else
|
||||
_props.put(key, val);
|
||||
}
|
||||
|
@ -447,6 +455,11 @@ public class PersistenceUnitInfoImpl
|
|||
// always record provider name for product derivations to access
|
||||
if (info.getPersistenceProviderClassName() != null)
|
||||
map.put(KEY_PROVIDER, info.getPersistenceProviderClassName());
|
||||
|
||||
// convert validation-mode enum to a StringValue
|
||||
if (info.getValidationMode() != null)
|
||||
map.put(VALIDATION_MODE, String.valueOf(info.getValidationMode()));
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
|
@ -518,8 +531,20 @@ public class PersistenceUnitInfoImpl
|
|||
"JPA 2.0 - Method not yet implemented");
|
||||
}
|
||||
|
||||
public ValidationMode getValidationMode() {
|
||||
public void setCaching(Caching cache) {
|
||||
throw new UnsupportedOperationException(
|
||||
"JPA 2.0 - Method not yet implemented");
|
||||
}
|
||||
|
||||
public ValidationMode getValidationMode() {
|
||||
return _validationMode;
|
||||
}
|
||||
|
||||
protected void setValidationMode(String mode) {
|
||||
setValidationMode(Enum.valueOf(ValidationMode.class, mode));
|
||||
}
|
||||
|
||||
public void setValidationMode(ValidationMode mode) {
|
||||
_validationMode = mode;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue