ConfigurableUDPSource { public static void sendFromDatagram(DatagramPacket packet, List<String> sources) { log.debug("UDP Packet received"); log.trace("{}", new String(packet.getData())); InetAddress address = packet.getAddress(); int port = packet.getPort(); log.debug("UDP message received from address '{}' and port '{}' for sources {}", address, port, sources); for (String sourceName : sources) { if (receivingFromServer(sourceName, port, address)) { log.debug("Sending Notification for source '{}'", sourceName); notificationHandlers.get(sourceName).handleMessage(packet); } } }  static DatagramSocket createUDPSocket(int port, InetAddress address, String sourceName); static DatagramSocket listenOnUDPSocket(int port, InetAddress address, String sourceName); static void setNotificationHandler(UDPNotificationHandler handler, String sourceName, Map incoming); static void main(String[] args); static void sendFromDatagram(DatagramPacket packet, List<String> sources);  }
@Test public void testSendFromDatagram() throws UnknownHostException { InetAddress address = InetAddress.getByName("localhost"); int port = 10213; UDPNotificationHandler firstHandler = new UDPNotificationHandler(new LinkedHashMap<>(), client) { @Override public void handleMessage(DatagramPacket packet) { variable.put("message", packet); variable.put("data", packet.getData()); } }; UDPNotificationHandler secondHandler = new UDPNotificationHandler(new LinkedHashMap<>(), client) { @Override public void handleMessage(DatagramPacket packet) { variable.put("message", packet); variable.put("data", packet.getData()); } }; String firstSource = "first"; client = new FalseClient(firstSource); ConfigurableUDPSource.notificationHandlers.put(firstSource, firstHandler); ConfigurableUDPSource.clients.put(firstSource, client); String secondSource = "second"; client = new FalseClient(secondSource); ConfigurableUDPSource.notificationHandlers.put(secondSource, secondHandler); ConfigurableUDPSource.clients.put(secondSource, client); ConfigurableUDPSource.sourceAddresses.put(firstSource, ConfigurableUDPSource.ALL_ADDR); List l = new ArrayList<>(); l.add(port); ConfigurableUDPSource.sourcePorts.put(firstSource, l); ConfigurableUDPSource.sourceAddresses.put(secondSource, ConfigurableUDPSource.ALL_ADDR); l = new ArrayList<>(); l.add(123); ConfigurableUDPSource.sourcePorts.put(secondSource, l); byte[] data = {10,53,27,-100}; DatagramPacket p = new DatagramPacket(data, data.length, address, port); l = new ArrayList(); l.add(firstSource); l.add(secondSource); ConfigurableUDPSource.sendFromDatagram(p, l); assert firstHandler.getVariable().get("message") == p; assert firstHandler.getVariable().get("data") == data; assert secondHandler.getVariable().get("message") == null; assert secondHandler.getVariable().get("data") == null; }