Package org.springframework.core.annotation

Examples of org.springframework.core.annotation.AnnotationAttributes


    AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();
    assertMultipleAnnotationsWithIdenticalAttributeNames(metadata);
  }

  private void assertMultipleAnnotationsWithIdenticalAttributeNames(AnnotationMetadata metadata) {
    AnnotationAttributes attributes1 = (AnnotationAttributes) metadata.getAnnotationAttributes(
        NamedAnnotation1.class.getName(), false);
    String name1 = attributes1.getString("name");
    assertThat("name of NamedAnnotation1", name1, is("name 1"));

    AnnotationAttributes attributes2 = (AnnotationAttributes) metadata.getAnnotationAttributes(
        NamedAnnotation2.class.getName(), false);
    String name2 = attributes2.getString("name");
    assertThat("name of NamedAnnotation2", name2, is("name 2"));

    AnnotationAttributes attributes3 = (AnnotationAttributes) metadata.getAnnotationAttributes(
        NamedAnnotation3.class.getName(), false);
    String name3 = attributes3.getString("name");
    assertThat("name of NamedAnnotation3", name3, is("name 3"));
  }
View Full Code Here


    assertThat(metadata.getAnnotationTypes().size(), is(6));
    assertThat(metadata.getAnnotationTypes().contains(Component.class.getName()), is(true));
    assertThat(metadata.getAnnotationTypes().contains(Scope.class.getName()), is(true));
    assertThat(metadata.getAnnotationTypes().contains(SpecialAttr.class.getName()), is(true));

    AnnotationAttributes compAttrs = (AnnotationAttributes) metadata.getAnnotationAttributes(Component.class.getName());
    assertThat(compAttrs.size(), is(1));
    assertThat(compAttrs.getString("value"), is("myName"));
    AnnotationAttributes scopeAttrs = (AnnotationAttributes) metadata.getAnnotationAttributes(Scope.class.getName());
    assertThat(scopeAttrs.size(), is(1));
    assertThat(scopeAttrs.getString("value"), is("myScope"));

    Set<MethodMetadata> methods = metadata.getAnnotatedMethods(DirectAnnotation.class.getName());
    MethodMetadata method = methods.iterator().next();
    assertEquals("direct", method.getAnnotationAttributes(DirectAnnotation.class.getName()).get("value"));
    List<Object> allMeta = method.getAllAnnotationAttributes(DirectAnnotation.class.getName()).get("value");
    assertThat(new HashSet<Object>(allMeta), is(equalTo(new HashSet<Object>(Arrays.asList("direct", "meta")))));

    assertTrue(metadata.isAnnotated(IsAnnotatedAnnotation.class.getName()));

    { // perform tests with classValuesAsString = false (the default)
      AnnotationAttributes specialAttrs = (AnnotationAttributes) metadata.getAnnotationAttributes(SpecialAttr.class.getName());
      assertThat(specialAttrs.size(), is(6));
      assertTrue(String.class.isAssignableFrom(specialAttrs.getClass("clazz")));
      assertTrue(specialAttrs.getEnum("state").equals(Thread.State.NEW));

      AnnotationAttributes nestedAnno = specialAttrs.getAnnotation("nestedAnno");
      assertThat("na", is(nestedAnno.getString("value")));
      assertTrue(nestedAnno.getEnum("anEnum").equals(SomeEnum.LABEL1));
      assertArrayEquals(new Class[] { String.class }, (Class[]) nestedAnno.get("classArray"));

      AnnotationAttributes[] nestedAnnoArray = specialAttrs.getAnnotationArray("nestedAnnoArray");
      assertThat(nestedAnnoArray.length, is(2));
      assertThat(nestedAnnoArray[0].getString("value"), is("default"));
      assertTrue(nestedAnnoArray[0].getEnum("anEnum").equals(SomeEnum.DEFAULT));
      assertArrayEquals(new Class[] { Void.class }, (Class[]) nestedAnnoArray[0].get("classArray"));
      assertThat(nestedAnnoArray[1].getString("value"), is("na1"));
      assertTrue(nestedAnnoArray[1].getEnum("anEnum").equals(SomeEnum.LABEL2));
      assertArrayEquals(new Class[] { Number.class }, (Class[]) nestedAnnoArray[1].get("classArray"));
      assertArrayEquals(new Class[] { Number.class }, nestedAnnoArray[1].getClassArray("classArray"));

      AnnotationAttributes optional = specialAttrs.getAnnotation("optional");
      assertThat(optional.getString("value"), is("optional"));
      assertTrue(optional.getEnum("anEnum").equals(SomeEnum.DEFAULT));
      assertArrayEquals(new Class[] { Void.class }, (Class[]) optional.get("classArray"));
      assertArrayEquals(new Class[] { Void.class }, optional.getClassArray("classArray"));

      AnnotationAttributes[] optionalArray = specialAttrs.getAnnotationArray("optionalArray");
      assertThat(optionalArray.length, is(1));
      assertThat(optionalArray[0].getString("value"), is("optional"));
      assertTrue(optionalArray[0].getEnum("anEnum").equals(SomeEnum.DEFAULT));
      assertArrayEquals(new Class[] { Void.class }, (Class[]) optionalArray[0].get("classArray"));
      assertArrayEquals(new Class[] { Void.class }, optionalArray[0].getClassArray("classArray"));

      assertEquals("direct", metadata.getAnnotationAttributes(DirectAnnotation.class.getName()).get("value"));
      allMeta = metadata.getAllAnnotationAttributes(DirectAnnotation.class.getName()).get("value");
      assertThat(new HashSet<Object>(allMeta), is(equalTo(new HashSet<Object>(Arrays.asList("direct", "meta")))));
    }
    { // perform tests with classValuesAsString = true
      AnnotationAttributes specialAttrs = (AnnotationAttributes) metadata.getAnnotationAttributes(
        SpecialAttr.class.getName(), true);
      assertThat(specialAttrs.size(), is(6));
      assertThat(specialAttrs.get("clazz"), is((Object) String.class.getName()));
      assertThat(specialAttrs.getString("clazz"), is(String.class.getName()));

      AnnotationAttributes nestedAnno = specialAttrs.getAnnotation("nestedAnno");
      assertArrayEquals(new String[] { String.class.getName() }, nestedAnno.getStringArray("classArray"));
      assertArrayEquals(new String[] { String.class.getName() }, nestedAnno.getStringArray("classArray"));

      AnnotationAttributes[] nestedAnnoArray = specialAttrs.getAnnotationArray("nestedAnnoArray");
      assertArrayEquals(new String[] { Void.class.getName() }, (String[]) nestedAnnoArray[0].get("classArray"));
      assertArrayEquals(new String[] { Void.class.getName() }, nestedAnnoArray[0].getStringArray("classArray"));
      assertArrayEquals(new String[] { Number.class.getName() }, (String[]) nestedAnnoArray[1].get("classArray"));
      assertArrayEquals(new String[] { Number.class.getName() }, nestedAnnoArray[1].getStringArray("classArray"));

      AnnotationAttributes optional = specialAttrs.getAnnotation("optional");
      assertArrayEquals(new String[] { Void.class.getName() }, (String[]) optional.get("classArray"));
      assertArrayEquals(new String[] { Void.class.getName() }, optional.getStringArray("classArray"));

      AnnotationAttributes[] optionalArray = specialAttrs.getAnnotationArray("optionalArray");
      assertArrayEquals(new String[] { Void.class.getName() }, (String[]) optionalArray[0].get("classArray"));
      assertArrayEquals(new String[] { Void.class.getName() }, optionalArray[0].getStringArray("classArray"));
View Full Code Here

  MergedSqlConfig(SqlConfig localSqlConfig, Class<?> testClass) {
    Assert.notNull(localSqlConfig, "Local @SqlConfig must not be null");
    Assert.notNull(testClass, "testClass must not be null");

    // Get global attributes, if any.
    AnnotationAttributes attributes = AnnotatedElementUtils.getAnnotationAttributes(testClass,
      SqlConfig.class.getName());

    // Override global attributes with local attributes.
    if (attributes != null) {
      for (String key : attributes.keySet()) {
        Object value = AnnotationUtils.getValue(localSqlConfig, key);
        if (value != null) {
          // Is the value explicit (i.e., not a 'default')?
          if (!value.equals("") && (value != TransactionMode.DEFAULT) && (value != ErrorMode.DEFAULT)) {
            attributes.put(key, value);
          }
        }
      }
    }
    else {
      // Otherwise, use local attributes only.
      attributes = AnnotationUtils.getAnnotationAttributes(localSqlConfig, false, false);
    }

    this.dataSource = attributes.getString("dataSource");
    this.transactionManager = attributes.getString("transactionManager");
    this.transactionMode = getEnum(attributes, "transactionMode", TransactionMode.DEFAULT, TransactionMode.INFERRED);
    this.encoding = attributes.getString("encoding");
    this.separator = getString(attributes, "separator", ScriptUtils.DEFAULT_STATEMENT_SEPARATOR);
    this.commentPrefix = getString(attributes, "commentPrefix", ScriptUtils.DEFAULT_COMMENT_PREFIX);
    this.blockCommentStartDelimiter = getString(attributes, "blockCommentStartDelimiter",
      ScriptUtils.DEFAULT_BLOCK_COMMENT_START_DELIMITER);
    this.blockCommentEndDelimiter = getString(attributes, "blockCommentEndDelimiter",
View Full Code Here

    Assert.notNull(testClass, "The test class of the supplied TestContext must not be null");
    Method testMethod = testContext.getTestMethod();
    Assert.notNull(testMethod, "The test method of the supplied TestContext must not be null");

    final String annotationType = DirtiesContext.class.getName();
    AnnotationAttributes methodAnnAttrs = AnnotatedElementUtils.getAnnotationAttributes(testMethod, annotationType);
    AnnotationAttributes classAnnAttrs = AnnotatedElementUtils.getAnnotationAttributes(testClass, annotationType);
    boolean methodDirtiesContext = methodAnnAttrs != null;
    boolean classDirtiesContext = classAnnAttrs != null;
    ClassMode classMode = classDirtiesContext ? classAnnAttrs.<ClassMode> getEnum("classMode") : null;

    if (logger.isDebugEnabled()) {
      logger.debug(String.format(
        "After test method: context %s, class dirties context [%s], class mode [%s], method dirties context [%s].",
        testContext, classDirtiesContext, classMode, methodDirtiesContext));
    }

    if (methodDirtiesContext || (classMode == AFTER_EACH_TEST_METHOD)) {
      HierarchyMode hierarchyMode = methodDirtiesContext ? methodAnnAttrs.<HierarchyMode> getEnum("hierarchyMode")
          : classAnnAttrs.<HierarchyMode> getEnum("hierarchyMode");
      dirtyContext(testContext, hierarchyMode);
    }
  }
View Full Code Here

  public void afterTestClass(TestContext testContext) throws Exception {
    Class<?> testClass = testContext.getTestClass();
    Assert.notNull(testClass, "The test class of the supplied TestContext must not be null");

    final String annotationType = DirtiesContext.class.getName();
    AnnotationAttributes annAttrs = AnnotatedElementUtils.getAnnotationAttributes(testClass, annotationType);
    boolean dirtiesContext = annAttrs != null;

    if (logger.isDebugEnabled()) {
      logger.debug(String.format("After test class: context %s, dirtiesContext [%s].", testContext,
        dirtiesContext));
    }
    if (dirtiesContext) {
      HierarchyMode hierarchyMode = annAttrs.<HierarchyMode> getEnum("hierarchyMode");
      dirtyContext(testContext, hierarchyMode);
    }
  }
View Full Code Here

   */
  TransactionConfigurationAttributes retrieveConfigurationAttributes(TestContext testContext) {
    if (this.configurationAttributes == null) {
      Class<?> clazz = testContext.getTestClass();

      AnnotationAttributes annAttrs = AnnotatedElementUtils.getAnnotationAttributes(clazz,
        TransactionConfiguration.class.getName());
      if (logger.isDebugEnabled()) {
        logger.debug(String.format("Retrieved @TransactionConfiguration attributes [%s] for test class [%s].",
          annAttrs, clazz));
      }

      String transactionManagerName;
      boolean defaultRollback;
      if (annAttrs != null) {
        transactionManagerName = annAttrs.getString("transactionManager");
        defaultRollback = annAttrs.getBoolean("defaultRollback");
      }
      else {
        transactionManagerName = DEFAULT_TRANSACTION_MANAGER_NAME;
        defaultRollback = DEFAULT_DEFAULT_ROLLBACK;
      }
View Full Code Here

    while (descriptor != null) {
      Class<?> rootDeclaringClass = descriptor.getRootDeclaringClass();
      Class<?> declaringClass = descriptor.getDeclaringClass();

      AnnotationAttributes annAttrs = descriptor.getAnnotationAttributes();
      if (logger.isTraceEnabled()) {
        logger.trace(String.format("Retrieved @ActiveProfiles attributes [%s] for declaring class [%s].",
          annAttrs, declaringClass.getName()));
      }
      validateActiveProfilesConfiguration(declaringClass, annAttrs);

      Class<? extends ActiveProfilesResolver> resolverClass = annAttrs.getClass("resolver");
      if (ActiveProfilesResolver.class.equals(resolverClass)) {
        resolverClass = DefaultActiveProfilesResolver.class;
      }

      ActiveProfilesResolver resolver = null;
      try {
        resolver = BeanUtils.instantiateClass(resolverClass, ActiveProfilesResolver.class);
      }
      catch (Exception e) {
        String msg = String.format("Could not instantiate ActiveProfilesResolver of "
            + "type [%s] for test class [%s].", resolverClass.getName(), rootDeclaringClass.getName());
        logger.error(msg);
        throw new IllegalStateException(msg, e);
      }

      String[] profiles = resolver.resolve(rootDeclaringClass);
      if (profiles == null) {
        String msg = String.format(
          "ActiveProfilesResolver [%s] returned a null array of bean definition profiles.",
          resolverClass.getName());
        logger.error(msg);
        throw new IllegalStateException(msg);
      }

      for (String profile : profiles) {
        if (StringUtils.hasText(profile)) {
          activeProfiles.add(profile.trim());
        }
      }

      descriptor = annAttrs.getBoolean("inheritProfiles") ? MetaAnnotationUtils.findAnnotationDescriptor(
        rootDeclaringClass.getSuperclass(), annotationType) : null;
    }

    return StringUtils.toStringArray(activeProfiles);
  }
View Full Code Here

    }
    else {
      // Traverse the class hierarchy...
      while (descriptor != null) {
        Class<?> declaringClass = descriptor.getDeclaringClass();
        AnnotationAttributes annAttrs = descriptor.getAnnotationAttributes();
        if (logger.isTraceEnabled()) {
          logger.trace(String.format(
            "Retrieved @TestExecutionListeners attributes [%s] for declaring class [%s].", annAttrs,
            declaringClass.getName()));
        }

        Class<? extends TestExecutionListener>[] valueListenerClasses = (Class<? extends TestExecutionListener>[]) annAttrs.getClassArray("value");
        Class<? extends TestExecutionListener>[] listenerClasses = (Class<? extends TestExecutionListener>[]) annAttrs.getClassArray("listeners");
        if (!ObjectUtils.isEmpty(valueListenerClasses) && !ObjectUtils.isEmpty(listenerClasses)) {
          throw new IllegalStateException(String.format(
            "Class [%s] configured with @TestExecutionListeners' "
                + "'value' [%s] and 'listeners' [%s] attributes. Use one or the other, but not both.",
            declaringClass.getName(), ObjectUtils.nullSafeToString(valueListenerClasses),
            ObjectUtils.nullSafeToString(listenerClasses)));
        }
        else if (!ObjectUtils.isEmpty(valueListenerClasses)) {
          listenerClasses = valueListenerClasses;
        }

        boolean inheritListeners = annAttrs.getBoolean("inheritListeners");
        AnnotationDescriptor<TestExecutionListeners> superDescriptor = MetaAnnotationUtils.findAnnotationDescriptor(
          descriptor.getRootDeclaringClass().getSuperclass(), annotationType);

        // If there are no listeners to inherit, we might need to merge the
        // locally declared listeners with the defaults.
        if ((!inheritListeners || superDescriptor == null)
            && (annAttrs.getEnum("mergeMode") == MergeMode.MERGE_WITH_DEFAULTS)) {
          if (logger.isDebugEnabled()) {
            logger.debug(String.format(
              "Merging default listeners with listeners configured via @TestExecutionListeners for class [%s].",
              descriptor.getRootDeclaringClass().getName()));
          }
View Full Code Here

    Assert.notNull(descriptor, String.format(
      "Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]",
      annotationType.getName(), testClass.getName()));

    while (descriptor != null) {
      AnnotationAttributes annAttrs = descriptor.getAnnotationAttributes();
      Class<?> rootDeclaringClass = descriptor.getRootDeclaringClass();

      if (logger.isTraceEnabled()) {
        logger.trace(String.format("Retrieved @TestPropertySource attributes [%s] for declaring class [%s].",
          annAttrs, rootDeclaringClass.getName()));
View Full Code Here

    }
    else {
      Class<?> rootDeclaringClass = descriptor.getRootDeclaringClass();
      Class<?> declaringClass = descriptor.getDeclaringClass();

      AnnotationAttributes annAttrs = descriptor.getAnnotationAttributes();
      if (logger.isTraceEnabled()) {
        logger.trace(String.format("Retrieved @ActiveProfiles attributes [%s] for declaring class [%s].",
          annAttrs, declaringClass.getName()));
      }

      Class<? extends ActiveProfilesResolver> resolverClass = annAttrs.getClass("resolver");
      if (!ActiveProfilesResolver.class.equals(resolverClass)) {
        String msg = String.format("Configuration error for test class [%s]: %s cannot be used "
            + "in conjunction with custom resolver [%s].", rootDeclaringClass.getName(),
          getClass().getSimpleName(), resolverClass.getName());
        logger.error(msg);
        throw new IllegalStateException(msg);
      }

      String[] profiles = annAttrs.getStringArray("profiles");
      String[] valueProfiles = annAttrs.getStringArray("value");
      boolean valueDeclared = !ObjectUtils.isEmpty(valueProfiles);
      boolean profilesDeclared = !ObjectUtils.isEmpty(profiles);

      if (valueDeclared && profilesDeclared) {
        String msg = String.format("Class [%s] has been configured with @ActiveProfiles' 'value' [%s] "
View Full Code Here

TOP

Related Classes of org.springframework.core.annotation.AnnotationAttributes

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.