diff --git a/pom.xml b/pom.xml index 28789b0b0..7b74cdaf1 100644 --- a/pom.xml +++ b/pom.xml @@ -76,6 +76,11 @@ + + commons-io + commons-io + 2.4 + junit diff --git a/src/test/java/com/omertron/themoviedbapi/TestLogger.java b/src/test/java/com/omertron/themoviedbapi/TestLogger.java index 0c605857f..439b89f27 100644 --- a/src/test/java/com/omertron/themoviedbapi/TestLogger.java +++ b/src/test/java/com/omertron/themoviedbapi/TestLogger.java @@ -20,9 +20,17 @@ package com.omertron.themoviedbapi; import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; +import java.util.Properties; +import java.util.logging.Level; import java.util.logging.LogManager; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -69,4 +77,68 @@ public class TestLogger { public static boolean Configure() { return Configure("ALL"); } + + /** + * Test if a filename exists + * + * @param filename + * @return + */ + public static boolean fileExists(String filename) { + return (new File(filename)).exists(); + } + + /** + * Load properties from a file + * + * @param props + * @param f + */ + public static void loadProperties(Properties props, File f) { + InputStream is = null; + try { + is = new FileInputStream(f); + } catch (Exception ex) { + LOG.warn("Failed to load properties file", ex); + } finally { + if (is != null) { + try { + is.close(); + } catch (IOException ex) { + LOG.warn("Failed to close properties file", ex); + } + } + } + } + + /** + * Save properties to a file + * + * @param props + * @param propertyFile + * @param headerText + */ + public static void saveProperties(Properties props, File propertyFile, String headerText) { + OutputStream out = null; + + try { + out = new FileOutputStream(propertyFile); + if (StringUtils.isNotBlank(headerText)) { + props.store(out, headerText); + } + } catch (FileNotFoundException ex) { + LOG.warn("Failed to find properties file", ex); + } catch (IOException ex) { + LOG.warn("Failed to read properties file", ex); + } finally { + if (out != null) { + try { + out.flush(); + out.close(); + } catch (IOException ex) { + LOG.warn("Failed to close properties file", ex); + } + } + } + } } diff --git a/src/test/java/com/omertron/themoviedbapi/TheMovieDbApiTest.java b/src/test/java/com/omertron/themoviedbapi/TheMovieDbApiTest.java index 51875dba3..32e0c5e16 100644 --- a/src/test/java/com/omertron/themoviedbapi/TheMovieDbApiTest.java +++ b/src/test/java/com/omertron/themoviedbapi/TheMovieDbApiTest.java @@ -67,6 +67,9 @@ import com.omertron.themoviedbapi.model.Trailer; import com.omertron.themoviedbapi.model.Translation; import com.omertron.themoviedbapi.results.TmdbResultsList; import com.omertron.themoviedbapi.results.TmdbResultsMap; +import java.io.File; +import java.util.Properties; +import org.apache.commons.lang3.math.NumberUtils; /** * Test cases for TheMovieDbApi API @@ -78,7 +81,10 @@ public class TheMovieDbApiTest { // Logger private static final Logger LOG = LoggerFactory.getLogger(TheMovieDbApiTest.class); // API Key - private static final String API_KEY = "INSERT_YOUR_KEY_HERE"; + private static final String PROP_FIlENAME = "testing.properties"; + private static String API_KEY; + private static String SESSION_ID_APITESTS; + private static int ACCOUNT_ID_APITESTS; private static TheMovieDbApi tmdb; // Test data private static final int ID_MOVIE_BLADE_RUNNER = 78; @@ -94,8 +100,6 @@ public class TheMovieDbApiTest { private static final String LANGUAGE_ENGLISH = "en"; private static final String LANGUAGE_RUSSIAN = "ru"; // session and account id of test users named 'apitests' - private static final String SESSION_ID_APITESTS = "INSERT_YOUR_SESSION_ID_HERE"; - private static final int ACCOUNT_ID_APITESTS = 6065849; public TheMovieDbApiTest() throws MovieDbException { } @@ -104,6 +108,28 @@ public class TheMovieDbApiTest { public static void setUpClass() throws Exception { TestLogger.Configure(); tmdb = new TheMovieDbApi(API_KEY); + + Properties props = new Properties(); + File f = new File(PROP_FIlENAME); + if (f.exists()) { + LOG.info("Loading properties from '{}'", PROP_FIlENAME); + TestLogger.loadProperties(props, f); + + API_KEY = props.getProperty("API_Key"); + SESSION_ID_APITESTS = props.getProperty("Account_ID"); + ACCOUNT_ID_APITESTS = NumberUtils.toInt(props.getProperty("Session_ID"), 0); + + } else { + LOG.info("Property file '{}' not found, creating dummy file.", PROP_FIlENAME); + + props.setProperty("API_Key", "INSERT_YOUR_KEY_HERE"); + props.setProperty("Account_ID", "ACCOUNT ID FOR SESSION TESTS"); + props.setProperty("Session_ID", "INSERT_YOUR_SESSION_ID_HERE"); + + TestLogger.saveProperties(props, f, "Properties file for tests"); + fail("Failed to get key information from properties file '" + PROP_FIlENAME + "'"); + } + } @AfterClass diff --git a/testing.properties b/testing.properties new file mode 100644 index 000000000..ac548e1cb --- /dev/null +++ b/testing.properties @@ -0,0 +1,5 @@ +#Properties file for tests +#Fill in the details here +Session_ID=INSERT_YOUR_SESSION_ID_HERE +Account_ID=ACCOUNT ID FOR SESSION TESTS +API_Key=INSERT_YOUR_KEY_HERE