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.welder.test;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
018    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
019    import com.liferay.portal.kernel.nio.intraband.welder.Welder;
020    
021    import java.io.ObjectInputStream;
022    import java.io.ObjectOutputStream;
023    
024    import java.nio.ByteBuffer;
025    import java.nio.channels.GatheringByteChannel;
026    import java.nio.channels.ScatteringByteChannel;
027    
028    import java.util.Random;
029    import java.util.concurrent.Callable;
030    import java.util.concurrent.FutureTask;
031    
032    import org.junit.Assert;
033    
034    /**
035     * @author Shuyang Zhou
036     */
037    public class WelderTestUtil {
038    
039            public static void assertConnectted(
040                            final ScatteringByteChannel scatteringByteChannel,
041                            final GatheringByteChannel gatheringByteChannel)
042                    throws Exception {
043    
044                    Random random = new Random();
045    
046                    final byte[] data = new byte[1024 * 1024];
047    
048                    random.nextBytes(data);
049    
050                    FutureTask<Void> writeFutureTask = new FutureTask<Void>(
051                            new Callable<Void>() {
052    
053                                    @Override
054                                    public Void call() throws Exception {
055                                            ByteBuffer byteBuffer = ByteBuffer.wrap(data);
056    
057                                            while (byteBuffer.hasRemaining()) {
058                                                    gatheringByteChannel.write(byteBuffer);
059                                            }
060    
061                                            return null;
062                                    }
063    
064                            });
065    
066                    Thread writeThread = new Thread(writeFutureTask);
067    
068                    writeThread.start();
069    
070                    FutureTask<byte[]> readFutureTask = new FutureTask<byte[]>(
071                            new Callable<byte[]>() {
072    
073                                    @Override
074                                    public byte[] call() throws Exception {
075                                            ByteBuffer byteBuffer = ByteBuffer.allocate(data.length);
076    
077                                            while (byteBuffer.hasRemaining()) {
078                                                    scatteringByteChannel.read(byteBuffer);
079                                            }
080    
081                                            return byteBuffer.array();
082                                    }
083    
084                            });
085    
086                    Thread readThread = new Thread(readFutureTask);
087    
088                    readThread.start();
089    
090                    writeFutureTask.get();
091    
092                    Assert.assertArrayEquals(data, readFutureTask.get());
093            }
094    
095            public static <T extends Welder> T transform(T welder) throws Exception {
096                    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
097                            new UnsyncByteArrayOutputStream();
098    
099                    try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(
100                                    unsyncByteArrayOutputStream)) {
101    
102                            objectOutputStream.writeObject(welder);
103                    }
104    
105                    ByteBuffer byteBuffer =
106                            unsyncByteArrayOutputStream.unsafeGetByteBuffer();
107    
108                    UnsyncByteArrayInputStream unsyncByteArrayInputStream =
109                            new UnsyncByteArrayInputStream(
110                                    byteBuffer.array(), 0, byteBuffer.remaining());
111    
112                    ObjectInputStream objectInputStream = new ObjectInputStream(
113                            unsyncByteArrayInputStream);
114    
115                    return (T)objectInputStream.readObject();
116            }
117    
118    }