OPENJPA-966: Support unwrap() for JPA 2.0

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@750788 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Pinaki Poddar 2009-03-06 07:08:20 +00:00
parent 19fd6b3927
commit 4b31d54b3e
4 changed files with 96 additions and 5 deletions

View File

@ -0,0 +1,68 @@
/*
* 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;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.apache.openjpa.kernel.QueryLanguages;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
public class TestUnwrap extends SingleEMFTestCase {
/**
* Tests a query can be unwrapped as an instance of a series of class or
* interface.
*/
public void testValidQueryUnwrap() {
OpenJPAEntityManager em = emf.createEntityManager();
Query query = em.createQuery(QueryLanguages.LANG_SQL,"");
Class[] validCasts = new Class[] {
org.apache.openjpa.persistence.OpenJPAQuery.class,
org.apache.openjpa.persistence.OpenJPAQuerySPI.class,
org.apache.openjpa.kernel.DelegatingQuery.class,
org.apache.openjpa.kernel.Query.class,
org.apache.openjpa.kernel.QueryImpl.class
};
for (Class c : validCasts) {
Object unwrapped = query.unwrap(c);
assertTrue(c.isInstance(unwrapped));
}
}
/**
* Tests a EntityManager can be unwrapped as an instance of a series of
* class or interface.
*/
public void testValidEntityManagerUnwrap() {
EntityManager em = emf.createEntityManager();
Class[] validCasts = new Class[] {
org.apache.openjpa.persistence.OpenJPAEntityManager.class,
org.apache.openjpa.persistence.OpenJPAEntityManagerSPI.class,
org.apache.openjpa.kernel.DelegatingBroker.class,
org.apache.openjpa.kernel.Broker.class
};
for (Class c : validCasts) {
Object unwrapped = em.unwrap(c);
assertTrue(c.isInstance(unwrapped));
}
}
}

View File

@ -1525,8 +1525,14 @@ public class EntityManagerImpl
}
public <T> T unwrap(Class<T> cls) {
throw new UnsupportedOperationException(
"JPA 2.0 - Method not yet implemented");
Object[] delegates = new Object[]{_broker.getInnermostDelegate(),
_broker.getDelegate(), _broker, this};
for (Object o : delegates) {
if (cls.isInstance(o))
return (T)o;
}
throw new PersistenceException(_loc.get("unwrap-em-invalid", cls)
.toString(), null, this, false);
}

View File

@ -562,9 +562,24 @@ public class QueryImpl implements OpenJPAQuerySPI, Serializable {
"JPA 2.0 - Method not yet implemented");
}
/**
* Returns the innermost implementation that is an instance of the given
* class.
*
* @throws PersistenceException if none in the delegate chain is an
* instance of the given class.
*
* @since 2.0.0
*/
public <T> T unwrap(Class<T> cls) {
throw new UnsupportedOperationException(
"JPA 2.0 - Method not yet implemented");
Object[] delegates = new Object[]{_query.getInnermostDelegate(),
_query.getDelegate(), _query, this};
for (Object o : delegates) {
if (cls.isInstance(o))
return (T)o;
}
throw new PersistenceException(_loc.get("unwrap-query-invalid", cls)
.toString(), null, this, false);
}
//

View File

@ -164,4 +164,6 @@ param-type-null: Parameter "{0}" declared in "{1}" is set to null, \
version-check-error: An error occurred while attempting to determine the \
version of "{0}".
no-result: Query "{0}" selected no result, but expected unique result.
non-unique-result: Query "{0}" selected {1} results, but expected unique result.
non-unique-result: Query "{0}" selected {1} results, but expected unique result.
unwrap-em-invalid: EntityManager can not be unwrapped to an instance of "{0}".
unwrap-query-invalid: Query can not be unwrapped to an instance of "{0}".