at.spardat.xma.mdl.test.PageSynchronization Maven / Gradle / Ivy
The newest version!
/*******************************************************************************
* Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* s IT Solutions AT Spardat GmbH - initial API and implementation
*******************************************************************************/
// @(#) $Id: PageSynchronization.java 10331 2013-02-07 10:31:35Z hoenninger $
package at.spardat.xma.mdl.test;
import junit.framework.TestCase;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import at.spardat.enterprise.util.Types;
import at.spardat.xma.component.ComponentClient;
import at.spardat.xma.component.ComponentServer;
import at.spardat.xma.mdl.AttachmentExceptionClient;
import at.spardat.xma.mdl.WModel;
import at.spardat.xma.mdl.list.ListDomWM;
import at.spardat.xma.mdl.list.ListDomWMClient;
import at.spardat.xma.mdl.list.ListWM;
import at.spardat.xma.mdl.list.ListWMClient;
import at.spardat.xma.mdl.simple.SimpleWM;
import at.spardat.xma.mdl.simple.SimpleWMClient;
import at.spardat.xma.mdl.table.TableWM;
import at.spardat.xma.mdl.table.TableWMClient;
import at.spardat.xma.mdl.tree.TreeWM;
import at.spardat.xma.mdl.tree.TreeWMClient;
import at.spardat.xma.page.Page;
import at.spardat.xma.page.PageClient;
import at.spardat.xma.page.PageServer;
import at.spardat.xma.page.Scaler;
import at.spardat.xma.serializer.Deserializer;
import at.spardat.xma.serializer.Serializer;
import at.spardat.xma.serializer.SerializerFactoryClient;
import at.spardat.xma.serializer.SerializerFactoryServer;
import at.spardat.xma.serializer.XmaInput;
import at.spardat.xma.serializer.XmaOutput;
import at.spardat.xma.test.TestUtil;
/**
* Tests the synchronization between two pages
*
* @author YSD, 03.05.2003 10:46:16
*/
public class PageSynchronization extends TestCase {
/**
* Constructor for PageSynchronization.
* @param name
*/
public PageSynchronization(String name) {
super(name);
}
public void testSync () throws Exception {
for (int i=0; i<10000; i++) {
makeSyncs (10);
}
}
/**
* Constructs two pages with random widget models, makes nSyncs
* synchronizations and modifies the models on both side, the client
* and the server side between each sychronization.
*/
public void makeSyncs (int nSyncs) throws Exception {
ComponentClient cc = new ComponentClient() {
public String getModelClass (short id) { return "dummy"; }
public String getResourceString(short id) { return null; }
public void invoke(Composite parent) {}
};
ComponentServer cs = new ComponentServer() {
public String getModelClass (short id) { return "dummy"; }
};
Page [] pms = newRandomPagePair (cc, cs, 1, 5, (short)47, (short)37);
Page pc = pms[0];
PageServer ps = (PageServer) pms[1];
for (int i=0; iS]");
throw ex;
}
if (false) {
printPages(pc, ps, "[C->S]");
}
pc.commit ();
// randomly change the server page
ps.randomlyChange(TestUtil.randomInt(0, 20));
// rollback changes
ps.rollback();
// all changes must be backed out
try {
pc.equalsCS (ps, 0);
} catch (Exception ex) {
printPages(pc, ps, "after server rollback [C->S]");
throw ex;
}
if (false) {
printPages(pc, ps, "after server rollback [C->S]");
}
// randomly change the server page
randomlyAddModels((PageServer)ps);
ps.randomlyChange(TestUtil.randomInt(0, 20));
SerializerFactoryServer sfac = new SerializerFactoryServer();
sfac.setSerializationMode("binary");
ser = sfac.createSerializer(null, 512);
XmaOutput scOut = ser;
ps.externalize (scOut, false);
deser = sfac.createDeserializer(null, ser.getResult().getBytes());
XmaInput scIn = deser;
pc.internalize (scIn, null);
// here is syncpoint 1; the two pages must be equal
try {
pc.equalsCS (ps, 1);
} catch (Exception ex) {
printPages(pc, ps, "[S->C]");
throw ex;
}
if (false) {
printPages(pc, ps, "[S->C]");
}
ps.commit ();
}
}
private void printPages (Page pc, PageServer ps, String praefix) {
// print client page
System.out.println (praefix + " client page:");
System.out.println (pc);
// print server page
System.out.println (praefix + " server page:");
System.out.println (ps);
}
/**
* Constructs a pair of pages (a client and a server page)
*
* @param cc the client component
* @param sc the server component
* @param minWidgets min number of widget models
* @param maxWidgets max number of widget models
* @param typeId type of the page
* @param pageId id of the page
* @return array x where x[0] is the client page and x[1] is the server page
*/
public Page[] newRandomPagePair (ComponentClient cc, ComponentServer sc, int minWidgets, int maxWidgets, short typeId, short pageId) {
boolean stateless = TestUtil.draw(0.5);
TestPageClient cp = new TestPageClient (cc, stateless, typeId, pageId);
TestPageServer sp = new TestPageServer (sc, stateless, typeId, pageId);
WModel[][] wModels = newRandomWModels(cp, sp, (short)0, minWidgets, maxWidgets);
cp.setWModelsFrom(wModels);
sp.setWModelsFrom(wModels);
return new Page[] { cp, sp };
}
/**
* Returns a two column matrix of randomly created widget models where the two
* entries at a particular index are client- and server buddies. The WModel
* at index 0 is the client widget model, the one at index 1 the server widget
* model.
*
* @param pc the client page
* @param ps the server page
* @param countMin minimum number of widget models to create
* @param countMax maximun number of widget models
* @return array with two columns and k rows having [countMin ≤ k ≤ countMax].
*/
public WModel[][] newRandomWModels (PageClient pc, PageServer ps,short baseID, int countMin, int countMax) {
int countWM = TestUtil.randomInt (countMin, countMax);
WModel[][] models = new WModel[countWM][2];
for (short i=0; iwModels.
*/
public TestPageServer (ComponentServer c, boolean stateless, short typeId, short id) {
super (c, stateless);
typeId_ = typeId;
setId(id);
}
public void setWModelsFrom (WModel [][] wModels) {
int col = isAtServer() ? 1 : 0;
staticModels=wModels.length;
wModels_ = new WModel[wModels.length];
for (int i=0; iwModels.
*/
public TestPageClient (ComponentClient c, boolean stateless, short typeId, short id) {
super (c, stateless);
typeId_ = typeId;
setId(id);
}
/**
* @see at.spardat.xma.mdl.PageClient#attachUIImpl()
*/
public void attachUIImpl() throws AttachmentExceptionClient {
}
public void setWModelsFrom (WModel [][] wModels) {
int col = isAtServer() ? 1 : 0;
wModels_ = new WModel[wModels.length];
for (int i=0; i