コード例 #1
0
 @GET
 @Produces({"image/*", "text/*"})
 public Response getResource() {
   try {
     final PluginContext context = pluginsService.getPluginContext(pluginId);
     final String resPath = context.getResDirPath() + resource;
     final String mimeType = getMimeType(getExtension(resPath));
     final InputStream res =
         pluginsService.getLoader().getResourceAsStream(context.getSource(), resPath);
     if (res != null) {
       return Response.ok(res, mimeType).build();
     }
     return Response.status(NOT_FOUND).build();
   } catch (Exception e) {
     logger.warn(
         String.format("Failed to find the resource %s for plugin %s", resource, pluginId), e);
     return Response.status(NOT_FOUND).build();
   }
 }
  /** Bad style test :( */
  @Test
  public void testThatAnnotatedMethodInvokerIsWorkingAsExpected() throws Exception {
    CamelContext context = mock(CamelContext.class);
    Plugin plugin = new Plugin();
    PluginContext pluginContext = new PluginContext();
    pluginContext.setClassLoader(getClass().getClassLoader());
    pluginContext.setInjector(new PluginContextInjectorImpl());
    pluginContext.setPluginClass(TestAggregatorImpl.class.getName());
    final BasicMessagesSerializer serializer = new BasicMessagesSerializer();
    pluginContext.setMessagesSerializer(serializer);
    plugin.setAggregator(TestAggregatorImpl.class.getName());
    plugin.setContext(pluginContext);
    AggregationRepository repo = mock(AggregationRepository.class);
    FoundMethodProcessor proc = mock(FoundMethodProcessor.class);
    TestState state = new TestState();
    Exchange exchange = mock(Exchange.class);
    Message message = spy(new JmsMessage(mock(javax.jms.Message.class), mock(JmsBinding.class)));
    message.setBody(
        serializer.processBodyAndHeadersBeforeSend(
            state, new HashMap<String, Object>(), getSystemClassLoader()));
    when(message.getHeader(BODY_CLASS)).thenReturn(TestState.class.getName());
    when(exchange.getIn()).thenReturn(message);
    when(repo.getKeys()).thenReturn(new HashSet<>(asList("key1", "key2")));
    when(repo.get(context, "key1")).thenReturn(exchange);
    when(repo.get(context, "key2")).thenReturn(exchange);
    when(proc.appliesTo(any(Method.class), any(Annotation.class))).thenReturn(true);
    pluginContext.setAggregationRepo(repo);

    AggregatorPluginAnnotatedMethodInvoker invoker =
        new AggregatorPluginAnnotatedMethodInvoker(context, plugin, OnClientMessage.class, false);
    invoker.process(proc);

    invoker.invoke("test");

    verify(proc)
        .appliesTo(
            eq(TestAggregatorImpl.class.getMethod("onBroadcast", TestState.class)),
            any(Annotation.class));
    verify(proc)
        .appliesTo(
            eq(TestAggregatorImpl.class.getMethod("onBroadcast2", TestState.class, String.class)),
            any(Annotation.class));
    verify(aggMock, times(2)).onBroadcast(any(TestState.class));
    verify(aggMock, times(2)).onBroadcast2(any(TestState.class), eq("test"));
    verify(message, atLeast(5)).setBody(any());
    verify(repo, times(2)).get(context, "key1");
    verify(repo, times(2)).get(context, "key2");
    verify(repo, times(2)).getKeys();
    verify(repo, times(2)).add(context, "key1", exchange);
    verify(repo, times(2)).add(context, "key2", exchange);
    verifyNoMoreInteractions(aggMock, repo);
  }