From e17db7a15b57fc99cbeaa13178c9b0476879c337 Mon Sep 17 00:00:00 2001 From: Stuart Boston Date: Mon, 25 Feb 2013 12:56:09 +0000 Subject: [PATCH] Tidied up test class --- .../omertron/themoviedbapi/TestLogger.java | 72 +++++++++++++++++++ .../themoviedbapi/TheMovieDbApiTest.java | 11 ++- 2 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 src/test/java/com/omertron/themoviedbapi/TestLogger.java diff --git a/src/test/java/com/omertron/themoviedbapi/TestLogger.java b/src/test/java/com/omertron/themoviedbapi/TestLogger.java new file mode 100644 index 000000000..bd7a4cc5e --- /dev/null +++ b/src/test/java/com/omertron/themoviedbapi/TestLogger.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2004-2013 Stuart Boston + * + * This file is part of the FanartTV API. + * + * The FanartTV API is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * The FanartTV API 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the FanartTV API. If not, see . + * + */ +package com.omertron.themoviedbapi; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.logging.LogManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class TestLogger { + + private static final Logger LOG = LoggerFactory.getLogger(TestLogger.class); + private static final String CRLF = "\n"; + + private TestLogger() { + throw new UnsupportedOperationException("Class can not be instantiated"); + } + + /** + * Configure the logger with a simple in-memory file for the required log level + * + * @param level The logging level required + * @return True if successful + */ + public static boolean Configure(String level) { + StringBuilder config = new StringBuilder("handlers = java.util.logging.ConsoleHandler\n"); + config.append(".level = ").append(level).append(CRLF); + config.append("java.util.logging.ConsoleHandler.level = ").append(level).append(CRLF); + // Only works with Java 7 or later + config.append("java.util.logging.SimpleFormatter.format = [%1$tc %4$s] %2$s - %5$s %6$s%n").append(CRLF); + // Exclude http logging + config.append("sun.net.www.protocol.http.HttpURLConnection.level = OFF").append(CRLF); + + InputStream ins = new ByteArrayInputStream(config.toString().getBytes()); + try { + LogManager.getLogManager().readConfiguration(ins); + } catch (IOException e) { + LOG.warn("Failed to configure log manager due to an IO problem", e); + return Boolean.FALSE; + } + LOG.debug("Logger initialized to '{}' level", level); + return Boolean.TRUE; + } + + /** + * Set the logging level to "ALL" + * + * @return True if successful + */ + public static boolean Configure() { + return Configure("ALL"); + } +} diff --git a/src/test/java/com/omertron/themoviedbapi/TheMovieDbApiTest.java b/src/test/java/com/omertron/themoviedbapi/TheMovieDbApiTest.java index 8f1860790..c39f911b0 100644 --- a/src/test/java/com/omertron/themoviedbapi/TheMovieDbApiTest.java +++ b/src/test/java/com/omertron/themoviedbapi/TheMovieDbApiTest.java @@ -42,11 +42,11 @@ import com.omertron.themoviedbapi.model.Translation; import java.io.IOException; import java.util.Collections; import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; import org.apache.commons.lang3.StringUtils; import org.junit.*; import static org.junit.Assert.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Test cases for TheMovieDbApi API @@ -56,7 +56,7 @@ import static org.junit.Assert.*; public class TheMovieDbApiTest { // Logger - private static final Logger LOG = Logger.getLogger(TheMovieDbApiTest.class.getSimpleName()); + private static final Logger LOG = LoggerFactory.getLogger(TheMovieDbApiTest.class); // API Key private static final String API_KEY = "5a1a77e2eba8984804586122754f969f"; private static TheMovieDbApi tmdb; @@ -78,9 +78,8 @@ public class TheMovieDbApiTest { @BeforeClass public static void setUpClass() throws Exception { - // Set the LOG level to ALL - LOG.setLevel(Level.ALL); tmdb = new TheMovieDbApi(API_KEY); + TestLogger.Configure(); } @AfterClass @@ -548,7 +547,7 @@ public class TheMovieDbApiTest { List movieList = tmdb.getPopularMovieList(LANGUAGE_DEFAULT, 0); for (MovieDb movie : movieList) { results = tmdb.getMovieChanges(movie.getId(), startDate, endDate); - LOG.log(Level.INFO, "{0} has {1} changes.", new Object[]{movie.getTitle(), results.size()}); + LOG.info("{} has {} changes.", new Object[]{movie.getTitle(), results.size()}); } assertNotNull("No results found", results);