View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.chukwa.database;
19  
20  import junit.framework.TestCase;
21  import java.util.Calendar;
22  import org.apache.hadoop.chukwa.database.Macro;
23  import org.apache.hadoop.chukwa.util.DatabaseWriter;
24  import org.apache.hadoop.chukwa.conf.ChukwaConfiguration;
25  import org.apache.hadoop.chukwa.util.ExceptionUtil;
26  import org.apache.hadoop.chukwa.database.TableCreator;
27  import java.io.BufferedReader;
28  import java.io.File;
29  import java.io.FileReader;
30  import java.io.IOException;
31  import java.sql.ResultSet;
32  import java.sql.SQLException;
33  import java.sql.Timestamp;
34  import java.util.ArrayList;
35  import java.util.Date;
36  
37  public class TestDatabasePrepareStatement extends TestCase {
38  
39    long[] timeWindow = {7, 30, 91, 365, 3650};
40    String cluster = "demo";
41    long current = Calendar.getInstance().getTimeInMillis();
42  
43    public void setUp() {
44      System.setProperty("CLUSTER","demo");
45      DatabaseWriter db = new DatabaseWriter(cluster);
46      String buffer = "";
47      File aFile = new File(System.getenv("CHUKWA_CONF_DIR")
48                   + File.separator + "database_create_tables.sql");
49      buffer = readFile(aFile);
50      String tables[] = buffer.split(";");
51      for(String table : tables) {
52        if(table.length()>5) {
53          try {
54            db.execute(table);
55          } catch (Exception e) {
56            fail("Fail to retrieve meta data from table:"+table);
57          }
58        }
59      }
60      db.close();
61      for(int i=0;i<timeWindow.length;i++) {
62        TableCreator tc = new TableCreator();
63        long start = current;
64        long end = current + (timeWindow[i]*1440*60*1000);
65        try {
66          tc.createTables(start, end);
67        } catch (Exception e) {
68          fail("Fail to create database tables.");
69        }
70      }
71    }
72  
73    public void tearDown() {
74      DatabaseWriter db = null;
75      try {
76        db = new DatabaseWriter(cluster);
77        ResultSet rs = db.query("show tables");
78        ArrayList<String> list = new ArrayList<String>();
79        while(rs.next()) {
80          String table = rs.getString(1);
81          list.add(table);
82        }
83        for(String table : list) {
84          db.execute("drop table "+table);
85        }
86      } catch(Throwable ex) {
87      } finally {
88        if(db!=null) {
89          db.close();
90        }
91      }
92    }
93  
94    public String readFile(File aFile) {
95      StringBuffer contents = new StringBuffer();
96      try {
97        BufferedReader input = new BufferedReader(new FileReader(aFile));
98        try {
99          String line = null; // not declared within while loop
100         while ((line = input.readLine()) != null) {
101           contents.append(line);
102           contents.append(System.getProperty("line.separator"));
103         }
104       } finally {
105         input.close();
106       }
107     } catch (IOException ex) {
108       ex.printStackTrace();
109     }
110     return contents.toString();
111   }
112 
113   public void testPrepareStatement() {
114     DatabaseWriter db = new DatabaseWriter(cluster);
115     Date today = new Date();
116     long current = today.getTime();
117     Timestamp timestamp = new Timestamp(current);
118     String hostname="chukwa.example.org";
119     String query = "insert into [system_metrics] set timestamp='"+timestamp.toString()+"', host='"+hostname+"', cpu_user_pcnt=100;";
120     Macro mp = new Macro(current, current, query);
121     query = mp.toString();
122     try {
123       db.execute(query);
124       query = "select timestamp,host,cpu_user_pcnt from [system_metrics] where timestamp=? and host=? and cpu_user_pcnt=?;";
125       mp = new Macro(current, current, query);
126       query = mp.toString();
127       ArrayList<Object> parms = new ArrayList<Object>();
128       parms.add(current);
129       parms.add(hostname);
130       parms.add(100); 
131       ResultSet rs = db.query(query, parms);
132       while(rs.next()) {
133         assertTrue(hostname.intern()==rs.getString(2).intern());
134         assertTrue(100==rs.getInt(3));
135       }
136       db.close();
137     } catch(SQLException ex) {
138       fail("Fail to run SQL statement:"+ExceptionUtil.getStackTrace(ex));
139     }
140   }
141 
142 }