List of usage examples for org.apache.commons.lang3.mutable MutableObject setValue
@Override public void setValue(final T value)
From source file:org.jtestplatform.common.transport.TransportHelperTest.java
@Theory public void testSend(MessageData data) throws TransportException, InstantiationException, IllegalAccessException { // prepare/*from w w w.jav a 2 s . co m*/ Message message = spy(data.createMessage()); Transport transport = mock(Transport.class); final MutableObject<Boolean> called = new MutableObject<Boolean>(false); TransportHelper helper = new TransportHelper() { @Override protected void sendImpl(Transport transport, Message message) throws TransportException { called.setValue(true); super.sendImpl(transport, message); } }; // test helper.send(transport, message); // verify assertThat(called.getValue()).as("sendImpl called").isTrue(); InOrder inOrder = inOrder(transport, message); inOrder.verify(transport, times(1)).send(eq(message.getClass().getName())); inOrder.verify(message, times(1)).sendWith(eq(transport)); for (int i = 0; i < data.expectedParts.length; i++) { inOrder.verify(transport, times(1)).send(eq(data.expectedParts[i])); } inOrder.verifyNoMoreInteractions(); }
From source file:org.jtestplatform.common.transport.TransportHelperTest.java
@Test public void testReceive_ErrorMessage() throws Exception { // prepare//from w w w. j a va2s .co m final String expectedErrorMessage = "errorMessage"; thrown.expect(TransportException.class); thrown.expect(new BaseMatcher<Throwable>() { private final String exceptionMessage = "Received Error : " + expectedErrorMessage; @Override public void describeTo(Description description) { description.appendText("a TransportException with ErrorMessage(" + exceptionMessage + ")"); } @Override public boolean matches(Object item) { if (!(item instanceof TransportException)) { return false; } TransportException exception = (TransportException) item; if (!equalTo(exceptionMessage).matches(exception.getMessage())) { return false; } ErrorMessage errorMessage = exception.getErrorMessage(); return (errorMessage != null) && equalTo(expectedErrorMessage).matches(errorMessage.getMessage()); } }); final MutableObject<Message> messageWrapper = new MutableObject<Message>(); Transport transport = mock(Transport.class); when(transport.receive()).thenReturn(ErrorMessage.class.getName(), expectedErrorMessage); TransportHelper helper = new TransportHelper() { @Override Message createMessage(Class<? extends Message> clazz) throws InstantiationException, IllegalAccessException { Message message = spy(super.createMessage(clazz)); messageWrapper.setValue(message); return message; } }; // test helper.receive(transport); }
From source file:org.jtestplatform.common.transport.UDPClientServerTest.java
private Callable<Object> createServer(final MutableObject<String> actualRequest) { return new Callable<Object>() { @Override/* w ww.j a v a 2s . c om*/ public Object call() throws Exception { LOGGER.info("SERVER: receiving request"); UDPTransport transport = new UDPTransport(SERVER_PORT); String request = transport.receive(); actualRequest.setValue(request); LOGGER.info("SERVER: sending answer"); transport.send(ANSWER); LOGGER.info("SERVER: finished"); return null; } }; }
From source file:org.jtestplatform.common.transport.UDPClientServerTest.java
private Callable<Object> createClient(final MutableObject<String> actualAnswer) { return new Callable<Object>() { @Override//from w w w .j a va 2 s.c o m public Object call() throws Exception { LOGGER.info("CLIENT: sending request"); UDPTransport transport = new UDPTransport(InetAddress.getLocalHost(), SERVER_PORT, TIMEOUT); transport.send(REQUEST); LOGGER.info("CLIENT: receiving answer"); String answer = transport.receive(); actualAnswer.setValue(answer); LOGGER.info("CLIENT: finished"); return null; } }; }
From source file:org.nuxeo.ecm.core.api.CoreInstance.java
/** * Runs the given {@link Function} with a system {@link CoreSession} while logged in as a system user. * * @param repositoryName the repository name for the {@link CoreSession} * @param function the function taking a system {@link CoreSession} and returning a result of type {@code <R>} * @param <R> the function return type * @return the result of the function/*ww w.j a va2s. co m*/ * @since 8.4 */ public static <R> R doPrivileged(String repositoryName, Function<CoreSession, R> function) { MutableObject<R> result = new MutableObject<R>(); new UnrestrictedSessionRunner(repositoryName, getCurrentPrincipalName()) { @Override public void run() { result.setValue(function.apply(session)); } }.runUnrestricted(); return result.getValue(); }
From source file:org.nuxeo.ecm.core.api.CoreInstance.java
/** * Runs the given {@link Function} with a system {@link CoreSession} while logged in as a system user. * * @param session an existing session//from w ww.ja v a2 s. co m * @param function the function taking a system {@link CoreSession} and returning a result of type {@code <R>} * @param <R> the function return type * @return the result of the function * @since 8.4 */ public static <R> R doPrivileged(CoreSession session, Function<CoreSession, R> function) { MutableObject<R> result = new MutableObject<R>(); new UnrestrictedSessionRunner(session) { @Override public void run() { result.setValue(function.apply(session)); } }.runUnrestricted(); return result.getValue(); }
From source file:org.pircbotx.CAPTest.java
public void runTest(String cap, final OutputParser callback) throws Exception { final MutableObject<Exception> connectionException = new MutableObject<Exception>(); botInWrite = new PipedOutputStream(); botIn = new BufferedInputStream(new PipedInputStream(botInWrite)); botOut = new ByteArrayOutputStream() { @Override/*from w w w . ja v a 2s . co m*/ public synchronized void write(byte[] bytes, int i, int i1) { super.write(bytes, i, i1); String outputText = new String(bytes, i, i1).trim(); try { try { String callbackText = callback.handleOutput(outputText); if (callbackText == null) //Will close bots input loop botInWrite.close(); else if (!callbackText.equals("")) { botInWrite.write((callbackText + "\r\n").getBytes()); botInWrite.flush(); } } catch (Exception ex) { log.error("Recieved error, closing bot and escelating", ex); connectionException.setValue(ex); botInWrite.close(); } } catch (IOException ex) { log.error("Recieved IO error, closing bot and escelating", ex); connectionException.setValue(ex); try { botInWrite.close(); } catch (Exception e) { throw new RuntimeException("Can't close botInWrite", e); } } } }; Socket socket = mock(Socket.class); when(socket.isConnected()).thenReturn(true); when(socket.getInputStream()).thenReturn(botIn); when(socket.getOutputStream()).thenReturn(botOut); Configuration.Builder configurationBuilder = TestUtils.generateConfigurationBuilder(); SocketFactory socketFactory = mock(SocketFactory.class); when(socketFactory.createSocket( InetAddress.getByName(configurationBuilder.getServers().get(0).getHostname()), 6667, null, 0)) .thenReturn(socket); configurationBuilder.getCapHandlers().clear(); configurationBuilder.getCapHandlers().addAll(capHandlers); bot = new PircBotX( configurationBuilder.setSocketFactory(socketFactory).setAutoReconnect(false).buildConfiguration()); botInWrite.write((":ircd.test CAP * LS :" + cap + "\r\n").getBytes()); bot.connect(); if (connectionException.getValue() != null) throw connectionException.getValue(); }
From source file:org.pircbotx.dcc.DCCTest.java
@Test public void incommingDccChatTest() throws IOException, InterruptedException, IrcException { Inet6Address localhost6 = (Inet6Address) InetAddress.getByName("::1"); //Create listener to handle everything final MutableObject<IncomingChatRequestEvent> mutableEvent = new MutableObject<IncomingChatRequestEvent>(); bot.getConfiguration().getListenerManager().addListener(new ListenerAdapter() { @Override//from ww w . j a v a 2 s . c o m public void onIncomingChatRequest(IncomingChatRequestEvent event) throws Exception { mutableEvent.setValue(event); } }); System.out.println("localhost6 byte array: " + Arrays.toString(localhost6.getAddress())); System.out.println("localhost6 int: " + new BigInteger(localhost6.getAddress())); bot.getInputParser().handleLine(":ANick!~ALogin@::2 PRIVMSG Quackbot5 :DCC CHAT chat 1 35589"); IncomingChatRequestEvent event = mutableEvent.getValue(); assertNotNull(event, "No IncomingChatRequestEvent dispatched"); System.out.println("Test ran"); }
From source file:org.pircbotx.hooks.managers.ThreadedListenerManagerTest.java
@Test public void exceptionTest() { ThreadedListenerManager manager = new ThreadedListenerManager(MoreExecutors.newDirectExecutorService()); final MutableObject<ListenerExceptionEvent> eventResult = new MutableObject<ListenerExceptionEvent>(); Listener testListener;/*from www . j av a 2s.co m*/ manager.addListener(testListener = new Listener() { @Override public void onEvent(Event event) throws Exception { if (event instanceof ListenerExceptionEvent) { eventResult.setValue((ListenerExceptionEvent) event); } else throw new RuntimeException("Default fail"); } }); Event event = new ConnectEvent(null); manager.onEvent(event); assertEquals(eventResult.getValue().getListener(), testListener); assertEquals(eventResult.getValue().getSourceEvent(), event); assertNotNull(eventResult.getValue().getException()); }
From source file:org.pircbotx.hooks.TemporaryListenerTest.java
@Test(singleThreaded = true) public void eventDispatched() throws IOException, IrcException { final MutableObject<MessageEvent> mutableEvent = new MutableObject<MessageEvent>(); Listener listener = new TemporaryListener(bot) { @Override/* w w w .j av a2s. c o m*/ public void onMessage(MessageEvent event) throws Exception { mutableEvent.setValue(event); } }; listenerManager.addListener(listener); //Make sure the listener is there assertTrue(listenerManager.listenerExists(listener), "Listener doesn't exist in ListenerManager"); //Send some arbitrary line bot.getInputParser() .handleLine(":" + userSource.getHostmask() + " PRIVMSG #aChannel :Some very long message"); MessageEvent mevent = mutableEvent.getValue(); //Verify event contents assertNotNull(mevent, "MessageEvent not dispatched"); assertEquals(mevent.getMessage(), "Some very long message", "Message sent does not match"); //Make sure the listner is still there assertTrue(listenerManager.listenerExists(listener), "Listener doesn't exist in ListenerManager"); }