public void testFindById() throws Exception {
    loadDataSetXml("admin/commserver/seedLocationsAndServices.xml");
    Location[] locations = m_out.getLocations();

    Location firstLocation = locations[0];
    int locationId = firstLocation.getId();

    Location locationById = m_out.getLocation(locationId);
    assertNotNull(locationById);
    assertEquals(firstLocation.getName(), locationById.getName());

    Collection<LocationSpecificService> services = locationById.getServices();
    assertNotNull(services);
    assertEquals(3, services.size());
  }
 @Test
 public void findMergedAnnotationAttributesOnSubSubInheritedAnnotationInterface() {
   AnnotationAttributes attributes =
       findMergedAnnotationAttributes(
           SubSubInheritedAnnotationInterface.class, Transactional.class);
   assertNotNull("Should find @Transactional on SubSubInheritedAnnotationInterface", attributes);
 }
 /**
  * If the "value" entry contains both "DerivedTxConfig" AND "TxConfig", then the algorithm is
  * accidentally picking up shadowed annotations of the same type within the class hierarchy. Such
  * undesirable behavior would cause the logic in {@link
  * org.springframework.context.annotation.ProfileCondition} to fail.
  *
  * @see
  *     org.springframework.core.env.EnvironmentSystemIntegrationTests#mostSpecificDerivedClassDrivesEnvironment_withDevEnvAndDerivedDevConfigClass
  */
 @Test
 public void
     getAllAnnotationAttributesOnClassWithLocalAnnotationThatShadowsAnnotationFromSuperclass() {
   MultiValueMap<String, Object> attributes =
       getAllAnnotationAttributes(DerivedTxConfig.class, TX_NAME);
   assertNotNull("Annotation attributes map for @Transactional on DerivedTxConfig", attributes);
   assertEquals("value for DerivedTxConfig.", asList("DerivedTxConfig"), attributes.get("value"));
 }
 @Test
 public void getAllAnnotationAttributesOnClassWithLocalComposedAnnotationAndInheritedAnnotation() {
   MultiValueMap<String, Object> attributes =
       getAllAnnotationAttributes(SubClassWithInheritedAnnotation.class, TX_NAME);
   assertNotNull(
       "Annotation attributes map for @Transactional on SubClassWithInheritedAnnotation",
       attributes);
   assertEquals(asList("composed2", "transactionManager"), attributes.get("qualifier"));
 }
 @Test
 public void getMergedAnnotationAttributesOnNonInheritedAnnotationInterface() {
   Class<?> element = NonInheritedAnnotationInterface.class;
   String name = Order.class.getName();
   AnnotationAttributes attributes = getMergedAnnotationAttributes(element, name);
   assertNotNull("Should find @Order on NonInheritedAnnotationInterface", attributes);
   // Verify contracts between utility methods:
   assertTrue(isAnnotated(element, name));
 }
 @Test
 public void getMergedAnnotationAttributesOnInheritedAnnotationInterface() {
   Class<?> element = InheritedAnnotationInterface.class;
   String name = TX_NAME;
   AnnotationAttributes attributes = getMergedAnnotationAttributes(element, name);
   assertNotNull("Should find @Transactional on InheritedAnnotationInterface", attributes);
   // Verify contracts between utility methods:
   assertTrue(isAnnotated(element, name));
 }
 @Test
 public void findMergedAnnotationAttributesInheritedFromInterfaceMethod()
     throws NoSuchMethodException {
   Method method = ConcreteClassWithInheritedAnnotation.class.getMethod("handleFromInterface");
   AnnotationAttributes attributes = findMergedAnnotationAttributes(method, Order.class);
   assertNotNull(
       "Should find @Order on ConcreteClassWithInheritedAnnotation.handleFromInterface() method",
       attributes);
 }
 @Test
 public void findMergedAnnotationAttributesInheritedFromAbstractMethod()
     throws NoSuchMethodException {
   Method method = ConcreteClassWithInheritedAnnotation.class.getMethod("handle");
   AnnotationAttributes attributes = findMergedAnnotationAttributes(method, Transactional.class);
   assertNotNull(
       "Should find @Transactional on ConcreteClassWithInheritedAnnotation.handle() method",
       attributes);
 }
  private AnnotationAttributes assertComponentScanAttributes(Class<?> element, String... expected) {
    AnnotationAttributes attributes = findMergedAnnotationAttributes(element, ComponentScan.class);

    assertNotNull("Should find @ComponentScan on " + element, attributes);
    assertArrayEquals("value: ", expected, attributes.getStringArray("value"));
    assertArrayEquals("basePackages: ", expected, attributes.getStringArray("basePackages"));

    return attributes;
  }
 @Test
 public void findMergedAnnotationWithAttributeAliasesInTargetAnnotation() {
   Class<?> element = AliasedTransactionalComponentClass.class;
   AliasedTransactional annotation = findMergedAnnotation(element, AliasedTransactional.class);
   assertNotNull("@AliasedTransactional on " + element, annotation);
   assertEquals("TX value via synthesized annotation.", "aliasForQualifier", annotation.value());
   assertEquals(
       "TX qualifier via synthesized annotation.", "aliasForQualifier", annotation.qualifier());
 }
 @Test
 public void getMergedAnnotationAttributesOnClassWithLocalAnnotation() {
   Class<?> element = TxConfig.class;
   String name = TX_NAME;
   AnnotationAttributes attributes = getMergedAnnotationAttributes(element, name);
   assertNotNull("Annotation attributes for @Transactional on TxConfig", attributes);
   assertEquals("value for TxConfig.", "TxConfig", attributes.getString("value"));
   // Verify contracts between utility methods:
   assertTrue(isAnnotated(element, name));
 }
 @Test
 public void
     getAllAnnotationAttributesFavorsInheritedComposedAnnotationsOverMoreLocallyDeclaredComposedAnnotations() {
   MultiValueMap<String, Object> attributes =
       getAllAnnotationAttributes(SubSubClassWithInheritedComposedAnnotation.class, TX_NAME);
   assertNotNull(
       "Annotation attributes map for @Transactional on SubSubClassWithInheritedComposedAnnotation",
       attributes);
   assertEquals(asList("composed1"), attributes.get("qualifier"));
 }
 @Test
 public void findMergedAnnotationAttributesOnClassWithMetaAndLocalTxConfig() {
   AnnotationAttributes attributes =
       findMergedAnnotationAttributes(MetaAndLocalTxConfigClass.class, Transactional.class);
   assertNotNull("Should find @Transactional on MetaAndLocalTxConfigClass", attributes);
   assertEquals(
       "TX qualifier for MetaAndLocalTxConfigClass.",
       "localTxMgr",
       attributes.getString("qualifier"));
 }
 /**
  * Note: this functionality is required by {@link
  * org.springframework.context.annotation.ProfileCondition}.
  *
  * @see org.springframework.core.env.EnvironmentSystemIntegrationTests
  */
 @Test
 public void getAllAnnotationAttributesOnClassWithMultipleComposedAnnotations() {
   MultiValueMap<String, Object> attributes =
       getAllAnnotationAttributes(TxFromMultipleComposedAnnotations.class, TX_NAME);
   assertNotNull(
       "Annotation attributes map for @Transactional on TxFromMultipleComposedAnnotations",
       attributes);
   assertEquals(
       "value for TxFromMultipleComposedAnnotations.",
       asList("TxInheritedComposed", "TxComposed"),
       attributes.get("value"));
 }
  @Test
  public void
      findMergedAnnotationAttributesOnClassWithAttributeAliasInComposedAnnotationAndNestedAnnotationsInTargetAnnotation() {
    AnnotationAttributes attributes =
        assertComponentScanAttributes(TestComponentScanClass.class, "com.example.app.test");

    Filter[] excludeFilters = attributes.getAnnotationArray("excludeFilters", Filter.class);
    assertNotNull(excludeFilters);

    List<String> patterns = stream(excludeFilters).map(Filter::pattern).collect(toList());
    assertEquals(asList("*Test", "*Tests"), patterns);
  }
 @Test
 public void getMergedAnnotationAttributesFavorsLocalComposedAnnotationOverInheritedAnnotation() {
   Class<?> element = SubClassWithInheritedAnnotation.class;
   String name = TX_NAME;
   AnnotationAttributes attributes = getMergedAnnotationAttributes(element, name);
   assertNotNull(
       "AnnotationAttributes for @Transactional on SubClassWithInheritedAnnotation", attributes);
   // Verify contracts between utility methods:
   assertTrue(isAnnotated(element, name));
   assertTrue(
       "readOnly flag for SubClassWithInheritedAnnotation.", attributes.getBoolean("readOnly"));
 }
  private void assertGetMergedAnnotation(Class<?> element, String... expected) {
    String name = ContextConfig.class.getName();
    ContextConfig contextConfig = getMergedAnnotation(element, ContextConfig.class);

    assertNotNull("Should find @ContextConfig on " + element.getSimpleName(), contextConfig);
    assertArrayEquals("locations", expected, contextConfig.locations());
    assertArrayEquals("value", expected, contextConfig.value());
    assertArrayEquals("classes", new Class<?>[0], contextConfig.classes());

    // Verify contracts between utility methods:
    assertTrue(isAnnotated(element, name));
  }
  @Test
  public void findAndSynthesizeAnnotationAttributesOnClassWithAttributeAliasesInTargetAnnotation() {
    String qualifier = "aliasForQualifier";

    // 1) Find and merge AnnotationAttributes from the annotation hierarchy
    AnnotationAttributes attributes =
        findMergedAnnotationAttributes(
            AliasedTransactionalComponentClass.class, AliasedTransactional.class);
    assertNotNull("@AliasedTransactional on AliasedTransactionalComponentClass.", attributes);

    // 2) Synthesize the AnnotationAttributes back into the target annotation
    AliasedTransactional annotation =
        AnnotationUtils.synthesizeAnnotation(
            attributes, AliasedTransactional.class, AliasedTransactionalComponentClass.class);
    assertNotNull(annotation);

    // 3) Verify that the AnnotationAttributes and synthesized annotation are equivalent
    assertEquals("TX value via attributes.", qualifier, attributes.getString("value"));
    assertEquals("TX value via synthesized annotation.", qualifier, annotation.value());
    assertEquals("TX qualifier via attributes.", qualifier, attributes.getString("qualifier"));
    assertEquals("TX qualifier via synthesized annotation.", qualifier, annotation.qualifier());
  }
  @Test
  public void getMergedAnnotationAttributesWithAliasedValueComposedAnnotation() {
    Class<?> element = AliasedValueComposedContextConfigClass.class;
    String name = ContextConfig.class.getName();
    AnnotationAttributes attributes = getMergedAnnotationAttributes(element, name);

    assertNotNull("Should find @ContextConfig on " + element.getSimpleName(), attributes);
    assertArrayEquals("locations", asArray("test.xml"), attributes.getStringArray("locations"));
    assertArrayEquals("value", asArray("test.xml"), attributes.getStringArray("value"));

    // Verify contracts between utility methods:
    assertTrue(isAnnotated(element, name));
  }
  @Test
  public void findMergedAnnotationForMultipleMetaAnnotationsWithClashingAttributeNames() {
    String[] xmlLocations = asArray("test.xml");
    String[] propFiles = asArray("test.properties");

    Class<?> element = AliasedComposedContextConfigAndTestPropSourceClass.class;

    ContextConfig contextConfig = findMergedAnnotation(element, ContextConfig.class);
    assertNotNull("@ContextConfig on " + element, contextConfig);
    assertArrayEquals("locations", xmlLocations, contextConfig.locations());
    assertArrayEquals("value", xmlLocations, contextConfig.value());

    // Synthesized annotation
    TestPropSource testPropSource = AnnotationUtils.findAnnotation(element, TestPropSource.class);
    assertArrayEquals("locations", propFiles, testPropSource.locations());
    assertArrayEquals("value", propFiles, testPropSource.value());

    // Merged annotation
    testPropSource = findMergedAnnotation(element, TestPropSource.class);
    assertNotNull("@TestPropSource on " + element, testPropSource);
    assertArrayEquals("locations", propFiles, testPropSource.locations());
    assertArrayEquals("value", propFiles, testPropSource.value());
  }
  @Test
  public void
      findMergedAnnotationWithLocalAliasesThatConflictWithAttributesInMetaAnnotationByConvention() {
    final String[] EMPTY = new String[0];
    Class<?> element = SpringAppConfigClass.class;
    ContextConfig contextConfig = findMergedAnnotation(element, ContextConfig.class);

    assertNotNull("Should find @ContextConfig on " + element, contextConfig);
    assertArrayEquals("locations for " + element, EMPTY, contextConfig.locations());
    // 'value' in @SpringAppConfig should not override 'value' in @ContextConfig
    assertArrayEquals("value for " + element, EMPTY, contextConfig.value());
    assertArrayEquals(
        "classes for " + element, new Class<?>[] {Number.class}, contextConfig.classes());
  }
  @Test
  public void getMergedAnnotationWithImplicitAliasesInMetaAnnotationOnComposedAnnotation() {
    Class<?> element = ComposedImplicitAliasesContextConfigClass.class;
    String name = ImplicitAliasesContextConfig.class.getName();
    ImplicitAliasesContextConfig config =
        getMergedAnnotation(element, ImplicitAliasesContextConfig.class);
    String[] expected = asArray("A.xml", "B.xml");

    assertNotNull(
        "Should find @ImplicitAliasesContextConfig on " + element.getSimpleName(), config);
    assertArrayEquals("groovyScripts", expected, config.groovyScripts());
    assertArrayEquals("xmlFiles", expected, config.xmlFiles());
    assertArrayEquals("locations", expected, config.locations());
    assertArrayEquals("value", expected, config.value());

    // Verify contracts between utility methods:
    assertTrue(isAnnotated(element, name));
  }
  private void getMergedAnnotationAttributesWithHalfConventionBasedAndHalfAliasedComposedAnnotation(
      Class<?> clazz) {
    String[] expected = asArray("explicitDeclaration");
    String name = ContextConfig.class.getName();
    String simpleName = clazz.getSimpleName();
    AnnotationAttributes attributes = getMergedAnnotationAttributes(clazz, name);

    assertNotNull("Should find @ContextConfig on " + simpleName, attributes);
    assertArrayEquals(
        "locations for class [" + clazz.getSimpleName() + "]",
        expected,
        attributes.getStringArray("locations"));
    assertArrayEquals(
        "value for class [" + clazz.getSimpleName() + "]",
        expected,
        attributes.getStringArray("value"));

    // Verify contracts between utility methods:
    assertTrue(isAnnotated(clazz, name));
  }
  /**
   * Bridge/bridged method setup code copied from {@link
   * org.springframework.core.BridgeMethodResolverTests#testWithGenericParameter()}.
   *
   * @since 4.2
   */
  @Test
  public void findMergedAnnotationAttributesFromBridgeMethod() throws NoSuchMethodException {
    Method[] methods = StringGenericParameter.class.getMethods();
    Method bridgeMethod = null;
    Method bridgedMethod = null;

    for (Method method : methods) {
      if ("getFor".equals(method.getName())
          && !method.getParameterTypes()[0].equals(Integer.class)) {
        if (method.getReturnType().equals(Object.class)) {
          bridgeMethod = method;
        } else {
          bridgedMethod = method;
        }
      }
    }
    assertTrue(bridgeMethod != null && bridgeMethod.isBridge());
    assertTrue(bridgedMethod != null && !bridgedMethod.isBridge());

    AnnotationAttributes attributes = findMergedAnnotationAttributes(bridgeMethod, Order.class);
    assertNotNull(
        "Should find @Order on StringGenericParameter.getFor() bridge method", attributes);
  }
Exemple #25
0
 public void assertTestRun(String name) {
   TestCaseEvent event = testCaseEvents.get(name);
   assertNotNull(name + " was never run!", event);
 }
Exemple #26
0
 @Mock(invocations = 1)
 public void $init(Invocation inv, String config) {
   assertNotNull(inv.getInvokedInstance());
   assertEquals("config", config);
 }
 private void assertWebMapping(AnnotatedElement element) throws ArrayComparisonFailure {
   WebMapping webMapping = findMergedAnnotation(element, WebMapping.class);
   assertNotNull(webMapping);
   assertArrayEquals("value attribute: ", asArray("/test"), webMapping.value());
   assertArrayEquals("path attribute: ", asArray("/test"), webMapping.path());
 }
 @Test
 public void findMergedAnnotationAttributesOnSubSubNonInheritedAnnotationInterface() {
   AnnotationAttributes attributes =
       findMergedAnnotationAttributes(SubSubNonInheritedAnnotationInterface.class, Order.class);
   assertNotNull("Should find @Order on SubSubNonInheritedAnnotationInterface", attributes);
 }
Exemple #29
0
 @Mock(minInvocations = 1, maxInvocations = 3)
 public int computeX(Invocation inv, int a, int b) {
   assertTrue(a + b >= 0);
   assertNotNull(inv.getInvokedInstance());
   return a - b;
 }
 @Test
 public void getAllAnnotationAttributesOnClassWithLocalAnnotation() {
   MultiValueMap<String, Object> attributes = getAllAnnotationAttributes(TxConfig.class, TX_NAME);
   assertNotNull("Annotation attributes map for @Transactional on TxConfig", attributes);
   assertEquals("value for TxConfig.", asList("TxConfig"), attributes.get("value"));
 }