/* $Id: MoviesTest.java 107 2007-08-08 06:38:06Z bananeweizen $
* GNU GPL Version 2, Copyright (C) 2005 Paul C. Kunysch */
package de.kunysch.localimdb.tests;
import java.io.*;
import java.util.*;
import junit.framework.TestCase;
import de.kunysch.localimdb.Movie;
import de.kunysch.localimdb.Movies;
public class MoviesTest extends TestCase {
public static void main(String[] args) {
junit.swingui.TestRunner.run(MoviesTest.class);
}
Movies moviesDefault;
Movies moviesNowG1;
Movies moviesNowG1H123;
final Date timestamp = new Date();
final static String titleA = "An id Title (2000)";
final static String titleB = "An Id title (2000)";
final static String titleC = "An-id Title!! (2000)";
final static String titleD = "An id Title (2001)";
final static String titleE = "An id Title (2000-2005) (fooo)";
final static String titleF = "";
final static String titleG = "Title G";
final static String titleH = "Title H";
Movie movieOne, movieTwo, movieThree;
protected void setUp() throws Exception {
movieOne = new Movie();
movieOne.setYear(1911);
movieOne.setRating(11, 11111);
movieTwo = new Movie();
movieTwo.setYear(2122);
movieThree = new Movie();
moviesDefault = new Movies();
long[] hashes = new long[1];
List<Movie>[] movieList = new List[1];
hashes[0] = Movies.hashTitle(titleG);
movieList[0] =Arrays.asList(new Movie[] { movieOne });
moviesNowG1 = new Movies(1, timestamp, hashes, movieList);
movieList = new List[2];
hashes = new long[2];
hashes[0] = Movies.hashTitle(titleG);
movieList[0] = Arrays.asList(new Movie[] { movieOne });
hashes[1] = Movies.hashTitle(titleH);
movieList[1] = Arrays.asList(new Movie[] { movieOne, movieTwo, movieThree });
moviesNowG1H123 = new Movies(3, timestamp, hashes, movieList);
}
protected void tearDown() throws Exception {
moviesDefault = moviesNowG1 = moviesNowG1H123 = null;
movieOne = movieTwo = movieThree = null;
}
public final void testHash() {
assertNotSame(Movies.hash(titleA), Movies.hash(titleB));
assertNotSame(Movies.hash(titleA), Movies.hash(titleC));
assertNotSame(Movies.hash(titleA), Movies.hash(titleD));
assertNotSame(Movies.hash(titleA), Movies.hash(titleE));
assertNotSame(Movies.hash(titleA), Movies.hash(titleF));
}
public final void testHashTitle() {
assertEquals(Movies.hashTitle(titleA), Movies.hashTitle(titleA));
assertEquals(Movies.hashTitle(titleA), Movies.hashTitle(titleB));
assertEquals(Movies.hashTitle(titleA), Movies.hashTitle(titleC));
assertNotSame(Movies.hashTitle(titleA), Movies.hashTitle(titleD));
assertNotSame(Movies.hashTitle(titleA), Movies.hashTitle(titleE));
assertNotSame(Movies.hashTitle(titleA), Movies.hashTitle(titleF));
}
/*
* Class under test for void Movies()
*/
public final void testMovies() {
assertTrue(moviesDefault.isEmpty());
assertEquals(0, moviesDefault.getTimestamp().getTime());
assertEquals(0, moviesDefault.findByTitle(titleA).length);
}
/*
* Class under test for void Movies(int, Date, Map)
*/
public final void testMoviesintDateMap() {
assertFalse(moviesNowG1.isEmpty());
assertEquals(timestamp, moviesNowG1.getTimestamp());
assertEquals(1, moviesNowG1.findByTitle(titleG).length);
assertEquals(movieOne, moviesNowG1.findByTitle(titleG)[0]);
assertEquals(0, moviesNowG1.findByTitle(titleH).length);
assertFalse(moviesNowG1H123.isEmpty());
assertEquals(timestamp, moviesNowG1H123.getTimestamp());
assertEquals(1, moviesNowG1H123.findByTitle(titleG).length);
assertEquals(movieOne, moviesNowG1H123.findByTitle(titleG)[0]);
assertEquals(3, moviesNowG1H123.findByTitle(titleH).length);
assertTrue(Arrays.asList(moviesNowG1H123.findByTitle(titleH)).contains(movieOne));
assertTrue(Arrays.asList(moviesNowG1H123.findByTitle(titleH)).contains(movieTwo));
assertTrue(Arrays.asList(moviesNowG1H123.findByTitle(titleH)).contains(movieThree));
}
public final void testFindByTitle() {
assertEquals(1, moviesNowG1.findByTitle(titleG).length);
assertEquals(1, moviesNowG1.findByTitle(titleG.toLowerCase()).length);
assertEquals(1, moviesNowG1.findByTitle(titleG.toUpperCase()).length);
assertEquals(1, moviesNowG1.findByTitle(titleG + " (!!)").length);
assertEquals(0, moviesNowG1.findByTitle("Not " + titleG).length);
}
public final void testGetStats() {
assertEquals(0, moviesDefault.getMovieCount());
assertEquals(0, moviesDefault.getTitleCount());
assertEquals(1, moviesNowG1.getMovieCount());
assertEquals(1, moviesNowG1.getTitleCount());
assertEquals(3, moviesNowG1H123.getMovieCount());
assertEquals(2, moviesNowG1H123.getTitleCount());
}
public final void testGetTimestamp() {
assertEquals(0, moviesDefault.getTimestamp().getTime());
assertEquals(timestamp, moviesNowG1.getTimestamp());
assertEquals(timestamp, moviesNowG1H123.getTimestamp());
moviesNowG1.getTimestamp().setTime(0);
assertEquals(timestamp, moviesNowG1.getTimestamp());
}
public final void testIsEmpty() {
assertTrue(moviesDefault.isEmpty());
assertFalse(moviesNowG1.isEmpty());
assertFalse(moviesNowG1H123.isEmpty());
}
public final void testReadExternal() {
try {
final ByteArrayOutputStream baos = new ByteArrayOutputStream(64);
final ObjectOutput out = new ObjectOutputStream(baos);
moviesDefault.writeExternal(out);
moviesNowG1.writeExternal(out);
moviesNowG1H123.writeExternal(out);
out.close();
final ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
final Movies readMovies = new Movies();
readMovies.readExternal(in);
readMovies.readExternal(in);
readMovies.readExternal(in);
assertFalse(readMovies.isEmpty());
assertEquals(timestamp, readMovies.getTimestamp());
assertEquals(1, readMovies.findByTitle(titleG).length);
assertEquals(movieOne, readMovies.findByTitle(titleG)[0]);
assertEquals(3, readMovies.findByTitle(titleH).length);
} catch (IOException e) {
fail(e.toString());
}
}
}