001/*
002 * $Id: RuntimeTester.java,v 1.8 2016/12/18 20:19:38 oboehm Exp $
003 *
004 * Copyright (c) 2011 by Oliver Boehm
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 *
018 * (c)reated 09.05.2011 by oliver (ob@oasd.de)
019 */
020
021package patterntesting.runtime.junit;
022
023import org.apache.commons.lang3.SystemUtils;
024import org.apache.logging.log4j.LogManager;
025import org.apache.logging.log4j.*;
026
027/**
028 * This is a tester to do some runtime checking like memory checks and other
029 * stuff.
030 *
031 * @author oliver
032 * @since 1.1.1 (09.05.2011)
033 */
034public final class RuntimeTester {
035
036        private static final Logger LOG = LogManager.getLogger(RuntimeTester.class);
037
038        /** Utility class - no need to instantiate it. */
039        private RuntimeTester() {
040        }
041
042        /**
043         * If you have some tests which needs a certain amount of memory this test
044         * is for you. This tests guarantees you that the VM is started with enough
045         * memory (usually option "-Xmx").
046         *
047         * @param required
048         *            the max memory in MB
049         */
050        public static void assertMaxMemory(final int required) {
051                long mem = (Runtime.getRuntime().maxMemory() + 0x7FFFF) / 0x100000;
052                LOG.debug("VM was started with " + mem + " MB max memory.");
053                if (required > mem) {
054                        String hint = (" (use 'java -Xmx" + required + "m ...')");
055                        if (SystemUtils.JAVA_VENDOR.toUpperCase().startsWith("IBM")) {
056                                hint = "";
057                        }
058                        throw new AssertionError(required + " MB max memory required but only " + mem + " MB available" + hint);
059                }
060        }
061
062        /**
063         * If you need a certain amount of free memory you can assert it with this
064         * method here.
065         *
066         * @param required
067         *            the required free memory in MB
068         */
069        public static void assertFreeMemory(final int required) {
070                long free = Runtime.getRuntime().freeMemory() / 0x100000;
071                if (required > free) {
072                        System.gc();
073                }
074                free = (Runtime.getRuntime().freeMemory() + 0x7FFFF) / 0x100000;
075                LOG.debug(free + " MB memory is free.");
076                if (required > free) {
077                        long maxMem = Runtime.getRuntime().maxMemory() / 0x100000 + required + 4 - free;
078                        String hint = " (use 'try -Xmx" + maxMem + "m ...')";
079                        throw new AssertionError(required + " MB free memory required but only " + free + " MB are free" + hint);
080                }
081        }
082
083}