У меня есть следующий тест JUnit:
@Test
public void testRunLocalhost() throws IOException, InterruptedException {
// Start an AnnouncerThread
final AnnouncerThread announcer = new AnnouncerThread();
announcer.start();
// Create the socket and listen on the right port.
final DatagramSocket socket = new DatagramSocket();
assert(socket != null);
// Create a packet to send.
final DatagramPacket packet = new DatagramPacket(new byte[0], 0, InetAddress.getByName(AnnouncerThread.GROUP), AnnouncerThread.PORT);
assert(packet != null);
// Send the packet.
socket.send(packet);
// Listen for the IP from the server.
final DatagramPacket receivedPacket = new DatagramPacket(new byte[256], 256);
socket.setSoTimeout(2000); // Only wait 2 seconds.
socket.receive(receivedPacket);
socket.close();
// Get localhost's address.
final InetAddress localhost = InetAddress.getLocalHost();
assert(localhost != null);
// Compare the receive IP to the localhost IP.
final String receivedIP = new String(receivedPacket.getData());
if (!receivedIP.startsWith(localhost.getHostAddress())) {
fail("Received IP: '" + receivedIP + "' not the same as localhost: '" + localhost.getHostAddress() + "'");
}
announcer.shutdown();
announcer.join();
}
А ПМД дает следующие нарушения:
Found 'UR'-anomaly for variable 'socket' (lines '36'-'36').
Found 'UR'-anomaly for variable 'localhost' (lines '36'-'36').
Found 'UR'-anomaly for variable 'packet' (lines '36'-'36').
Строка 36 в моем файле - это строка, в которой определен метод:
public void testRunLocalhost() throws IOException, InterruptedException {
Я не понимаю, о чем говорят нарушения. Где я должен определить эти три переменные? Почему не был AnnouncerThread в нарушениях? Он объявлен так же, как я пытался изменить порядок объявлений безрезультатно.