This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
youji.zzl
committed
Sep 25, 2023
1 parent
b858490
commit 90f07f1
Showing
5 changed files
with
154 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
.../java/com/alipay/sofa/serverless/plugin/manager/handler/BizUninstallEventHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.serverless.plugin.manager.handler; | ||
|
||
import com.alipay.sofa.ark.spi.event.biz.BeforeBizStartupEvent; | ||
import com.alipay.sofa.ark.spi.event.biz.BeforeBizStopEvent; | ||
import com.alipay.sofa.ark.spi.model.Biz; | ||
import com.alipay.sofa.serverless.common.BizRuntimeContext; | ||
import com.alipay.sofa.serverless.common.BizRuntimeContextRegistry; | ||
import com.alipay.sofa.serverless.plugin.BaseRuntimeAutoConfiguration; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.WebApplicationType; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
|
||
import java.util.Properties; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class BizUninstallEventHandlerTest { | ||
private static final String bizName = "eventHandlerTest"; | ||
@Mock | ||
private Biz biz; | ||
|
||
private ConfigurableApplicationContext ctx; | ||
|
||
private SpringApplication springApplication; | ||
|
||
@Before | ||
public void before() { | ||
Properties properties = new Properties(); | ||
properties.setProperty("spring.application.name", bizName); | ||
|
||
springApplication = new SpringApplication(BaseRuntimeAutoConfiguration.class); | ||
springApplication.setDefaultProperties(properties); | ||
|
||
springApplication.setWebApplicationType(WebApplicationType.NONE); | ||
// ctx = springApplication.run(); | ||
} | ||
|
||
@Test | ||
public void handleEvent() { | ||
Mockito.when(biz.getBizName()).thenReturn(bizName); | ||
Mockito.when(biz.getBizClassLoader()).thenReturn(this.getClass().getClassLoader()); | ||
|
||
BeforeBizStartupEventHandler beforeBizStartupEventHandler = new BeforeBizStartupEventHandler(); | ||
beforeBizStartupEventHandler.handleEvent(new BeforeBizStartupEvent(biz)); | ||
|
||
ctx = springApplication.run(); | ||
|
||
BizRuntimeContext bizRuntimeContext = BizRuntimeContextRegistry.getBizRuntimeContext(biz); | ||
Assert.assertEquals(bizRuntimeContext.getRootApplicationContext(), ctx); | ||
|
||
BizUninstallEventHandler bizUninstallEventHandler = new BizUninstallEventHandler(); | ||
bizUninstallEventHandler.handleEvent(new BeforeBizStopEvent(biz)); | ||
Assert.assertFalse(ctx.isActive()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...fa-serverless-common/src/test/java/com/alipay/sofa/serverless/common/ConditionalTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* 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.serverless.common; | ||
|
||
import com.alipay.sofa.serverless.common.environment.ConditionalOnMasterBiz; | ||
import com.alipay.sofa.serverless.common.environment.ConditionalOnNotMasterBiz; | ||
import com.alipay.sofa.serverless.common.exception.BizRuntimeException; | ||
import com.alipay.sofa.serverless.common.exception.ErrorCodes; | ||
import org.junit.Test; | ||
import org.springframework.boot.context.annotation.UserConfigurations; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
public class ConditionalTest { | ||
private final ApplicationContextRunner runner = new ApplicationContextRunner() | ||
.withConfiguration(UserConfigurations | ||
.of(ConditionalAutoConfiguration.class)); | ||
|
||
@Test | ||
public void testConditional() { | ||
runner.run(context -> { | ||
context.assertThat().doesNotHaveBean("nonMasterBizRuntimeException"); | ||
context.assertThat().hasBean("masterBizRuntimeException"); | ||
}); | ||
} | ||
|
||
/** | ||
* @author mingmen | ||
* @date 2023/6/14 | ||
*/ | ||
@Configuration | ||
public static class ConditionalAutoConfiguration { | ||
|
||
@Bean | ||
@ConditionalOnNotMasterBiz | ||
public BizRuntimeException nonMasterBizRuntimeException() { | ||
return new BizRuntimeException(ErrorCodes.SpringContextManager.E100001, "error test."); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMasterBiz | ||
public BizRuntimeException masterBizRuntimeException() { | ||
return new BizRuntimeException(ErrorCodes.SpringContextManager.E100002, "error test."); | ||
} | ||
} | ||
} |