ReplicateDb4oList.java :  » Database-DBMS » db4o-6.4 » com » db4o » test » replication » old » Java Open Source

Java Open Source » Database DBMS » db4o 6.4 
db4o 6.4 » com » db4o » test » replication » old » ReplicateDb4oList.java
/* Copyright (C) 2004 - 2007  db4objects Inc.  http://www.db4o.com

This file is part of the db4o open source object database.

db4o is free software; you can redistribute it and/or modify it under
the terms of version 2 of the GNU General Public License as published
by the Free Software Foundation and as clarified by db4objects' GPL 
interpretation policy, available at
http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
Suite 350, San Mateo, CA 94403, USA.

db4o is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */
package com.db4o.test.replication.old;

import java.util.*;

import com.db4o.*;
import com.db4o.ext.*;
import com.db4o.query.*;
import com.db4o.replication.*;
import com.db4o.test.*;


public class ReplicateDb4oList {
    
    List list;
    
    public void configure() {
        Db4o.configure().generateUUIDs(Integer.MAX_VALUE);
        Db4o.configure().generateVersionNumbers(Integer.MAX_VALUE);
    }
    
    public void storeOne() {
      
        list = Test.objectContainer().collections().newLinkedList();
        list.add(new RDLElement("store1"));
        list.add(new RDLElement("store2"));
    }
    
    public void testOne(){

        Test.deleteAll(Test.replica());
        
        replicate(false);
        
        ObjectContainer oc = Test.replica();
        Query q = oc.query();
        q.constrain(ReplicateDb4oList.class);
        ObjectSet objectSet = q.execute();
        Test.ensure(objectSet.size() == 1);
        ReplicateDb4oList rdl = (ReplicateDb4oList)objectSet.next();
        RDLElement elem = (RDLElement)rdl.list.get(0);
        Test.ensure(elem.name.equals("store1"));
        elem = (RDLElement)rdl.list.get(1);
        Test.ensure(elem.name.equals("store2"));
        
        // Test.reOpen();
        
        list.add(new RDLElement("afterReplication"));
        elem = (RDLElement)list.get(0);
        elem.name = "replicated";
        Test.store(elem);
        
        // storing this to make sure it is dirty
        Test.store(this);
        
        replicate(true);
        
        oc = Test.replica();
        q = oc.query();
        q.constrain(ReplicateDb4oList.class);
        objectSet = q.execute();
        Test.ensure(objectSet.size() == 1);
        rdl = (ReplicateDb4oList)objectSet.next();
        elem = (RDLElement)rdl.list.get(0);
        Test.ensure(elem.name.equals("replicated"));
        elem = (RDLElement)rdl.list.get(1);
        Test.ensure(elem.name.equals("store2"));
        elem = (RDLElement)rdl.list.get(2);
        Test.ensure(elem.name.equals("afterReplication"));
    }
    
    private void replicate(boolean modifiedOnly) {
        ExtObjectContainer peerA = Test.objectContainer().ext();
        ObjectContainer peerB = Test.replica();
        
        ReplicationProcess replication = peerA.replicationBegin(peerB, new ReplicationConflictHandler() {
            public Object resolveConflict(ReplicationProcess replicationProcess, Object a, Object b) {
                return null;
            }
        });
        Query q = peerA.query();
        q.constrain(ReplicateDb4oList.class);
        if (modifiedOnly) {
            replication.whereModified(q);
        }
        ObjectSet objectSet = q.execute();
        while (objectSet.hasNext()) {
            ReplicateDb4oList rdl = (ReplicateDb4oList)objectSet.next(); 
            replication.replicate(rdl);
            // replication.replicate(rdl.list);
        }
        replication.commit();
    }

    
    public static class RDLElement{
        
        private String name;
        
        public RDLElement(String name){
            this.name = name;
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.