07.11.2014 Views

Enterprise Library Test Guide - Willy .Net

Enterprise Library Test Guide - Willy .Net

Enterprise Library Test Guide - Willy .Net

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

162<br />

<strong>Enterprise</strong> <strong>Library</strong> <strong>Test</strong> <strong>Guide</strong><br />

<strong>Test</strong> case<br />

Verify that the application block<br />

handles strings as entire strings instead<br />

of as a series of characters.<br />

This is especially important when<br />

sorting or searching for substrings.<br />

When the application block must<br />

parse individual characters, verify<br />

that the application block uses the<br />

.NET Framework StringInfo class to<br />

parse the individual text elements.<br />

Verify that strings that the application<br />

block stores internally (the end<br />

user cannot see them) are stored in<br />

a culture-independent format.<br />

Verify that the application block’s<br />

buffers are large enough to hold<br />

translated strings. Translated<br />

strings are often longer than the<br />

original strings. For example, when<br />

you translate strings from English<br />

to German, the German strings are<br />

longer than the English strings.<br />

Verify that the application block<br />

uses culture-independent operations<br />

to compare strings such as<br />

file names, persistence formats, or<br />

symbolic information that the end<br />

user does not see.<br />

Example<br />

The StringInfo class contains methods that retrieve individual<br />

text elements from a given string. A text element<br />

can be a base character, a surrogate pair, or a combined<br />

character sequence. In the following example, the application<br />

block retrieves individual text elements from a string<br />

and displays them on the console.<br />

TextElementEnumerator charEnum = StringInfo.<br />

GetTextElementEnumerator(s);<br />

while (charEnum.MoveNext())<br />

{<br />

Console.WriteLine(charEnum.GetTextElement());<br />

Console.WriteLine(Environment.NewLine);<br />

}<br />

By storing strings in a culture-independent format, you can<br />

ensure that processes that require culture-independent<br />

results behave consistently, regardless of the actual culture.<br />

Use the .NET Framework CultureInfo.InvariantCulture<br />

property with strings that are culture-independent. This is<br />

shown in the following example.<br />

CultureInfo Invc = CultureInfo.InvariantCulture;<br />

Thread.CurrentThread.CurrentCulture = Invc<br />

In the following example, the application block reads an<br />

array of 10 characters from a file. This may result in lost<br />

data if the number of characters in the file for a local<br />

language is larger than the number of characters used in<br />

English.<br />

char[] buffer = new char[10];<br />

using (FileStream fs = new FileStream("", File-<br />

Mode.Open))<br />

{<br />

using (StreamReader sr = new StreamReader(fs))<br />

{<br />

sr.Read(buffer, 0, 10);<br />

}<br />

}<br />

If an application block uses a comparison operation to determine<br />

if a string is a recognized XML tag, that comparison<br />

should be culture-independent. If the application block<br />

bases a security decision on the result of a string comparison<br />

or case change operation, the operation should<br />

be culture-independent to ensure that the result is not<br />

affected by the value of the CultureInfo.CurrentCulture<br />

property. To make the comparison culture independent,<br />

use the overload of the .NET Framework String.Compare<br />

method that takes a CultureInfo object as a parameter.<br />

Pass in the CultureInfo.InvariantCulture property, This is<br />

shown in the following example.<br />

int compareResult = String.Compare(string1,<br />

string2, false, CultureInfo.InvariantCulture);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!