@Test public void execute_handles_unexpected_exceptions() throws Exception { final Context context = Context.create("key", "val"); Parameter[] parameters = new Parameter[]{new Parameter("toto")}; Collection<Context> contexts = new ArrayList<Context>() { private static final long serialVersionUID = 1L; { add(context); }}; ManagementProvider<?> managementProvider = mock(ManagementProvider.class); when(managementProvider.supports(context)).thenReturn(true); RuntimeException runtimeException = new RuntimeException("Oups, that was not supposed to happen !"); when(managementProvider.callAction(context, "myMethodName", String.class, parameters)).thenThrow(runtimeException); CapabilityManagementSupport capabilityManagementSupport = mock(CapabilityManagementSupport.class); Collection<ManagementProvider<?>> managementProviders = new ArrayList<ManagementProvider<?>>(); managementProviders.add(managementProvider); when(capabilityManagementSupport.getManagementProvidersByCapability("myCapabilityName")).thenReturn(managementProviders); DefaultCallQuery<String> defaultCallQuery = new DefaultCallQuery<String>(capabilityManagementSupport, "myCapabilityName", "myMethodName", String.class, parameters, contexts); ResultSet<ContextualReturn<String>> executeResults = defaultCallQuery.execute(); ContextualReturn<String> singleResult = executeResults.getSingleResult(); try { singleResult.getValue(); fail(); } catch (Exception e) { assertThat(e, is(instanceOf(ExecutionException.class))); assertThat(e.getMessage(), equalTo("Oups, that was not supposed to happen !")); } }
@Override public ResultSet<ContextualReturn<T>> execute() { Map<Context, ContextualReturn<T>> contextualResults = new LinkedHashMap<Context, ContextualReturn<T>>(contexts.size()); Collection<ManagementProvider<?>> managementProviders = capabilityManagement.getManagementProvidersByCapability(capabilityName); for (Context context : contexts) { ContextualReturn<T> result = ContextualReturn.notExecuted(capabilityName, context, methodName); for (ManagementProvider<?> managementProvider : managementProviders) { if (managementProvider.supports(context)) { try { result = ContextualReturn.of(capabilityName, context, methodName, managementProvider.callAction(context, methodName, returnType, parameters)); } catch (ExecutionException e) { result = ContextualReturn.error(capabilityName, context, methodName, e); } catch (Exception e) { result = ContextualReturn.error(capabilityName, context, methodName, new ExecutionException(e.getMessage(), e)); } break; } } contextualResults.put(context, result); } return new DefaultResultSet<ContextualReturn<T>>(contextualResults); }
DefaultCallQuery implements CallQuery<T> { @Override public ResultSet<ContextualReturn<T>> execute() { Map<Context, ContextualReturn<T>> contextualResults = new LinkedHashMap<Context, ContextualReturn<T>>(contexts.size()); Collection<ManagementProvider<?>> managementProviders = capabilityManagement.getManagementProvidersByCapability(capabilityName); for (Context context : contexts) { ContextualReturn<T> result = ContextualReturn.notExecuted(capabilityName, context, methodName); for (ManagementProvider<?> managementProvider : managementProviders) { if (managementProvider.supports(context)) { try { result = ContextualReturn.of(capabilityName, context, methodName, managementProvider.callAction(context, methodName, returnType, parameters)); } catch (ExecutionException e) { result = ContextualReturn.error(capabilityName, context, methodName, e); } catch (Exception e) { result = ContextualReturn.error(capabilityName, context, methodName, new ExecutionException(e.getMessage(), e)); } break; } } contextualResults.put(context, result); } return new DefaultResultSet<ContextualReturn<T>>(contextualResults); } }
DefaultCallQuery implements CallQuery<T> { @Override public ResultSet<ContextualReturn<T>> execute() { Map<Context, ContextualReturn<T>> contextualResults = new LinkedHashMap<Context, ContextualReturn<T>>(contexts.size()); Collection<ManagementProvider<?>> managementProviders = capabilityManagement.getManagementProvidersByCapability(capabilityName); for (Context context : contexts) { ContextualReturn<T> result = ContextualReturn.notExecuted(capabilityName, context, methodName); for (ManagementProvider<?> managementProvider : managementProviders) { if (managementProvider.supports(context)) { try { result = ContextualReturn.of(capabilityName, context, methodName, managementProvider.callAction(context, methodName, returnType, parameters)); } catch (ExecutionException e) { result = ContextualReturn.error(capabilityName, context, methodName, e); } catch (Exception e) { result = ContextualReturn.error(capabilityName, context, methodName, new ExecutionException(e.getMessage(), e)); } break; } } contextualResults.put(context, result); } return new DefaultResultSet<ContextualReturn<T>>(contextualResults); } @SuppressFBWarnings("EI_EXPOSE_REP2")  DefaultCallQuery(CapabilityManagementSupport capabilityManagement, String capabilityName, String methodName, Class<T> returnType, Parameter[] parameters, Collection<Context> contexts); }
DefaultCallQuery implements CallQuery<T> { @Override public ResultSet<ContextualReturn<T>> execute() { Map<Context, ContextualReturn<T>> contextualResults = new LinkedHashMap<Context, ContextualReturn<T>>(contexts.size()); Collection<ManagementProvider<?>> managementProviders = capabilityManagement.getManagementProvidersByCapability(capabilityName); for (Context context : contexts) { ContextualReturn<T> result = ContextualReturn.notExecuted(capabilityName, context, methodName); for (ManagementProvider<?> managementProvider : managementProviders) { if (managementProvider.supports(context)) { try { result = ContextualReturn.of(capabilityName, context, methodName, managementProvider.callAction(context, methodName, returnType, parameters)); } catch (ExecutionException e) { result = ContextualReturn.error(capabilityName, context, methodName, e); } catch (Exception e) { result = ContextualReturn.error(capabilityName, context, methodName, new ExecutionException(e.getMessage(), e)); } break; } } contextualResults.put(context, result); } return new DefaultResultSet<ContextualReturn<T>>(contextualResults); } @SuppressFBWarnings("EI_EXPOSE_REP2")  DefaultCallQuery(CapabilityManagementSupport capabilityManagement, String capabilityName, String methodName, Class<T> returnType, Parameter[] parameters, Collection<Context> contexts); @Override Class<T> getReturnType(); @Override String getCapabilityName(); @Override Collection<Context> getContexts(); @Override String getMethodName(); @Override Parameter[] getParameters(); @Override ResultSet<ContextualReturn<T>> execute(); }
DefaultCallQuery implements CallQuery<T> { @Override public ResultSet<ContextualReturn<T>> execute() { Map<Context, ContextualReturn<T>> contextualResults = new LinkedHashMap<Context, ContextualReturn<T>>(contexts.size()); Collection<ManagementProvider<?>> managementProviders = capabilityManagement.getManagementProvidersByCapability(capabilityName); for (Context context : contexts) { ContextualReturn<T> result = ContextualReturn.notExecuted(capabilityName, context, methodName); for (ManagementProvider<?> managementProvider : managementProviders) { if (managementProvider.supports(context)) { try { result = ContextualReturn.of(capabilityName, context, methodName, managementProvider.callAction(context, methodName, returnType, parameters)); } catch (ExecutionException e) { result = ContextualReturn.error(capabilityName, context, methodName, e); } catch (Exception e) { result = ContextualReturn.error(capabilityName, context, methodName, new ExecutionException(e.getMessage(), e)); } break; } } contextualResults.put(context, result); } return new DefaultResultSet<ContextualReturn<T>>(contextualResults); } @SuppressFBWarnings("EI_EXPOSE_REP2")  DefaultCallQuery(CapabilityManagementSupport capabilityManagement, String capabilityName, String methodName, Class<T> returnType, Parameter[] parameters, Collection<Context> contexts); @Override Class<T> getReturnType(); @Override String getCapabilityName(); @Override Collection<Context> getContexts(); @Override String getMethodName(); @Override Parameter[] getParameters(); @Override ResultSet<ContextualReturn<T>> execute();  }