/*
* 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;
/**
*
* @author alex
*/
public class EntryTest extends TestCase
{
TestConfig config;
public EntryTest(String testName)
{
super(testName);
}
protected void setUp() throws Exception
{
}
protected void tearDown() throws Exception
{
}
public void testBasics()
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());
loop(log,client);
loop(log,client);
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<100; i++) {
Reference feedRef = new Reference(config.getServerLocation()+"R/"+i+"/");
FeedClient feedClient = new FeedClient(client,feedRef);
feedClient.setIdentity("admin","admin");
Entry entry = null;
if (!feedClient.exists()) {
log.info("Creating feed "+i);
Status status = feedClient.create("<feed xmlns='http://www.w3.org/2005/Atom'><title>Feed "+i+"</title></feed>");
if (!status.isSuccess()) {
log.info("Failure, status="+status.getCode());
}
assertTrue(status.isSuccess());
} else {
log.info("Checking feed "+i);
final AtomicReference<Entry> theEntry = new AtomicReference<Entry>();
feedClient.get(new FeedDestination() {
public void onEntry(Document entryDoc) {
Entry entry = new Entry(entryDoc);
entry.index();
theEntry.set(entry);
}
});
entry = theEntry.get();
}
if (entry==null) {
try {
log.info("Creating entry in "+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 status = entryClient.update();
if (!status.isSuccess()) {
log.info("Failure, status="+status.getCode());
}
assertTrue(status.isSuccess());
}
}
}
}