JAL-931 - groovy test harness code.
[jalview.git] / examples / groovy / JvLoadTester.groovy
1 import jalview.gui.*;\r
2 import jalview.io.*;\r
3 \r
4 def class JvLoadTest {\r
5     FileLoader fl = null;\r
6     def String safename = null;\r
7     JvLoadTest(String sname) { \r
8         if (!new File(sname).exists() || new File(sname).canWrite())\r
9             {\r
10                 safename = sname;\r
11             } else {\r
12                 System.err.println("Warning : "+sname+" isn't being used to store temporary files.");\r
13             }   \r
14     }\r
15     def public boolean doTest (file) {\r
16         fl = new FileLoader(false);\r
17         System.gc();\r
18         AlignFrame af = fl.LoadFileWaitTillLoaded(file\r
19                                                   ,FormatAdapter.FILE);\r
20         return doTest(af);\r
21     }\r
22     def public boolean doSequentialReadTest (file) {\r
23         return doSequentialReadTest(file, 0);\r
24     }\r
25     // Return true if there is more data to read.\r
26     def public boolean peekFp(FileParse fp) {\r
27         try { fp.mark(); }  catch (Exception ex) { System.err.println("FAILED mark."+ex); return false; };\r
28         try {\r
29           def String nl;\r
30           for (i in 1..3) { \r
31            nl = fp.nextLine();\r
32            if (nl==null) { return false; }\r
33            System.out.println(nl +"\\n");\r
34           }\r
35         } catch (Exception e) { // end of file.\r
36                 return false; };\r
37         try { fp.reset(); } catch (Exception ex) { System.err.println("FAILED rewind."+ex); return false; };\r
38         return true;\r
39     }\r
40     /*\r
41       Halt after loading the mx'th entry in the filestream\r
42     */\r
43     def public boolean doSequentialReadTest (file, int mx) {\r
44         // first properly open the file\r
45         //      if (!doTest(file)) { return };\r
46         def FileParse fp = null;\r
47         try {\r
48                 fp = new FileParse(file, AppletFormatAdapter.FILE);\r
49         } catch (Exception e) { System.err.println("Couldn't open "+file+"\\n"); e.printStackTrace(); return false;};\r
50         Desktop.instance.closeAll_actionPerformed(null)\r
51             System.gc();\r
52         while (fp!=null && fp.isValid() && (mx==0 || mx!=fp.index)) {\r
53             if (!peekFp(fp)) return false;\r
54             fl = new FileLoader(false);\r
55             AlignFrame af = fl.LoadFileWaitTillLoaded(fp, null);\r
56             System.out.println("FileParse index: "+fp.index);   \r
57             if (af!=null && (mx==0 || mx!=fp.index))\r
58                 {       def boolean res = doTest(af);\r
59                 if (!res)\r
60                     {\r
61                         // return false;\r
62                     }\r
63                 } else {\r
64                     // return false;\r
65                 }\r
66         }\r
67         return true;\r
68     }\r
69     def public void waitTillSettled(AlignFrame af)\r
70     {\r
71         if (af==null) { return; }\r
72         Thread.sleep(10);\r
73         while (af.getViewport().updatingConsensus || af.getViewport().updatingConservation) {\r
74             Thread.sleep(150); // wait until things settle down\r
75         }\r
76     }\r
77     def public boolean doTest(AlignFrame af) {\r
78         Object pr = af.getViewport().getAlignment().getProperty("AC");\r
79         if (pr!=null) { System.out.println("Accession = "+(String) pr); }\r
80         af.selectAllSequenceMenuItem_actionPerformed(null)\r
81             def boolean done = false;\r
82         // Just try to save - don\'t mess around with clipboard\r
83         /*while (!done) {\r
84           try {\r
85           af.copy_actionPerformed(null)\r
86           done = true;\r
87           } catch (Exception e) {\r
88           Thread.sleep(100); // wait until clipboard might be available again\r
89           }\r
90           }*/\r
91         if (af==null) { return false; }\r
92         waitTillSettled(af);\r
93         // Try and save as a jalview project and reload\r
94         try {\r
95             //      af.saveAlignment(safename, "Jalview")\r
96             new Jalview2XML().SaveState(new java.io.File(safename));\r
97             Thread.sleep(100);\r
98                 } catch (Exception ex) { \r
99                     System.out.println("Couldn\'t save.");\r
100                     ex.printStackTrace(System.err);\r
101                     return false;\r
102                 }\r
103         waitTillSettled(af);\r
104         try {\r
105             Desktop.instance.closeAll_actionPerformed(null);\r
106         } catch (Exception ex) {}\r
107         System.gc();\r
108         try {\r
109             af = new FileLoader(false).LoadFileWaitTillLoaded(safename, FormatAdapter.FILE);    \r
110         } \r
111         catch (Exception ex) {\r
112             System.out.println("Couldn't reload saved file.");\r
113             System.gc();\r
114             return false;\r
115         }\r
116         waitTillSettled(af);\r
117 \r
118         Desktop.instance.closeAll_actionPerformed(null);\r
119 \r
120         // af.paste(true)\r
121         // af.newView_actionPerformed(null)\r
122         // af.newView_actionPerformed(null)\r
123 \r
124         return true;\r
125     }\r
126     def public boolean TestForAll(String dir) {\r
127         println "For directory or file : "+dir;\r
128         File fd = new File(dir);\r
129         if (!fd.isDirectory()) { return doSequentialReadTest(dir); }\r
130         fd.eachFile() { file -> TestForAll(file.getAbsolutePath()) };\r
131     }\r
132 }\r
133 def JvLoadTest newJvLoadTest(String tempFile) {\r
134         jalview.gui.Desktop.instance.closeAll_actionPerformed(null);\r
135         System.gc();\r
136         jalview.gui.Desktop.instance.desktop.showMemoryUsage(true);\r
137         return new JvLoadTest(tempFile)\r
138 }