Package de.kunysch.localimdb.tests

Source Code of de.kunysch.localimdb.tests.MovieTest

/* $Id: MovieTest.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.security.InvalidParameterException;
import junit.framework.TestCase;
import de.kunysch.localimdb.Movie;

public class MovieTest extends TestCase {
  public static void main(String[] args) {
    junit.swingui.TestRunner.run(MovieTest.class);
  }

  private final Movie copyMovie(Movie movie) {
    Movie copy = new Movie();
    copy.setYear(movie.getYear());
    copy.setRating(movie.getRank(), movie.getVotes());
    return copy;
  }

  private Movie movieDefault, movieA, movieA2, movieB;

  protected void setUp() throws Exception {
    movieDefault = new Movie();
    movieA = new Movie();
    movieA.setYear(1934);
    movieA.setRating(100, 12345);
    movieA2 = copyMovie(movieA);
    movieB = copyMovie(movieA);
    movieB.setYear(movieB.getYear() - 1);
  }

  protected void tearDown() throws Exception {
    movieDefault = movieA = movieA2 = movieB = null;
  }

  public final void testHashCode() {
    assertTrue(movieDefault.hashCode() == movieDefault.hashCode());
    assertTrue(movieA.hashCode() == movieA2.hashCode());
    assertTrue(movieA2.hashCode() == movieA2.hashCode());
  }

  public final void testCompareTo() {
    assertTrue(0 == movieDefault.compareTo(movieDefault));
    assertTrue(0 != movieDefault.compareTo(movieA));
    assertTrue(0 != movieDefault.compareTo(movieA2));
    assertTrue(0 != movieDefault.compareTo(movieB));
    assertTrue(0 != movieA.compareTo(movieDefault));
    assertTrue(0 == movieA.compareTo(movieA));
    assertTrue(0 == movieA.compareTo(movieA2));
    assertTrue(0 != movieA.compareTo(movieB));
    assertTrue(0 != movieA2.compareTo(movieDefault));
    assertTrue(0 == movieA2.compareTo(movieA));
    assertTrue(0 == movieA2.compareTo(movieA2));
    assertTrue(0 != movieA2.compareTo(movieB));
    assertTrue(0 != movieB.compareTo(movieDefault));
    assertTrue(0 != movieB.compareTo(movieA));
    assertTrue(0 != movieB.compareTo(movieA2));
    assertTrue(0 == movieB.compareTo(movieB));
    assertTrue(movieDefault.compareTo(movieA) == -1 * movieA.compareTo(movieDefault));
    assertTrue(movieDefault.compareTo(movieA2) == -1 * movieA2.compareTo(movieDefault));
    assertTrue(movieDefault.compareTo(movieB) == -1 * movieB.compareTo(movieDefault));
    assertTrue(movieA.compareTo(movieB) == -1 * movieB.compareTo(movieA));
    assertTrue(movieA2.compareTo(movieB) == -1 * movieB.compareTo(movieA2));
    try {
      movieA.compareTo(null);
      fail("compareTo(null) didn't throw a NullPointerException");
    } catch (NullPointerException e) {
    }
    try {
      movieA.compareTo("String");
      fail("compareTo(\"String\") didn't throw a ClassCastException");
    } catch (ClassCastException e) {
    }
  }

  /*
   * Class under test for boolean equals(Object)
   */
  public final void testEqualsObject() {
    assertTrue(movieDefault.equals(movieDefault));
    assertTrue(!movieDefault.equals(movieA));
    assertTrue(!movieDefault.equals(movieA2));
    assertTrue(!movieDefault.equals(movieB));
    assertTrue(!movieDefault.equals(null));
    assertTrue(!movieA.equals(movieDefault));
    assertTrue(movieA.equals(movieA));
    assertTrue(movieA.equals(movieA2));
    assertTrue(!movieA.equals(movieB));
    assertTrue(!movieA.equals(null));
    assertTrue(!movieA2.equals(movieDefault));
    assertTrue(movieA2.equals(movieA));
    assertTrue(movieA2.equals(movieA2));
    assertTrue(!movieA2.equals(movieB));
    assertTrue(!movieA2.equals(null));
    assertTrue(!movieB.equals(movieDefault));
    assertTrue(!movieB.equals(movieA));
    assertTrue(!movieB.equals(movieA2));
    assertTrue(movieB.equals(movieB));
    assertTrue(!movieB.equals(null));
  }

  public final void testGetSetRank() {
    assertEquals(0, movieDefault.getRank());
    for (int i=10; i<=100; ++i) {
      movieDefault.setRating(i, 12345);
      assertEquals(i, movieDefault.getRank());
    }
    movieDefault.setRating(0, 0);
    assertEquals(0, movieDefault.getRank());
    int[] invalid = new int[] { -1, 1, 2, 8, 9, 101 };
    for (int i=0; i<invalid.length; ++i) {
      try {
        movieDefault.setRating(invalid[i], 12345);
        fail("invalid rank accepted");
      } catch (InvalidParameterException e) {
      }
    }
    try {
      movieDefault.setRating(0, 12345);
      fail("invalid rank/votes combination accepted");
    } catch (InvalidParameterException e) {
    }
  }

  public final void testGetSetVotes() {
    assertEquals(0, movieDefault.getVotes());
    for (int i=1; i<=130000; i=i*2+7) {
      movieDefault.setRating(100, i);
      assertEquals(i, movieDefault.getVotes());
    }
    movieDefault.setRating(0, 0);
    assertEquals(0, movieDefault.getVotes());
    try {
      movieDefault.setRating(100, -1);
      fail("invalid votes accepted");
    } catch (InvalidParameterException e) {
    }
    try {
      movieDefault.setRating(100, 0);
      fail("invalid rank/votes combination accepted");
    } catch (InvalidParameterException e) {
    }
  }

  public final void testGetSetYear() {
    assertEquals(0, movieDefault.getYear());
    for (int i=1901; i<=2050; ++i) {
      movieDefault.setYear(i);
      assertEquals(i, movieDefault.getYear());
    }
    movieDefault.setYear(0);
    assertEquals(0, movieDefault.getYear());
  }

  public final void testReadWriteExternal() {
    try {
      final ByteArrayOutputStream baos = new ByteArrayOutputStream(64);
      final ObjectOutput out = new ObjectOutputStream(baos);
      movieDefault.writeExternal(out);
      movieA.writeExternal(out);
      movieA2.writeExternal(out);
      movieB.writeExternal(out);
      out.close();
      final ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
      final Movie readMovie = new Movie();
      readMovie.readExternal(in);
      assertEquals(movieDefault, readMovie);
      readMovie.readExternal(in);
      assertEquals(movieA, readMovie);
      readMovie.readExternal(in);
      assertEquals(movieA2, readMovie);
      readMovie.readExternal(in);
      assertEquals(movieB, readMovie);
    } catch (IOException e) {
      fail(e.toString());
    }
  }
}
TOP

Related Classes of de.kunysch.localimdb.tests.MovieTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.