comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:7ecd1a4ef557
1 package nabble.view.lib;
2
3 import java.io.IOException;
4 import java.sql.Connection;
5 import java.sql.Statement;
6 import java.sql.ResultSet;
7 import java.sql.SQLException;
8 import java.util.List;
9 import java.util.ArrayList;
10 import java.util.ListIterator;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13 import fschmidt.db.DbDatabase;
14 import fschmidt.util.java.IoUtils;
15 import nabble.model.ModelHome;
16 import nabble.model.Site;
17 import nabble.model.Db;
18 import nabble.model.User;
19 import nabble.model.ModelException;
20 import nabble.model.Message;
21 import nabble.model.Node;
22
23
24 public final class Test {
25 private Test() {} // never
26
27 private static final Logger logger = LoggerFactory.getLogger(Test.class);
28
29 public static interface Task {
30 public void run(Site site) throws Exception;
31 }
32
33 private static final String PREFEX = "test-";
34 private static final List<Site> sites = new ArrayList<Site>();
35 private static final List<Thread> threads = new ArrayList<Thread>();
36 /*
37 static {
38 try {
39 Connection con = Db.dbGlobal().getConnection();
40 Statement stmt = con.createStatement();
41 ResultSet rs = stmt.executeQuery(
42 "select site_id from site_global"
43 );
44 while( rs.next() ) {
45 long siteId = rs.getLong("site_id");
46 Site site = ModelHome.getSite(siteId);
47 String name = name(site);
48 if( name.startsWith(PREFEX) )
49 sites.add(site);
50 }
51 rs.close();
52 stmt.close();
53 con.close();
54 } catch(SQLException e) {
55 logger.error("init",e);
56 System.exit(-1);
57 }
58 }
59 */
60 public static void delete() {
61 checkThreads();
62 for( Site site : sites ) {
63 site.getRootNode().deleteRecursively();
64 }
65 sites.clear();
66 logger.info("sites deleted");
67 }
68
69 public static void populate(int n,String email) {
70 if( !sites.isEmpty() )
71 throw new RuntimeException("sites not empty");
72 DbDatabase db = Db.dbGlobal();
73 for( int i=1; i<=n; i++ ) {
74 logger.info("creating site "+i);
75 db.beginTransaction();
76 try {
77 Site site = ModelHome.newSite( "forum", PREFEX+i, "", Message.Format.TEXT, email, "tester" );
78 Permissions.addToGroup( (User)site.getRootNode().getOwner(), Permissions.ADMINISTRATORS_GROUP );
79 String key = site.newRegistration(email,"tester","tester","/");
80 User user = (User)site.getRootNode().getOwner();
81 user.setPassword("tester");
82 user.register();
83 user.update();
84 db.commitTransaction();
85 site = site.getGoodCopy();
86 sites.add(site);
87 } catch (ModelException e) {
88 throw new RuntimeException(e);
89 } finally {
90 db.endTransaction();
91 }
92 }
93 logger.info(""+n+" sites populated");
94 }
95
96 public static void getGoodCopies() {
97 for( ListIterator<Site> iter = sites.listIterator(); iter.hasNext(); ) {
98 iter.set( iter.next().getGoodCopy() );
99 }
100 }
101
102 private static void checkThreads() {
103 if( !threads.isEmpty() )
104 throw new RuntimeException("tasks running");
105 }
106
107 public static void run(final Task task) {
108 for( Site site : sites ) {
109 final Site taskSite = site;
110 Thread thread = new Thread(new Runnable(){public void run(){
111 try {
112 task.run(taskSite);
113 } catch(Exception e) {
114 logger.error("task failed for "+taskSite,e);
115 }
116 }});
117 thread.start();
118 threads.add(thread);
119 }
120 }
121
122 public static void waitForThreads() {
123 for( Thread thread : threads ) {
124 try {
125 thread.join();
126 } catch(InterruptedException e) {
127 throw new RuntimeException(e);
128 }
129 }
130 threads.clear();
131 logger.info("threads done");
132 }
133
134
135 // utils
136
137 public static String name(Site site) {
138 return site.getRootNode().getSubject();
139 }
140
141 public static Node newPost(Node parent,String subject) throws ModelException {
142 Node node;
143 DbDatabase db = parent.getSite().getDb();
144 db.beginTransaction();
145 try {
146 node = parent.getGoodCopy().getOwner().newChildNode(Node.Kind.POST,subject,"some message",Message.Format.TEXT,parent);
147 node.insert(false);
148 db.commitTransaction();
149 } finally {
150 db.endTransaction();
151 }
152 return node.getGoodCopy();
153 }
154
155 public static String readPage(Node node) throws IOException {
156 return IoUtils.readPage( Jtp.url(node) );
157 }
158
159 public static void clearPageCache() {
160 MyJtpServlet.getJtpContext().getHttpCache().clear();
161 }
162
163 }