I need to design a JUnit test that will test the functionality of the file Book.Java. I have included the test file that I have made thus far, but it is coming back as failing the necessary tests, and I am not sure as to why. This is for a Test Driven Development. I think I have the implementation mostly correct, but I need to know where I am lacking. Also, I am not sure how he is going to implement the boolean, so it leaves me unsure as to how to set up the test. I have completed parts of the program so that I can test my code, and most of it is working. I just need some advice on where I need to go from here.
Book.java
package edu.odu.cs.cs350;
import java.util.Iterator;
/**
* A book object as it might be recorded in a publisher's catalog or a
library.
*
* <p>
* This is not a capture of the contents of the book, but of the metadata
that
* describes the book: authors, title, etc.
*
* @author zeil
*
*/
public class Book implements Cloneable, Iterable<Author> {
private String title1;
String publisher1;
String isbn1;
/**
* Create a "blank" book with empty strings for title, publisher, ISBN and
* an empty (zero-length) list of authors.
*/
public Book() {
title1 = "";
publisher1 = "";
isbn1 = "";
}
/**
* Create a new book.
*
* @param title
* title of the book
* @param publisher
* publisher of the book.
* @param isbn
* ISBN identifier for the book.
* @param authors
* list of authors for this book.
*/
public Book(String title, String publisher, String isbn, Author[] authors) {
// TODO
}
/**
* Get the title of this book.
*
* @return the title
*/
public String getTitle() {
return this.title1;
}
/**
* Set the title of this book.
*
* @param title
* the title to set
*/
public void setTitle(String title) {
this.title1 = title;
}
/**
* Get the ISBN of this book.
*
* @return the ISBN
*/
public String getISBN() {
return this.isbn1;
}
/**
* Set the ISBN of this book.
*
* @param isbn
* the isbn to set
*/
public void setISBN(String isbn) {
this.isbn1 = isbn;
}
/**
* Get the publisher of this book.
*
* @return the publisher
*/
public String getPublisher() {
// TODO
return this.publisher1;
}
/**
* Set the publisher of this book.
*
* @param publisher
* the publisher to set
*/
public void setPublisher(String publisher) {
this.publisher1 = publisher;
}
/**
* How many authors does this book have?
*
* @return number of authors
*/
public int numAuthors() {
// TODO
return 0;
}
/**
* Add an author to the end of the books's list (if that author is not
* already in there).
*
* @param au
* author to be added
*/
public void addAuthor(Author au) {
// TODO
}
/**
* Render the book as a string in a format guaranteed to contain all fields.
*/
public String toString() {
// TODO
return "";
}
// Comparison and hashing
/**
* Compares two books for equality. They are considered equal if they have
* the same ISBN.
*
* @param obj
* object to be compared for equality with this duration
* @return <tt>true</tt> if the specified object is equal to this one
*/
public boolean equals(Object obj) {
// TODO
return false;
}
/**
* Returns the hash code value for this object.
*
* @return the hash code value for this book
*/
public int hashCode() {
return (2 * this.title1.length() + 4 * this.isbn1.length() + 8 * this.publisher1.length());
}
/**
* Return a (deep) copy of this object.
*/
@Override
public Object clone() {
// TODO
return null;
}
/**
* Provide access to the list of authors. e.g., Book book = new Book(...);
* for (Author au: book) { doSomethingWithAuthor (au); }
*
* @return iterator over the authors.
*/
public Iterator<Author> iterator() {
// TODO
return null;
}
}
Author.java
package edu.odu.cs.cs350;
/**
* An author of a book. Currently, this contains only the author's name.
*
* @author zeil
*
*/
public class Author implements Cloneable, Comparable<Author> {
/**
* The surname ("family name") of this author.
*/
private String surname;
/**
* The given name ("personal name" or "first name") of the author.
*/
private String givenName;
/**
* Create an author.
*
* @param givenName
* the personal name of the author
* @param surname
* the family name of the author
*/
public Author(String givenName, String surname) {
this.givenName = givenName;
this.surname = surname;
}
/**
* Compare two authors for equality.
*
* @param obj
* another author
* @return true ff they have the same name
*/
public boolean equals(Object obj) {
if (obj instanceof Author) {
Author au = (Author) obj;
return givenName.equals(au.givenName) && surname.equals(au.surname);
} else {
return false;
}
}
/**
* Get the name of the author in a form suitable for sorting. surname,
* givenName
*
* @return sortable name of the author
*/
public String getSortingName() {
return surname + ", " + givenName;
}
/**
* Return the author's name in conventional form. givenName surname
*/
public String toString() {
return givenName + " " + surname;
}
/**
* Return the surname of this author
*
* @return the surname
*/
public String getSurname() {
return surname;
}
/**
* Return the given name of this author
*
* @return the given name
*/
public String getGivenName() {
return givenName;
}
/**
* Compare this author to another, returning a negative value if this author
* should follow the other in a sorted list, zero if they are equal, and a
* positive value if this author should precede the other in a sorted list.
*
* @param au
* the other author to be compared against.
* @return number whose sign indicates the comparison result
*/
public int compareTo(Author au) {
return getSortingName().compareTo(au.getSortingName());
}
}
TestBook.java
package edu.odu.cs.cs350;
import static org.junit.Assert.*;
import org.junit.Test;
public class TestBook {
@Test
public void testSetTitle()
{
Book title = new Book();
int oldHashCode = title.hashCode();
title.setTitle("abcde");
assertEquals( "abcde", title.getTitle());
assertEquals("", title.getISBN());
assertEquals("", title.getPublisher());
assertNotEquals(oldHashCode, title.hashCode());
}
@Test
public void testSetISBN()
{
Book ISBN = new Book();
int oldHashCode = ISBN.hashCode();
ISBN.setISBN("123-456-789");
assertEquals("123-456-789", ISBN.getISBN());
assertEquals("", ISBN.getTitle());
assertEquals("", ISBN.getPublisher());
assertNotEquals(oldHashCode, ISBN.hashCode());
}
@Test
public void testDuplicateISBN()
{
Book newBook = new Book();
int oldHashCode = newBook.hashCode();
newBook.setISBN("123-456-789");
Book Repeat = new Book();
Repeat.setISBN("123-456-789");
assertNotEquals(oldHashCode, newBook.hashCode());
assertNotEquals(Repeat.getISBN(), newBook.getISBN());
}
@Test
public void testSetPublisher()
{
Book Publisher = new Book();
int oldHashCode = Publisher.hashCode();
Publisher.setPublisher("abcde");
assertEquals("abcde", Publisher.getPublisher());
assertEquals("", Publisher.getTitle());
assertEquals("", Publisher.getISBN());
assertNotEquals(oldHashCode, Publisher.hashCode());
}
@Test
public void testAddAuthor()
{
Author Auth = new Author("abc", "def");
Book test = new Book();
test.addAuthor(Auth);
assertEquals("abc", Auth.getGivenName());
assertEquals("def", Auth.getSurname());
assertEquals("def, abc", Auth.getSortingName());
}
}
Aucun commentaire:
Enregistrer un commentaire