1 /**
2  * Copyright (C) 2011-2013 IBM Corporation and Others. All Rights Reserved.
3  */
4 //##header J2SE15
5 
6 package org.unicode.cldr.unittest.web;
7 
8 import java.io.BufferedReader;
9 import java.io.File;
10 import java.io.PrintWriter;
11 import java.sql.SQLException;
12 
13 import javax.sql.DataSource;
14 
15 import org.apache.derby.jdbc.EmbeddedDataSource;
16 import org.unicode.cldr.test.CheckCLDR;
17 import org.unicode.cldr.util.CLDRConfig;
18 import org.unicode.cldr.util.CLDRConfig.Environment;
19 import org.unicode.cldr.util.CLDRFile;
20 import org.unicode.cldr.util.CLDRPaths;
21 import org.unicode.cldr.util.CldrUtility;
22 import org.unicode.cldr.util.Factory;
23 import org.unicode.cldr.util.FileReaders;
24 import org.unicode.cldr.util.StandardCodes;
25 import org.unicode.cldr.util.SupplementalDataInfo;
26 import org.unicode.cldr.web.CLDRProgressIndicator;
27 import org.unicode.cldr.web.DBUtils;
28 import org.unicode.cldr.web.SurveyLog;
29 
30 import com.ibm.icu.dev.test.TestFmwk.TestGroup;
31 import com.ibm.icu.dev.test.TestLog;
32 import com.ibm.icu.dev.util.ElapsedTimer;
33 import com.ibm.icu.text.Collator;
34 import com.ibm.icu.text.RuleBasedCollator;
35 
36 /**
37  * Top level test used to run all other tests as a batch.
38  */
39 public class TestAll extends TestGroup {
40 
41     private static final String DB_SUBDIR = "db";
42     private static final String CLDR_TEST_JDBC = TestAll.class.getPackage().getName() + ".jdbcurl";
43     private static final String CLDR_TEST_KEEP_DB = TestAll.class.getPackage().getName() + ".KeepDb";
44     private static final String CLDR_TEST_DISK_DB = TestAll.class.getPackage().getName() + ".DiskDb";
45     private static boolean sane = false;
46     private static final boolean DEBUG = CldrUtility.getProperty("DEBUG", false);
47 
48     /**
49      * Verify some setup things
50      */
sanity()51     public static synchronized final void sanity() {
52         if (!sane) {
53             verifyIsDir(CLDRPaths.BASE_DIRECTORY, "CLDR_DIR", "=${workspace_loc:common/..}");
54             verifyIsDir(CLDRPaths.MAIN_DIRECTORY, "CLDR_MAIN", "=${workspace_loc:common/main}");
55             verifyIsFile(new File(CLDRPaths.MAIN_DIRECTORY, "root.xml"));
56             sane = true;
57         }
58     }
59 
verifyIsFile(File file)60     private static void verifyIsFile(File file) {
61         if (!file.isFile() || !file.canRead()) {
62             throw new IllegalArgumentException("Not a file: " + file.getAbsolutePath());
63         }
64     }
65 
verifyIsDir(String f, String string, String sugg)66     private static void verifyIsDir(String f, String string, String sugg) {
67         if (f == null) {
68             pleaseSet(string, "is null", sugg);
69         }
70         verifyIsDir(new File(f), string, sugg);
71     }
72 
verifyIsDir(File f, String string, String sugg)73     private static void verifyIsDir(File f, String string, String sugg) {
74         if (f == null) {
75             pleaseSet(string, "is null", sugg);
76         }
77         if (!f.isDirectory()) {
78             pleaseSet(string, "is not a directory", sugg);
79         }
80     }
81 
pleaseSet(String var, String err, String sugg)82     private static void pleaseSet(String var, String err, String sugg) {
83         throw new IllegalArgumentException("Error: variable " + var + " " + err + ", please set -D" + var + sugg);
84     }
85 
86     private static final String DERBY_DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
87     public static final String DERBY_PREFIX = "jdbc:derby:";
88 
main(String[] args)89     public static void main(String[] args) {
90         main(args, null);
91         /* NOTREACHED */
92     }
93 
main(String[] args, PrintWriter logs)94     public static int main(String[] args, PrintWriter logs) {
95         CheckCLDR.setDisplayInformation(CLDRConfig.getInstance().getEnglish());
96         args = TestAll.doResetDb(args);
97         TestAll test = new TestAll();
98         if(logs != null) {
99             return test.run(args, logs);
100         } else {
101             test.run(args);
102             /* NOTREACHED */
103             return -1; // does not actually return
104         }
105     }
106 
doResetDb(String[] args)107     public static String[] doResetDb(String[] args) {
108         if (CLDRConfig.getInstance().getEnvironment() != Environment.UNITTEST) {
109             throw new InternalError(
110                 "Error: the CLDRConfig Environment is not UNITTEST. Please set -DCLDR_ENVIRONMENT=UNITTESTS");
111         }
112         if (CldrUtility.getProperty(CLDR_TEST_KEEP_DB, false)) {
113             if (DEBUG)
114                 SurveyLog.logger.warning("Keeping database..");
115         } else {
116             if (DEBUG)
117                 SurveyLog.logger.warning("Removing old test database..  set -D" + CLDR_TEST_KEEP_DB
118                     + "=true if you want to keep it..");
119             File f = getEmptyDir(DB_SUBDIR);
120             f.delete();
121             if (DEBUG)
122                 SurveyLog.logger.warning("Erased: " + f.getAbsolutePath() + " - now exists=" + f.isDirectory());
123         }
124         return args;
125     }
126 
TestAll()127     public TestAll() {
128         super(new String[] {
129             // use class.getName so we are in sync with name changes and
130             // removals (if not additions)
131             TestIntHash.class.getName(),
132             TestXPathTable.class.getName(),
133             TestMisc.class.getName(),
134             TestSTFactory.class.getName(),
135             TestUserSettingsData.class.getName(),
136             TestAnnotationVotes.class.getName()
137         },
138             "All tests in CLDR Web");
139     }
140 
141     public static final String CLASS_TARGET_NAME = "CLDR.Web";
142 
143     /**
144      *
145      * @author srl
146      * @see TestInfo
147      */
148     public static class WebTestInfo {
149         private static WebTestInfo INSTANCE = null;
150 
151         private SupplementalDataInfo supplementalDataInfo;
152         private StandardCodes sc;
153         private Factory cldrFactory;
154         private CLDRFile english;
155         private CLDRFile root;
156         private RuleBasedCollator col;
157 
getInstance()158         public static WebTestInfo getInstance() {
159             synchronized (WebTestInfo.class) {
160                 if (INSTANCE == null) {
161                     INSTANCE = new WebTestInfo();
162                 }
163             }
164             return INSTANCE;
165         }
166 
WebTestInfo()167         private WebTestInfo() {
168         }
169 
getSupplementalDataInfo()170         public SupplementalDataInfo getSupplementalDataInfo() {
171             synchronized (this) {
172                 if (supplementalDataInfo == null) {
173                     supplementalDataInfo = SupplementalDataInfo.getInstance(CLDRPaths.SUPPLEMENTAL_DIRECTORY);
174                 }
175             }
176             return supplementalDataInfo;
177         }
178 
getStandardCodes()179         public StandardCodes getStandardCodes() {
180             synchronized (this) {
181                 if (sc == null) {
182                     sc = StandardCodes.make();
183                 }
184             }
185             return sc;
186         }
187 
getCldrFactory()188         public Factory getCldrFactory() {
189             synchronized (this) {
190                 if (cldrFactory == null) {
191                     cldrFactory = Factory.make(CLDRPaths.MAIN_DIRECTORY, ".*");
192                 }
193             }
194             return cldrFactory;
195         }
196 
getEnglish()197         public CLDRFile getEnglish() {
198             synchronized (this) {
199                 if (english == null) {
200                     english = getCldrFactory().make("en", true);
201                 }
202             }
203             return english;
204         }
205 
getRoot()206         public CLDRFile getRoot() {
207             synchronized (this) {
208                 if (root == null) {
209                     root = getCldrFactory().make("root", true);
210                 }
211             }
212             return root;
213         }
214 
getCollator()215         public Collator getCollator() {
216             synchronized (this) {
217                 if (col == null) {
218                     col = (RuleBasedCollator) Collator.getInstance();
219                     col.setNumericCollation(true);
220                 }
221             }
222             return col;
223         }
224     }
225 
226     static boolean dbSetup = false;
227 
228     /**
229      * Set up the CLDR db
230      */
setupTestDb()231     public synchronized static void setupTestDb() {
232         if (dbSetup == false) {
233             sanity();
234             makeDataSource();
235             dbSetup = true;
236         }
237     }
238 
shutdownDb()239     public static void shutdownDb() throws SQLException {
240         setupTestDb();
241         DBUtils.getInstance().doShutdown();
242     }
243 
244     public static final String CORE_TEST_PATH = "cldr_db_test";
245     public static final String CLDR_WEBTEST_DIR = TestAll.class.getPackage().getName() + ".dir";
246     public static final String CLDR_WEBTEST_DIR_STRING = CldrUtility.getProperty(CLDR_WEBTEST_DIR,
247         System.getProperty("user.home") + File.separator + CORE_TEST_PATH);
248     public static final File CLDR_WEBTEST_FILE = new File(CLDR_WEBTEST_DIR_STRING);
249     static File baseDir = null;
250 
getBaseDir()251     public synchronized static File getBaseDir() {
252         if (baseDir == null) {
253             File newBaseDir = CLDR_WEBTEST_FILE;
254             if (!newBaseDir.exists()) {
255                 newBaseDir.mkdir();
256             }
257             if (!newBaseDir.isDirectory()) {
258                 throw new IllegalArgumentException("Bad dir [" + CLDR_WEBTEST_DIR + "]: " + newBaseDir.getAbsolutePath());
259             }
260             SurveyLog.debug("Note: using test dir [" + CLDR_WEBTEST_DIR + "]: " + newBaseDir.getAbsolutePath());
261             baseDir = newBaseDir;
262         }
263         return baseDir;
264     }
265 
getDir(String forWhat)266     public static File getDir(String forWhat) {
267         return new File(getBaseDir(), forWhat);
268     }
269 
getEmptyDir(String forWhat)270     public static File getEmptyDir(String forWhat) {
271         return emptyDir(getDir(forWhat));
272     }
273 
emptyDir(File dir)274     public static File emptyDir(File dir) {
275         if (DEBUG)
276             System.err.println("Erasing: " + dir.getAbsolutePath());
277         if (dir.isDirectory()) {
278             File cachedBFiles[] = dir.listFiles();
279             if (cachedBFiles != null) {
280                 for (File f : cachedBFiles) {
281                     if (f.isFile()) {
282                         f.delete();
283                     } else if (f.isDirectory()) {
284                         if (f.getAbsolutePath().contains(CORE_TEST_PATH)) {
285                             emptyDir(f);
286                             f.delete();
287                         } else {
288                             SurveyLog.logger.warning("Please manually remove: " + f.getAbsolutePath());
289                         }
290                     }
291                 }
292             }
293         } else {
294             dir.mkdir();
295         }
296         return dir;
297     }
298 
initDerby()299     static void initDerby() {
300         long start = System.currentTimeMillis();
301         try {
302             Class.forName(DERBY_DRIVER);
303         } catch (ClassNotFoundException e) {
304             throw new RuntimeException(e);
305         } finally {
306             if (DEBUG)
307                 System.err.println("Load " + DERBY_DRIVER + " - " + ElapsedTimer.elapsedTime(start));
308         }
309     }
310 
makeDataSource()311     static void makeDataSource() {
312         final String jdbcUrl = CldrUtility.getProperty(CLDR_TEST_JDBC, "");
313         System.err.println(CLDR_TEST_JDBC +"="+jdbcUrl);
314         if (!jdbcUrl.isEmpty()) {
315             DBUtils.makeInstanceFrom(null, jdbcUrl);
316         } else if (CldrUtility.getProperty(CLDR_TEST_DISK_DB, false)) {
317             initDerby();
318             DBUtils.makeInstanceFrom(setupDerbyDataSource(getDir(DB_SUBDIR)), null);
319         } else {
320             initDerby();
321             DBUtils.makeInstanceFrom(setupDerbyDataSource(null), null);
322         }
323     }
324 
325     // from
326     // http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/doc/ManualPoolingDataSourceExample.java?view=co
327 
328     private static boolean isSetup = false;
329 
330     /**
331      * null = inmemory.
332      *
333      * @param theDir
334      * @return
335      */
setupDerbyDataSource(File theDir)336     public static DataSource setupDerbyDataSource(File theDir) {
337         org.apache.derby.jdbc.EmbeddedDataSource ds = new EmbeddedDataSource();
338         if (theDir != null) {
339             ds.setDatabaseName(theDir.getAbsolutePath());
340         } else {
341             ds.setDatabaseName("memory:sttest");
342         }
343         if (isSetup == false || (theDir != null && !theDir.exists())) {
344             isSetup = true;
345             if (theDir != null) {
346                 if (DEBUG)
347                     SurveyLog.logger.warning("Using new: " + theDir.getAbsolutePath() + " baseDir = "
348                         + getBaseDir().getAbsolutePath());
349             }
350 
351             ds.setCreateDatabase("create");
352         }
353         return ds;
354     }
355 
getProgressIndicator(TestLog t)356     public static CLDRProgressIndicator getProgressIndicator(TestLog t) {
357         final TestLog test = t;
358         return new CLDRProgressIndicator() {
359 
360             @Override
361             public CLDRProgressTask openProgress(String what) {
362                 return openProgress(what, 0);
363             }
364 
365             @Override
366             public CLDRProgressTask openProgress(String what, int max) {
367                 final String whatP = what;
368                 return new CLDRProgressTask() {
369 
370                     @Override
371                     public void close() {
372 
373                     }
374 
375                     @Override
376                     public void update(int count) {
377                         update(count, "");
378                     }
379 
380                     @Override
381                     public void update(int count, String what) {
382                         test.logln(whatP + " Update: " + what + ", " + count);
383                     }
384 
385                     @Override
386                     public void update(String what) {
387                         update(0, what);
388                     }
389 
390                     @Override
391                     public long startTime() {
392                         return 0;
393                     }
394                 };
395             }
396         };
397     }
398 
399     /**
400      * Fetch data from jar
401      *
402      * @param name
403      *            name of thing to load
404      *            (org.unicode.cldr.unittest.web.data.name)
405      */
406     static public BufferedReader getUTF8Data(String name) throws java.io.IOException {
407         return FileReaders.openFile(TestAll.class, "data/" + name);
408     }
409 }
410