-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Showing
9 changed files
with
296 additions
and
3 deletions.
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
76 changes: 76 additions & 0 deletions
76
isle-sofa-boot-starter/src/test/java/com/alipay/sofa/isle/integration/FailModuleTest.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,76 @@ | ||
/* | ||
* 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.isle.integration; | ||
|
||
import com.alipay.sofa.healthcheck.core.HealthChecker; | ||
import com.alipay.sofa.isle.ApplicationRuntimeModel; | ||
import com.alipay.sofa.isle.constants.SofaModuleFrameworkConstants; | ||
import com.alipay.sofa.isle.deployment.DeploymentDescriptor; | ||
import com.alipay.sofa.isle.util.AddCustomJar; | ||
import com.alipay.sofa.isle.util.SeparateClassLoaderTestRunner; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.boot.actuate.health.Status; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationContextAware; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* @author ruoshan | ||
* @since 2.6.0 | ||
*/ | ||
@RunWith(SeparateClassLoaderTestRunner.class) | ||
@SpringBootTest(classes = SofaBootTestApplication.class) | ||
@AddCustomJar({ "dev-module-0.1.0.jar", "fail-module-0.1.0.jar" }) | ||
@DirtiesContext | ||
public class FailModuleTest implements ApplicationContextAware { | ||
private ApplicationContext applicationContext; | ||
|
||
@Test | ||
public void test() { | ||
ApplicationRuntimeModel applicationRuntimeModel = (ApplicationRuntimeModel) applicationContext | ||
.getBean(SofaModuleFrameworkConstants.APPLICATION); | ||
|
||
// contains three Deployments | ||
assertEquals(3, applicationRuntimeModel.getAllDeployments().size()); | ||
assertEquals(2, applicationRuntimeModel.getInstalled().size()); | ||
assertEquals(1, applicationRuntimeModel.getFailed().size()); | ||
|
||
// check module not in installed list | ||
DeploymentDescriptor failModule = applicationRuntimeModel.getFailed().get(0); | ||
Assert.assertEquals("com.alipay.sofa.fail", failModule.getModuleName()); | ||
Assert.assertFalse(applicationRuntimeModel.getInstalled().contains(failModule)); | ||
} | ||
|
||
@Test | ||
public void testHealthChecker() { | ||
Assert.assertNotNull(applicationContext.getBean("sofaModuleHealthChecker")); | ||
HealthChecker healthChecker = (HealthChecker) applicationContext | ||
.getBean("sofaModuleHealthChecker"); | ||
Assert.assertEquals(Status.DOWN, healthChecker.isHealthy().getStatus()); | ||
} | ||
|
||
@Override | ||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { | ||
this.applicationContext = applicationContext; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...ot-starter/src/test/java/com/alipay/sofa/isle/integration/FailModuleWithParallelTest.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,29 @@ | ||
/* | ||
* 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.isle.integration; | ||
|
||
import org.springframework.context.ApplicationContextAware; | ||
import org.springframework.test.context.TestPropertySource; | ||
|
||
/** | ||
* @author ruoshan | ||
* @since 2.6.0 | ||
*/ | ||
@TestPropertySource(properties = "com.alipay.sofa.boot.module-start-up-parallel=true") | ||
public class FailModuleWithParallelTest extends FailModuleTest implements ApplicationContextAware { | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
isle-sofa-boot-starter/src/test/java/com/alipay/sofa/isle/scan/FailBean.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,32 @@ | ||
/* | ||
* 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.isle.scan; | ||
|
||
import org.springframework.beans.factory.InitializingBean; | ||
|
||
/** | ||
* Test For Fail Module | ||
* @author ruoshan | ||
* @since 2.6.0 | ||
*/ | ||
public class FailBean implements InitializingBean { | ||
|
||
@Override | ||
public void afterPropertiesSet() throws Exception { | ||
throw new RuntimeException("fail bean test!!!"); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
isle-sofa-boot-starter/src/test/java/com/alipay/sofa/isle/util/AddCustomJar.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,35 @@ | ||
/* | ||
* 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.isle.util; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* | ||
* @author ruoshan | ||
* @since 2.6.0 | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
public @interface AddCustomJar { | ||
|
||
String[] value(); | ||
|
||
} |
101 changes: 101 additions & 0 deletions
101
...a-boot-starter/src/test/java/com/alipay/sofa/isle/util/SeparateClassLoaderTestRunner.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,101 @@ | ||
/* | ||
* 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.isle.util; | ||
|
||
import org.junit.runner.notification.RunNotifier; | ||
import org.junit.runners.model.InitializationError; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.Method; | ||
import java.net.URLClassLoader; | ||
|
||
/** | ||
* | ||
* @author xuanbei | ||
* @author ruoshan | ||
* @since 2.6.0 | ||
*/ | ||
public class SeparateClassLoaderTestRunner extends SpringJUnit4ClassRunner { | ||
|
||
private final SeparateClassLoader separateClassloader = new SeparateClassLoader(); | ||
|
||
private Method runMethod; | ||
private Object runnerObject; | ||
|
||
public SeparateClassLoaderTestRunner(Class<?> clazz) throws InitializationError { | ||
super(clazz); | ||
try { | ||
AddCustomJar customJar = getAddCustomJarAnnotationRecursively(clazz); | ||
if (customJar != null) { | ||
for (String jar : customJar.value()) { | ||
separateClassloader.addJar(jar); | ||
} | ||
} | ||
Class springJUnit4ClassRunnerClass = separateClassloader | ||
.loadClass(SpringJUnit4ClassRunner.class.getName()); | ||
Constructor constructor = springJUnit4ClassRunnerClass.getConstructor(Class.class); | ||
runnerObject = constructor.newInstance(separateClassloader.loadClass(clazz.getName())); | ||
runMethod = springJUnit4ClassRunnerClass.getMethod("run", RunNotifier.class); | ||
|
||
} catch (Throwable e) { | ||
throw new InitializationError(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void run(RunNotifier notifier) { | ||
ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader(); | ||
try { | ||
Thread.currentThread().setContextClassLoader(separateClassloader); | ||
runMethod.invoke(runnerObject, notifier); | ||
} catch (Throwable e) { | ||
throw new RuntimeException(e); | ||
} finally { | ||
Thread.currentThread().setContextClassLoader(oldContextClassLoader); | ||
} | ||
} | ||
|
||
public static AddCustomJar getAddCustomJarAnnotationRecursively(Class<?> klass) { | ||
AddCustomJar addCustomJar = klass.getAnnotation(AddCustomJar.class); | ||
|
||
if (addCustomJar != null || klass == Object.class) { | ||
return addCustomJar; | ||
} | ||
|
||
return getAddCustomJarAnnotationRecursively(klass.getSuperclass()); | ||
} | ||
|
||
public static class SeparateClassLoader extends URLClassLoader { | ||
public SeparateClassLoader() { | ||
super(((URLClassLoader) getSystemClassLoader()).getURLs(), null); | ||
} | ||
|
||
@Override | ||
public Class<?> loadClass(String name) throws ClassNotFoundException { | ||
if (name.startsWith("org.junit") || name.startsWith("java")) { | ||
return getSystemClassLoader().loadClass(name); | ||
} | ||
|
||
return super.loadClass(name); | ||
} | ||
|
||
void addJar(String jar) { | ||
super.addURL(SeparateClassLoaderTestRunner.class.getClassLoader().getResource(jar)); | ||
} | ||
} | ||
} |
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
Binary file not shown.