View Javadoc

1   /**
2    * Copyright 2004-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.hr.time.test;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.apache.log4j.Logger;
22  import org.junit.After;
23  import org.junit.Assert;
24  import org.junit.Before;
25  import org.junit.Ignore;
26  import org.junit.Rule;
27  import org.junit.rules.TestName;
28  import org.kuali.rice.test.TransactionalLifecycle;
29   @Ignore
30  public class TestHarnessBase extends Assert {
31  
32      
33      private static final Logger LOG = Logger.getLogger(TestHarnessBase.class);
34      protected List<String> reports = new ArrayList<String>();
35      @Rule public TestName testName = new TestName();
36      protected TransactionalLifecycle transactionalLifecycle;
37  
38      @Before
39      public void setUp() throws Exception {
40  	logBeforeRun();
41  	transactionalLifecycle = new TransactionalLifecycle();
42  	transactionalLifecycle.start();
43      }
44  
45      @After
46      public void tearDown() throws Exception {
47  	logAfterRun();
48  	try {
49  	    if (transactionalLifecycle != null) {
50  		transactionalLifecycle.stop();
51  	    }
52  	} finally {
53  	    transactionalLifecycle = null;
54  	}
55      }
56  
57      protected void logBeforeRun() {
58  	LOG.info("##############################################################");
59  	LOG.info("# Starting test " + getFullTestName() + "...");
60  	LOG.info("# " + dumpMemory());
61  	LOG.info("##############################################################");
62      }
63  
64      protected void logAfterRun() {
65  	LOG.info("##############################################################");
66  	LOG.info("# ...finished test " + getFullTestName());
67  	LOG.info("# " + dumpMemory());
68  	for (final String report : this.reports) {
69  	    LOG.info("# " + report);
70  	}
71  	LOG.info("##############################################################\n\n\n");
72      }
73  
74      protected void report(final String report) {
75  	this.reports.add(report);
76      }
77  
78      protected String getFullTestName() {
79  	return getClass().getSimpleName() + "." + getTestName();
80      }
81      
82      public String getTestName() {
83  	return (testName != null ? testName.getMethodName() : "null");
84      }
85  
86      protected String dumpMemory() {
87  	final long total = Runtime.getRuntime().totalMemory();
88  	final long free = Runtime.getRuntime().freeMemory();
89  	final long max = Runtime.getRuntime().maxMemory();
90  	return "[Memory] max: " + max + ", total: " + total + ", free: " + free;
91      }
92  
93  }