FileDocCategorySizeDatePackage
TestStorageModifier.javaAPI DocApache Lucene 2.1.015981Wed Feb 14 10:46:02 GMT 2007org.apache.lucene.gdata.storage.lucenestorage

TestStorageModifier

public class TestStorageModifier extends TestCase
Copyright 2004 The Apache Software Foundation Licensed 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.

Fields Summary
private StorageModifier
modifier
private int
count
private org.apache.lucene.gdata.server.registry.ProvidedService
configurator
private Directory
dir
private StorageCoreController
controller
private static String
feedId
private static String
username
private static String
password
private static String
service
Constructors Summary
Methods Summary
private java.lang.ThreadgetRunnerThread(int idIndex)

        Thread t = new Thread(new Runner(idIndex));
        return t;
    
private org.apache.lucene.gdata.data.ServerBaseEntrygetServerEntry(com.google.gdata.data.BaseEntry e)

        ServerBaseEntry en = new ServerBaseEntry(e);
        en.setFeedId(feedId);
        en.setServiceConfig(this.configurator);
        return en;
    
protected voidsetUp()


         
        this.controller = new StorageCoreController();
        this.dir = new RAMDirectory();
        this.controller.setStorageDir(this.dir);
        this.controller.setKeepRecoveredFiles(false);
        this.controller.setOptimizeInterval(10);
        this.controller.setRecover(false);
        this.controller.setBufferSize(10);
        this.controller.setPersistFactor(10);
        this.controller.initialize();
        this.configurator = new ProvidedServiceStub();
        this.modifier = this.controller.getStorageModifier();
        this.dir = this.controller.getDirectory();

    
protected voidtearDown()

        this.count = 1;
        // destroy all resources
        this.controller.destroy();
        


    
public voidtestDeleteEntry()

        testInsertEntry();
        for (int i = 1; i < this.count; i++) {
            if (i % 2 == 0 || i < 10) {
                ServerBaseEntry entry = new ServerBaseEntry();
                entry.setId("" + i);
                entry.setFeedId(feedId);
                this.modifier.deleteEntry(new StorageEntryWrapper(entry,StorageOperation.DELETE));
            }
            ReferenceCounter<StorageQuery> query = this.controller
                    .getStorageQuery();
            if (i % 2 == 0 || i < 10) {
                assertNull(query.get().singleEntryQuery("" + i, feedId,
                        this.configurator));
            } else
                assertEquals("" + i, query.get().singleEntryQuery("" + i,
                        feedId, this.configurator).getId());
            query.decrementRef();
        }

        this.controller.forceWrite();
        IndexSearcher searcher = new IndexSearcher(this.dir);

        for (int i = 1; i < this.count; i++) {
            Query luceneQuery = new TermQuery(new Term(
                    StorageEntryWrapper.FIELD_ENTRY_ID, "" + i));
            Hits hits = searcher.search(luceneQuery);
            if (i % 2 == 0 || i < 10) {

                assertEquals(0, hits.length());
            } else
                assertEquals(1, hits.length());
        }
        searcher.close();

    
public voidtestDeleteFeed()

        testSaveFeed();
        Entry e = new Entry();
        e.setTitle(new PlainTextConstruct("hello world"));
        ServerBaseEntry entry = new ServerBaseEntry(e);
        entry.setFeedId(feedId);
        entry.setId("testme");
        entry.setServiceConfig(this.configurator);
        StorageEntryWrapper entryWrapper = new StorageEntryWrapper(entry,StorageOperation.INSERT);
        this.modifier.insertEntry(entryWrapper);
        this.modifier.forceWrite();
        this.modifier.deleteFeed(feedId);
        IndexSearcher searcher = new IndexSearcher(this.dir);
        Query q = new TermQuery(new Term(StorageFeedWrapper.FIELD_FEED_ID,
                feedId));
        Query q1 = new TermQuery(new Term(StorageEntryWrapper.FIELD_FEED_REFERENCE,
                feedId));
        BooleanQuery boolQuery = new BooleanQuery();
        boolQuery.add(q,BooleanClause.Occur.SHOULD);
        boolQuery.add(q1,BooleanClause.Occur.SHOULD);
        Hits h = searcher.search(boolQuery);
        assertEquals("length == 0", 0, h.length());
        searcher.close();
        
        
        
    
public voidtestDeleteUser()

        testSaveUser();
        this.modifier.deleteAccount(username);
        IndexSearcher searcher = new IndexSearcher(this.dir);
        Query q = new TermQuery(new Term(StorageAccountWrapper.FIELD_ACCOUNTNAME,
                username));
        Hits h = searcher.search(q);
        assertEquals("length == 0", 0, h.length());
        searcher.close();
    
public voidtestInsertEntry()


        Thread a = getRunnerThread(this.count);
        

        Thread b = getRunnerThread((this.count += 10));
        b.start();
        a.start();
//         wait for the first thread to check for the inserted entries
        a.join();  
        try{
        for (int i = 1; i < this.count; i++) {
           
            ReferenceCounter<StorageQuery> innerQuery = this.controller
                    .getStorageQuery();
            BaseEntry e = innerQuery.get().singleEntryQuery("" + i, feedId,
                    this.configurator);
            assertNotNull(e);
            assertEquals("get entry for id" + i, "" + i, e.getId());

        }
        }finally{
        	/*
        	 * if an exception occures the tread can at least finnish running before the
        	 * controller will be closed in the tearDown method
        	 */
        	 b.join();	
        }
      
       
        ReferenceCounter<StorageQuery> query = this.controller
                .getStorageQuery();

        this.count += 10;
        for (int i = 1; i < this.count; i++) {
            BaseEntry e = query.get().singleEntryQuery("" + i, feedId,
                    this.configurator);
            assertEquals("get entry for id" + i, "" + i, e.getId());
        }

        BaseEntry e = query.get().singleEntryQuery("" + this.count, feedId,
                this.configurator);
        assertNull("not entry for ID", e);
        query.decrementRef();

    
public voidtestSaveFeed()

        String title = "myTitle";
        ServerBaseFeed feed = new ServerBaseFeed();
        feed.setId(feedId);
        feed.setTitle(new PlainTextConstruct(title));
        feed.setServiceType(service);
        feed.setServiceConfig(this.configurator);
        StorageFeedWrapper wrapper = new StorageFeedWrapper(feed,username);
        this.modifier.createFeed(wrapper);
        
        IndexSearcher searcher = new IndexSearcher(this.dir);
        Query q = new TermQuery(new Term(StorageFeedWrapper.FIELD_FEED_ID,
                feedId));
        Hits h = searcher.search(q);
        assertEquals("length == 1", 1, h.length());
        searcher.close();
        
    
public voidtestSaveUser()


        GDataAccount user = new GDataAccount();
        user.setName(username);
        user.setPassword(password);
        StorageAccountWrapper wrapper = new StorageAccountWrapper(user);
        this.modifier.createAccount(wrapper);
        IndexSearcher searcher = new IndexSearcher(this.dir);
        Query q = new TermQuery(new Term(StorageAccountWrapper.FIELD_ACCOUNTNAME,
                username));
        Hits h = searcher.search(q);
        assertEquals("length == 1", 1, h.length());
        GDataAccount storedUser = StorageAccountWrapper.buildEntity(h.doc(0));
        assertTrue(storedUser.equals(user));
        searcher.close();
    
public voidtestUpdateEntry()

        testInsertEntry();
        for (int i = 1; i < this.count; i++) {
            Entry e = new Entry();
            e.setId("" + i);
            String insertString = "Hello world" + i;
            e.setTitle(new PlainTextConstruct(insertString));
            ServerBaseEntry en = getServerEntry(e);
            StorageEntryWrapper wrapper = new StorageEntryWrapper(en,
                    StorageOperation.UPDATE);
            this.modifier.updateEntry(wrapper);
            ReferenceCounter<StorageQuery> innerQuery = this.controller
                    .getStorageQuery();
            BaseEntry fetchedEntry = innerQuery.get().singleEntryQuery("" + i,
                    feedId, this.configurator);
            assertEquals("updated Title:", insertString, fetchedEntry
                    .getTitle().getPlainText());
        }
        // double updates
        for (int i = 1; i < this.count; i++) {
            Entry e = new Entry();
            e.setId("" + i);
            String insertString = "Hello world" + i;
            e.setTitle(new PlainTextConstruct(insertString));
            ServerBaseEntry en = getServerEntry(e);
            StorageEntryWrapper wrapper = new StorageEntryWrapper(en,
                    StorageOperation.UPDATE);
            this.modifier.updateEntry(wrapper);

            e = new Entry();
            e.setId("" + i);
            insertString = "Foo Bar" + i;
            e.setTitle(new PlainTextConstruct(insertString));
            en = getServerEntry(e);
            wrapper = new StorageEntryWrapper(en,
                    StorageOperation.UPDATE);
            this.modifier.updateEntry(wrapper);

            ReferenceCounter<StorageQuery> innerQuery = this.controller
                    .getStorageQuery();

            BaseEntry fetchedEntry = innerQuery.get().singleEntryQuery("" + i,
                    feedId, this.configurator);
            assertEquals("updated Title:", insertString, fetchedEntry
                    .getTitle().getPlainText());
        }
        
       
    
public voidtestUpdateFeed()

throws
IOException
throws
StorageException

        testSaveFeed();
        ServerBaseFeed feed = new ServerBaseFeed();
        String title = "myTitle";
        String newusername = "doug";
        feed.setTitle(new PlainTextConstruct(title));
        feed.setId(feedId);
        feed.setServiceType(service);
        feed.setServiceConfig(this.configurator);
        StorageFeedWrapper wrapper = new StorageFeedWrapper(feed,newusername);
        this.modifier.updateFeed(wrapper);
        IndexSearcher searcher = new IndexSearcher(this.dir);
        Query q = new TermQuery(new Term(StorageFeedWrapper.FIELD_FEED_ID,
                feedId));
        Hits h = searcher.search(q);
        assertEquals("length == 1", 1, h.length());
        assertTrue(h.doc(0).get(StorageFeedWrapper.FIELD_ACCOUNTREFERENCE).equals(newusername));
        searcher.close();

    
public voidtestUpdateUser()

        testSaveUser();
        GDataAccount user = new GDataAccount();
        user.setName(username);
        user.setPassword("newPass");
        StorageAccountWrapper wrapper = new StorageAccountWrapper(user);
        this.modifier.updateAccount(wrapper);
        IndexSearcher searcher = new IndexSearcher(this.dir);
        Query q = new TermQuery(new Term(StorageAccountWrapper.FIELD_ACCOUNTNAME,
                username));
        Hits h = searcher.search(q);
        assertEquals("length == 1", 1, h.length());
        GDataAccount storedUser = StorageAccountWrapper.buildEntity(h.doc(0));
        assertTrue(storedUser.equals(user));

        assertFalse(storedUser.getPassword().equals(password));
        searcher.close();