/*
* FeedTest.java
* JUnit based test
*
* Created on April 16, 2007, 9:56 AM
*/
package test.stress;
import java.io.File;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Logger;
import junit.framework.*;
import org.atomojo.app.client.Entry;
import org.atomojo.app.client.EntryClient;
import org.atomojo.app.client.FeedClient;
import org.atomojo.app.client.FeedDestination;
import org.atomojo.app.client.StatusException;
import org.infoset.xml.Document;
import org.restlet.Client;
import org.restlet.Context;
import org.restlet.data.Reference;
import org.restlet.data.Status;
import test.util.APPTestCase;
import test.util.TestConfig;
/**
*
* @author alex
*/
public class EntryTest extends APPTestCase
{
TestConfig config;
public EntryTest(String testName)
{
super(testName);
}
protected void setUp() throws Exception
{
}
protected void tearDown() throws Exception
{
}
public void testStress()
throws Exception
{
config = new TestConfig("test.app.stress","localhost","localhost",8080);
File dir = TestConfig.getTestDir("test.app.stress");
TestConfig.deltree(dir);
Logger log = Logger.getLogger(EntryTest.class.getName());
// Make sure we start clean
log.info("Using database directory: "+config.getDatabaseDirectory());
config.startServer();
Client client = new Client(new Context(),config.getServerLocation().getSchemeProtocol());
long start = System.currentTimeMillis();
loop(log,client);
loop(log,client);
long elapsed = System.currentTimeMillis() - start;
log.info("Elapsed: "+elapsed+"ms");
config.stopServer();
}
void loop(Logger log,Client client)
throws Exception
{
/*
Reference feedRef = new Reference(config.getServerLocation()+"R/");
FeedClient feedClient = new FeedClient(client,feedRef);
feedClient.setIdentity("admin","admin");
Status status = feedClient.create("<feed xmlns='http://www.w3.org/2005/Atom'><title>Feed</title></feed>");
log.info("status="+status.getCode());
assertTrue(status.isSuccess());
*/
for (int j=0; j<10; j++)
for (int i=0; i<10; i++) {
Reference feedRef = new Reference(config.getServerLocation()+"R/"+i+"/");
FeedClient feedClient = new FeedClient(client,feedRef);
feedClient.setIdentity("admin","admin");
log.info("Checking feed "+j+","+i);
Entry entry = null;
final AtomicReference<Entry> theEntry = new AtomicReference<Entry>();
Status status = feedClient.get(new FeedDestination() {
public void onEntry(Document entryDoc) {
Entry entry = new Entry(entryDoc);
entry.index();
theEntry.set(entry);
}
}).getStatus();
if (status.getCode()==404) {
log.info("Creating feed "+j+","+i);
status = feedClient.create("<feed xmlns='http://www.w3.org/2005/Atom'><title>Feed "+j+","+i+"</title></feed>");
if (!status.isSuccess()) {
log.info("Failure, status="+status.getCode());
}
assertTrue(status.isSuccess());
} else if (status.isSuccess()) {
entry = theEntry.get();
} else {
if (!status.isSuccess()) {
log.info("Failure, status="+status.getCode());
}
assertTrue(status.isSuccess());
}
if (entry==null) {
try {
log.info("Creating entry in "+j+","+i);
feedClient.createEntry("<entry xmlns='http://www.w3.org/2005/Atom'><title>Entry "+i+"</title></entry>");
} catch (StatusException ex) {
log.info("Failure, status="+ex.getStatus().getCode());
assertTrue(false);
}
} else {
log.info("Updating entry in "+i);
entry.setTitle("Updated "+entry.getTitle());
EntryClient entryClient = feedClient.getEntryClient(entry);
status = entryClient.update();
if (!status.isSuccess()) {
log.info("Failure, status="+status.getCode());
}
assertTrue(status.isSuccess());
}
}
}
}