KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > test > UniqueIndexTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.mdr.test;
21
22 import java.util.*;
23
24 import junit.extensions.*;
25 import junit.framework.*;
26
27 import org.openide.util.Lookup;
28
29 import org.netbeans.mdr.util.*;
30 import org.netbeans.mdr.NBMDRepositoryImpl;
31
32 import org.netbeans.mdr.persistence.memoryimpl.StorageFactoryImpl;
33 import org.netbeans.mdr.persistence.*;
34
35 import javax.jmi.reflect.*;
36 import javax.jmi.model.*;
37
38 /**
39  * Multivalued index test case.
40  */

41 public class UniqueIndexTest extends MDRTestCase {
42
43     public static final String JavaDoc KEY_STR = "key_";
44     public static final String JavaDoc VAL_STR = "value_";
45     
46     public UniqueIndexTest(String JavaDoc testName) {
47         super (testName);
48     }
49     
50     public static void main (String JavaDoc[] args) {
51         junit.textui.TestRunner.run (suite ());
52     }
53     
54     public static Test suite () {
55         TestSuite suite = new TestSuite ();
56         suite.addTestSuite (UniqueIndexTest.class);
57         
58         TestSetup setup = new TestSetup (suite) {
59             public void setUp () {
60 // org.netbeans.mdr.handlers.BaseObjectHandler.setDefaultClassLoader (this.getClass ().getClassLoader ());
61
}
62             public void tearDown () {
63             }
64         };
65         return setup;
66     }
67     
68     public void test () throws Exception JavaDoc {
69         StorageFactoryImpl factory = new StorageFactoryImpl();
70         Map map = new HashMap();
71         map.put(StorageFactoryImpl.STORAGE_ID, "UniqueIndexTestStorage");
72         Storage storage = factory.createStorage(map);
73         MultivaluedIndex uniIndex = storage.createMultivaluedIndex("index_1", Storage.EntryType.STRING, Storage.EntryType.STRING, true);
74         MultivaluedIndex index = storage.createMultivaluedIndex("index_2", Storage.EntryType.STRING, Storage.EntryType.STRING, false);
75         MultivaluedOrderedIndex uniOrderedIndex = storage.createMultivaluedOrderedIndex("index_3", Storage.EntryType.STRING, Storage.EntryType.STRING, true);
76         boolean failed;
77         
78         for (int x = 0; x < 10; x++) {
79             String JavaDoc key = KEY_STR + x;
80             for (int y = 0; y < 10; y++) {
81                 String JavaDoc value = VAL_STR + y;
82                 uniIndex.add(key, value);
83                 index.add(key, value);
84                 uniOrderedIndex.add(key, value);
85             }
86         }
87         
88         String JavaDoc key = KEY_STR + 2;
89         String JavaDoc value = VAL_STR + 4;
90         int pos = 4;
91         
92         failed = false;
93         try {
94             uniIndex.add(key, value);
95         } catch (StorageBadRequestException e) {
96             failed = true;
97         }
98         if (!failed)
99             fail();
100         
101         failed = false;
102         try {
103             uniOrderedIndex.add(key, value);
104         } catch (StorageBadRequestException e) {
105             failed = true;
106         }
107         if (!failed)
108             fail();
109         
110         // should not fail
111
index.add(key, value);
112         uniIndex.remove(key, value);
113         uniOrderedIndex.remove(key, value);
114         uniIndex.add(key, value);
115         uniOrderedIndex.add(key, pos, value);
116         uniOrderedIndex.replace(key, pos, value);
117         
118         failed = false;
119         try {
120             uniOrderedIndex.replace(key, 1, value);
121         } catch (StorageBadRequestException e) {
122             failed = true;
123         }
124         if (!failed)
125             fail();
126         
127         Collection coll_1 = uniIndex.getItems(key);
128         coll_1.add(VAL_STR + 20);
129         coll_1.remove(VAL_STR + 20);
130         List coll_2 = uniOrderedIndex.getItemsOrdered(key);
131         coll_2.add(VAL_STR + 20);
132         coll_2.remove(VAL_STR + 20);
133         
134         failed = false;
135         try {
136             coll_2.set(7, value);
137         } catch (RuntimeStorageException e) {
138             failed = true;
139         }
140         if (!failed)
141             fail();
142         
143         ListIterator iter = coll_2.listIterator();
144         iter.add(VAL_STR + 11);
145         iter.next();
146         iter.remove();
147         iter.next();
148         
149         failed = false;
150         try {
151             iter.set(VAL_STR + 8);
152         } catch (RuntimeStorageException e) {
153             failed = true;
154         }
155         if (!failed)
156             fail();
157         
158         iter.next();
159         failed = false;
160         try {
161             iter.add(VAL_STR + 11);
162         } catch (RuntimeStorageException e) {
163             failed = true;
164         }
165         if (!failed)
166             fail();
167         
168     }
169
170 }
Popular Tags