Question: Need to write JUnit Test class for the code below: ******************************************************************************************* import java.util.ArrayList; public class Bibliography { private ArrayList publications; public Bibliography() { publications =
Need to write JUnit Test class for the code below:
*******************************************************************************************
import java.util.ArrayList;
public class Bibliography {
private ArrayList
public Bibliography() {
publications = new ArrayList
}
//Add a new Publication to collection.
public boolean add( Publication pub ) {
boolean success = false;
if ( pub != null && pub.canAdd() ) {
if ( publications.size() > 0 ) {
boolean duplicate = false;
for ( int n = 0; n < publications.size(); n++ ) {
if ( publications.get( n ).equals( pub ) ) {
duplicate = true;
}
}
if ( !duplicate ) {
int count = 0;
while ( !success && count <= publications.size() ) {
if ( count == publications.size() ) {
publications.add( pub );
success = true;
} else {
success = checkAuthor( pub, count );
}
count++;
}
}
} else {
publications.add( pub );
success = true;
}
}
return success;
}
// Check the author of two publication objects
private boolean checkAuthor( Publication pub, int count ) {
boolean success = false;
String pubAuthor = pub.getAuthor();
String nAuthor = publications.get( count ).getAuthor();
if ( pubAuthor.compareTo( nAuthor ) < 0 ) {
publications.add( count, pub );
success = true;
} else if ( pubAuthor.compareTo( nAuthor ) == 0 ) {
success = checkTitle( pub, count );
}
return success;
}
//Check the title of two publication objects
private boolean checkTitle( Publication pub, int count ) {
boolean success = false;
String pubTitle = pub.getTitle();
String nTitle = publications.get( count )
.getTitle();
if ( pubTitle.compareTo( nTitle ) < 0 ) {
publications.add( count, pub );
success = true;
} else if ( pubTitle.compareTo( nTitle ) == 0 ) {
success = checkYear( pub, count );
} else {
publications.add( count + 1, pub );
success = true;
}
return success;
}
//Check the year of two publication objects to alphabetize them.
private boolean checkYear( Publication pub, int count ) {
boolean success = false;
int pubYear = pub.getYear();
int nYear = publications.get( count ).getYear();
if ( pubYear < nYear ) {
publications.add( count, pub );
success = true;
} else {
publications.add( count + 1, pub );
success = true;
}
return success;
}
public boolean delete( int whichOne ) {
boolean success = false;
if ( !publications.isEmpty() ) {
if ( whichOne >= 0 && whichOne < publications.size() ) {
publications.remove( whichOne );
success = true;
}
}
return success;
}
// Get the Publication specified by the parameter whichOne.
public Publication get( int whichOne ) {
if ( whichOne < 0 || whichOne >= publications.size() ) {
return null;
} else {
return publications.get( whichOne );
}
}
public int size() {
return publications.size();
}
//Get the difference between the current Bibliography object and other.
public Bibliography difference( Bibliography other ) {
Bibliography differenceSet = new Bibliography();
if ( publications.size() > 0 ) {
for ( int n = 0; n < publications.size(); n++ ) {
boolean inIncoming = false;
for ( int z = 0; z < other.size(); z++ ) {
if ( publications.get( n ).equals( other.get( z ) ) ) {
inIncoming = true;
}
}
if ( !inIncoming ) {
differenceSet.add( publications.get( n ) );
}
}
}
return differenceSet;
}
// Get the intersection between the current Bibliography object and other.
public Bibliography intersection( Bibliography other ) {
Bibliography intersectionSet = new Bibliography();
for ( int n = 0; n < publications.size(); n++ ) {
for ( int z = 0; z < other.size(); z++ ) {
if ( publications.get( n ).equals( other.get( z ) ) ) {
intersectionSet.add( publications.get( n ) );
}
}
}
return intersectionSet;
}
// Determine whether other is a subset of the current Bibliography object.
public boolean subset( Bibliography other ) {
boolean success = true;
for ( int n = 0; n < other.size(); n++ ) {
boolean temp = false;
for ( int z = 0; z < publications.size(); z++ ) {
if ( other.get( n ).equals( publications.get( z ) ) ) {
temp = true;
}
}
if ( !temp ) {
success = false;
}
}
return success;
}
// Get the union of other and the current Bibliography object.
public Bibliography union( Bibliography other ) {
Bibliography unionSet = new Bibliography();
for ( int n = 0; n < publications.size(); n++ ) {
unionSet.add( publications.get( n ) );
}
for ( int n = 0; n < other.size(); n++ ) {
unionSet.add( other.get( n ) );
}
return unionSet;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
