2016-06-03 7 views
1

visuaforce Seite:kann Dateierweiterung auf Testklasse nicht testen?

<apex:page sidebar="false" controller="UploadOpportunityScheduleLineItem123"> 
    <apex:form > 
     <apex:sectionHeader title="Upload data from CSV file"/> 
      <apex:pagemessages /> 
      <center> 
       <apex:inputFile value="{!contentFile}" filename="{!nameFile}" /> 
       <apex:commandButton action="{!ReadFile}" value="Upload File" id="theButton" style="width:70px;"/> 
       <br/> <br/> 
      </center> 
    </apex:form> 
</apex:page> 

Spitze:

public with sharing class UploadOpportunityScheduleLineItem123{ 

    // Global variables 
    public string nameFile{get;set;} 


    Public Id parentId{get;set;} 

    public Blob contentFile{get;set;} 


    List<account> lstScheduleToUpdate = new List<account>(); 

    public account objSchedule{get;set;} 
    //String array for taking csv data by line. 
    String[] filelines = new String[]{}; 


    //set for storing all id's from csv. 
    set<String> opptoupload{get;set;} 



     //Main constructor 
    public UploadOpportunityScheduleLineItem123() 
    { 
     //Initalizing required objects. 
     objSchedule = new account(); 
     opptoupload = new set<String>(); 

    } 
    //Method to read file content and check extension and file format. 
    public Pagereference ReadFile() 
    { 

     parentId=Apexpages.currentPage().getParameters().get('ParentId'); 
     //If without selecting csv file you clicked on upload it will give error message. 
     if(nameFile == null) 
     { 
      ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'You should select csv file to upload'); 
      ApexPages.addMessage(errormsg); 
      return null; 
     } 
     //Taking file extension. 
     String extension = nameFile.substring(nameFile.lastIndexOf('.')+1); 
     //Checking if file extension is .csv. 
     if(extension == 'csv' ||extension == 'CSV') 
     { 
      nameFile =blobToString(contentFile,'ISO-8859-1'); 
      //Spliting by new line 

      filelines = nameFile.split('\n'); 
      //Spliting values by (,) for checking coloumn size 

       for (Integer i=1;i<filelines.size();i++){    
       String[] inputconvalues = new String[]{}; 
       inputconvalues = filelines[i].split(','); 
       account b = new account(); 
       b.name= inputconvalues[0]; 
       b.billingcountry = inputconvalues[1]; 
       b.billingcity = inputconvalues[2]; 
       lstScheduleToUpdate.add(b);     
       } 
       //Checking if list is not empty then updating. 
       if(lstScheduleToUpdate.Size()>0) 
       { 
        insert lstScheduleToUpdate; 
       } 
       ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.info,'Batches File uploaded successfully'); 
       ApexPages.addMessage(errormsg); 
      return null; 
     } 
     //If file is not csv type then it will give error message. 
     else 
     { 
      ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'File type should be csv type'); 
      ApexPages.addMessage(errormsg); 
      return null; 
     } 
    } 
    public static String blobToString(Blob input, String inCharset){ 
     String hex = EncodingUtil.convertToHex(input); 
     System.assertEquals(0, hex.length() & 1); 
     final Integer bytesCount = hex.length() >> 1; 
     String[] bytes = new String[bytesCount]; 
     for(Integer i = 0; i < bytesCount; ++i) 
     bytes[i] = hex.mid(i << 1, 2); 
     return EncodingUtil.urlDecode('%' + String.join(bytes, '%'), inCharset); 
    }  
} 

Testklasse:

@IsTest(SeeAllData=true) 

    private class testexceltoaccount 
    { 
      static testmethod void testLoadData() { 
        StaticResource testdoc = [Select Id,Body from StaticResource where name ='testMethodCSVUpload1']; 
      UploadOpportunityScheduleLineItem123 testUpload = new UploadOpportunityScheduleLineItem123(); 
      testUpload.contentFile= testdoc.Body; 
      testUpload.ReadFile(); 
       } 

    } 

Cant der Lage, diesen Abschnitt des Codes in Codeabdeckung zu überqueren:

String extension = nameFile.substring(nameFile.lastIndexOf('.')+1); 
     //Checking if file extension is .csv. 
     if(extension == 'csv' ||extension == 'CSV') 
     { 

Ich habe versucht, viele mögliche Code Coverage zu überqueren, aber es ist immer noch an diesem Punkt. Bitte helfen Sie mir in dieser Hinsicht.

Vielen Dank im Voraus

Antwort

2

Wenn wir Spitze verwenden: Eingabedatei auf VF Seite und laden Sie jede Datei, dann Name der Datei wird automatisch Feld in das Feld in Dateinamen Attribute angegeben aktualisieren, aber wenn Sie schreiben, Testklasse Sie geben nur den Inhalt der Datei an testUpload.contentFile = testdoc.Body; Sie sollen Namen in Namedatei global Variable testUpload.nameFile = 'test.csv' manuell hinzufügen;

@IsTest(SeeAllData=true) 
private class testexceltoaccount 
{ 
     static testmethod void testLoadData() { 
       StaticResource testdoc = [Select Id,Body,Name from StaticResource where name ='testMethodCSVUpload1']; 
     UploadOpportunityScheduleLineItem123 testUpload = new UploadOpportunityScheduleLineItem123(); 
     testUpload.contentFile= testdoc.Body; 
     testUpload.nameFile= 'test.csv'; 
     testUpload.ReadFile(); 
      } 

}