1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: TestShl2Runner.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
35 import java
.io
.FileWriter
;
36 import java
.io
.RandomAccessFile
;
37 import java
.io
.PrintWriter
;
38 import java
.io
.FileOutputStream
;
39 import java
.io
.IOException
;
40 import java
.io
.InputStream
;
41 import java
.util
.ArrayList
;
43 import java
.sql
.Connection
;
44 import java
.sql
.DriverManager
;
45 import java
.sql
.Statement
;
46 import java
.sql
.ResultSet
;
48 import java
.util
.HashMap
;
50 import java
.util
.Iterator
;
51 import java
.util
.Properties
;
53 import java
.io
.FileInputStream
;
55 // -----------------------------------------------------------------------------
57 * This class offers helper function, to easily connect to a database
58 * delete values and insert values.
63 public static Properties
getProperties(String name
)
65 Properties prop
= new Properties();
66 FileInputStream propFile
= null;
68 propFile
= new FileInputStream(name
);
71 } catch (Exception e
) {
77 public static void setProperties(Properties prop
, String name
) {
78 FileOutputStream propFile
= null;
80 propFile
= new FileOutputStream(name
);
81 prop
.store(propFile
,"");
83 } catch (Exception e
) {
92 * This method inserts given values into<br>
94 * @param values a set of comma separated values to be inserted
97 public static void SQLinsertValues(String values
)
99 Statement oStmt
= null;
100 Connection oCon
= null;
103 oCon
= getMySQLConnection();
104 oStmt
= oCon
.createStatement();
105 ResultSet oResult
= oStmt
.executeQuery("insert into states values ("
110 System
.out
.println("Couldn't insert values to db");
119 catch (Exception e
) {}
123 * This method establishes a Connection<br>
124 * with the database 'module_unit' on jakobus
127 public static Connection
getMySQLConnection() throws Exception
129 Class
.forName("org.gjt.mm.mysql.Driver");
130 Connection mysql
= DriverManager
.getConnection(
131 "jdbc:mysql://jakobus:3306/module_unit","admin","admin");
135 * This method removes all entries of the given<br>
136 * module/platform combination
137 * @param mdl the name of the module, e.g. sal
138 * @param os the name of the platform, e.g. unxsols
141 public static void SQLdeleteValues(String _sModulName
, String _sEnvironment
)
143 Statement oStmt
= null;
144 Connection oCon
= null;
147 oCon
= getMySQLConnection();
148 oStmt
= oCon
.createStatement();
149 ResultSet oResult
= oStmt
.executeQuery("delete from states where mdl='"
150 + _sModulName
+ "' AND pf='" + _sEnvironment
+ "'");
154 System
.out
.println("Couldn't delete values from db");
163 catch (Exception e
) {}
168 // -----------------------------------------------------------------------------
171 * This class helps to build only one output line if the classname and the methodname are the same.
176 public String m_sDate
;
177 public String m_sClassName
;
178 public String m_sMethodName
;
180 ArrayList m_sMethodTestNames
= new ArrayList();
181 ArrayList m_sComments
= new ArrayList();
182 ArrayList m_sStates
= new ArrayList();
184 public String m_sCommentContainer
= "";
189 public String
getState()
191 // build the right State
192 String sAllIsRight
= "PASSED#OK#";
193 ArrayList sFailedStates
= new ArrayList();
194 boolean bFailed
= false;
195 for (int i
=0; i
<m_sStates
.size();i
++)
197 String sStatus
= (String
) m_sStates
.get(i
);
198 if (!sStatus
.equals(sAllIsRight
))
201 sFailedStates
.add(sStatus
);
204 if (bFailed
== false)
209 // interpret the failures
210 return "PASSED#FAILED#";
213 public void add(String _sDate
, String _sClassName
, String _sMethodName
, String _sMethodTestName
, String _sState
, String _sComment
)
216 m_sClassName
= _sClassName
;
217 m_sMethodName
= _sMethodName
;
218 m_sMethodTestNames
.add(_sMethodTestName
);
219 m_sStates
.add(_sState
);
221 if (_sComment
.length() > 0)
223 String sComment
= _sComment
.replace('\'',' ');
225 m_sComments
.add(sComment
);
227 if (m_sCommentContainer
.length() > 0)
229 m_sCommentContainer
+= ";<BR>";
231 m_sCommentContainer
+= _sMethodTestName
+ "(): " + _sComment
;
236 // -----------------------------------------------------------------------------
239 * HashMap Helper. At the moment classname and Methodname build a key value
240 * if they already exist, we store additional information into the already existing value
241 * if not, build an new entry.
246 HashMap aMap
= new HashMap();
248 public DatabaseEntry()
252 public String
Quote(String _sToQuote
)
255 return ts
+ _sToQuote
+ ts
;
259 * Write down all collected lines into the database.
261 public void writeDown(String _sProjectName
, String _sEnvironment
)
263 System
.out
.println(" ");
264 Set aSet
= (Set
)aMap
.keySet();
265 Iterator aIter
= aSet
.iterator();
268 while (aIter
.hasNext())
270 String sKey
= (String
)aIter
.next();
271 CurrentEntry aEntry
= (CurrentEntry
)aMap
.get(sKey
);
273 String db_line
= Quote(_sProjectName
) + sComma
+
274 Quote( aEntry
.m_sClassName
) + sComma
+
275 Quote( aEntry
.m_sMethodName
) + sComma
+
276 Quote( aEntry
.getState()) + sComma
+
277 Quote( _sEnvironment
) + sComma
+
278 Quote( aEntry
.m_sDate
) + sComma
+
279 Quote( aEntry
.m_sCommentContainer
);
280 System
.out
.println(db_line
);
281 DBHelper
.SQLinsertValues(db_line
);
286 public void add(String _sDate
, String _sClassName
,
287 String _sMethodName
, String _sMethodTestName
,
288 String _sState
, String _sComment
)
290 String sKey
= _sClassName
+ "." + _sMethodName
;
291 if (aMap
.containsKey(sKey
))
293 CurrentEntry aEntry
= (CurrentEntry
)aMap
.get(sKey
);
294 aEntry
.add(_sDate
, _sClassName
, _sMethodName
, _sMethodTestName
, _sState
, _sComment
);
298 CurrentEntry aEntry
= new CurrentEntry();
299 aEntry
.add(_sDate
, _sClassName
, _sMethodName
, _sMethodTestName
, _sState
, _sComment
);
300 aMap
.put(sKey
, aEntry
);
307 * This class will run the testshl2 script
309 * According to a given baseDir it'll first
310 * get all scenario files, clean them up<br>
311 * Then all sources will be compiled and
312 * the scenarios started.<br>
313 * After the a scenarios are finished the result
314 * will be stored in the database.
317 public class TestShl2Runner
322 String m_sVersion
= "udk399";
323 String m_sExtension
= "";
324 String m_sProjectDir
= "";
325 String m_sEnvironment
= "";
326 String m_sProjectName
= "";
327 String m_sShellExecutable
="";
328 String m_sLocalEnv
="";
331 * The constructor to use this class from any other java-class
332 * @param bdir the base directory where the qa-subfolder can be found
333 * @param pf the platform e.g. unxsols
334 * @param mod the module e.g. sal
335 * @param ver the version e.g. udk304
336 * @param pre the path to the shell, may be "", but is needed on Windows<br>
337 * Systems to ensure that the created scripts run in an 4NT-shell
340 // public TestShl2Runner(String bdir, String pf, String project, String ver, String pre, String localenv)
341 public TestShl2Runner(String filename
)
343 String fs
= System
.getProperty("file.separator");
345 m_aProps
= PropertyHelper
.getProperties(filename
);
347 m_sEnvironment
= m_aProps
.getProperty("ENV"); // unxsols3.pro
348 m_sProjectName
= m_aProps
.getProperty("PROJECTNAME"); // sal
349 m_sProjectDir
= m_aProps
.getProperty("BASEDIR") + fs
+ m_sProjectName
; // /usr/qaapi/projects/udk/sal
350 m_sVersion
= m_aProps
.getProperty("BUILD"); // udk305
351 m_sShellExecutable
= m_aProps
.getProperty("SHELL"); // /bin/tcsh
352 m_sLocalEnv
= m_aProps
.getProperty("LOCALENV"); // /usr/qaapi/projects/solartmp
354 // if (m_sVersion.indexOf("-pro") > 1) m_sExtension=".pro";
360 * The main Method makes it possible to run this class standalone
361 * @param args the command line arguments
363 public static void main(String args
[])
365 if (args
.length
!= 1)
367 System
.out
.println("Usage: TestShl2Runner <config.file>");
368 System
.out
.println("Example: TestShl2Runner /usr/qaapi/projects/udk/sal/qa/configfile");
372 TestShl2Runner aRunner
= new TestShl2Runner(args
[0]);
375 // -----------------------------------------------------------------------------
379 String fs
= System
.getProperty("file.separator");
380 String sJobFile
= m_sProjectDir
+ fs
+ m_aProps
.getProperty("INPUT") + fs
+ m_aProps
.getProperty("JOBFILE");
381 ArrayList aLines
= getLines(sJobFile
);
385 // -----------------------------------------------------------------------------
387 public void startAll()
389 String fs
= System
.getProperty("file.separator");
391 //get all scenario-files
392 System
.out
.println("Getting jobs");
393 ArrayList aJobList
= getJobs();
395 //cleanup scenorio-files and return a working array
396 // Object[] the_array = cleanup(m_sProjectDir + fs + "qa" + fs + "sce" + fs,scene_files);
397 // System.out.println("...done");
399 //create a script to build the sources and run the tests
400 System
.out
.println("creating start script for testprocess");
401 String scriptName
= createScript(aJobList
);
402 System
.out
.println("...done");
405 System
.out
.println("starting script for testprocess");
406 String sScript
= m_sShellExecutable
+ " " + getOutputDir() + fs
+ scriptName
;
407 executeScript(sScript
);
408 System
.out
.println("...done");
410 //remove old values from db-table
411 DBHelper
.SQLdeleteValues(m_sProjectName
,m_sEnvironment
);
412 DBHelper
.SQLdeleteValues(m_sProjectName
,m_sEnvironment
);
414 //parse the output and store it
415 parseOut (aJobList
, m_sProjectName
, m_sEnvironment
, m_sProjectDir
);
419 * This method reads the lines of a file and puts<br>
420 * them into an ArrayList
421 * @param filename the name of the file to be read
424 public ArrayList
getLines(String filename
)
426 File the_file
= new File(filename
);
427 ArrayList the_lines
= new ArrayList();
428 if (! the_file
.exists())
430 System
.out
.println("couldn't find file " + filename
);
433 RandomAccessFile the_reader
= null;
436 the_reader
= new RandomAccessFile(the_file
,"r");
438 while (aLine
!= null)
440 aLine
= the_reader
.readLine();
441 if ( (aLine
!= null) &&
442 (! (aLine
.length() < 2) ) &&
443 (! aLine
.startsWith("#")))
445 the_lines
.add(aLine
);
449 catch (java
.io
.FileNotFoundException fne
)
451 System
.out
.println("couldn't open file " + filename
);
452 System
.out
.println("Message: " + fne
.getMessage());
454 catch (java
.io
.IOException ie
)
456 System
.out
.println("Exception while reading file " + filename
);
457 System
.out
.println("Message: " + ie
.getMessage());
463 catch (java
.io
.IOException ie
)
465 System
.out
.println("Couldn't close file " + filename
);
466 System
.out
.println("Message: " + ie
.getMessage());
474 * This method calls the createScript-methods<br>
475 * for the given platform
476 * @param basedir the name of the base-directory, e.g. /usr/qaapi/projects/udk/sal
477 * @param qajobs jobs to be done
480 public String
createScript(ArrayList _aJobList
)
483 if (m_sEnvironment
.startsWith("wnt"))
487 res
= createwntmsci(_aJobList
);
489 catch (IOException ioe
)
491 System
.out
.println("Couldn't create Script");
494 else if (m_sEnvironment
.startsWith("unx"))
498 res
= createunx(_aJobList
);
500 catch (IOException ioe
)
502 System
.out
.println("Couldn't create Script");
507 // can't handle unknown environment
512 public String
getSignalFileParameter()
515 sParameter
= " -sf /var/tmp/signalfile_" + m_sProjectName
+ "_" + m_sEnvironment
+ ".txt ";
519 public String
getLogParameter(String job
)
521 String sParameter
= " -log " + getLogName(job
) + " ";
525 public String
getLogName(String job
)
527 // create the ' -log file '
528 String fs
= System
.getProperty("file.separator");
529 String sLogName
= getOutputDir() + fs
+ job
+ ".log";
533 public String
getInputDir()
535 String fs
= System
.getProperty("file.separator");
536 String sInputDir
= m_sProjectDir
+ fs
+ m_aProps
.getProperty("INPUT");
541 public String
getOutputDir()
543 String fs
= System
.getProperty("file.separator");
544 String sOutputDir
= m_sProjectDir
+ fs
+ m_sEnvironment
+ fs
+ m_aProps
.getProperty("OUTPUT");
546 File aFile
= new File(sOutputDir
);
551 public String
getCAXParameter()
554 // Due to the fu....g java that we havn't access to our well formed ;-)
555 // environment variables the simple switch -cax do not longer work :-(
557 // String sSolTmp = System.getenv("SOL_TMP");
558 if (m_sLocalEnv
.length() > 0)
560 sCAX
= " -cax -tmp " + m_sLocalEnv
;
564 // due to the fact that we don't want to get hole lokal env if no one exist, do nothing here.
569 public String
getProParameter()
572 if (m_sEnvironment
.endsWith(".pro"))
579 public String
getCompEnvName()
582 if (m_sEnvironment
.endsWith(".pro"))
584 sCompEnv
= String
.copyValueOf(m_sEnvironment
.toCharArray(), 0, 8);
588 sCompEnv
= m_sEnvironment
;
590 return " " + sCompEnv
;
594 * This method create the script needed to compile and run the<br>
595 * the tests on the Windows platform
596 * @param basedir the name of the base-directory, e.g. /usr/qaapi/projects/udk/sal
597 * @param qajobs jobs to be done
600 public void PreNPost(FileWriter out
, String _sWhat
) throws IOException
602 String ls
= System
.getProperty("line.separator");
604 out
.write(getRemark() + " do " + _sWhat
+ ls
);
605 for (int i
= 1;i
<9;i
++)
607 String sWhat
= m_aProps
.getProperty(_sWhat
+ i
);
610 out
.write(sWhat
+ ls
);
616 public String
getChangeDirFkt()
618 if (m_sEnvironment
.startsWith("wnt"))
624 public String
getRemark()
626 if (m_sEnvironment
.startsWith("wnt"))
633 public void do_setsolar(FileWriter out
, String _sPreExec
) throws IOException
635 String ls
= System
.getProperty("line.separator");
637 out
.write(getRemark() + " do a setsolar" + ls
);
638 out
.write(_sPreExec
+ " setsolar -" + m_sVersion
+ getCAXParameter() + getProParameter() + getCompEnvName() + ls
);
642 public void do_cvs(FileWriter out
, String _sPreExec
, ArrayList _aJobList
) throws IOException
644 String fs
= System
.getProperty("file.separator");
645 String ls
= System
.getProperty("line.separator");
646 out
.write(getChangeDirFkt() + " " + getInputDir() + ls
);
647 out
.write(getRemark() + " do a cvs and a dmake" + ls
);
649 out
.write(_sPreExec
+ " " + "cvs -d" + m_aProps
.getProperty("CVSROOT") + " update -d" + ls
);
651 for (int i
=0; i
<_aJobList
.size();i
++)
653 String sJob
= (String
) _aJobList
.get(i
);
655 out
.write(getChangeDirFkt() + " " + getInputDir() + fs
+ sJob
+ ls
);
658 out
.write(_sPreExec
+ " " + "dmake -u" + ls
);
663 public void do_testtool(FileWriter out
, String _sPreExec
, ArrayList _aJobList
) throws IOException
665 String ls
= System
.getProperty("line.separator");
666 out
.write(getRemark() + " call the test tool" + ls
);
667 out
.write(getChangeDirFkt() + " " + getOutputDir() + ls
);
668 if (m_sEnvironment
.startsWith("wnt"))
670 out
.write("set DISABLE_SAL_DBGBOX=t" + ls
);
673 for (int i
=0; i
<_aJobList
.size();i
++)
675 String sLine
= (String
) _aJobList
.get(i
);
676 // String sJob = getJob(sLine);
677 out
.write(_sPreExec
+ " " + m_aProps
.getProperty("TESTTOOL") + " " + getLibName(sLine
, m_sEnvironment
) + getLogParameter(sLine
) + getSignalFileParameter() + ls
);
682 public String
createwntmsci(ArrayList _aJobList
) throws IOException
684 String fs
= System
.getProperty("file.separator");
685 String ls
= System
.getProperty("line.separator");
687 String sBatchFile
= m_aProps
.getProperty("BATCHFILE");
688 File outputFile
= new File(getOutputDir(), sBatchFile
);
689 FileWriter out
= new FileWriter(outputFile
.toString());
691 out
.write("rem do not edit, will be created automatically by TestShl2Runner.java" + ls
);
693 // String sBuildEnvironment = "wntmsci9";
694 PreNPost(out
, "PRE");
696 String sBatchPreExec
= "call";
697 do_setsolar(out
, sBatchPreExec
);
698 do_cvs(out
, sBatchPreExec
, _aJobList
);
699 do_testtool(out
, sBatchPreExec
, _aJobList
);
701 PreNPost(out
, "POST");
703 // the exit is alsolute need here, because we do not get back, until
704 // shell is stopped with exit.
705 out
.write("exit" + ls
);
711 * This method create the script needed to compile and run the<br>
712 * the tests on the Unix platform
713 * @param basedir the name of the base-directory, e.g. /usr/qaapi/projects/udk/sal
714 * @param qajobs jobs to be done
717 public String
createunx(ArrayList _aJobList
) throws IOException
719 String fs
= System
.getProperty("file.separator");
720 String ls
= System
.getProperty("line.separator");
722 String sBatchFile
= m_aProps
.getProperty("BATCHFILE");
723 File outputFile
= new File(getOutputDir(), sBatchFile
);
724 FileWriter out
= new FileWriter(outputFile
.toString());
726 out
.write("#!/bin/tcsh" + ls
); // shebang
727 out
.write("# do not edit, will be created automatically by TestShl2Runner.java" + ls
);
729 PreNPost(out
, "PRE");
731 String sBatchPreExec
= "";
732 do_setsolar(out
, sBatchPreExec
);
733 do_cvs(out
, sBatchPreExec
, _aJobList
);
734 do_testtool(out
, sBatchPreExec
, _aJobList
);
736 PreNPost(out
, "POST");
738 out
.write("exit" + ls
);
742 String sExec
= "chmod u+x " + getOutputDir() + fs
+ sBatchFile
;
743 Runtime
.getRuntime().exec(sExec
);
747 // -----------------------------------------------------------------------------
750 * This method extracts the libname from a given<br>
751 * job and adds a relative path based on the platform
752 * @param job the job to be done
753 * @param os the platform
756 public String
getLibName(String job
, String _sEnvironment
)
758 String fs
= System
.getProperty("file.separator");
759 // BACK: String like "../wntmsci9.pro/bin/rtl_OString.dll"
762 int nIndex
= job
.indexOf(".");
766 String sPureFilename
= job
;
767 String purelibname
= "";
768 if (_sEnvironment
.startsWith("wnt"))
770 purelibname
= "bin" + fs
+ sPureFilename
+ ".dll";
772 else if (_sEnvironment
.startsWith("unx"))
774 purelibname
= "lib" + fs
+ "lib" + sPureFilename
+ ".so";
778 // _sEnvironment unknown
782 // we are in the wntmsci9[.pro]/qa directory
783 // to go to the bin directory we have to go to
785 res
= m_sProjectDir
+ fs
+ m_sEnvironment
+ fs
+ purelibname
;
789 // -----------------------------------------------------------------------------
792 public Object[] cleanup(String sceneDir, String[] scene_files)
794 ArrayList res = new ArrayList();
795 for (int j=0;j<scene_files.length;j++)
797 if (scene_files[j].endsWith(".sce"))
801 removeRemark(sceneDir,scene_files[j]);
803 catch (IOException ioe)
805 System.out.println("Couldn't cleanup Scene-File " + scene_files[j]);
807 String pure = scene_files[j].substring(0,
808 scene_files[j].indexOf("."));
809 res.add(pure + ".qadev");
812 return res.toArray();
816 public void removeRemark(String sceneDir,String scene_file)
819 ArrayList org = getLines(sceneDir + scene_file);
820 String pure = scene_file.substring(0,scene_file.indexOf("."));
821 String ls = System.getProperty("line.separator");
822 File outputFile = new File(sceneDir,pure + ".qadev");
823 FileWriter out = new FileWriter(outputFile.toString());
824 for (int k=0;k<org.size();k++)
826 String toWrite = (String)org.get(k);
827 if (toWrite.startsWith("#"))
829 toWrite=toWrite.substring(1);
831 out.write(toWrite + ls);
837 public void parseOut (ArrayList sJobList
, String _sProjectName
, String _sEnvironment
, String basedir
)
839 //parse the out-files and store the data
840 String fs
= System
.getProperty("file.separator");
841 for (int i
=0; i
<sJobList
.size();i
++)
843 String sJob
= (String
) sJobList
.get(i
);
844 String outFile
= getLogName(sJob
);
845 ArrayList out_lines
= getLines(outFile
);
848 DatabaseEntry aEntry
= new DatabaseEntry();
850 for (int j
=0;j
<out_lines
.size();j
++)
852 String sLine
= (String
)out_lines
.get(j
);
853 if (sLine
.startsWith("["))
855 date
= sLine
.substring(1, 11);
856 date
= date
.replace('.','-');
860 if (sLine
.startsWith("Test #PASSED#")) break;
861 // if (out_lines.get(j).equals(
862 // " ---------- ")) break;
866 String sName
= sLine
.substring(0, sLine
.indexOf(';'));
869 int nFirstDot
= sName
.indexOf('.');
870 int nSecondDot
= sName
.indexOf('.', nFirstDot
+ 1);
871 String sClassName
= "";
872 String sMethodName
= "";
873 String sMethodTestName
= "";
877 sClassName
= sName
.substring(0, nFirstDot
);
880 sMethodName
= sName
.substring(nFirstDot
+ 1, nSecondDot
);
881 sMethodTestName
= sName
.substring(nSecondDot
+ 1);
885 sMethodName
= sName
.substring(nFirstDot
);
890 // no FirstDot, also no secondDot!
894 String sComment
= sLine
.substring(sLine
.lastIndexOf('#') + 1);
897 int nSecondNo
= sLine
.indexOf('#');
898 nSecondNo
= sLine
.indexOf('#', nSecondNo
+ 1);
899 String sState
= sLine
.substring(sLine
.indexOf(";") + 1, nSecondNo
+ 1);
901 aEntry
.add(date
, sClassName
, sMethodName
, sMethodTestName
, sState
, sComment
);
903 catch(IndexOutOfBoundsException e
)
905 System
.out
.println("IndexOutOfBoundException: caught in parseOut()");
909 aEntry
.writeDown(_sProjectName
, _sEnvironment
);
913 // -----------------------------------------------------------------------------
915 private void shortWait(int nMilliSec
)
919 Thread
.sleep(nMilliSec
);
921 catch (InterruptedException ex
)
927 // -----------------------------------------------------------------------------
929 public boolean executeScript(String scriptFile
)
931 System
.out
.println("Running " + scriptFile
);
933 // Process testshl = Runtime.getRuntime().exec(scriptFile);
934 ProcessHandler aHandler
= new ProcessHandler(scriptFile
);
935 boolean bBackValue
= aHandler
.executeSynchronously();