From 1111ee16ff9fdb72c7e5d12dd0afad6ddda2be5c Mon Sep 17 00:00:00 2001 From: Pinaki Poddar Date: Thu, 24 Jul 2008 18:45:44 +0000 Subject: [PATCH] OPENJPA-665: Sets column i/o conditions accordingly when column is set as not nullable. git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@679487 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/openjpa/jdbc/meta/MappingInfo.java | 5 +- .../persistence/nullity/NullValues.java | 94 ++++++++++++++ .../nullity/TestBasicFieldNullity.java | 119 ++++++++++++++++++ 3 files changed, 217 insertions(+), 1 deletion(-) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/NullValues.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/TestBasicFieldNullity.java diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingInfo.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingInfo.java index 9dd16ccaa..a0937361d 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingInfo.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingInfo.java @@ -527,13 +527,16 @@ public abstract class MappingInfo */ 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()); } /** diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/NullValues.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/NullValues.java new file mode 100644 index 000000000..02736a76b --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/NullValues.java @@ -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; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/TestBasicFieldNullity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/TestBasicFieldNullity.java new file mode 100644 index 000000000..ece0cf5b0 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/TestBasicFieldNullity.java @@ -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(); + } + } + + +} +