|
|
|
Pinaki Poddar
|
Author: ppoddar
Date: Thu Jul 24 11:45:44 2008 New Revision: 679487 URL: http://svn.apache.org/viewvc?rev=679487&view=rev Log: OPENJPA-665: Sets column i/o conditions accordingly when column is set as not nullable. Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/NullValues.java openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/TestBasicFieldNullity.java Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingInfo.java Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingInfo.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingInfo.java?rev=679487&r1=679486&r2=679487&view=diff ============================================================================== --- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingInfo.java (original) +++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingInfo.java Thu Jul 24 11:45:44 2008 @@ -527,13 +527,16 @@ */ private void setIOFromColumnFlags(Column col, int i) { if (col == null || (!col.getFlag(Column.FLAG_UNINSERTABLE) - && !col.getFlag(Column.FLAG_UNUPDATABLE))) + && !col.getFlag(Column.FLAG_UNUPDATABLE) + && !col.isNotNull())) return; if (_io == null) _io = new ColumnIO(); _io.setInsertable(i, !col.getFlag(Column.FLAG_UNINSERTABLE)); _io.setUpdatable(i, !col.getFlag(Column.FLAG_UNUPDATABLE)); + _io.setNullInsertable(i, !col.isNotNull()); + _io.setNullUpdatable(i, !col.isNotNull()); } /** Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/NullValues.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/NullValues.java?rev=679487&view=auto ============================================================================== --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/NullValues.java (added) +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/NullValues.java Thu Jul 24 11:45:44 2008 @@ -0,0 +1,94 @@ +/* + * 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.nullity; + +import javax.persistence.Basic; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; + +/** + * Persistent entity used to test behavior of null constraint on basic fields. + * + * @author Pinaki Poddar + * + */ +@Entity +public class NullValues { + @Id + private long id; + + @Column(nullable=true) + private Integer nullable; + + @Column(nullable=false) + private Integer notNullable; + + @Basic(optional=true) + private Integer optional; + + @Basic(optional=false) + private Integer notOptional; + + /** + * Construct with all fields set to non-null values. + */ + public NullValues() { + setOptional(42); + setNotOptional(42); + setNotNullable(42); + setNullable(42); + } + + public long getId() { + return id; + } + + public Integer getNullable() { + return nullable; + } + + public void setNullable(Integer nullable) { + this.nullable = nullable; + } + + public Integer getNotNullable() { + return notNullable; + } + + public void setNotNullable(Integer notNullable) { + this.notNullable = notNullable; + } + + public Integer getOptional() { + return optional; + } + + public void setOptional(Integer optional) { + this.optional = optional; + } + + public Integer getNotOptional() { + return notOptional; + } + + public void setNotOptional(Integer notOptional) { + this.notOptional = notOptional; + } +} Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/TestBasicFieldNullity.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/TestBasicFieldNullity.java?rev=679487&view=auto ============================================================================== --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/TestBasicFieldNullity.java (added) +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/TestBasicFieldNullity.java Thu Jul 24 11:45:44 2008 @@ -0,0 +1,119 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.openjpa.persistence.nullity; + +import javax.persistence.EntityManager; +import javax.persistence.RollbackException; + +import org.apache.openjpa.persistence.InvalidStateException; +import org.apache.openjpa.persistence.test.SingleEMFTestCase; + + +/** + * Test @Basic(optional=true|false) and @Column(nullable=true|false) + * specification is honored. + * Note: null constraint violation manifests as different exception types + * for option and nullable condition. + * + * @author Pinaki Poddar + */ +public class TestBasicFieldNullity extends SingleEMFTestCase { + + + public void setUp() { + setUp(NullValues.class); + } + + public void testNullOnOptionalFieldIsAllowed() { + NullValues pc = new NullValues(); + pc.setOptional(null); + assertCommitSucceeds(pc); + } + + public void testNullOnNonOptionalFieldIsDisallowed() { + NullValues pc = new NullValues(); + pc.setNotOptional(null); + assertCommitFails(pc, InvalidStateException.class); + } + + public void testNotNullOnOptionalFieldIsAllowed() { + NullValues pc = new NullValues(); + assertCommitSucceeds(pc); + } + + public void testNotNullOnNonOptionalFieldIsAllowed() { + NullValues pc = new NullValues(); + assertCommitSucceeds(pc); + } + + public void testNullOnNullableColumnAllowed() { + NullValues pc = new NullValues(); + pc.setNullable(null); + assertCommitSucceeds(pc); + } + + public void testNullOnNonNullableColumnIsDisallowed() { + NullValues pc = new NullValues(); + pc.setNotNullable(null); + assertCommitFails(pc, RollbackException.class); + } + + public void testNotNullOnNullableColumnIsAllowed() { + NullValues pc = new NullValues(); + assertCommitSucceeds(pc); + } + + public void testNotNullOnNonNullableColumnIsAllowed() { + NullValues pc = new NullValues(); + assertCommitSucceeds(pc); + } + + /** + * Asserts that the given instance can not be committed. + */ + void assertCommitFails(Object pc, Class expected) { + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + em.persist(pc); + try { + em.getTransaction().commit(); + fail(); + } catch (RuntimeException e) { + if (!expected.isAssignableFrom(e.getClass())) { + fail("Expected " + expected.getName()); + e.printStackTrace(); + } + } + } + + void assertCommitSucceeds(Object pc) { + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + em.persist(pc); + try { + em.getTransaction().commit(); + } catch (RuntimeException e) { + fail(); + e.printStackTrace(); + } + } + + +} + |
|||||||||||||||
| Free Forum Powered by Nabble | Forum Help |