All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.evosuite.runtime.mock.java.util.MockTimer Maven / Gradle / Ivy

There is a newer version: 1.0.6
Show newest version
/**
 * Copyright (C) 2010-2017 Gordon Fraser, Andrea Arcuri and EvoSuite
 * contributors
 *
 * This file is part of EvoSuite.
 *
 * EvoSuite is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3.0 of the License, or
 * (at your option) any later version.
 *
 * EvoSuite is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with EvoSuite. If not, see .
 */
package org.evosuite.runtime.mock.java.util;

import java.util.Date;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;

import org.evosuite.runtime.mock.MockFramework;
import org.evosuite.runtime.mock.OverrideMock;
import org.evosuite.runtime.thread.ThreadCounter;

public class MockTimer extends Timer implements OverrideMock{

	private static final Set instances = new LinkedHashSet<>();  
	
	/**
	 * As interrupting threads might not work on Timer objects, 
	 * explicitly kill all created instances
	 */
	public static synchronized void stopAllTimers(){
		for(Timer timer : instances) {
			try {
				// Since SUT classes may inherit from MockTimer, this code (called from EvoSuite)
				// may lead to SUT exceptions.
				timer.cancel();
			} catch(Throwable t) {
				// Ignore, since this is only part of post-test cleanup
			}
		}
		instances.clear();
	}
	
	private static synchronized void registerTimer(Timer timer){
        if(MockFramework.isEnabled()) {
            try{
                ThreadCounter.getInstance().checkIfCanStartNewThread();
            } catch(RuntimeException e) {
                timer.cancel();
            }
        }
        instances.add(timer);
	}
	
	// ---------  constructors  --------------
	
	public MockTimer() {
		super();
		registerTimer(this);
	}

	public MockTimer(boolean isDaemon) {
		super(isDaemon);
		registerTimer(this);
	}

	public MockTimer(String name) {
		super(name);
		registerTimer(this);
	}

	public MockTimer(String name, boolean isDaemon) {
		super(name,isDaemon);
		registerTimer(this);
	}

	
	// ---------- unchanged methods ----------
	
	@Override
	public void schedule(TimerTask task, long delay) {
		super.schedule(task, delay);
	}

	@Override
	public void schedule(TimerTask task, Date time) {
		super.schedule(task, time);
	}

	@Override
	public void schedule(TimerTask task, long delay, long period) {
		super.schedule(task, delay, period);
	}

	@Override
	public void schedule(TimerTask task, Date firstTime, long period) {
		super.schedule(task, firstTime, period);
	}

	@Override
	public void scheduleAtFixedRate(TimerTask task, long delay, long period) {
		super.scheduleAtFixedRate(task, delay, period);
	}

	@Override
	public void scheduleAtFixedRate(TimerTask task, Date firstTime,
			long period) {
		super.scheduleAtFixedRate(task, firstTime, period);
	}

	@Override
	public void cancel() {
		super.cancel();
	}

	@Override
	public int purge() {
		return super.purge();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy