/** 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 the correct template is created. */
 @Test
 public void testCreateOAuthTemplate() {
   SyncCommandTestImpl cmd = createCommand(null);
   OAuthTemplate templ = cmd.createOAuthTemplate();
   assertEquals("Wrong OAuth URI", OAUTH_URI, templ.getOAuthEndpointURI());
   assertEquals("Wrong service URI", SVC_URI, templ.getServiceURI());
 }
 /** 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 a correct command for synchronizing a song is created. */
 @Test
 public void testCreateSongSyncCommand() {
   OAuthTemplate templ = new OAuthTemplate(OAUTH_URI, SVC_URI);
   SongEntity song = createTestSong(1);
   SyncCommandTestImpl cmd = createCommand(null);
   SyncSongCommand syncSongCmd = (SyncSongCommand) cmd.createSyncSongCommand(song, templ);
   assertSame("Wrong template", templ, syncSongCmd.getOAuthTemplate());
   assertSame("Wrong song", song, syncSongCmd.getSong());
   assertSame("Wrong controller", controller, syncSongCmd.getController());
 }
 /** Tests the command for notifying the controller about the end of the sync. */
 @Test
 public void testCreateEndSyncCommand() throws Exception {
   SyncCommandTestImpl cmd = createCommand(null);
   controller.endSynchronization();
   EasyMock.replay(controller, observer, queue);
   Command endCommand = cmd.createEndSyncCommand();
   endCommand.execute();
   EasyMock.verify(controller, observer, queue);
   assertFalse("Wrong UI flag", ((CommandBase) endCommand).isUpdateGUI());
 }
 /** 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 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);
   }
 }
 /** Tests the execution of a sync command. */
 @Test
 public void testExecute() throws Exception {
   SyncCommandTestImpl cmd = createCommand(null);
   List<SongEntity> songs = new LinkedList<SongEntity>();
   List<Command> syncCmds = new LinkedList<Command>();
   Command endCmd = EasyMock.createMock(Command.class);
   for (int i = 1; i <= SONG_COUNT; i++) {
     SongEntity song = createTestSong(i);
     songs.add(song);
     Command sc = EasyMock.createMock(Command.class);
     EasyMock.replay(sc);
     queue.execute(sc);
     syncCmds.add(sc);
   }
   observer.commandCompletedBackground(songs);
   controller.startSynchronization(SONG_COUNT);
   queue.execute(endCmd);
   EasyMock.replay(endCmd, observer, controller, queue);
   cmd.installMockEndCommand(endCmd);
   cmd.installMockEntities(new ArrayList<SongEntity>(songs), syncCmds);
   cmd.execute();
   EasyMock.verify(endCmd, observer, controller, queue);
 }