Package org.xlightweb

Source Code of org.xlightweb.AsyncConnectTest$ConnectHandler

/*
*  Copyright (c) xlightweb.org, 2008 - 2009. All rights reserved.
*
*  This library 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 library 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 library; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
* The latest copy of this software may be found on http://www.xlightweb.org/
*/
package org.xlightweb;



import java.io.IOException;
import java.net.BindException;
import java.net.InetAddress;
import java.nio.BufferUnderflowException;
import java.util.logging.Level;

import org.junit.Assert;
import org.junit.Test;
import org.xlightweb.client.HttpClientConnection;
import org.xlightweb.client.IHttpClientEndpoint;
import org.xsocket.MaxReadSizeExceededException;
import org.xsocket.connection.IConnectExceptionHandler;
import org.xsocket.connection.IConnectHandler;
import org.xsocket.connection.INonBlockingConnection;
import org.xsocket.connection.NonBlockingConnection;



/**
*
* @author grro@xlightweb.org
*/
public final class AsyncConnectTest  {

 
  @Test
  public void testLifeAsyncConnect() throws Exception {
      
      ConnectHandler ch = new ConnectHandler();
      new NonBlockingConnection(InetAddress.getByName("www.web.de"), 80, ch, false, 2000);
     
      QAUtil.sleep(1500);
     
      IHttpClientEndpoint httpEndpoint = ch.getHttpConnection();
      IHttpResponse response = httpEndpoint.call(new GetRequest("/"));

      Assert.assertEquals(200, response.getStatus());
  }

 
  @Test
  public void testAsyncConnectFailed() throws Exception {
         
    int connectionTimeoutMillis = 1000;
   
      ConnectHandler ch = new ConnectHandler();
      new NonBlockingConnection(InetAddress.getByName("192.168.255.255"), 80, ch, false, connectionTimeoutMillis);
         
      QAUtil.sleep(3000);
     
      if (ch.getIOException() == null) {
        System.out.println("exception expected");
        Assert.fail("exception expected");
      } else {
        System.out.println("expected exception: " + ch.getIOException().toString());
      }         
  }

 
  private static final class ConnectHandler implements IConnectHandler, IConnectExceptionHandler {
     
      private HttpClientConnection httpCon;
      private IOException ioe;
     
      public boolean onConnect(INonBlockingConnection connection) throws IOException, BufferUnderflowException, MaxReadSizeExceededException {
          httpCon = new HttpClientConnection(connection);
          return true;
      }
     
   
      public boolean onConnectException(INonBlockingConnection connection, IOException ioe) throws IOException {
          this.ioe = ioe;
          return true;
      }
     
     
      HttpClientConnection getHttpConnection() {
          return httpCon;
      }
     
      IOException getIOException( ) {
          return ioe;
      }
  }
}
TOP

Related Classes of org.xlightweb.AsyncConnectTest$ConnectHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.