Network rule for datagram junit tests.
Defines, if the test should be execute with native datagram sockets (NATIVE),
and/or direct datagram sockets using "in process" messaging (DIRECT). The
supported modes of the test are provided as parameter of the constructor. If
DIRECT is supported, additional parameter may also be used.
Though the
DatagramSocket#setDatagramSocketImplFactory could only be
called once, all executed tests must use the same DatagramSocketImpl.
Therefore this rule doesn't setup the DatagramSocketImpl, it just filters the
test according the supported modes. For usage within maven, a category
NativeDatagramSocketImplRequired is also available.
The used socket mode is setup on the first usage, according the definition of
the property "org.eclipse.californium.junit.socketmode". The value must be
either "NATIVE" or "DIRECT". When tests are executed, it's checked, if the
test supports the used mode, and, if not the test is skipped. For executing
the junit test within eclipse, you may provide the mode either at
project/runner level as VM arguments, or general for a JRE as default VM
arguments (Window->Preferences | Java -> Installed JREs | select and EDIT).
The intended use within maven is therefore to use the introduced category
NativeDatagramSocketImplRequired to select the tests running DIRECT
and run the NATIVE test in a separate execution.
The intended use within eclipse is setup two configurations, one for DIRECT
using "-Dorg.eclipse.californium.junit.socketmode=DIRECT", and one for NATIVE
using "-Dorg.eclipse.californium.junit.socketmode=NATIVE". Currently the
default is NATIVE. I hope, after a introduction period, it could be changed
to DIRECT.
If the rule scope is left, the rule checks for left open DatagramSockets.
Therefore it's important to chose the right scope for the rule. The
@ClassRule is required for tests, which starts a server once
and then reuse it on several tests. @Rule could be used, if
every test cleans up on its own.
public class AbcNetworkTest {
@ClassRule
public static NetworkRule network = new NetworkRule(Mode.DIRECT, Mode.NATIVE);