2009-07-19 5 views
0

je le test JUnit suivant:PMD règle de la bizarrerie

@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(); 
} 

Et PMD donne les violations suivantes:

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'). 

Ligne 36 dans mon dossier est la ligne la méthode est définie:

public void testRunLocalhost() throws IOException, InterruptedException { 

Je ne comprends pas ce que les violations disent. Où devrais-je définir ces trois variables? Pourquoi l'AnnouncerThread n'était-il pas dans les violations? C'est déclaré de la même manière que j'ai essayé de réorganiser les déclarations en vain.

Répondre

3

Cela semble avoir quelque chose à voir avec les appels assert que vous effectuez juste après avoir alloué ces trois dernières variables.

Les docs PMD (http://pmd.sourceforge.net/rules/controversial.html#DataflowAnomalyAnalysis) disent:

UR - Anomaly: Il y a une référence à une variable qui n'a pas été défini avant

+0

Retrait de la assert se sont débarrassés des violations. –

+0

est-ce un bug sur PMD? –

+0

Oui, 'DataflowAnomalyAnalysis' est assez obsolète. Ce problème particulier avec asserts a été [récemment corrigé et fera partie de PMD 6.0.0] (https://github.com/pmd/pmd/pull/420), mais il y a encore [un tas de problèmes en suspens] (https : //github.com/pmd/pmd/labels/in%3Adata-flow) – Johnco

0

Cela semble plutôt étrange. Fait intéressant, il se produit pour les trois variables qui sont définies pour les objets alloués via «nouveau», que vous vérifiez ensuite pour la nullité. Je m'attendrais à ce que le résultat de 'new' soit toujours valide/not-null - sinon un OutOfMemoryException devrait être lancé. Est-ce le problème, je me demande?

Questions connexes