コード例 #1
0
  @Before
  public void setUp() throws Exception {
    authorizationManager = mocker.registerMockComponent(ContextualAuthorizationManager.class);

    BeanDescriptor mockBeanDescriptor = mock(BeanDescriptor.class);
    when(mockBeanDescriptor.getProperties()).thenReturn(Collections.EMPTY_LIST);

    BeanManager beanManager = mocker.getInstance(BeanManager.class);
    when(beanManager.getBeanDescriptor(any(Class.class))).thenReturn(mockBeanDescriptor);

    Macro velocityMacro = mocker.getComponentUnderTest();
    MacroManager mockMacroManager = mocker.registerMockComponent(MacroManager.class);
    when(mockMacroManager.getMacro(any(MacroId.class))).thenReturn(velocityMacro);
  }
コード例 #2
0
  /**
   * Populate the provided bean with provided values.
   *
   * <p><code>values</code> is "consumed": when method executing is finished it only contains not
   * populated properties.
   *
   * @param bean the java bean to populate
   * @param values the values to convert and inject in the java bean
   * @throws PropertyException error when populating the bean
   */
  private void populateBean(Object bean, Map<String, Object> values) throws PropertyException {
    BeanDescriptor beanDescriptor = getBeanDescriptor(bean.getClass());

    // Lower case provided properties to easily ignore properties name case
    Map<String, String> lowerKeyMap = new HashMap<String, String>(values.size());
    for (Map.Entry<String, ?> entry : values.entrySet()) {
      lowerKeyMap.put(entry.getKey().toLowerCase(), entry.getKey());
    }

    for (PropertyDescriptor propertyDescriptor : beanDescriptor.getProperties()) {
      String propertyId = propertyDescriptor.getId();
      Object value = values.get(propertyId);

      if (value == null) {
        propertyId = propertyId.toLowerCase();
        value = values.get(lowerKeyMap.get(propertyId));
      }

      if (value != null) {
        try {
          // Convert
          Object convertedValue =
              this.converterManager.convert(propertyDescriptor.getPropertyClass(), value);

          if (propertyDescriptor.getWriteMethod() != null) {
            propertyDescriptor.getWriteMethod().invoke(bean, convertedValue);
          } else if (propertyDescriptor.getField() != null) {
            propertyDescriptor.getField().set(bean, convertedValue);
          }
        } catch (Exception e) {
          throw new PropertyException("Failed to populate property [" + propertyId + "]", e);
        }

        // "Tick" already populated properties
        values.remove(propertyId);
      } else if (propertyDescriptor.isMandatory()) {
        throw new PropertyMandatoryException(propertyId);
      }
    }
  }