001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.hr.time.test;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    
021    import org.apache.log4j.Logger;
022    import org.junit.After;
023    import org.junit.Assert;
024    import org.junit.Before;
025    import org.junit.Ignore;
026    import org.junit.Rule;
027    import org.junit.rules.TestName;
028    import org.kuali.rice.test.TransactionalLifecycle;
029     @Ignore
030    public class TestHarnessBase extends Assert {
031    
032        
033        private static final Logger LOG = Logger.getLogger(TestHarnessBase.class);
034        protected List<String> reports = new ArrayList<String>();
035        @Rule public TestName testName = new TestName();
036        protected TransactionalLifecycle transactionalLifecycle;
037    
038        @Before
039        public void setUp() throws Exception {
040            logBeforeRun();
041            transactionalLifecycle = new TransactionalLifecycle();
042            transactionalLifecycle.start();
043        }
044    
045        @After
046        public void tearDown() throws Exception {
047            logAfterRun();
048            try {
049                if (transactionalLifecycle != null) {
050                    transactionalLifecycle.stop();
051                }
052            } finally {
053                transactionalLifecycle = null;
054            }
055        }
056    
057        protected void logBeforeRun() {
058            LOG.info("##############################################################");
059            LOG.info("# Starting test " + getFullTestName() + "...");
060            LOG.info("# " + dumpMemory());
061            LOG.info("##############################################################");
062        }
063    
064        protected void logAfterRun() {
065            LOG.info("##############################################################");
066            LOG.info("# ...finished test " + getFullTestName());
067            LOG.info("# " + dumpMemory());
068            for (final String report : this.reports) {
069                LOG.info("# " + report);
070            }
071            LOG.info("##############################################################\n\n\n");
072        }
073    
074        protected void report(final String report) {
075            this.reports.add(report);
076        }
077    
078        protected String getFullTestName() {
079            return getClass().getSimpleName() + "." + getTestName();
080        }
081        
082        public String getTestName() {
083            return (testName != null ? testName.getMethodName() : "null");
084        }
085    
086        protected String dumpMemory() {
087            final long total = Runtime.getRuntime().totalMemory();
088            final long free = Runtime.getRuntime().freeMemory();
089            final long max = Runtime.getRuntime().maxMemory();
090            return "[Memory] max: " + max + ", total: " + total + ", free: " + free;
091        }
092    
093    }