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.Aggregator;
27  import org.apache.hadoop.chukwa.database.TableCreator;
28  import java.io.BufferedReader;
29  import java.io.File;
30  import java.io.FileReader;
31  import java.io.IOException;
32  import java.sql.ResultSet;
33  import java.sql.SQLException;
34  import java.util.ArrayList;
35  
36  public class TestDatabaseAggregator extends TestCase {
37      public DatabaseSetup dbSetup = new DatabaseSetup();
38  
39      public void setUp() {
40        try{
41  	dbSetup.setUpDatabase();
42        } catch (Exception e) {
43          fail(ExceptionUtil.getStackTrace(e));
44        }
45      }
46  
47      public void tearDown() {
48  	dbSetup.tearDownDatabase();
49      }
50  
51    public void verifyTable(String table) {
52      ChukwaConfiguration cc = new ChukwaConfiguration();
53      String query = "select * from ["+table+"];";
54      Macro mp = new Macro(dbSetup.current,query);
55      query = mp.toString();
56      try {
57        DatabaseWriter db = new DatabaseWriter(dbSetup.cluster);
58        ResultSet rs = db.query(query);
59        while(rs.next()) {
60          int i = 1;
61          String value = rs.getString(i);
62        }
63        db.close();
64      } catch(SQLException ex) {
65        fail("SQL Exception: "+ExceptionUtil.getStackTrace(ex));
66      }
67    }
68  
69    public void testAggregator() {
70      Aggregator dba = new Aggregator();
71      DatabaseWriter db = new DatabaseWriter(dbSetup.cluster);
72      dba.setWriter(db);
73      String queries = Aggregator.getContents(new File(System
74          .getenv("CHUKWA_CONF_DIR")
75          + File.separator + "aggregator.sql"));
76      String[] query = queries.split("\n");
77      for (int i = 0; i < query.length; i++) {
78        if(query[i].indexOf("#")==-1) {
79          try {
80            dba.process(query[i]);
81            assertTrue("Completed query: "+query[i],true);
82          } catch(Throwable ex) {
83            fail("Exception: "+ExceptionUtil.getStackTrace(ex));
84          }
85        }
86      }
87      db.close();
88    }
89  
90  }