001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.nio.intraband;
016    
017    import com.liferay.portal.kernel.util.ProxyUtil;
018    import com.liferay.portal.kernel.util.SocketUtil;
019    import com.liferay.portal.kernel.util.SocketUtil.ServerSocketConfigurator;
020    
021    import java.io.IOException;
022    
023    import java.lang.reflect.InvocationHandler;
024    import java.lang.reflect.Method;
025    
026    import java.net.InetAddress;
027    import java.net.InetSocketAddress;
028    import java.net.ServerSocket;
029    import java.net.SocketException;
030    
031    import java.nio.channels.ScatteringByteChannel;
032    import java.nio.channels.ServerSocketChannel;
033    import java.nio.channels.SocketChannel;
034    
035    import java.util.logging.LogRecord;
036    
037    import org.junit.Assert;
038    
039    /**
040     * @author Shuyang Zhou
041     */
042    public class IntrabandTestUtil {
043    
044            public static void assertMessageStartWith(
045                    LogRecord logRecord, String messagePrefix) {
046    
047                    String message = logRecord.getMessage();
048    
049                    Assert.assertTrue(message.startsWith(messagePrefix));
050            }
051    
052            public static <T> T createProxy(Class<?>... interfaces) {
053                    return (T)ProxyUtil.newProxyInstance(
054                            IntrabandTestUtil.class.getClassLoader(), interfaces,
055                            new InvocationHandler() {
056    
057                                    @Override
058                                    public Object invoke(
059                                            Object proxy, Method method, Object[] args) {
060    
061                                            throw new UnsupportedOperationException();
062                                    }
063    
064                            });
065            }
066    
067            public static SocketChannel[] createSocketChannelPeers()
068                    throws IOException {
069    
070                    SocketChannel clientPeerSocketChannel = null;
071                    SocketChannel serverPeerSocketChannel = null;
072    
073                    try (ServerSocketChannel serverSocketChannel =
074                                    SocketUtil.createServerSocketChannel(
075                                            InetAddress.getLocalHost(), 15238,
076                                            _serverSocketConfigurator)) {
077    
078                            ServerSocket serverSocket = serverSocketChannel.socket();
079    
080                            clientPeerSocketChannel = SocketChannel.open(
081                                    new InetSocketAddress(
082                                            InetAddress.getLocalHost(), serverSocket.getLocalPort()));
083    
084                            serverPeerSocketChannel = serverSocketChannel.accept();
085                    }
086    
087                    SocketChannel[] socketChannels = new SocketChannel[2];
088    
089                    socketChannels[0] = serverPeerSocketChannel;
090                    socketChannels[1] = clientPeerSocketChannel;
091    
092                    return socketChannels;
093            }
094    
095            public static Datagram readDatagramFully(
096                            ScatteringByteChannel scatteringByteChannel)
097                    throws IOException {
098    
099                    Datagram datagram = DatagramHelper.createReceiveDatagram();
100    
101                    while (!DatagramHelper.readFrom(datagram, scatteringByteChannel));
102    
103                    return datagram;
104            }
105    
106            private static final ServerSocketConfigurator _serverSocketConfigurator =
107                    new ServerSocketConfigurator() {
108    
109                            @Override
110                            public void configure(ServerSocket serverSocket)
111                                    throws SocketException {
112    
113                                    serverSocket.setReuseAddress(true);
114                            }
115    
116                    };
117    
118    }