Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

TestFODBSynchro.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  * 
00024  */
00025 package org.openmobileis.database.fastobjectdb.db.test;
00026 
00027 import java.io.File;
00028 import java.io.IOException;
00029 import java.util.Properties;
00030 
00031 import org.odbms.ObjectSet;
00032 import org.odbms.Query;
00033 import org.openmobileis.common.user.UserManager;
00034 import org.openmobileis.common.user.impl.MonoUserUserManagerFactory;
00035 import org.openmobileis.common.user.profil.ProfilDataManager;
00036 import org.openmobileis.common.user.profil.impl.XmlFileProfilDataFactory;
00037 import org.openmobileis.common.util.PropertiesManager;
00038 import org.openmobileis.common.util.exception.BadDataFormatException;
00039 import org.openmobileis.common.util.exception.SynchroException;
00040 import org.openmobileis.common.util.log.LogManager;
00041 import org.openmobileis.database.DatabaseException;
00042 import org.openmobileis.database.fastobjectdb.FODBIndexDescriptor;
00043 import org.openmobileis.database.fastobjectdb.FODBStringIndexDescriptor;
00044 import org.openmobileis.database.fastobjectdb.FastObjectDB;
00045 import org.openmobileis.database.fastobjectdb.FastObjectDBManager;
00046 import org.openmobileis.database.fastobjectdb.synchro.client.SynchroFastObjectDBManager;
00047 import org.openmobileis.database.fastobjectdb.synchro.server.FODBSynchroManager;
00048 import org.openmobileis.embedded.webserver.WebServer;
00049 import org.openmobileis.synchro.algo.syncnumber.SyncNumberManager;
00050 import org.openmobileis.synchro.algo.syncnumber.impl.EmptySyncNumberManagerDelegate;
00051 import org.openmobileis.synchro.client.SynchroDescriptor;
00052 import org.openmobileis.synchro.client.SynchroManager;
00053 import org.openmobileis.synchro.openmsp.client.OpenMSPSynchroManager;
00054 import org.openmobileis.synchro.openmsp.client.conduit.HttpOpenMSPSynchroConduit;
00055 import org.openmobileis.synchro.openmsp.client.core.NumSyncManagerDB;
00056 import org.openmobileis.synchro.openmsp.server.synctarget.SynchroTargerManager;
00057 import org.openmobileis.synchro.openmsp.server.synctarget.proxy.GenericProxySynchroTarget;
00058 import org.openmobileis.synchro.security.auth.Credential;
00059 
00067 public final class TestFODBSynchro {
00068 
00072   public TestFODBSynchro() {
00073     super();
00074   }
00075   
00076   private void prepateEnvironement() throws InterruptedException {
00077     //client environement
00078    PropertiesManager.getManager().addProperty("org.openmobileis.synchro.client.openmsp.numsyncpropsfile",System.getProperty("user.dir") + "/WEB-INF/conf/numsync.txt");
00079     //synchro environement is declared by FODBSynchroManager with default parameters.
00080     
00081     //server enviromenent;
00082     PropertiesManager.getManager().addProperty("org.openmobileis.common.user.profil.xmlprofilfile.path",System.getProperty("user.dir") + "/WEB-INF/conf/defaultprofils.xml");
00083     UserManager.getManager().registerUserManagerFactoryForGroup("default", new MonoUserUserManagerFactory());
00084     ProfilDataManager.getManager().registerProfilDataFactoryForGroup("default", new XmlFileProfilDataFactory());
00085     FODBSynchroManager.getManager().registerCollection(new TestFODBSyncTarget());
00086     SynchroTargerManager.getManager().registerProxyTargetForTerminal(new GenericProxySynchroTarget(), null);
00087     SyncNumberManager.getManager().registerDelegate(new EmptySyncNumberManagerDelegate());
00088     //start server
00089     Runnable startWebServer = new Runnable() {
00090 
00091       public void run() {
00092         try {
00093 
00094           System.out.println("start WebServer");
00095 
00096           Properties props = new Properties();
00097           props.put("LOGFILE", System.getProperty("user.dir") + "/WEB-INF/log/server/log");
00098           LogManager.registerLogManager(props); //log to a file
00099 
00100           WebServer server = null;
00101           server = new WebServer();
00102 
00103           server.serve();
00104         } catch (Throwable ex) {
00105           ex.printStackTrace();
00106         }
00107       }
00108 
00109     };
00110     Thread th = new Thread(startWebServer);
00111     th.start();
00112     //do synchro
00113     //wait 1 second until the webserver is started.
00114     Thread.currentThread().sleep(1000);
00115 
00116   }
00117   
00118   private void initDatabase() throws DatabaseException, ClassNotFoundException, IOException, BadDataFormatException  {
00119     String tempdir = System.getProperty("user.dir") + File.separator;
00120     //init db system parameters
00121     PropertiesManager.getManager().addProperty("fastobjectdb.database.path", tempdir+"database");
00122     PropertiesManager.getManager().addProperty("fastobjectdb.database.name", "testdbsync");
00123     SynchroFastObjectDBManager.getCurrentFODB(); //init synchro db to be use as default db
00124     
00125     FastObjectDB db = FastObjectDBManager.getCurrentFODB();
00126     if (!db.isCollectionExist("CLIENT")) {
00127       db.createCollection("CLIENT", TestData.class);
00128       FODBStringIndexDescriptor KeyDescriptor = new FODBStringIndexDescriptor("KEY", FODBIndexDescriptor.UNIQUE, "getKey()", 12, 15);
00129       db.addIndex("CLIENT", KeyDescriptor);
00130       FODBStringIndexDescriptor debKeyDescriptor = new FODBStringIndexDescriptor("DEBKEY", FODBIndexDescriptor.MULTIPLE, "getDebKey()", 10,
00131           TestData.NB_BEGIN_KEY_LETTER, 5);
00132       db.addIndex("CLIENT", debKeyDescriptor);
00133     }
00134     if (!db.isCollectionExist("SERVER")) {
00135       db.createCollection("SERVER", TestData.class);
00136       FODBStringIndexDescriptor KeyDescriptor = new FODBStringIndexDescriptor("KEY", FODBIndexDescriptor.UNIQUE, "getKey()", 12, 15);
00137       db.addIndex("SERVER", KeyDescriptor);
00138       FODBStringIndexDescriptor debKeyDescriptor = new FODBStringIndexDescriptor("DEBKEY", FODBIndexDescriptor.MULTIPLE, "getDebKey()", 10,
00139           TestData.NB_BEGIN_KEY_LETTER, 5);
00140       db.addIndex("SERVER", debKeyDescriptor);
00141     }
00142    
00143   }
00144   
00145   private void prepareServerDataForSynchro() throws DatabaseException {
00146     TestData data = this.getServerTestData("FIRSTS");
00147     if (data == null) { //first synchro create data
00148      data = new TestData("FIRSTS");
00149      data.setToModify("0S");
00150      FastObjectDBManager.getCurrentFODB().add("SERVER", data);
00151     }
00152     
00153   }
00154   
00155   private TestData getClientTestData(String key) throws DatabaseException {
00156     Query q = FastObjectDBManager.getCurrentFODB().query();
00157     q.constrain("CLIENT");
00158     Query q2 = q.descend("getKey()");
00159     q2.constrain(key).equal();
00160     ObjectSet set = q.execute();
00161     if (set.hasNext())  {
00162       return (TestData) set.next();
00163     }
00164     return null;
00165   }
00166   
00167   private TestData getServerTestData(String key) throws DatabaseException {
00168     Query q = FastObjectDBManager.getCurrentFODB().query();
00169     q.constrain("SERVER");
00170     Query q2 = q.descend("getKey()");
00171     q2.constrain(key).equal();
00172     ObjectSet set = q.execute();
00173     if (set.hasNext())  {
00174       return (TestData) set.next();
00175     }
00176     return null;
00177   }
00178   
00179   private void doSynchro() throws SynchroException {
00180     Credential cred = new Credential("test", "test");
00181     SynchroDescriptor descriptor = new SynchroDescriptor();
00182     descriptor.addProperty("OpenMSPsynchrotype", "DR");
00183     String useragent = OpenMSPSynchroManager.getManager().getSynchroUserAgent(descriptor);
00184     descriptor.setSynchroConduit(new HttpOpenMSPSynchroConduit(System.getProperty("user.dir") + "/synchro",useragent));
00185     descriptor.setSynchroGroup("default");
00186     descriptor.setServerURL("http://localhost:9091/services/openmspservice");
00187     
00188     SynchroManager.getManager().doSynchro(cred, descriptor);
00189   }
00190 
00194   public static void main(String[] args) {
00195     try  {
00196       TestFODBSynchro test = new TestFODBSynchro();
00197       test.prepateEnvironement();
00198       test.initDatabase();
00199       test.prepareServerDataForSynchro();
00200       NumSyncManagerDB.getManager().saveSyncNumberForService(0, "CLIENT"); //set to 0 for first synchro
00201       test.doSynchro();
00202       TestData data = test.getClientTestData("FIRSTS");
00203       if (data != null) LogManager.traceError(0, "First Synchro OK");
00204       else LogManager.traceError(0, "First Synchro NOK");
00205       
00206       // add client size data and modif server side
00207       data = new TestData("SECONDC");
00208       data.setToModify("0C");
00209       TestData datadb = test.getClientTestData("SECONDC");
00210       if (datadb == null) FastObjectDBManager.getCurrentFODB().add("CLIENT", data);
00211       test.doSynchro();
00212       data = test.getServerTestData("SECONDC");
00213       TestData databis = test.getClientTestData("FIRSTS");
00214       if ((data != null) && (databis != null) && (databis.getToModify().equals("1S"))) LogManager.traceError(0, "Second Synchro OK");
00215       else LogManager.traceError(0, "Second Synchro NOK");
00216  
00217       // update client size data and delete server side
00218       data = new TestData("SECONDC");
00219       data.setToModify("1C");
00220       FastObjectDBManager.getCurrentFODB().replace("CLIENT", data);
00221       test.doSynchro();
00222       data = test.getServerTestData("SECONDC");
00223       databis = test.getClientTestData("FIRSTS");
00224       if ((data != null) && (databis == null) && (data.getToModify().equals("1C"))) LogManager.traceError(0, "Second Synchro OK");
00225       else LogManager.traceError(0, "Second Synchro NOK");
00226 
00227       // delete client and server complete synchro
00228       FastObjectDBManager.getCurrentFODB().deleteWithId("CLIENT", "SECONDC");
00229       test.doSynchro();
00230       data = test.getServerTestData("SECONDC");
00231       databis = test.getClientTestData("THIRD");
00232       TestData databis2 = test.getClientTestData("FOURTH");
00233       if ((data == null) && (databis != null) && (databis2 != null)) LogManager.traceError(0, "Third Synchro OK");
00234       else LogManager.traceError(0, "Second Synchro NOK");
00235     } catch (Throwable ex)  {
00236       LogManager.traceError(0, ex);
00237     }
00238 
00239   }
00240 
00241 }

Generated on Wed Dec 14 21:05:35 2005 for OpenMobileIS by  doxygen 1.4.4