mirror of https://github.com/apache/openjpa.git
OPENJPA-44: Add test cases for processing metadata-complete tag.
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@680566 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
9174b3f174
commit
5f138fe08d
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* 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.annotations.xml;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.NamedQuery;
|
||||
|
||||
/**
|
||||
* Used to test effect of metadata-complete tag.
|
||||
*
|
||||
* @author Pinaki Poddar
|
||||
*
|
||||
*/
|
||||
@Entity
|
||||
@NamedQuery(name="DerivedA.SelectAll", query="SELECT a FROM DerivedA a")
|
||||
public class DerivedA extends EntityA {
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* 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.annotations.xml;
|
||||
|
||||
import javax.persistence.Basic;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.NamedQuery;
|
||||
|
||||
/**
|
||||
* Used to test effect of metadata-complete tag.
|
||||
*
|
||||
* @author Pinaki Poddar
|
||||
*
|
||||
*/
|
||||
@Entity
|
||||
public class DerivedB extends EntityB {
|
||||
@Basic
|
||||
@Column(name="xyz")
|
||||
private String data;
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
* 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.annotations.xml;
|
||||
|
||||
import javax.persistence.Basic;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Inheritance;
|
||||
import javax.persistence.InheritanceType;
|
||||
|
||||
/**
|
||||
* Used to test effect of metadata-complete tag.
|
||||
*
|
||||
* The annotations should not be processed as corresponding orm.xml will
|
||||
* switch off annotation processing via metadata-complete tag.
|
||||
*
|
||||
* @author Pinaki Poddar
|
||||
*
|
||||
*/
|
||||
@Entity
|
||||
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
|
||||
public class EntityA {
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
@Basic(optional=true)
|
||||
private String name;
|
||||
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
* 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.annotations.xml;
|
||||
|
||||
import javax.persistence.Basic;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Inheritance;
|
||||
import javax.persistence.InheritanceType;
|
||||
import javax.persistence.NamedQuery;
|
||||
|
||||
/**
|
||||
* Used to test effect of metadata-complete tag.
|
||||
*
|
||||
* The annotations should be processed in conjunction with corresponding orm.xml
|
||||
*
|
||||
* @author Pinaki Poddar
|
||||
*
|
||||
*/
|
||||
@Entity
|
||||
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
|
||||
@NamedQuery(name="EntityB.SelectOne", query="SELECT b FROM DerivedB b WHERE name=?1")
|
||||
public class EntityB {
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
@Basic(optional=true)
|
||||
private String name;
|
||||
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,124 @@
|
|||
/*
|
||||
* 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.annotations.xml;
|
||||
|
||||
import org.apache.openjpa.jdbc.meta.ClassMapping;
|
||||
import org.apache.openjpa.jdbc.meta.strats.FlatClassStrategy;
|
||||
import org.apache.openjpa.jdbc.meta.strats.FullClassStrategy;
|
||||
import org.apache.openjpa.jdbc.meta.strats.VerticalClassStrategy;
|
||||
import org.apache.openjpa.meta.ClassMetaData;
|
||||
import org.apache.openjpa.meta.MetaDataRepository;
|
||||
import org.apache.openjpa.meta.ValueStrategies;
|
||||
import org.apache.openjpa.persistence.ArgumentException;
|
||||
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
|
||||
import org.apache.openjpa.persistence.OpenJPAPersistence;
|
||||
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
|
||||
|
||||
/**
|
||||
* Tests metadata-complete tag switches off any annotation processing.
|
||||
*
|
||||
* EntityA uses metadata-complete tag and hence all its annotated mapping info
|
||||
* must not be processed.
|
||||
*
|
||||
* EntityB does not use metadata-complete tag and hence its mapping info should
|
||||
* be combination of annotation mapping info overwritten by xml description
|
||||
* mapping info.
|
||||
*
|
||||
* @author Pinaki Poddar
|
||||
*
|
||||
*/
|
||||
public class TestMetaDataComplete extends SingleEMFTestCase {
|
||||
private static OpenJPAEntityManagerFactorySPI oemf;
|
||||
|
||||
private static ClassMetaData entityA, entityB, derivedA, derivedB;
|
||||
|
||||
public void setUp() throws Exception {
|
||||
if (oemf == null) {
|
||||
super.setUp(EntityA.class, EntityB.class, DerivedA.class,
|
||||
DerivedB.class);
|
||||
oemf = (OpenJPAEntityManagerFactorySPI) OpenJPAPersistence
|
||||
.cast(emf);
|
||||
MetaDataRepository repos = oemf.getConfiguration()
|
||||
.getMetaDataRepositoryInstance();
|
||||
|
||||
entityA = repos.getMetaData(EntityA.class, null, true);
|
||||
entityB = repos.getMetaData(EntityB.class, null, true);
|
||||
derivedA = repos.getMetaData(DerivedA.class, null, true);
|
||||
derivedB = repos.getMetaData(DerivedB.class, null, true);
|
||||
}
|
||||
}
|
||||
|
||||
public void tearDown() {
|
||||
|
||||
}
|
||||
|
||||
protected String getPersistenceUnitName() {
|
||||
return "test-metadata-complete";
|
||||
}
|
||||
|
||||
public void testIgnoresClassAnnotationIfMetaDataComplete() {
|
||||
// inheritance strategy of EntityA by annotation is SINGLE_TABLE
|
||||
// inheritance strategy of EntityA in xml descriptor is JOINED
|
||||
assertEquals(FullClassStrategy.class, ((ClassMapping) entityA)
|
||||
.getStrategy().getClass());
|
||||
assertEquals(VerticalClassStrategy.class, ((ClassMapping) derivedA)
|
||||
.getStrategy().getClass());
|
||||
}
|
||||
|
||||
public void testProcessesClassAnnotationIfMetaDataIsNotComplete() {
|
||||
// inheritance strategy of EntityB by annotation is SINGLE_TABLE
|
||||
// inheritance strategy of EntityB in xml descriptor is not specified
|
||||
assertEquals(FullClassStrategy.class, ((ClassMapping) entityB)
|
||||
.getStrategy().getClass());
|
||||
assertEquals(FlatClassStrategy.class, ((ClassMapping) derivedB)
|
||||
.getStrategy().getClass());
|
||||
}
|
||||
|
||||
public void testIgnoresFieldAnnotationIfMetaDataComplete() {
|
||||
// generation strategy of EntityA.id by annotation is IDENTITY
|
||||
// inheritance strategy of EntityA in xml descriptor is SEQUENCE
|
||||
int valueStrategyA = entityA.getField("id").getValueStrategy();
|
||||
assertEquals(ValueStrategies.SEQUENCE, valueStrategyA);
|
||||
}
|
||||
|
||||
public void testProcessesFieldAnnotationIfMetaDataIsNotComplete() {
|
||||
// generation strategy of EntityB.id by annotation is IDENTITY
|
||||
// inheritance strategy of EntityA in xml descriptor is not specified
|
||||
int valueStrategyB = entityB.getField("id").getValueStrategy();
|
||||
assertEquals(ValueStrategies.AUTOASSIGN, valueStrategyB);
|
||||
}
|
||||
|
||||
public void testIgnoresNamedQueryIfMetaDataComplete() {
|
||||
// DerivedA has annotated NamedQuery
|
||||
String namedQuery = "DerivedA.SelectAll";
|
||||
try {
|
||||
oemf.createEntityManager().createNamedQuery(namedQuery);
|
||||
fail("Expected not to find NamedQuery [" + namedQuery + "]");
|
||||
} catch (ArgumentException e) {
|
||||
assertTrue(e.getMessage().contains(namedQuery));
|
||||
}
|
||||
}
|
||||
|
||||
public void testProcessesNamedQueryIfMetaDataIsNotComplete() {
|
||||
// EntityB has annotated NamedQuery
|
||||
// EntityB has a Named Query in xml descriptor
|
||||
oemf.createEntityManager().createNamedQuery("EntityB.SelectOne");
|
||||
oemf.createEntityManager().createNamedQuery("EntityB.SelectAll");
|
||||
}
|
||||
}
|
|
@ -96,4 +96,8 @@
|
|||
<class>org.apache.openjpa.persistence.jdbc.unique.UniqueB</class>
|
||||
</persistence-unit>
|
||||
|
||||
<persistence-unit name="test-metadata-complete">
|
||||
<mapping-file>org/apache/openjpa/persistence/annotations/xml/orm.xml</mapping-file>
|
||||
</persistence-unit>
|
||||
|
||||
</persistence>
|
||||
|
|
|
@ -0,0 +1,63 @@
|
|||
<?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.
|
||||
-->
|
||||
|
||||
<!-- A pair of entities are defined with metadata-complete attribute set to
|
||||
true and false respectively. Both entities carry identical annotation.
|
||||
|
||||
-->
|
||||
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
|
||||
version="1.0">
|
||||
<entity class="org.apache.openjpa.persistence.annotations.xml.EntityA" metadata-complete="true">
|
||||
<inheritance strategy="JOINED"/>
|
||||
<attributes>
|
||||
<id name="id">
|
||||
<column name="ID" />
|
||||
<generated-value strategy="SEQUENCE"/>
|
||||
</id>
|
||||
<basic name="name" optional="false">
|
||||
</basic>
|
||||
</attributes>
|
||||
</entity>
|
||||
|
||||
<entity class="org.apache.openjpa.persistence.annotations.xml.EntityB" metadata-complete="false">
|
||||
<named-query name="EntityB.SelectAll">
|
||||
<query>SELECT b FROM EntityB b</query>
|
||||
</named-query>
|
||||
<attributes>
|
||||
<id name="id">
|
||||
<column name="ID" />
|
||||
</id>
|
||||
<basic name="name">
|
||||
</basic>
|
||||
</attributes>
|
||||
</entity>
|
||||
<entity class="org.apache.openjpa.persistence.annotations.xml.DerivedA"
|
||||
metadata-complete="true">
|
||||
</entity>
|
||||
<entity class="org.apache.openjpa.persistence.annotations.xml.DerivedB"
|
||||
metadata-complete="false">
|
||||
<attributes>
|
||||
<basic name="data">
|
||||
</basic>
|
||||
</attributes>
|
||||
</entity>
|
||||
</entity-mappings>
|
Loading…
Reference in New Issue