/* Copyright (C) 2004 - 2005  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. */
using System;
using com.db4o;
using com.db4o.query;
using com.db4o.ext;

namespace com.db4o.test {
    public class TwoClients : AllTestsConfAll {
	
        public void test(){
            if(Tester.clientServer){
                Tester.deleteAllInstances(new Atom());
                Tester.commit();

                ExtObjectContainer client2 = null;
                try {
                    client2 =
                        Db4o.openClient(SERVER_HOSTNAME, SERVER_PORT, DB4O_USER, DB4O_PASSWORD).ext();
                
                    // client2 = Tester.server().openClient().ext();
                } catch (Exception e) {
                    Console.WriteLine(e);
                    return ;
                }
                ExtObjectContainer client1 = Tester.objectContainer();
                Atom a_1_1 = new Atom("One");
                Atom a_1_2 = new Atom("Two");
                Atom a_1_3 = new Atom("Three");
                client1.set(a_1_1);
                client1.set(a_1_2);
                client1.set(a_1_3);
                ensureAtomCount(client2,null, 0);
				
				Tester.commitSync(client1, client2);
				
                ensureAtomCount(client2,null, 3);
                Atom a_2_1 = (Atom)client2.get(new Atom("One")).next();
                a_1_1.child = new Atom("OneChild");
                client1.set(a_1_1);
                ensureAtomCount(client2,null, 3);
                Tester.commitSync(client1, client2);
                ensureAtomCount(client2,null, 4);
                client2.deactivate(a_2_1, int.MaxValue);
                client2.activate(a_2_1, int.MaxValue);
                Tester.ensure(a_2_1.child.name.Equals("OneChild"));
                a_2_1.name = "Zulu";
                client2.set(a_2_1);
            
                Atom a_1_4 = new Atom("Zorro");
                client1.set(a_1_4);
                Atom a_1_5 = new Atom("Zzerk");
                client1.set(a_1_5);
            
                ensureAtomCount(client1, "Zulu", 0);
            
                Tester.commitSync(client2, client1);
            
                ensureAtomCount(client1, "Zulu", 1);

            
                Query q = client1.query();
                q.constrain(typeof(Atom));
                    q.descend("name").constrain("Zulu");
                ObjectSet os = q.execute();
                Atom q_1_1 = (Atom)os.next();
            
                Tester.ensure(a_1_1 == q_1_1);
                a_1_1.name = "Bozo";
                client1.set(a_1_1);
                a_1_1.child.name = "BozoChild";
                client1.set(a_1_1.child);
                a_1_4.name = "Bozo";
                client1.set(a_1_4);
                a_1_5.name = "Cue";
                client1.set(a_1_5);
            
                client2.refresh(a_2_1, int.MaxValue);
                Tester.ensure(a_2_1.name.Equals("Zulu"));
                Tester.ensure(a_2_1.child.name.Equals("OneChild"));
                ensureAtomCount(client2, "Bozo", 0);
            
				Tester.commitSync(client1, client2);
				
                client2.refresh(a_2_1, int.MaxValue);
                Tester.ensure(a_2_1.name.Equals("Bozo"));
                Tester.ensure(a_2_1.child.name.Equals("BozoChild"));
                ensureAtomCount(client2, "Bozo", 2);
                ensureAtomCount(client2, "Cue", 1);
                ensureAtomCount(client2, "BozoChild", 1);
            
                client2.close();
            }
        }
	
        private void ensureAtomCount(ObjectContainer con, String name, int count){
		
			Query q = con.query();
			q.constrain(typeof(Atom));
			if(name != null){
				q.descend("name").constrain(name);
			}
			if(q.execute().size() == count){
				Tester.assertionCount ++;
				return;
			}
            Tester.error();
        }
    }
}
