View Javadoc

1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.jdbc;
17  
18  import java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.Collections;
21  import java.util.List;
22  import java.util.Properties;
23  
24  import org.codehaus.plexus.util.StringUtils;
25  import org.junit.Ignore;
26  import org.junit.Test;
27  import org.kuali.common.util.Assert;
28  import org.kuali.common.util.FormatUtils;
29  import org.kuali.common.util.LoggerUtils;
30  import org.kuali.common.util.PropertyUtils;
31  
32  public class TableStatsTest {
33  
34  	@Test
35  	@Ignore
36  	public void test() {
37  		try {
38  			String location = "/Users/jcaddel/sts/3.1.0.RELEASE/workspace/ks-sandbox/ks-deployments/ks-deployment-resources/src/main/resources/org/kuali/student/ks-source-db/tablestats.properties";
39  
40  			Properties p = PropertyUtils.load(location);
41  
42  			// Should always be an even number of properties
43  			Assert.isEven(p.size(), "There must always be exactly 2 keys per table");
44  
45  			List<TableStats> tables = getTables(p);
46  			sortTables(tables);
47  
48  			List<String> columns = Arrays.asList("name", "rows", "size");
49  			List<Object[]> rows = getRows(tables);
50  
51  			LoggerUtils.logTable(columns, rows);
52  
53  		} catch (Exception e) {
54  			e.printStackTrace();
55  		}
56  	}
57  
58  	protected List<Object[]> getRows(List<TableStats> tables) {
59  		List<Object[]> rows = new ArrayList<Object[]>();
60  		for (TableStats table : tables) {
61  			String size = FormatUtils.getCount(table.getRows());
62  			String rowCount = FormatUtils.getSize(table.getSize());
63  			Object[] row = { table.getName(), size, rowCount };
64  			rows.add(row);
65  		}
66  		return rows;
67  	}
68  
69  	protected void sortTables(List<TableStats> tables) {
70  		Collections.sort(tables, new TableStatsSizeComparator());
71  		Collections.reverse(tables);
72  	}
73  
74  	protected List<TableStats> getTables(Properties p) {
75  		List<String> keys = PropertyUtils.getSortedKeys(p);
76  		List<TableStats> tables = new ArrayList<TableStats>();
77  		for (int i = 0; i < keys.size(); i += 2) {
78  			String rowsKey = keys.get(i);
79  			String sizeKey = keys.get(i + 1);
80  			long rows = Long.parseLong(p.getProperty(rowsKey));
81  			long size = Long.parseLong(p.getProperty(sizeKey));
82  			String name = StringUtils.substring(rowsKey, 0, rowsKey.indexOf("."));
83  			TableStats table = new TableStats();
84  			table.setRows(rows);
85  			table.setSize(size);
86  			table.setName(name);
87  			if (rows > 0) {
88  				tables.add(table);
89  			}
90  		}
91  		return tables;
92  	}
93  }