/** Tests whether the song entities have been fully initialized. */
 @Test
 public void testFetchSongsInitialized() {
   persistTestSongs();
   SyncCommandTestImpl cmd = createCommand(null);
   List<SongEntity> songs = cmd.fetchSongsToSync(jpaHelper.getEM());
   jpaHelper.closeEM();
   for (SongEntity song : songs) {
     assertNotNull("No artist", song.getArtist());
     assertNotNull("No album", song.getAlbum());
   }
 }
  /** Tests a sync operation if no songs can be found. */
  @Test
  public void testProduceResultsNoSongs() {
    initMocks();
    List<SongEntity> songs = Collections.emptyList();
    List<Command> syncCmds = Collections.emptyList();
    controller.startSynchronization(0);
    Command endCmd = EasyMock.createMock(Command.class);
    queue.execute(endCmd);
    EasyMock.replay(endCmd, observer, controller, queue);
    SyncCommandTestImpl cmd =
        new SyncCommandTestImpl(init, observer, controller, queue, null) {
          @Override
          protected EntityManager getEntityManager() {
            return jpaHelper.getEM();
          }

          @Override
          OAuthTemplate createOAuthTemplate() {
            throw new UnsupportedOperationException("Unexpected method call!");
          }
        };
    cmd.installMockEndCommand(endCmd);
    cmd.installMockEntities(songs, syncCmds);
    cmd.produceResults(jpaHelper.getEM());
    EasyMock.verify(endCmd, observer, controller, queue);
  }
 /** Tests whether a limit is taken into account when retrieving songs. */
 @Test
 public void testFetchSongsWithLimit() {
   persistTestSongs();
   SyncCommandTestImpl cmd = createCommand(SONG_COUNT - 1);
   List<SongEntity> songs = cmd.fetchSongsToSync(jpaHelper.getEM());
   assertEquals("Wrong number of songs", SONG_COUNT - 1, songs.size());
   for (SongEntity song : songs) {
     assertTrue("No play count: " + song, song.getCurrentPlayCount() > 0);
   }
 }
 /** Tests whether all songs that need to be synchronized are found. */
 @Test
 public void testFetchSongsToSyncNoLimit() {
   persistTestSongs();
   SyncCommandTestImpl cmd = createCommand(null);
   List<SongEntity> songs = cmd.fetchSongsToSync(jpaHelper.getEM());
   assertEquals("Wrong number of songs", SONG_COUNT, songs.size());
   for (int i = 1; i <= SONG_COUNT; i++) {
     String songName = SONG_NAME + i;
     boolean found = false;
     for (SongEntity song : songs) {
       if (songName.equals(song.getName())) {
         found = true;
         break;
       }
     }
     assertTrue("Song not found: " + i, found);
   }
 }
 /**
  * Stores the test songs in the database. Also some other song entities are persisted with no
  * current play count.
  */
 private void persistTestSongs() {
   jpaHelper.begin();
   SongEntity e = new SongEntity();
   e.setName("another Song");
   jpaHelper.persist(e, false);
   ArtistEntity artist = new ArtistEntity();
   artist.setName(ARTIST);
   AlbumEntity album = new AlbumEntity();
   album.setName(ALBUM);
   jpaHelper.persist(artist, false);
   jpaHelper.persist(album, false);
   for (int i = 1; i <= SONG_COUNT; i++) {
     SongEntity song = createTestSong(i);
     artist.addSong(song);
     album.addSong(song);
     jpaHelper.persist(song, false);
   }
   e = new SongEntity();
   e.setName("And still another song");
   jpaHelper.persist(e, false);
   jpaHelper.commit();
   jpaHelper.closeEM();
 }
 /** Initializes the mock objects needed for the test command. */
 private void initMocks() {
   init = new ConstantInitializer<EntityManagerFactory>(jpaHelper.getEMF());
   observer = createObserver();
   controller = EasyMock.createMock(SyncController.class);
   queue = EasyMock.createMock(CommandQueue.class);
 }
 @After
 public void tearDown() throws Exception {
   jpaHelper.close();
 }