11/*
2- * Copyright (c) 1997, 2020 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 1997, 2026 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2525 * @bug 4091803 7021373
2626 * @summary this tests that the constructor of DatagramPacket rejects
2727 * bogus arguments properly.
28- * @run testng Constructor
28+ * @run junit ${test.main.class}
2929 */
3030
3131import java .net .DatagramPacket ;
3232import java .net .InetAddress ;
3333import java .net .InetSocketAddress ;
3434
35- import org .testng .annotations .Test ;
36-
37- import static org .testng .Assert .assertFalse ;
38- import static org .testng .Assert .assertTrue ;
39- import static org .testng .Assert .expectThrows ;
35+ import org .junit .jupiter .api .Test ;
36+ import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
37+ import static org .junit .jupiter .api .Assertions .assertEquals ;
38+ import static org .junit .jupiter .api .Assertions .assertNull ;
39+ import static org .junit .jupiter .api .Assertions .assertSame ;
40+ import static org .junit .jupiter .api .Assertions .assertThrows ;
4041
4142public class Constructor {
4243
@@ -48,50 +49,48 @@ public class Constructor {
4849
4950 @ Test
5051 public void testNullPacket () {
51- expectThrows (NPE ,
52+ assertThrows (NPE ,
5253 () -> new DatagramPacket (null , 100 ));
5354 }
5455 @ Test
5556 public void testNull () throws Exception {
56- expectThrows (NPE , () -> new DatagramPacket (null , 100 ));
57- expectThrows (NPE , () -> new DatagramPacket (null , 0 , 10 ));
58- expectThrows (NPE , () -> new DatagramPacket (null , 0 , 10 , LOOPBACK , 80 ));
59- expectThrows (NPE , () -> new DatagramPacket (null , 10 , LOOPBACK , 80 ));
60- expectThrows (NPE , () -> new DatagramPacket (null , 0 , 10 , new InetSocketAddress (80 )));
61- expectThrows (NPE , () -> new DatagramPacket (null , 10 , new InetSocketAddress (80 )));
57+ assertThrows (NPE , () -> new DatagramPacket (null , 100 ));
58+ assertThrows (NPE , () -> new DatagramPacket (null , 0 , 10 ));
59+ assertThrows (NPE , () -> new DatagramPacket (null , 0 , 10 , LOOPBACK , 80 ));
60+ assertThrows (NPE , () -> new DatagramPacket (null , 10 , LOOPBACK , 80 ));
61+ assertThrows (NPE , () -> new DatagramPacket (null , 0 , 10 , new InetSocketAddress (80 )));
62+ assertThrows (NPE , () -> new DatagramPacket (null , 10 , new InetSocketAddress (80 )));
6263
6364 // no Exception expected for null addresses
64- new DatagramPacket (buf , 10 , null , 0 );
65- new DatagramPacket (buf , 10 , 10 , null , 0 );
65+ assertDoesNotThrow (() -> new DatagramPacket (buf , 10 , null , 0 ) );
66+ assertDoesNotThrow (() -> new DatagramPacket (buf , 10 , 10 , null , 0 ) );
6667 }
6768
6869 @ Test
6970 public void testNegativeBufferLength () {
7071 /* length lesser than buffer length */
71- expectThrows (IAE ,
72- () -> new DatagramPacket (buf , -128 ));
72+ assertThrows (IAE , () -> new DatagramPacket (buf , -128 ));
7373 }
7474
7575 @ Test
7676 public void testPacketLengthTooLarge () {
7777 /* length greater than buffer length */
78- expectThrows (IAE ,
79- () -> new DatagramPacket (buf , 256 ));
78+ assertThrows (IAE , () -> new DatagramPacket (buf , 256 ));
8079 }
8180
8281 @ Test
8382 public void testNegativePortValue () throws Exception {
8483 /* negative port */
8584 InetAddress addr = InetAddress .getLocalHost ();
8685
87- expectThrows (IAE ,
86+ assertThrows (IAE ,
8887 () -> new DatagramPacket (buf , 100 , addr , -1 ));
8988 }
9089
9190 @ Test
9291 public void testPortValueTooLarge () {
9392 /* invalid port value */
94- expectThrows (IAE ,
93+ assertThrows (IAE ,
9594 () -> new DatagramPacket (buf , 128 , LOOPBACK , Integer .MAX_VALUE ));
9695 }
9796
@@ -101,8 +100,9 @@ public void testSimpleConstructor() {
101100 int length = 50 ;
102101 DatagramPacket pkt = new DatagramPacket (buf , offset , length );
103102
104- assertFalse ((pkt .getData () != buf || pkt .getOffset () != offset ||
105- pkt .getLength () != length ), "simple constructor failed" );
103+ assertSame (buf , pkt .getData ());
104+ assertEquals (offset , pkt .getOffset ());
105+ assertEquals (length , pkt .getLength ());
106106 }
107107
108108 @ Test
@@ -112,20 +112,21 @@ public void testFullConstructor() {
112112 int port = 8080 ;
113113 DatagramPacket packet = new DatagramPacket (buf , offset , length , LOOPBACK , port );
114114
115- assertFalse ((packet .getData () != buf || packet .getOffset () != offset ||
116- packet .getLength () != length ||
117- packet .getAddress () != LOOPBACK ||
118- packet .getPort () != port ), "full constructor failed" );
115+ assertSame (buf , packet .getData ());
116+ assertEquals (offset , packet .getOffset ());
117+ assertEquals (length , packet .getLength ());
118+ assertSame (LOOPBACK , packet .getAddress ());
119+ assertEquals (port , packet .getPort ());
119120 }
120121
121122 @ Test
122123 public void testDefaultValues () {
123124 DatagramPacket packet = new DatagramPacket (buf , 0 );
124- assertTrue (packet .getAddress () == null );
125- assertTrue ( packet .getPort () == 0 );
125+ assertNull (packet .getAddress ());
126+ assertEquals ( 0 , packet .getPort ());
126127
127128 DatagramPacket packet1 = new DatagramPacket (buf , 0 , 0 );
128- assertTrue (packet1 .getAddress () == null );
129- assertTrue ( packet1 .getPort () == 0 );
129+ assertNull (packet1 .getAddress ());
130+ assertEquals ( 0 , packet1 .getPort ());
130131 }
131132}
0 commit comments