io.micronaut.context.annotation.Property Java Examples

The following examples show how to use io.micronaut.context.annotation.Property. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GrpcServerConfiguration.java    From micronaut-grpc with Apache License 2.0 6 votes vote down vote up
/**
 * Default constructor.
 * @param environment The environment
 * @param serverHost The server host
 * @param serverPort The server port
 * @param executorService The IO executor service
 */
public GrpcServerConfiguration(
        Environment environment,
        @Property(name = HOST) @Nullable String serverHost,
        @Property(name = PORT) @Nullable Integer serverPort,
        @Named(TaskExecutors.IO) ExecutorService executorService) {
    this.environment = environment;
    this.serverPort = serverPort != null ? serverPort :
            environment.getActiveNames().contains(Environment.TEST) ? SocketUtils.findAvailableTcpPort() : DEFAULT_PORT;
    this.serverHost = serverHost;
    if (serverHost != null) {
        this.serverBuilder = NettyServerBuilder.forAddress(
                new InetSocketAddress(serverHost, this.serverPort)
        );
    } else {
        this.serverBuilder = NettyServerBuilder.forPort(this.serverPort);
    }
    this.serverBuilder.executor(executorService);
}
 
Example #2
Source File: GrpcServerConfiguration.java    From micronaut-grpc with Apache License 2.0 6 votes vote down vote up
/**
 * Default constructor.
 * @param environment The environment
 * @param serverHost The server host
 * @param serverPort The server port
 * @param executorService The IO executor service
 */
public GrpcServerConfiguration(
        Environment environment,
        @Property(name = HOST) @Nullable String serverHost,
        @Property(name = PORT) @Nullable Integer serverPort,
        @Named(TaskExecutors.IO) ExecutorService executorService) {
    this.environment = environment;
    this.serverPort = serverPort != null ? serverPort :
            environment.getActiveNames().contains(Environment.TEST) ? SocketUtils.findAvailableTcpPort() : DEFAULT_PORT;
    this.serverHost = serverHost;
    if (serverHost != null) {
        this.serverBuilder = NettyServerBuilder.forAddress(
                new InetSocketAddress(serverHost, this.serverPort)
        );
    } else {
        this.serverBuilder = NettyServerBuilder.forPort(this.serverPort);
    }
    this.serverBuilder.executor(executorService);
}
 
Example #3
Source File: HibernateMetricsBinder.java    From micronaut-sql with Apache License 2.0 5 votes vote down vote up
/**
 * Default constructor.
 * @param meterRegistryProvider The meter registry provider
 * @param tags The tags
 */
public HibernateMetricsBinder(
        Provider<MeterRegistry> meterRegistryProvider,
        @Property(name = MICRONAUT_METRICS_BINDERS + ".hibernate.tags")
        @MapFormat(transformation = MapFormat.MapTransformation.FLAT)
        Map<String, String> tags) {
    this.meterRegistryProvider = meterRegistryProvider;
    if (CollectionUtils.isNotEmpty(tags)) {
        this.tags = tags.entrySet().stream().map(entry -> Tag.of(entry.getKey(), entry.getValue())).collect(Collectors.toList());
    } else {
        this.tags = Collections.emptyList();
    }

}
 
Example #4
Source File: RepositoryTypeElementVisitor.java    From micronaut-data with Apache License 2.0 5 votes vote down vote up
private AnnotationValue<?>[] parameterBindingToAnnotationValues(Map<String, String> finalParameterBinding) {
    AnnotationValue<?>[] annotationParameters = new AnnotationValue[finalParameterBinding.size()];
    int i = 0;
    for (Map.Entry<String, String> entry : finalParameterBinding.entrySet()) {
        annotationParameters[i++] = AnnotationValue.builder(Property.class)
                .member("name", entry.getKey())
                .member("value", entry.getValue())
                .build();
    }
    return annotationParameters;
}
 
Example #5
Source File: SpringTransactionTestExecutionListener.java    From micronaut-test with Apache License 2.0 5 votes vote down vote up
/**
 * @param transactionManager Spring's {@code PlatformTransactionManager}
 * @param rollback {@code true} if the transaction should be rollback
 */
public SpringTransactionTestExecutionListener(
    PlatformTransactionManager transactionManager,
    @Property(name = AbstractMicronautExtension.TEST_ROLLBACK) boolean rollback) {

    this.transactionManager = transactionManager;
    this.rollback = rollback;

}
 
Example #6
Source File: TestTransactionInterceptorListener.java    From micronaut-test with Apache License 2.0 5 votes vote down vote up
/**
 * @param interceptor the interceptor
 * @param rollback {@code true} if the transaction should be rollback
 */
public TestTransactionInterceptorListener(
    TestTransactionInterceptor interceptor,
    @Property(name = AbstractMicronautExtension.TEST_ROLLBACK) boolean rollback) {

  this.interceptor = interceptor;
  this.rollback = rollback;

}
 
Example #7
Source File: AbstractMicronautExtension.java    From micronaut-test with Apache License 2.0 5 votes vote down vote up
/**
 * To be called by the different implementations before each test method.
 *
 * @param context The test context
 * @param testInstance The test instance
 * @param method The test method
 * @param propertyAnnotations The {@code @Property} annotations found in the test method, if any
 */
protected void beforeEach(C context, @Nullable Object testInstance, @Nullable AnnotatedElement method, List<Property> propertyAnnotations) {
    int testCount = (int) testProperties.compute("micronaut.test.count", (k, oldCount) -> (int) (oldCount != null ? oldCount : 0) + 1);
    if (method != null) {
        if (propertyAnnotations != null && !propertyAnnotations.isEmpty()) {
            for (Property property : propertyAnnotations) {
                final String name = property.name();
                oldValues.put(name,
                        testProperties.put(name, property.value())
                );
            }
        } else {
            oldValues.forEach((k, v) -> testProperties.put(k, v));
        }

        if (testAnnotation.rebuildContext() && testCount > 1) {
            embeddedApplication.stop();
            applicationContext.stop();
            applicationContext = builder.build();
            startApplicationContext();
            embeddedApplication.start();
        } else if (!oldValues.isEmpty()) {
            final Map<String, Object> diff = applicationContext.getEnvironment().refreshAndDiff();
            refreshScope.onRefreshEvent(new RefreshEvent(diff));
        }
    }

    if (testInstance != null) {
        if (applicationContext != null) {
            if (refreshScope != null) {
                refreshScope.onRefreshEvent(new RefreshEvent(Collections.singletonMap(
                        TestActiveCondition.ACTIVE_MOCKS, "changed"
                )));
            }
            applicationContext.inject(testInstance);
            alignMocks(context, testInstance);
        }
    }
}
 
Example #8
Source File: MicronautJunit5Extension.java    From micronaut-test with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeEach(ExtensionContext extensionContext) throws Exception {
    final Optional<Object> testInstance = extensionContext.getTestInstance();
    final Optional<? extends AnnotatedElement> testMethod = extensionContext.getTestMethod();
    List<Property> propertyAnnotations = null;
    if (testMethod.isPresent()) {
        Property[] annotationsByType = testMethod.get().getAnnotationsByType(Property.class);
        propertyAnnotations = Arrays.asList(annotationsByType);
    }
    beforeEach(extensionContext, testInstance.orElse(null), testMethod.orElse(null), propertyAnnotations);
    beforeTestMethod(buildContext(extensionContext));
}
 
Example #9
Source File: MappedEntityVisitor.java    From micronaut-data with Apache License 2.0 4 votes vote down vote up
private void computeMappingDefaults(
            NamingStrategy namingStrategy,
            PersistentProperty property,
            Map<String, DataType> dataTypes) {
        AnnotationMetadata annotationMetadata = property.getAnnotationMetadata();
        SourcePersistentProperty spp = (SourcePersistentProperty) property;
        PropertyElement propertyElement = spp.getPropertyElement();

        DataType dataType = annotationMetadata.getValue(TypeDef.class, "type", DataType.class)
                .orElse(null);

        if (dataType == null) {
            ClassElement type = propertyElement.getType();
            dataType = TypeUtils.resolveDataType(type, dataTypes);
        }

        boolean isRelation = propertyElement.hasStereotype(Relation.class);
        if (dataType == DataType.ENTITY && !isRelation) {
            propertyElement = (PropertyElement) propertyElement.annotate(Relation.class, builder ->
                builder.value(Relation.Kind.MANY_TO_ONE)
            );
        } else if (isRelation) {
            Relation.Kind kind = propertyElement.enumValue(Relation.class, Relation.Kind.class).orElse(Relation.Kind.MANY_TO_ONE);
            if (kind == Relation.Kind.EMBEDDED) {
                // handled embedded
                SourcePersistentEntity embeddedEntity = entityResolver.apply(propertyElement.getType());
                List<SourcePersistentProperty> persistentProperties = embeddedEntity.getPersistentProperties();

                List<AnnotationValue<Property>> embeddedProperties = new ArrayList<>(persistentProperties.size());

                for (SourcePersistentProperty embeddedProperty : persistentProperties) {
                    if (!(embeddedProperty instanceof Association)) {
                        String mappedName = embeddedProperty.stringValue(MappedProperty.class)
                                .orElseGet(() -> namingStrategy.mappedName(
                                        property.getName() + embeddedProperty.getCapitilizedName()));
                        AnnotationValue<Property> av = AnnotationValue.builder(Property.class)
                                .value(mappedName)
                                .member("name", embeddedProperty.getName()).build();
                        embeddedProperties.add(av);
                    }
//                    else {
//                        // TODO: handle nested embedded
//                    }
                }

                propertyElement.annotate(MappedProperty.class, builder ->
                    builder.member(MappedProperty.EMBEDDED_PROPERTIES, embeddedProperties.toArray(new AnnotationValue[0]))
                );
            }
        }

        Optional<String> mapping = annotationMetadata.stringValue(MappedProperty.class);
        if (mappedEntity && !mapping.isPresent()) {
            propertyElement.annotate(MappedProperty.class, builder -> builder.value(namingStrategy.mappedName(spp)));
        }
        DataType finalDataType = dataType;
        propertyElement.annotate(MappedProperty.class, builder -> builder.member("type", finalDataType));
    }
 
Example #10
Source File: PropertyValueTest.java    From micronaut-test with Apache License 2.0 4 votes vote down vote up
@Property(name = "foo.bar", value = "changed")
@Test
@Order(2)
void testValueChanged() {
    assertEquals("changed", val);
}
 
Example #11
Source File: PropertyValueRequiresTest.java    From micronaut-test with Apache License 2.0 4 votes vote down vote up
@Property(name = "foo.bar", value = "changed")
@Test
@Order(2)
void testValueChanged() {
    MatcherAssert.assertThat(myService, IsInstanceOf.instanceOf(MyServiceChanged.class));
}