/*
 * Copyright 2008-2014 by Emeric Vernat
 *
 *     This file is part of Java Melody.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.bull.javamelody;

import java.io.File;
import java.util.HashSet;

import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;

import org.jrobin.core.RrdBackendFactory;
import org.jrobin.core.RrdException;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;

/**
 * Classe utilitaire pour les tests unitaires.
 * @author Emeric Vernat
 */
final class Utils {
	private static final String SYSTEM_ACTIONS_PROPERTY_NAME = Parameters.PARAMETER_SYSTEM_PREFIX
			+ Parameter.SYSTEM_ACTIONS_ENABLED.getCode();

	private Utils() {
		super();
	}

	static void setProperty(Parameter parameter, String value) {
		setProperty(Parameters.PARAMETER_SYSTEM_PREFIX + parameter.getCode(), value);
	}

	static void setProperty(String string, String value) {
		if (value == null) {
			System.getProperties().remove(string);
		} else {
			System.setProperty(string, value);
		}
	}

	static void initialize() {
		for (final Object systemProperty : new HashSet<Object>(System.getProperties().keySet())) {
			if (systemProperty.toString().startsWith(Parameters.PARAMETER_SYSTEM_PREFIX)) {
				System.getProperties().remove(systemProperty.toString());
			}
		}
		JRobin.stop();
		try {
			// shutdown seems needed at the moment in order that this job does not run forever:
			// https://javamelody.ci.cloudbees.com/job/javamelody/
			StdSchedulerFactory.getDefaultScheduler().shutdown();
		} catch (final SchedulerException e) {
			throw new IllegalStateException(e);
		}

		Parameters.initialize((FilterConfig) null);
		Parameters.initialize((ServletContext) null);
		Parameters.initJdbcDriverParameters(null, null);
		// pour avoir les informations sur les connections, l'initialisation de la classe JdbcWrapper
		// doit se faire avec les actions systèmes activées
		System.setProperty(SYSTEM_ACTIONS_PROPERTY_NAME, "true");
		JdbcWrapper.USED_CONNECTION_INFORMATIONS.clear();
		System.getProperties().remove(SYSTEM_ACTIONS_PROPERTY_NAME);
		new File(System.getProperty("java.io.tmpdir")).mkdirs();

		try {
			// we must initialize default factory before creating any rrd
			if (!RrdBackendFactory.getDefaultFactory().getFactoryName()
					.equals(RrdNioBackendFactory.FACTORY_NAME)) {
				RrdBackendFactory.registerAndSetAsDefaultFactory(new RrdNioBackendFactory());
			}
		} catch (final RrdException e) {
			throw new RuntimeException(e);
		}
	}
}

@@ -739,16 +739,39 @@
 ashSet;%0D
+%0Aimport java.util.Map;%0D
 %0A%0D%0Aimpor
@@ -1934,16 +1934,54 @@
 top();%0D%0A
+%09%09if (isQuartzSchedulerStarted()) %7B%0D%0A%09
 %09%09try %7B%0D
@@ -1977,24 +1977,25 @@
 %0D%0A%09%09%09try %7B%0D%0A
+%09
 %09%09%09// shutdo
@@ -2070,16 +2070,17 @@
 rever:%0D%0A
+%09
 %09%09%09// ht
@@ -2133,16 +2133,17 @@
 dy/%0D%0A%09%09%09
+%09
 StdSched
@@ -2187,24 +2187,25 @@
 tdown();%0D%0A%09%09
+%09
 %7D catch (fin
@@ -2224,32 +2224,33 @@
 Exception e) %7B%0D%0A
+%09
 %09%09%09throw new Ill
@@ -2269,24 +2269,30 @@
 eption(e);%0D%0A
+%09%09%09%7D%0D%0A
 %09%09%7D%0D%0A%0D%0A%09%09Par
@@ -3177,20 +3177,20 @@
 on(e);%0D%0A
-
 %09%09%7D%0D%0A%09%7D%0D
 %0A%7D%0D%0A
@@ -3185,12 +3185,303 @@
 %09%09%7D%0D%0A%09%7D%0D
+%0A%0D%0A%09private static boolean isQuartzSchedulerStarted() %7B%0D%0A%09%09final Map%3CThread, StackTraceElement%5B%5D%3E stackTraces = Thread.getAllStackTraces();%0D%0A%09%09for (final Thread thread : stackTraces.keySet()) %7B%0D%0A%09%09%09if (thread.getName().contains(%22Quartz%22)) %7B%0D%0A%09%09%09%09return true;%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%09%09return false;%0D%0A%09%7D%0D
 %0A%7D%0D%0A

optimize unit tests