Package test.autoconf

Source Code of test.autoconf.AutoConfigurationTest

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package test.autoconf;

import java.io.File;
import java.io.FileFilter;
import java.util.Map;
import java.util.logging.Logger;
import junit.framework.TestCase;
import org.atomojo.app.ServerConfiguration;
import org.atomojo.app.StorageFactory;
import org.atomojo.app.WebComponent;
import org.atomojo.app.client.EntryCollection;
import org.atomojo.app.client.FeedClient;
import org.atomojo.app.client.Identity;
import org.atomojo.app.client.IntrospectionClient;
import org.atomojo.app.db.DB;
import org.atomojo.app.storage.file.FileStorageFactory;
import org.restlet.data.Reference;
import org.restlet.data.Status;

/**
*
* @author alex
*/
public class AutoConfigurationTest extends TestCase{

   static Logger LOG = Logger.getLogger(AutoConfigurationTest.class.getName());
  
   protected void deleteDir(File dir) {
      dir.listFiles(new FileFilter() {
         public boolean accept(File file) {
            if (file.isDirectory()) {
               deleteDir(file);
            } else {
               file.delete();
            }
            return false;
         }
      });
      dir.delete();
   }
   protected void setUp() throws Exception
   {
      File dir = new File("test/test/autoconf/db");
      assertTrue(dir.exists());
      LOG.info("Cleaning "+dir.getAbsolutePath()+" before test.");
      File dataDir  = new File(dir,"data");
      if (dataDir.exists()) {
         deleteDir(dataDir);
      }
      File test1Dir  = new File(dir,"test1");
      if (test1Dir.exists()) {
         deleteDir(test1Dir);
      }
   }

   protected void tearDown() throws Exception
   {
      File dir = new File("test/test/autoconf/db");
      assertTrue(dir.exists());
      LOG.info("Cleaning "+dir.getAbsolutePath()+" after test.");
      File dataDir  = new File(dir,"data");
      if (dataDir.exists()) {
         deleteDir(dataDir);
      }
      File test1Dir  = new File(dir,"test1");
      if (test1Dir.exists()) {
         deleteDir(test1Dir);
      }
      File derbyLog = new File(dir,"derby.log");
      derbyLog.delete();
      File logDir = new File(dir,"logs");
      logDir.listFiles(new FileFilter() {
         public boolean accept(File file) {
            file.delete();
            return false;
         }
      });
   }
  
   public void testConf()
      throws Exception
   {
      File dir = new File("test/test/autoconf/db");
      assertTrue(dir.exists());
      Map<String,DB> dbList = DB.getDatabases(LOG,dir);
     
      ServerConfiguration conf = new ServerConfiguration();
      File serverConfFile = new File(dir,"server.conf");
      assertTrue(serverConfFile.exists());
      conf.load(serverConfFile.toURI());
      StorageFactory storageFactory = new FileStorageFactory();
      WebComponent web = new WebComponent(dir,dbList,storageFactory,conf);
      for (DB db : dbList.values()) {
         db.connect();
      }
      web.start();
     
      Identity admin = new Identity("admin","admin");
      Reference autoRef = new Reference("http://localhost:8080/R/");
      FeedClient client = new FeedClient(autoRef,admin);
     
      Status status = client.create("<feed xmlns='http://www.w3.org/2005/Atom'><title>autoconf</title></feed>");
      assertTrue(status.isSuccess());
     
      client.createEntry(
         "<entry xmlns='http://www.w3.org/2005/Atom'>" +
         "<title>test1 db</title>" +
         "<category scheme='http://www.atomojo.org/O/configuration/' term='db'/>"+
         "<category scheme='http://www.atomojo.org/O/configuration/db/' term='auth' value='auth'/>" +
         "<category scheme='http://www.atomojo.org/O/configuration/db/' term='name' value='test1'/>" +
         "<category scheme='http://www.atomojo.org/O/configuration/db/' term='group' value='test'/>" +
         "<category scheme='http://www.atomojo.org/O/configuration/db/' term='alias' value='web1'/>" +
         "</entry>");
     
      client.createEntry(
         "<entry xmlns='http://www.w3.org/2005/Atom'>" +
         "<title>testweb1 host</title>" +
         "<category scheme='http://www.atomojo.org/O/configuration/' term='host'/>"+
         "<content type='text/xml'>" +
         "<host name='testweb1' xmlns='http://www.atomojo.org/Vocabulary/Admin/2007/1/0' database='test1'/>" +
         "</content>" +
         "</entry>");
     
      // Wait one minute for autoconf to recognize entries;
      Thread.sleep(7000);
     
      Reference testweb1 = new Reference("http://testweb1:8080/");
      IntrospectionClient introspect = new IntrospectionClient(LOG,testweb1);
      introspect.setIdentity(admin);
     
      introspect.introspect(new IntrospectionClient.ServiceListener() {
         public void onStartWorkspace(String title) {}
         public void onCollection(EntryCollection collection) {}
         public void onEndWorkspace() {}
      });

      Reference resourceWeb1 = new Reference("http://testweb2:8080/test/web1/");
      IntrospectionClient introspect2 = new IntrospectionClient(LOG,resourceWeb1);
      introspect2.setIdentity(admin);

      introspect2.introspect(new IntrospectionClient.ServiceListener() {
         public void onStartWorkspace(String title) {}
         public void onCollection(EntryCollection collection) {}
         public void onEndWorkspace() {}
      });

     
      web.stop();
     
   }
  
}
TOP

Related Classes of test.autoconf.AutoConfigurationTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.