alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2001 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.mdr.test;

import java.util.*;

import junit.extensions.*;
import junit.framework.*;

import org.openide.util.Lookup;

import org.netbeans.mdr.util.*;
import org.netbeans.mdr.NBMDRepositoryImpl;

import org.netbeans.mdr.persistence.memoryimpl.StorageFactoryImpl;
import org.netbeans.mdr.persistence.*;

import javax.jmi.reflect.*;
import javax.jmi.model.*;

/**
 * Multivalued index test case.
 */
public class UniqueIndexTest extends MDRTestCase {

    public static final String KEY_STR = "key_";
    public static final String VAL_STR = "value_";
    
    public UniqueIndexTest(String testName) {
        super (testName);
    }
    
    public static void main (String[] args) {
        junit.textui.TestRunner.run (suite ());
    }
    
    public static Test suite () {
        TestSuite suite = new TestSuite ();
        suite.addTestSuite (UniqueIndexTest.class);
        
        TestSetup setup = new TestSetup (suite) {
            public void setUp () {
//                org.netbeans.mdr.handlers.BaseObjectHandler.setDefaultClassLoader (this.getClass ().getClassLoader ());
            }
            public void tearDown () {
            }
        };
        return setup;
    }
    
    public void test () throws Exception {
        StorageFactoryImpl factory = new StorageFactoryImpl();
        Map map = new HashMap();
        map.put(StorageFactoryImpl.STORAGE_ID, "UniqueIndexTestStorage");
        Storage storage = factory.createStorage(map);
        MultivaluedIndex uniIndex = storage.createMultivaluedIndex("index_1", Storage.EntryType.STRING, Storage.EntryType.STRING, true);
        MultivaluedIndex index = storage.createMultivaluedIndex("index_2", Storage.EntryType.STRING, Storage.EntryType.STRING, false);
        MultivaluedOrderedIndex uniOrderedIndex = storage.createMultivaluedOrderedIndex("index_3", Storage.EntryType.STRING, Storage.EntryType.STRING, true);
        boolean failed;
        
        for (int x = 0; x < 10; x++) {
            String key = KEY_STR + x;
            for (int y = 0; y < 10; y++) {
                String value = VAL_STR + y;
                uniIndex.add(key, value);
                index.add(key, value);
                uniOrderedIndex.add(key, value);
            }
        }
        
        String key = KEY_STR + 2;
        String value = VAL_STR + 4;
        int pos = 4;
        
        failed = false;
        try {
            uniIndex.add(key, value);
        } catch (StorageBadRequestException e) {
            failed = true;
        }
        if (!failed)
            fail();
        
        failed = false;
        try {
            uniOrderedIndex.add(key, value);
        } catch (StorageBadRequestException e) {
            failed = true;
        }
        if (!failed)
            fail();
        
        // should not fail
        index.add(key, value);
        uniIndex.remove(key, value);
        uniOrderedIndex.remove(key, value);
        uniIndex.add(key, value);
        uniOrderedIndex.add(key, pos, value);
        uniOrderedIndex.replace(key, pos, value);
        
        failed = false;
        try {
            uniOrderedIndex.replace(key, 1, value);
        } catch (StorageBadRequestException e) {
            failed = true;
        }
        if (!failed)
            fail();
        
        Collection coll_1 = uniIndex.getItems(key);
        coll_1.add(VAL_STR + 20);
        coll_1.remove(VAL_STR + 20);
        List coll_2 = uniOrderedIndex.getItemsOrdered(key);
        coll_2.add(VAL_STR + 20);
        coll_2.remove(VAL_STR + 20);
        
        failed = false;
        try {
            coll_2.set(7, value);
        } catch (RuntimeStorageException e) {
            failed = true;
        }
        if (!failed)
            fail();
        
        ListIterator iter = coll_2.listIterator();
        iter.add(VAL_STR + 11);
        iter.next();
        iter.remove();
        iter.next();
        
        failed = false;
        try {
            iter.set(VAL_STR + 8);
        } catch (RuntimeStorageException e) {
            failed = true;
        }
        if (!failed)
            fail();
        
        iter.next();
        failed = false;
        try {
            iter.add(VAL_STR + 11);
        } catch (RuntimeStorageException e) {
            failed = true;
        }
        if (!failed)
            fail();
        
    }

}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.