package de.linwave.junit.querybyexample;
import org.odbms.ObjectContainer;
import org.odbms.ObjectSet;
import de.linwave.gtm.GTM;
import de.linwave.junit.AbstractTestCase;
import de.linwave.music.Album;
import de.linwave.music.Artist;
import de.linwave.music.Band;
import de.linwave.music.Track;
import freedb.Disc;
public class QueryByExample extends AbstractTestCase
{
private static ObjectContainer gtm = GTM.getInstance();
private static int artistCnt;
private static int bandCnt;
private static int albumCnt;
private static int trackCnt;
public void testAllAritsts()
{
ObjectSet<Artist> artists = gtm.queryByExample(Artist.class);
for (Artist artist : artists) {
artistCnt++;
}
assertEquals(50, artistCnt);
}
public void testBands()
{
ObjectSet<Band> bands = gtm.queryByExample(Band.class);
for (Band band : bands) {
bandCnt++;
}
assertEquals(10, bandCnt);
}
public void testAlbums()
{
ObjectSet<Album> albums = gtm.queryByExample(Album.class);
for (Album album : albums) {
albumCnt++;
}
assertEquals(40, albumCnt);
}
public void testTracks()
{
ObjectSet<Track> tracks = gtm.queryByExample(Track.class);
for (Track track : tracks) {
trackCnt++;
}
assertEquals(160, trackCnt);
}
public void testRecalculateAll()
{
int cntArtist = 0;
int cntAlbum = 0;
int cntTrack = 0;
ObjectSet<Artist> artists = gtm.queryByExample(Artist.class);
for (Artist artist : artists) {
cntArtist++;
for (Album album : artist.getAlbums()) {
cntAlbum++;
for (Track track : album.getTracks()) {
cntTrack++;
}
}
}
assertEquals("Invalid artist count", cntArtist, artistCnt);
assertEquals("Invalid album count", cntAlbum, albumCnt);
assertEquals("Invalid track count", cntTrack, trackCnt);
}
public void testArtistJohnLennon()
{
int cnt = 0;
ObjectSet<Artist> artists = gtm.queryByExample(new Artist("John Lennon"), 1);
for (Artist artist : artists) {
if (!artist.getName().equals("John Lennon"))
fail("Invalid artist. Expected 'John Lennon' but got '" + artist.getName() + "'");
cnt++;
}
assertEquals(10, cnt);
}
public void testAlbumYelloSubmarine()
{
int cnt = 0;
ObjectSet<Album> albums = gtm.queryByExample(new Album("Yellow Submarine"), 1);
for (Album album : albums) {
if (!album.getName().equals("Yellow Submarine"))
fail("Invalid album. Expected 'Yellow Submarin' but got '" + album.getName() + "'");
cnt++;
}
assertEquals(10, cnt);
}
public void testAlbumYelloSubmarine1970()
{
int cnt = 0;
ObjectSet<Album> albums = gtm.queryByExample(new Album("Yellow Submarine", 1970));
for (Album album : albums) {
if (!album.getName().equals("Yellow Submarine"))
fail("Invalid album. Expected 'Yellow Submarin' but got '" + album.getName() + "'");
if (album.getYear() != 1970)
fail("Invalid year. Expected 1970 but got " + album.getYear());
cnt++;
}
assertEquals(10, cnt);
}
public void testAlbumYear1969()
{
int cnt = 0;
ObjectSet<Album> albums = gtm.queryByExample(new Album(null, 1969), 1);
for (Album album : albums) {
if (album.getYear() != 1969)
fail("Invalid year. Expected 1969 but got " + album.getYear());
cnt++;
}
assertEquals(10, cnt);
}
public void testDiscsStartsWithBeatles1965()
{
int cnt = 0;
Disc d = new Disc();
d.setDTITLE("The Beatles*");
ObjectSet<Disc> discs = gtm.queryByExample(d);
for (Disc disc : discs) {
if (!disc.getDTITLE().startsWith("The Beatles"))
fail("Invalid DTITLE. Expected starts with 'The Beatles' but got " + disc.getDTITLE());
cnt++;
}
assertEquals(11, cnt);
}
public void testDiscsEndsWithFLAC()
{
int cnt = 0;
Disc d = new Disc();
d.setDTITLE("*FLAC");
ObjectSet<Disc> discs = gtm.queryByExample(d);
for (Disc disc : discs) {
if (!disc.getDTITLE().endsWith("FLAC"))
fail("Invalid DTITLE. Expected ends with 'FLAC' but got " + disc.getDTITLE());
cnt++;
}
assertEquals(36, cnt);
}
public void testDiscsContainsBeatles()
{
int cnt = 0;
Disc d = new Disc();
d.setDTITLE("*Beatles*");
ObjectSet<Disc> discs = gtm.queryByExample(d);
for (Disc disc : discs) {
if (!disc.getDTITLE().contains("Beatles"))
fail("Invalid DTITLE. Expected contains 'Beatles' but got " + disc.getDTITLE());
cnt++;
}
assertEquals(12, cnt);
}
public static void main(String[] args)
{
long t1 = System.currentTimeMillis();
QueryByExample test = new QueryByExample();
test.testAllAritsts();
test.testBands();
test.testAlbums();
test.testTracks();
test.testRecalculateAll();
test.testArtistJohnLennon();
test.testAlbumYelloSubmarine();
test.testAlbumYelloSubmarine1970();
test.testAlbumYear1969();
//
test.testDiscsStartsWithBeatles1965();
test.testDiscsEndsWithFLAC();
test.testDiscsContainsBeatles();
}
}