void fetchOneFolder( MailConnection mailConn, boolean usePOP3, String realIMAPFolder,
    String realOutputFolder, String targetAttachmentFolder, String realMoveToIMAPFolder,
    String realFilenamePattern, int nbrmailtoretrieve, SimpleDateFormat df ) throws KettleException {
    try {
      // if it is not pop3 and we have non-default imap folder...
      if ( !usePOP3 && !Utils.isEmpty( realIMAPFolder ) ) {
        mailConn.openFolder( realIMAPFolder, true );
      } else {
        mailConn.openFolder( true );
      }

      mailConn.retrieveMessages();

      int messagesCount = mailConn.getMessagesCount();

      if ( isDetailed() ) {
        logDetailed( BaseMessages.getString( PKG, "JobGetMailsFromPOP.TotalMessagesFolder.Label", ""
          + messagesCount, Const.NVL( mailConn.getFolderName(), MailConnectionMeta.INBOX_FOLDER ) ) );
      }

      messagesCount =
        nbrmailtoretrieve > 0
          ? ( nbrmailtoretrieve > messagesCount ? messagesCount : nbrmailtoretrieve ) : messagesCount;

      if ( messagesCount > 0 ) {
        switch ( getActionType() ) {
          case MailConnectionMeta.ACTION_TYPE_DELETE:
            if ( nbrmailtoretrieve > 0 ) {
              // We need to fetch all messages in order to retrieve
              // only the first nbrmailtoretrieve ...
              for ( int i = 0; i < messagesCount && !parentJob.isStopped(); i++ ) {
                // Get next message
                mailConn.fetchNext();
                // Delete this message
                mailConn.deleteMessage();
                if ( isDebug() ) {
                  logDebug( BaseMessages.getString( PKG, "JobGetMailsFromPOP.MessageDeleted", "" + i ) );
                }
              }
            } else {
              // Delete messages
              mailConn.deleteMessages( true );
              if ( isDebug() ) {
                logDebug( BaseMessages.getString( PKG, "JobGetMailsFromPOP.MessagesDeleted", "" + messagesCount ) );
              }
            }
            break;
          case MailConnectionMeta.ACTION_TYPE_MOVE:
            if ( nbrmailtoretrieve > 0 ) {
              // We need to fetch all messages in order to retrieve
              // only the first nbrmailtoretrieve ...
              for ( int i = 0; i < messagesCount && !parentJob.isStopped(); i++ ) {
                // Get next message
                mailConn.fetchNext();
                // Move this message
                mailConn.moveMessage();
                if ( isDebug() ) {
                  logDebug( BaseMessages.getString(
                    PKG, "JobGetMailsFromPOP.MessageMoved", "" + i, realMoveToIMAPFolder ) );
                }
              }
            } else {
              // Move all messages
              mailConn.moveMessages();
              if ( isDebug() ) {
                logDebug( BaseMessages.getString(
                  PKG, "JobGetMailsFromPOP.MessagesMoved", "" + messagesCount, realMoveToIMAPFolder ) );
              }
            }
            break;
          default:
            // Get messages and save it in a local file
            // also save attached files if needed
            for ( int i = 0; i < messagesCount && !parentJob.isStopped(); i++ ) {
              // Get next message
              mailConn.fetchNext();
              int messagenumber = mailConn.getMessage().getMessageNumber();
              boolean okPOP3 = usePOP3 ? true : false; // (mailConn.getMessagesCounter()<nbrmailtoretrieve &&
                                                       // retrievemails==2)||(retrievemails!=2):false;
              boolean okIMAP = !usePOP3;

              if ( okPOP3 || okIMAP ) {
                // display some infos on the current message
                //
                if ( isDebug() && mailConn.getMessage() != null ) {
                  logDebug( "--------------------------------------------------" );
                  logDebug( BaseMessages.getString( PKG, "JobGetMailsFromPOP.MessageNumber.Label", ""
                    + messagenumber ) );
                  if ( mailConn.getMessage().getReceivedDate() != null ) {
                    logDebug( BaseMessages.getString( PKG, "JobGetMailsFromPOP.ReceivedDate.Label", df
                      .format( mailConn.getMessage().getReceivedDate() ) ) );
                  }
                  logDebug( BaseMessages.getString( PKG, "JobGetMailsFromPOP.ContentType.Label", mailConn
                    .getMessage().getContentType() ) );
                  logDebug( BaseMessages.getString( PKG, "JobGetMailsFromPOP.EmailFrom.Label", Const.NVL( mailConn
                    .getMessage().getFrom()[0].toString(), "" ) ) );
                  logDebug( BaseMessages.getString( PKG, "JobGetMailsFromPOP.EmailSubject.Label", Const.NVL(
                    mailConn.getMessage().getSubject(), "" ) ) );
                }
                if ( isSaveMessage() ) {
                  // get local message filename
                  String localfilename_message = replaceTokens( realFilenamePattern, i );

                  if ( isDebug() ) {
                    logDebug( BaseMessages.getString(
                      PKG, "JobGetMailsFromPOP.LocalFilename.Label", localfilename_message ) );
                  }

                  // save message content in the file
                  mailConn.saveMessageContentToFile( localfilename_message, realOutputFolder );
                  // PDI-10942 explicitly set message as read
                  mailConn.getMessage().setFlag( Flag.SEEN, true );

                  if ( isDetailed() ) {
                    logDetailed( BaseMessages.getString( PKG, "JobGetMailsFromPOP.MessageSaved.Label", ""
                      + messagenumber, localfilename_message, realOutputFolder ) );
                  }
                }

                // Do we need to save attached file?
                if ( isSaveAttachment() ) {
                  mailConn.saveAttachedFiles( targetAttachmentFolder, attachementPattern );
                }
                // We successfully retrieved message
                // do we need to make another action (delete, move)?
                if ( usePOP3 ) {
                  if ( getDelete() ) {
                    mailConn.deleteMessage();
                    if ( isDebug() ) {
                      logDebug( BaseMessages.getString( PKG, "JobGetMailsFromPOP.MessageDeleted", ""
                        + messagenumber ) );
                    }
                  }
                } else {
                  switch ( getAfterGetIMAP() ) {
                    case MailConnectionMeta.AFTER_GET_IMAP_DELETE:
                      // Delete messages
                      mailConn.deleteMessage();
                      if ( isDebug() ) {
                        logDebug( BaseMessages.getString( PKG, "JobGetMailsFromPOP.MessageDeleted", ""
                          + messagenumber ) );
                      }
                      break;
                    case MailConnectionMeta.AFTER_GET_IMAP_MOVE:
                      // Move messages
                      mailConn.moveMessage();
                      if ( isDebug() ) {
                        logDebug( BaseMessages.getString( PKG, "JobGetMailsFromPOP.MessageMoved", ""
                          + messagenumber, realMoveToIMAPFolder ) );
                      }
                      break;
                    default:
                  }
                }

              }
            }
            break;
        }
      }
    } catch ( Exception e ) {
      throw new KettleException( e );
    }
  }
@Test
  public void testFetchOneFolderModeIMAPWithNonDefFolder() throws KettleException, MessagingException {
    entry.fetchOneFolder( mailConn, false, "junitImapFolder", "junitRealOutputFolder", "junitTargetAttachmentFolder",
        "junitRealMoveToIMAPFolder", "junitRealFilenamePattern", 0, Mockito.mock( SimpleDateFormat.class ) );
    Mockito.verify( mailConn ).openFolder( "junitImapFolder", true );
    Mockito.verify( message ).setFlag( Flag.SEEN, true );
  }