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