KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > test > PerformanceServerConnection


1 package org.netbeans.mdr.test;
2
3 import java.net.InetAddress JavaDoc;
4 /*
5  * PerformanceServerConnection.java
6  *
7  * Created on June 27, 2001, 5:38 PM
8  */

9
10 import java.util.Properties JavaDoc;
11 import java.util.Enumeration JavaDoc;
12 import java.net.MalformedURLException JavaDoc;
13 import java.sql.*;
14 import java.util.ResourceBundle JavaDoc;
15
16 /** class maintaining connection to Performance Server Database
17  *
18  * @author <a HREF="mailto:adam.sotona@sun.com">Adam Sotona</a>
19  * @version 1.0
20  */

21 public class PerformanceServerConnection {
22
23     static {
24         try {
25             Class.forName("org.gjt.mm.mysql.Driver");
26         } catch (ClassNotFoundException JavaDoc ex) {
27             ex.printStackTrace();
28         }
29     }
30
31     private static PerformanceServerConnection connection;
32
33     private Connection conn=null;
34     private String JavaDoc URL;
35     private String JavaDoc username;
36     private String JavaDoc password;
37     private String JavaDoc IDEbranch;
38     private String JavaDoc buildNumber;
39     private String JavaDoc testSuite;
40     private int totalTestCases;
41     private int executionId;
42
43
44     /** performs lookup for property inside given properties and then inside System properties
45      * @param p connection properties
46      * @param key property key
47      * @throws Exception throws Exception when key is missing
48      * @return property value
49      */

50     private static String JavaDoc readProperty (final Properties JavaDoc p, final String JavaDoc key) throws Exception JavaDoc {
51         String JavaDoc value=null;
52         if (p!=null) value=p.getProperty(key,System.getProperty(key));
53         else value=System.getProperty(key);
54         if (value==null) throw new Exception JavaDoc("PerformanceServerConnection is missing option : "+key);
55         return value;
56     }
57
58     /** creates new connection with all informations stored in System properties
59      */

60     public PerformanceServerConnection() {
61         this(System.getProperties());
62     }
63
64     /** creates new connection with all informations stored in given properties properties merged with System properties
65      * <br>
66      * needed properties are:<br><pre>
67      * netbeans.performance.serverURL - URL of Performance Server Database
68      * netbeans.performance.serverUsername - user name
69      * netbeans.performance.serverPassword - pasword
70      * netbeans.build.branch - additional information about tested IDE
71      * org.openide.version - build number of tested IDE (set by default)
72      * netbeans.performance.testSuite - testsuite name
73      * netbeans.performance.totalTestCases - number of reported testcases for verification of complete testsuite
74      * os.name, os.version and os.arch - current OS informations (set by default)
75      * java.vendor, java.version - current Java informations (set by default)
76      * @param p connection properties
77      */

78     public PerformanceServerConnection(final Properties JavaDoc p) {
79         try {
80             executionId=0;
81             URL=readProperty(p,"netbeans.performance.serverURL");
82             username=readProperty(p,"netbeans.performance.serverUsername");
83             password=readProperty(p,"netbeans.performance.serverPassword");
84             IDEbranch=readProperty(p,"netbeans.build.branch");
85             buildNumber=readProperty(p,"org.openide.version");
86             testSuite=readProperty(p,"netbeans.performance.testSuite");
87             totalTestCases=Integer.parseInt(readProperty(p,"netbeans.performance.totalTestCases"));
88             String JavaDoc systemInfo=readProperty(p,"os.name")+" "+readProperty(p,"os.version")+" "+readProperty(p,"os.arch");
89             String JavaDoc javaInfo=readProperty(p,"java.vendor")+" "+readProperty(p,"java.version");
90             InetAddress JavaDoc local=InetAddress.getLocalHost();
91             String JavaDoc computer=local.getHostName()+" "+local.getHostAddress();
92             System.out.println(testSuite+", "+systemInfo+", "+javaInfo+", "+IDEbranch+", "+buildNumber+", "+String.valueOf(totalTestCases));
93             executionId=getExecutionId(testSuite, computer, systemInfo, javaInfo, IDEbranch, buildNumber, totalTestCases);
94         } catch (Exception JavaDoc ex) {
95             ex.printStackTrace();
96             executionId=-1;
97         }
98     }
99     
100     /** compares set of connection properties foe equality
101      * @param p connection properties to comapre
102      * @return boolean result
103      */

104     private boolean equalOptions(final Properties JavaDoc p) {
105         try {
106             return
107                 URL.equals(readProperty(p,"netbeans.performance.serverURL"))&&
108                 username.equals(readProperty(p,"netbeans.performance.serverUsername"))&&
109                 password.equals(readProperty(p,"netbeans.performance.serverPassword"))&&
110                 IDEbranch.equals(readProperty(p,"netbeans.build.branch"))&&
111                 buildNumber.equals(readProperty(p,"org.openide.version"))&&
112                 testSuite.equals(readProperty(p,"netbeans.performance.testSuite"))&&
113                 (totalTestCases==Integer.parseInt(readProperty(p,"netbeans.performance.totalTestCases")));
114         } catch (Exception JavaDoc ex) {
115             ex.printStackTrace();
116             return false;
117         }
118     }
119
120     /** returns default connection holded or created according to System properties
121      * @return PerformanceServerConnection
122      */

123     public static PerformanceServerConnection getConnection() {
124         return getConnection(System.getProperties());
125     }
126     
127     /** returns default connection holded or created according to given and System properties
128      * @param p connection properties
129      * @return PerformanceServerConnection
130      */

131     public static PerformanceServerConnection getConnection(final Properties JavaDoc p) {
132         if (connection==null) {
133             connection=new PerformanceServerConnection(p);
134         } else {
135             if (!connection.equalOptions(p)) {
136                 connection=new PerformanceServerConnection(p);
137             }
138         }
139         return connection;
140     }
141
142     /** returns default connection holded or created according to properties stored in given file and System properties
143      * @param clazz class neneded to determine propertiesFile location (lookup is performed by class loader)
144      * @param propertiesFile connection properties file name
145      * @return PerformanceServerConnection
146      */

147     public static PerformanceServerConnection getConnection(final Class JavaDoc clazz, final String JavaDoc propertiesFile) {
148         Properties JavaDoc props=new Properties JavaDoc();
149         try {
150             props.load(clazz.getClassLoader().getResourceAsStream(propertiesFile));
151         } catch (Exception JavaDoc ex) {
152             ex.printStackTrace();
153         }
154         return getConnection(props);
155     }
156
157     
158     /** sets default PerformanceServerConnection
159      * @param newConnection new default PerformanceServerConnection
160      */

161     public static void setConnection(PerformanceServerConnection newConnection) {
162         connection=newConnection;
163     }
164     
165     /** logs one testcase to performance server
166      * @param testCase test case name
167      * @param time time in ms
168      */

169     public void logTestCase(final String JavaDoc testCase, final long time) {
170         if (executionId>0) {
171             try {
172                 logTestCase(executionId, testCase, time);
173             } catch (Exception JavaDoc ex) {
174                 ex.printStackTrace();
175             }
176         }
177         System.out.println(testCase+": "+String.valueOf(time)+" ms.");
178     }
179
180     /** logs several testcase stored in properties to performance server
181      * @param testCases properties with testcases
182      */

183     public void logTestCases(final Properties JavaDoc testCases) {
184         if (executionId>0) {
185             Enumeration JavaDoc keys=testCases.propertyNames();
186             String JavaDoc key;
187             while (keys.hasMoreElements()) {
188                 try {
189                     key=(String JavaDoc)keys.nextElement();
190                     logTestCase(executionId, key, Integer.parseInt(testCases.getProperty(key)));
191                 } catch (Exception JavaDoc ex) {
192                     ex.printStackTrace();
193                 }
194             }
195         }
196     }
197     
198     private int getExecutionId(final String JavaDoc testSuite, final String JavaDoc computer, final String JavaDoc systemInfo, final String JavaDoc javaInfo, final String JavaDoc IDEbranch, final String JavaDoc buildNumber, final int totalTestCases) throws SQLException {
199         try {
200             if (conn==null)
201                 conn=DriverManager.getConnection(URL, username, password);
202             PreparedStatement stat=conn.prepareStatement("insert into executions (branch,build,computer,java,rundate,system,testsuite,totalkeys) values (?,?,?,?,?,?,?,?)");
203             stat.setString(1,IDEbranch);
204             stat.setString(2,buildNumber);
205             stat.setString(3,computer);
206             stat.setString(4,javaInfo);
207             stat.setTimestamp(5,new Timestamp((new java.util.Date JavaDoc()).getTime()));
208             stat.setString(6,systemInfo);
209             stat.setString(7,testSuite);
210             stat.setInt(8,totalTestCases);
211             stat.executeUpdate();
212             SQLWarning w;
213             while ((w=stat.getWarnings())!=null) {
214                 System.err.println("["+testSuite+" "+systemInfo+" "+javaInfo+" "+IDEbranch+" "+buildNumber+" "+totalTestCases+"]: "+w.getMessage());
215             }
216             ResultSet res=stat.executeQuery("select last_insert_id()");
217             while ((w=res.getWarnings())!=null) {
218                 System.err.println("["+testSuite+" "+systemInfo+" "+javaInfo+" "+IDEbranch+" "+buildNumber+" "+totalTestCases+"]: "+w.getMessage());
219             }
220             res.first();
221             //System.out.println(res.getInt(1)+" "+testSuite+" "+systemInfo+" "+javaInfo+" "+IDEbranch+" "+buildNumber+" "+totalTestCases);
222
return res.getInt(1);
223         } catch (SQLException ex) {
224             System.err.println("["+testSuite+" "+systemInfo+" "+javaInfo+" "+IDEbranch+" "+buildNumber+" "+totalTestCases+"]: "+ex.getMessage());
225             ex.printStackTrace();
226             throw ex;
227         }
228     }
229     
230     private void logTestCase(final int executionId, final String JavaDoc testCase, final long time) throws SQLException {
231         try {
232             if (conn==null)
233                 conn=DriverManager.getConnection(URL, username, password);
234             PreparedStatement stat=conn.prepareStatement("insert into data (id,testcase,time) values (?,?,?)");
235             stat.setInt(1,executionId);
236             stat.setString(2,testCase);
237             stat.setLong(3,time);
238             stat.executeUpdate();
239             SQLWarning w;
240             while ((w=stat.getWarnings())!=null) {
241                 System.err.println("["+executionId+" "+testCase+" "+time+"]: "+w.getMessage());
242             }
243             stat=conn.prepareStatement("update executions set finishedkeys=finishedkeys+1 where id=?");
244             stat.setInt(1,executionId);
245             stat.executeUpdate();
246             while ((w=stat.getWarnings())!=null) {
247                 System.err.println("["+executionId+" "+testCase+" "+time+"]: "+w.getMessage());
248             }
249         } catch (SQLException ex) {
250             System.err.println("["+executionId+" "+testCase+" "+time+"]: "+ex.getMessage());
251             ex.printStackTrace();
252             throw ex;
253         }
254     }
255     
256     
257     /** example of class usage
258      * @param args command line arguments
259      */

260     public static void main(String JavaDoc args[]) {
261         System.setProperty("netbeans.performance.serverPassword","");
262         System.setProperty("netbeans.performance.serverUsername","perfserver");
263         System.setProperty("netbeans.performance.serverURL","jdbc:mysql://beetle.czech.sun.com:3306/performance");
264         System.setProperty("netbeans.performance.testSuite","test");
265         System.setProperty("netbeans.build.branch","test");
266         System.setProperty("netbeans.performance.totalTestCases","1");
267
268         getConnection().logTestCase("test value",100);
269     }
270 }
271
Popular Tags