001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.db4ounit.common.constraints;
022:
023: import com.db4o.config.*;
024: import com.db4o.constraints.*;
025: import com.db4o.query.*;
026:
027: import db4ounit.*;
028: import db4ounit.extensions.*;
029:
030: public class UniqueFieldIndexTestCase extends AbstractDb4oTestCase {
031:
032: public static void main(String[] arguments) {
033: new UniqueFieldIndexTestCase().runAll();
034: }
035:
036: public static class Item {
037:
038: public String _str;
039:
040: public Item() {
041: }
042:
043: public Item(String str) {
044: _str = str;
045: }
046: }
047:
048: protected void configure(Configuration config) throws Exception {
049: super .configure(config);
050: indexField(config, Item.class, "_str");
051: config.add(new UniqueFieldValueConstraint(Item.class, "_str"));
052: }
053:
054: protected void store() throws Exception {
055: addItem("1");
056: addItem("2");
057: addItem("3");
058: }
059:
060: public void testNewViolates() {
061: addItem("2");
062: commitExpectingViolation();
063: }
064:
065: public void testUpdateViolates() {
066: updateItem("2", "3");
067: commitExpectingViolation();
068: }
069:
070: public void testUpdateDoesNotViolate() {
071: updateItem("2", "4");
072: db().commit();
073: }
074:
075: public void testUpdatingSameObjectDoesNotViolate() {
076: updateItem("2", "2");
077: db().commit();
078: }
079:
080: public void testNewAfterDeleteDoesNotViolate() {
081: deleteItem("2");
082: addItem("2");
083: db().commit();
084: }
085:
086: public void testDeleteAfterNewDoesNotViolate() {
087: Item existing = queryItem("2");
088: addItem("2");
089: db().delete(existing);
090: db().commit();
091: }
092:
093: private void deleteItem(String value) {
094: db().delete(queryItem(value));
095: }
096:
097: private void commitExpectingViolation() {
098: Assert.expect(
099: UniqueFieldValueConstraintViolationException.class,
100: new CodeBlock() {
101: public void run() throws Throwable {
102: db().commit();
103: }
104: });
105: db().rollback();
106: }
107:
108: private Item queryItem(String str) {
109: Query q = newQuery(Item.class);
110: q.descend("_str").constrain(str);
111: return (Item) q.execute().next();
112: }
113:
114: private void addItem(String value) {
115: store(new Item(value));
116: }
117:
118: private void updateItem(String existing, String newValue) {
119: Item item = queryItem(existing);
120: item._str = newValue;
121: store(item);
122: }
123: }
|