Tuesday, May 10, 2011

Avoid single-line Salesforce Apex if expressions for code coverage

In Salesforce Apex a single line if expression should be avoided if you want to ensure full code coverage by the automated tests.

I found that Apex code would show as having full test coverage even if the expression never evaluated to true. For example, the following would show as being completely tested even though the doSomething method was never called.

Boolean exampleCondition = false;
if (exampleCondition) { doSomething(); }

Friday, May 6, 2011

Location of Sitecore data folder and App_Data

Sitecore recommends moving the data folder outside of the webroot because of security issues. This is normally a good idea, as the datafolder contains information, which shouldn’t be accessible for end users. However you might as well use .NET’s security applied to the App_data folder.
Jens Mikkelsen: Consider using the App_data folder as your Sitecore data folder

Thursday, May 5, 2011

How not to calculate the databases UTC offset using LINQ to SQL

Spent a bit of time debugging a subtle issue with calculating the UTC offset for datetimes stored in a SQL server. The code in question needed to determine the timezone difference between the database server and UTC.

Problematic code that should be avoided when calculating the UTC offset

using (DatabaseDataContext db = new DatabaseDataContext())
{
 DateTime current = db.ExecuteQuery("SELECT GETDATE()").First();
 DateTime utcCurrent = db.ExecuteQuery("SELECT GETUTCDATE()").First();

 TimeSpan differece = utcCurrent - current;

 double diff = differece.Hours;

 logger.DebugFormat("current:{0}, utcCurrent:{1}, differece:{2}, diff:{3}", current, utcCurrent, differece, diff);
}

This produces the following. Notice the diff toggling between -11 and -12 based on the timing of the calls.
DEBUG - current:05/05/2011 11:32:06, utcCurrent:05/04/2011 23:32:06, differece:-11:59:59.9970000, diff:-11
DEBUG - current:05/05/2011 11:32:06, utcCurrent:05/04/2011 23:32:06, differece:-12:00:00, diff:-12
DEBUG - current:05/05/2011 11:32:06, utcCurrent:05/04/2011 23:32:06, differece:-11:59:59.9830000, diff:-11
DEBUG - current:05/05/2011 11:32:06, utcCurrent:05/04/2011 23:32:06, differece:-11:59:59.9900000, diff:-11
DEBUG - current:05/05/2011 11:32:06, utcCurrent:05/04/2011 23:32:06, differece:-11:59:59.9930000, diff:-11

Fixed query to get the offset in a single call

 ExecuteQuery("SELECT DATEDIFF(hour,GETDATE(),GETUTCDATE())").First();

Note that this doesn't handle the case where the offset may include partial hours.

See Also:

Tuesday, April 26, 2011

Apex Static Resource

StaticResources can be used in unit tests to store data that would otherwise clutter the test cases.

StaticResource xml = [select body, name from StaticResource where name = 'StaticResourceName'  limit 1];

Thursday, April 21, 2011

Salesforce Apex Test class and callouts

The Test.isRunningTest() method is a useful method to bypass web service callouts when running automated test cases that would otherwise fail.

For example, in any wsdl2apex generated classes find the WebServiceCallout.invoke() method calls and then wrap them in an test for Test.isRunningTest(). If the test is false the code can still call invoke as generated. Otherwise the code can simulate the web service response, allowing the test cases to complete.

Monday, April 4, 2011

MSBuild - sort an ItemGroup

An automated MSBuild script that creates a database from scratch started failing after switching from vcvarall.bat provided by a Visual Studio 2008 install to the Visual Studio 2010 version.

For some unknown reason, the order of files listed in a ItemGroup was reversed. With files beginning with an underscore running last rather than first.

The issue was resolved by explicitly sorting the ItemGroup using a custom task from the MSBuild Extension Pack.


    <!-- Have a fallback check depending on path locally or on build server -->
    <PropertyGroup>
        <TPath>$(MSBuildProjectDirectory)\ExtensionPack\MSBuild.ExtensionPack.tasks</TPath>
        <TPath Condition="Exists('C:\Program Files (x86)\MSBuild\ExtensionPack\MSBuild.ExtensionPack.tasks')">C:\Program Files (x86)\MSBuild\ExtensionPack\MSBuild.ExtensionPack.tasks</TPath>
    </PropertyGroup>
    <Import Project="$(TPath)"/>

    <!-- ... -->

    <Target Name="MainData">
        <Message Text="Inserting Data Main (Main Data)" />

        <!-- Sort an ItemGroup alphabetically -->
        <MSBuild.ExtensionPack.Framework.MsBuildHelper TaskAction="Sort" InputItems1="@(MainDataFiles)">
            <Output TaskParameter="OutputItems" ItemName="sorted"/>
        </MSBuild.ExtensionPack.Framework.MsBuildHelper>
        <Message Text="Sorted Items: %(sorted.Identity)"/>
    
        <Exec Command="$(SqlCmdPrefix) -i %(sorted.Identity)" />
    </Target>


See Also:

Tuesday, March 29, 2011

Modifying TFS build server output to seperate project outputpaths

By default a TFS build server outputs all projects in a solution, with the exception of web applications, into a single directory.

To separate the build output directoyr import a modified version of Microsoft.WebApplication.targets into the applicable csproj files.

<Import Project="Modified.Microsoft.WebApplication.targets" />

See also: