/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2013, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.wsf.stack.cxf.addons.transports.udp;

import java.net.NetworkInterface;
import java.util.Enumeration;

import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

public class UDPTransportTest
{
   private static final String PORT = "9434";
   private static Server server;
   private static Bus bus;

   @BeforeClass
   public static void setUpBeforeClass() throws Exception
   {
      bus = BusFactory.newInstance().createBus();
      BusFactory.setDefaultBus(bus);
      JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
      factory.setBus(bus);
      factory.setAddress("udp://:" + PORT);
      factory.setServiceBean(new GreeterImpl());
      server = factory.create();
   }

   @AfterClass
   public static void shutdown() throws Exception
   {
      server.stop();
      server = null;
      if (bus != null) {
         bus.shutdown(true);
      }
      bus = null;
   }

   @Test
   public void testSimpleUDP() throws Exception
   {
      JaxWsProxyFactoryBean fact = new JaxWsProxyFactoryBean();
      fact.setAddress("udp://localhost:" + PORT);
      Greeter g = fact.create(Greeter.class);
      for (int x = 0; x < 5; x++)
      {
         Assert.assertEquals("Hello World", g.greetMe("World"));
      }

      ((java.io.Closeable) g).close();
   }

   @Test
   public void testBroadcastUDP() throws Exception
   {
      Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
      int count = 0;
      while (interfaces.hasMoreElements())
      {
         NetworkInterface networkInterface = interfaces.nextElement();
         if (!networkInterface.isUp() || networkInterface.isLoopback())
         {
            continue;
         }
         count++;
      }
      if (count == 0)
      {
         //no non-loopbacks, cannot do broadcasts
         System.out.println("Skipping broadcast test");
         return;
      }

      JaxWsProxyFactoryBean fact = new JaxWsProxyFactoryBean();
      fact.setAddress("udp://:" + PORT + "/foo");
      Greeter g = fact.create(Greeter.class);
      Assert.assertEquals("Hello World", g.greetMe("World"));
      ((java.io.Closeable) g).close();
   }

   @Test
   public void testLargeRequest() throws Exception
   {
      JaxWsProxyFactoryBean fact = new JaxWsProxyFactoryBean();
      fact.setAddress("udp://localhost:" + PORT);
      Greeter g = fact.create(Greeter.class);
      StringBuilder b = new StringBuilder(100000);
      for (int x = 0; x < 10000; x++)
      {
         b.append("Hello ");
      }
      Assert.assertEquals("Hello " + b.toString(), g.greetMe(b.toString()));

      ((java.io.Closeable) g).close();
   }
}

@@ -1081,16 +1081,79 @@
 s.udp;%0A%0A
+import java.net.InetAddress;%0Aimport java.net.InterfaceAddress;%0A
 import j
@@ -3003,16 +3003,66 @@
 opback()
+ %7C%7C !isBroadcastAddressAvailable(networkInterface)
 )%0A      
@@ -3257,16 +3257,58 @@
 ast test
+: no non-loopback IPv4 interface available
 %22);%0A    
@@ -4077,10 +4077,359 @@
 );%0A   %7D%0A
+   %0A   private boolean isBroadcastAddressAvailable(NetworkInterface networkInterface) %7B%0A      for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) %7B%0A         InetAddress broadcast = interfaceAddress.getBroadcast();%0A         if (broadcast != null) %7B%0A             return true;%0A         %7D%0A      %7D%0A      return false;%0A   %7D%0A
 %7D%0A

Improve testcase to avoid verifying broadcast when ipv6 only interfaces are available