/*
 * (C) Copyright 2014 Kurento (http://kurento.org/)
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * This library 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 General Public License for more details.
 *
 */
package org.kurento.test.base;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.junit.internal.runners.model.MultipleFailureException;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.kurento.test.services.KurentoServicesTestHelper;

/**
 * Utility class to print KMS log when a test fails.
 * 
 * @author Boni Garcia (bgarcia@gsyc.es)
 * @since 4.2.3
 */
@SuppressWarnings("deprecation")
public class KmsLogOnFailure extends TestWatcher {

	@Override
	protected void succeeded(Description description) {
		super.succeeded(description);

		// Delete logs
		File folder = new File(KurentoServicesTestHelper.getTestDir() + "/"
				+ KurentoServicesTestHelper.getTestCaseName());

		final File[] files = folder.listFiles(new FilenameFilter() {
			@Override
			public boolean accept(final File dir, final String name) {
				return name.matches(".*\\.log");
			}
		});
		for (final File file : files) {
			if (!file.delete()) {
				System.err.println("Can't remove " + file.getAbsolutePath());
			}
		}
	}

	@Override
	protected void failed(Throwable e, Description description) {

		if (KurentoServicesTestHelper.printKmsLog()) {
			List<File> logFiles = KurentoServicesTestHelper.getServerLogFiles();
			if (logFiles != null) {
				final String separator = "******************************************************************************";
				for (File logFile : logFiles) {
					if (logFile != null && logFile.exists()) {
						System.err.println(separator);
						System.err.println(
								"Log file path: " + logFile.getAbsolutePath());
						System.err.println("Content:");

						try {
							for (String line : FileUtils.readLines(logFile)) {
								System.err.println(line);
							}
						} catch (IOException e1) {
							System.err
									.println("Error reading lines in log file");
							e1.printStackTrace();
						}
						System.err.println(separator);
					}
				}
			}
		}

	}

	@SuppressWarnings({ "unused" })
	private void showException(Throwable e) {
		if (e instanceof org.junit.internal.runners.model.MultipleFailureException) {

			MultipleFailureException multipleEx = (MultipleFailureException) e;
			for (Throwable failure : multipleEx.getFailures()) {
				failure.printStackTrace();
			}

		} else {
			e.printStackTrace();
		}
	}

}

@@ -1306,16 +1306,21 @@
 ew File(
+%0A%09%09%09%09
 KurentoS
@@ -1355,21 +1355,17 @@
 () + %22/%22
-%0A%09%09%09%09
+ 
 + Kurent
@@ -1591,16 +1591,40 @@
 %7D%0A%09%09%7D);%0A
+%09%09if (files != null) %7B%0A%09
 %09%09for (f
@@ -1648,24 +1648,25 @@
 files) %7B%0A%09%09%09
+%09
 if (!file.de
@@ -1671,24 +1671,25 @@
 delete()) %7B%0A
+%09
 %09%09%09%09System.e
@@ -1742,16 +1742,22 @@
 ath());%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D
@@ -2241,25 +2241,16 @@
 println(
-%0A%09%09%09%09%09%09%09%09
 %22Log fil
@@ -2470,24 +2470,24 @@
 ption e1) %7B%0A
+
 %09%09%09%09%09%09%09Syste
@@ -2495,18 +2495,8 @@
 .err
-%0A%09%09%09%09%09%09%09%09%09
 .pri

Check that kms logs files is not null at the end of tests