Thursday 25 October 2012

Critical Update - Generate correctly escaped markup

Prior to the Winter '13 release, text in some Visualforce pages and components might have been generated incorrectly. This markup could contain fragments that should have been escaped (for example, the "<" character generated as &'lt';) but were not. These fragments might be interpreted by the browser as markup rather than as text in the page. This problem has been corrected for all pages with API version 26.0 or later. Your organization might contain pages or components that depend on this incorrect processing. These pages need to be fixed. To fix them, you will generally need to use with the attribute escape="false" to cause the text to be generated unescaped, as it was previously.
If your page contains either:

<apex:outputText value="{!something}"/>, or
A free-standing expression {!something}

And you’re expecting the string returned by the controller for {!something} to be treated as markup, rather than displayed in the page, then this page needs to be changed.

For the first case, you need simply set the escape attribute to false, <apex:outputText value="{!something}" escape="false"/>.

For the second case, you need to modify the expression to be output using <apex:outputText> as with the first case:

<apex:outputText value="{!something}" escape="false"/>

When you have fixed any affected pages, you should activate this Critical Update.

VFP Page



  
  
  



class


global class criticalUpdate {

    public String dummyString
    { 
        get 
        {
            return '

Test

'; } set; } }

Saturday 20 October 2012

Testing Web Service Callouts

In Winter 13, Generated code is saved as an Apex class containing the methods you can invoke for calling the Web service. To deploy or package this Apex class and other accompanying code, 75% of the code must have test coverage, including the methods in the generated class. By default, test methods don’t support Web service callouts and tests that perform Web service callouts are skipped. To prevent tests from being skipped and to increase code coverage, Apex provides the built-in WebServiceMock interface and the Test.setMock method that you can use to receive fake responses in a test method.

Auto-generated apex class from WSDL


//Generated by wsdl2apex 
    

public class docSample {

    public class EchoStringResponse_element {

        public String EchoStringResult;

        private String[] EchoStringResult_type_info = new String[]{
                            'EchoStringResult',
                            'http://www.w3.org/2001/XMLSchema',
                            'string','0','1','false'};

        private String[] apex_schema_type_info = new String[]{
                            'http://doc.sample.com/docSample',
                            'true'};

        private String[] field_order_type_info = new String[]{
                            'EchoStringResult'};
    }

    public class DocSamplePort {

        public String endpoint_x = 'http://YourServer/YourService';

        private String[] ns_map_type_info = new String[]{
                             'http://doc.sample.com/docSample', 
                             'docSample'};

        public String EchoString(String input) {
            docSample.EchoString_element request_x = 
                               new docSample.EchoString_element();
            docSample.EchoStringResponse_element response_x;
            request_x.input = input;
            Map response_map_x = 
                      new Map();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
                 'urn:dotnet.callouttest.soap.sforce.com/EchoString',
                 'http://doc.sample.com/docSample',
                 'EchoString',
                 'http://doc.sample.com/docSample',
                 'EchoStringResponse',
                 'docSample.EchoStringResponse_element'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.EchoStringResult;
        }
    }

    public class EchoString_element {

        public String input;
        private String[] input_type_info = new String[]{
                                 'input',
                                 'http://www.w3.org/2001/XMLSchema',
                                 'string','0','1','false'};
        private String[] apex_schema_type_info = new String[]{
                                 'http://doc.sample.com/docSample',
                                 'true'};
        private String[] field_order_type_info = new String[]{'input'};
    }
}


Web Service Callouts class

Class

public class WebSvcCallout {
    public static String callEchoString(String input) {
        docSample.DocSamplePort sample = new docSample.DocSamplePort();
        sample.endpoint_x = 'http://api.salesforce.com/foo/bar';
        
        // This invokes the EchoString method in the generated class 
    
        String echo = sample.EchoString(input);
        
        return echo;
    }   
}


First, implement the WebServiceMock interface and specify the fake response in the doInvoke method.

Test Class

@isTest
global class WebServiceMockImpl implements WebServiceMock {
   global void doInvoke(
           Object stub,
           Object request,
           Map response,
           String endpoint,
           String soapAction,
           String requestName,
           String responseNS,
           String responseName,
           String responseType) {
       docSample.EchoStringResponse_element respElement = new docSample.EchoStringResponse_element();
       respElement.EchoStringResult = 'Mock response';
       response.put('response_x', respElement); 
   }
}


Test class of Web Service Callouts class

Test Class

@isTest
private class WebSvcCalloutTest {
    @isTest static void testEchoString() {              
        // This causes a fake response to be generated 
    
        Test.setMock(WebServiceMock.class, new WebServiceMockImpl());
        
        // Call the method that invokes a callout 
    
        String output = WebSvcCallout.callEchoString('Hello World!');
        
        // Verify that a fake result is returned 
    
        System.assertEquals('Mock response', output); 
    }
}