diff src/nabble/view/lib/Test.java @ 0:7ecd1a4ef557

add content
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 21 Mar 2019 19:15:52 -0600
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/nabble/view/lib/Test.java	Thu Mar 21 19:15:52 2019 -0600
@@ -0,0 +1,163 @@
+package nabble.view.lib;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.Statement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.ListIterator;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import fschmidt.db.DbDatabase;
+import fschmidt.util.java.IoUtils;
+import nabble.model.ModelHome;
+import nabble.model.Site;
+import nabble.model.Db;
+import nabble.model.User;
+import nabble.model.ModelException;
+import nabble.model.Message;
+import nabble.model.Node;
+
+
+public final class Test {
+	private Test() {}  // never
+
+	private static final Logger logger = LoggerFactory.getLogger(Test.class);
+
+	public static interface Task {
+		public void run(Site site) throws Exception;
+	}
+
+	private static final String PREFEX = "test-";
+	private static final List<Site> sites = new ArrayList<Site>();
+	private static final List<Thread> threads = new ArrayList<Thread>();
+/*
+	static {
+		try {
+			Connection con = Db.dbGlobal().getConnection();
+			Statement stmt = con.createStatement();
+			ResultSet rs = stmt.executeQuery(
+				"select site_id from site_global"
+			);
+			while( rs.next() ) {
+				long siteId = rs.getLong("site_id");
+				Site site = ModelHome.getSite(siteId);
+				String name = name(site);
+				if( name.startsWith(PREFEX) )
+					sites.add(site);
+			}
+			rs.close();
+			stmt.close();
+			con.close();
+		} catch(SQLException e) {
+			logger.error("init",e);
+			System.exit(-1);
+		}
+	}
+*/
+	public static void delete() {
+		checkThreads();
+		for( Site site : sites ) {
+			site.getRootNode().deleteRecursively();
+		}
+		sites.clear();
+		logger.info("sites deleted");
+	}
+
+	public static void populate(int n,String email) {
+		if( !sites.isEmpty() )
+			throw new RuntimeException("sites not empty");
+		DbDatabase db = Db.dbGlobal();
+		for( int i=1; i<=n; i++ ) {
+			logger.info("creating site "+i);
+			db.beginTransaction();
+			try {
+				Site site = ModelHome.newSite( "forum", PREFEX+i, "", Message.Format.TEXT, email, "tester" );
+				Permissions.addToGroup( (User)site.getRootNode().getOwner(), Permissions.ADMINISTRATORS_GROUP );
+				String key = site.newRegistration(email,"tester","tester","/");
+				User user = (User)site.getRootNode().getOwner();
+				user.setPassword("tester");
+				user.register();
+				user.update();
+				db.commitTransaction();
+				site = site.getGoodCopy();
+				sites.add(site);
+			} catch (ModelException e) {
+				throw new RuntimeException(e);
+			} finally {
+				db.endTransaction();
+			}
+		}
+		logger.info(""+n+" sites populated");
+	}
+
+	public static void getGoodCopies() {
+		for( ListIterator<Site> iter = sites.listIterator(); iter.hasNext(); ) {
+			iter.set( iter.next().getGoodCopy() );
+		}
+	}
+
+	private static void checkThreads() {
+		if( !threads.isEmpty() )
+			throw new RuntimeException("tasks running");
+	}
+
+	public static void run(final Task task) {
+		for( Site site : sites ) {
+			final Site taskSite = site;
+			Thread thread = new Thread(new Runnable(){public void run(){
+				try {
+					task.run(taskSite);
+				} catch(Exception e) {
+					logger.error("task failed for "+taskSite,e);
+				}
+			}});
+			thread.start();
+			threads.add(thread);
+		}
+	}
+
+	public static void waitForThreads() {
+		for( Thread thread : threads ) {
+			try {
+				thread.join();
+			} catch(InterruptedException e) {
+				throw new RuntimeException(e);
+			}
+		}
+		threads.clear();
+		logger.info("threads done");
+	}
+
+
+	// utils
+
+	public static String name(Site site) {
+		return site.getRootNode().getSubject();
+	}
+
+	public static Node newPost(Node parent,String subject) throws ModelException {
+		Node node;
+		DbDatabase db = parent.getSite().getDb();
+		db.beginTransaction();
+		try {
+			node = parent.getGoodCopy().getOwner().newChildNode(Node.Kind.POST,subject,"some message",Message.Format.TEXT,parent);
+			node.insert(false);
+			db.commitTransaction();
+		} finally {
+			db.endTransaction();
+		}
+		return node.getGoodCopy();
+	}
+
+	public static String readPage(Node node) throws IOException {
+		return IoUtils.readPage( Jtp.url(node) );
+	}
+
+	public static void clearPageCache() {
+		MyJtpServlet.getJtpContext().getHttpCache().clear();
+	}
+
+}