27.07.2013 Views

2 Why We Need Model-Based Testing

2 Why We Need Model-Based Testing

2 Why We Need Model-Based Testing

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

5.6.2 Code<br />

Systems with Finite <strong>Model</strong>s 75<br />

are sensitive to the number of digits, so we choose 99.9 and 100.0. These particular<br />

values are not important; we assume that any other numbers with the same number<br />

of digits would behave the same way. Limiting temperatures to just two values is an<br />

example of data abstraction.<br />

There are several ways to limit the temperature to two values. <strong>We</strong> could replace the<br />

numeric type with an enumerated type. <strong>We</strong> could even replace the single ServerSend<br />

method (where the temperature is a parameter) with two parameterless methods,<br />

each coded with one particular temperature. It is more convenient for testing if the<br />

methods and parameters are similar in the model and the implementation, so we keep<br />

the single method with its numeric parameter, but limit the number of parameter<br />

values with the Domain attribute, which we will describe in the next section.<br />

The actions and state variables we chose determine the level of abstraction of this<br />

model program. Many implementation details are left out. <strong>We</strong> do not need any actual<br />

network machinery (IP addresses, port numbers, sockets, etc.) because we model the<br />

client, the server, and the network in a single model program. Other simplifications<br />

become possible because the model program can use global information that is not<br />

available to the separate client and server programs of the implementation. This will<br />

become clear in the next section.<br />

The client/server model program appears in Figures 5.7–5.11. Compare this to<br />

the implementation code in Chapter 2, Figures 2.3–2.5. Usually a model program<br />

is much shorter than the implementation. That is not the case here, because the<br />

implementation is so simple (it is really just a wrapper around some methods in<br />

the .NET socket class). Much of the code in the model is enabling conditions,<br />

which describe the control structure that must be present in any applications that<br />

use the implementation (such as Monitor and Logger, Chapter 2, Section 2.5). The<br />

structure of this model program, like all model programs, is similar to the first one<br />

(Section 5.4, Figures 5.4 and 5.5). The following paragraphs explain some features<br />

that might not be obvious.<br />

Unlike the implementation, here the client and server are not instances. The state<br />

of both client and server are static variables in one static class, and the actions of both<br />

are static methods in this class. Therefore, we use a C# static class. The compiler<br />

checks that all the variables and methods in a static class are declared static.<br />

There are different methods for the actions of the client and the server, with<br />

different names. The ClientSocket and ServerSocket action methods here model<br />

the Socket methods of the client and server classes in the implementation, and so<br />

on. All of the action methods in a model (in the model namespace) must have<br />

different names. It is possible to have more than one class in a model, but even then<br />

more free ebooks download links at:<br />

http://www.ebook-x.com

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

Saved successfully!

Ooh no, something went wrong!