/* $Id: RatingsParserTest.java 4 2005-02-17 07:42:45Z pck $
* GNU GPL Version 2, Copyright (C) 2005 Paul C. Kunysch */
package de.kunysch.localimdb.tests;
import java.io.*;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.zip.GZIPOutputStream;
import junit.framework.TestCase;
import de.kunysch.localimdb.Movies;
import de.kunysch.localimdb.MoviesBuilder;
import de.kunysch.localimdb.RatingsParser;
public class RatingsParserTest extends TestCase {
public static void main(String[] args) {
junit.swingui.TestRunner.run(RatingsParserTest.class);
}
private static class TempfileRatingsParser extends RatingsParser {
private final File file;
public TempfileRatingsParser(File file, MoviesBuilder builder) {
super(builder);
this.file = file;
}
public String getFilename() {
return file.getName();
}
}
public final void testProcessMatchedLine() {
PrintWriter pw = null;
File file = null;
try {
file = File.createTempFile("unittest-ratings", ".gz");
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new GZIPOutputStream(
new FileOutputStream(file)), Charset.forName("ISO-8859-1"))));
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("PST"));
pw.println("timestamp: " + sdf.format(new Date()));
pw.println("foo bar baz");
pw.println("FOO RATINGS REPORT");
pw.println(" 1111111111 123456 1.0 Not parsed (0000)");
pw.println("");
pw.println("MOVIE RATINGS REPORT");
pw.println(" 1111111111 1234567 1.0 One (1111)");
pw.println(" .........* 5 10.0 \"Ten\" (2222)");
pw.println(" 0262000..0 333 3.3 Third Title, A (3333)");
pw.println("");
pw.println("BAR RATINGS REPORT");
pw.println(" 1111111111 1234567 1.0 Also not parsed (0000)");
pw.close();
final MoviesBuilder builder = new MoviesBuilder();
final RatingsParser parser = new TempfileRatingsParser(file, builder);
final int count = parser.fetch(file.getParentFile().toURL(), null);
assertEquals(3, count);
final Movies movies = builder.getMovies();
assertEquals(3, movies.getTitleCount());
assertEquals(3, movies.getMovieCount());
assertEquals(1, movies.findByTitle("one").length);
assertEquals(1, movies.findByTitle("ten").length);
assertEquals(1, movies.findByTitle("a third title").length);
assertEquals(10, movies.findByTitle("one")[0].getRank());
assertEquals(100, movies.findByTitle("ten")[0].getRank());
assertEquals(33, movies.findByTitle("a third title")[0].getRank());
assertEquals(1234567, movies.findByTitle("one")[0].getVotes());
assertEquals(5, movies.findByTitle("ten")[0].getVotes());
assertEquals(333, movies.findByTitle("a third title")[0].getVotes());
} catch (IOException e) {
throw new RuntimeException(e);
} catch (ParseException e) {
fail(e.getMessage());
} finally {
if (null != pw) pw.close();
if (null != file) file.delete();
}
}
public final void testRatingsParser() {
RatingsParser parser = new RatingsParser(new MoviesBuilder());
assertEquals("ratings.list.gz", parser.getFilename());
}
}