Skip to content

Commit

Permalink
bean on switch smoke test (#1093)
Browse files Browse the repository at this point in the history
* bean on switch smoke test

* fix format

* fix format

---------

Co-authored-by: yuanxuan <wt150181@antgroup.com>
  • Loading branch information
taobaorun and yuanxuan authored Feb 22, 2023
1 parent 2634056 commit c5b8315
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.springframework.util.StringUtils;

/**
* An {@link ApplicationContextInitializer} that could use property to dynamic enable initializer.
* An {@link ApplicationContextInitializer} that could use property to dynamic enable initializer.
*
* @author yuanxuan
* @version : SwitchableApplicationContextInitializer.java, v 0.1 2023年02月09日 11:57 yuanxuan Exp $
Expand All @@ -30,7 +30,7 @@ public abstract class SwitchableApplicationContextInitializer
implements
ApplicationContextInitializer<ConfigurableApplicationContext> {

protected static final String CONFIG_KEY_PREFIX = "sofa.boot.switch.initializer";
protected static final String CONFIG_KEY_PREFIX = "sofa.boot.switch.initializer.";

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
Expand All @@ -52,7 +52,8 @@ public void initialize(ConfigurableApplicationContext applicationContext) {
protected abstract String switchKey();

protected boolean isEnable(ConfigurableApplicationContext applicationContext) {
String switchStr = applicationContext.getEnvironment().getProperty(switchKey());
String realKey = switchKey() + ".enabled";
String switchStr = applicationContext.getEnvironment().getProperty(realKey);
if (StringUtils.hasText(switchStr)) {
return Boolean.parseBoolean(switchStr);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public abstract class SwitchableApplicationListener<E extends ApplicationEvent>
implements
ApplicationListener<E> {

protected static final String CONFIG_KEY_PREFIX = "sofa.boot.switch.initializer";
protected static final String CONFIG_KEY_PREFIX = "sofa.boot.switch.initializer.";

@Override
public void onApplicationEvent(E event) {
Expand Down Expand Up @@ -82,7 +82,8 @@ public void onApplicationEvent(E event) {
protected abstract String switchKey();

protected boolean isEnable(Environment environment) {
String switchStr = environment.getProperty(switchKey());
String realKey = switchKey() + ".enabled";
String switchStr = environment.getProperty(realKey);
if (StringUtils.hasText(switchStr)) {
return Boolean.parseBoolean(switchStr);
} else {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.boot.initializer;

import com.alipay.sofa.boot.Initializer.SwitchableApplicationContextInitializer;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Profiles;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author yuanxuan
* @version : SampleSwitchSpringContextInitializerTests.java, v 0.1 2023年02月22日 11:30 yuanxuan Exp $
*/
public class SampleSwitchSpringContextInitializerTests {

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withInitializer(new SampleSwitchSpringContextInitializer());

@Test
void enableFalse() {
contextRunner.withPropertyValues("sofa.boot.switch.initializer.sampleswitchabletest.enabled=false").run(context -> {
assertThat(context.getEnvironment().acceptsProfiles(Profiles.of("sampleswitchtest"))).isFalse();
});
}

static class SampleSwitchSpringContextInitializer extends
SwitchableApplicationContextInitializer {

@Override
protected void doInitialize(ConfigurableApplicationContext applicationContext) {
applicationContext.getEnvironment().addActiveProfile("sampleswitchtest");
}

@Override
protected String switchKey() {
return CONFIG_KEY_PREFIX + "sampleswitchabletest";
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@
</dependency>
</dependencies>

</project>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

spring.config.activate.on-profile=function

sofa.boot.switch.bean.functionBean.enabled=true



Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

spring.config.activate.on-profile=function

sofa.boot.switch.bean.functionBean.enabled=false



Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

spring.application.name=smoke-tests-boot
logging.path=./logs
logging.level.com.alipay.test=INFO
logging.level.com.test.demo=WARN

any.key=any.value


Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.smoke.tests.boot;

import com.alipay.sofa.boot.autoconfigure.condition.ConditionalOnSwitch;
import com.alipay.sofa.smoke.tests.boot.SofaConditionOnSwitchTests.BeanTestConfiguration;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.assertj.core.api.Assertions.catchException;

/**
* @author yuanxuan
* @version : SofaConditionOnSwitchTests.java, v 0.1 2023年02月22日 10:09 yuanxuan Exp $
*/
@SpringBootTest(classes = { BootSofaBootApplication.class, BeanTestConfiguration.class }, properties = { "spring.profiles.active=function" })
public class SofaConditionOnSwitchTests {

@Autowired
private ApplicationContext context;

@Test
void beanSwitchOnScene() {
assertThat(catchException(() -> {
context.getBean(FunctionBean.class);
})).hasMessage("No qualifying bean of type 'com.alipay.sofa.smoke.tests.boot.SofaConditionOnSwitchTests$FunctionBean' "
+ "available");
assertThatNoException().isThrownBy(() -> {
context.getBean(FunctionFeatureBean.class);
});
}

@Configuration(proxyBeanMethods = false)
@ConditionalOnSwitch
static class BeanTestConfiguration {

@Bean
@ConditionalOnSwitch
public FunctionBean functionBean() {
return new FunctionBean();
}

@Bean
@ConditionalOnSwitch
public FunctionFeatureBean functionFeatureBean() {
return new FunctionFeatureBean();
}
}

static class FunctionBean {

}

static class FunctionFeatureBean {

}
}

0 comments on commit c5b8315

Please sign in to comment.