Skip to content

Commit

Permalink
fix test case in ci machine
Browse files Browse the repository at this point in the history
  • Loading branch information
QilongZhang committed May 9, 2018
1 parent eb4096d commit e426068
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 40 deletions.
Expand Up @@ -17,8 +17,8 @@
package com.alipay.sofa.ark.container.model;

import com.alipay.sofa.ark.bootstrap.MainMethodRunner;
import com.alipay.sofa.ark.common.util.ClassloaderUtils;
import com.alipay.sofa.ark.common.util.StringUtils;
import com.alipay.sofa.ark.container.service.classloader.ClassloaderUtil;
import com.alipay.sofa.ark.exception.ArkException;
import com.alipay.sofa.ark.spi.constant.Constants;
import com.alipay.sofa.ark.spi.model.Biz;
Expand Down Expand Up @@ -141,14 +141,14 @@ public void start(String[] args) throws ArkException {
throw new ArkException(String.format("biz: %s has no main method", getBizName()));
}

ClassLoader oldClassloader = ClassloaderUtil.pushContextClassloader(this.classLoader);
ClassLoader oldClassloader = ClassloaderUtils.pushContextClassloader(this.classLoader);
try {
MainMethodRunner mainMethodRunner = new MainMethodRunner(mainClass, args);
mainMethodRunner.run();
} catch (Throwable e) {
throw new ArkException(e.getMessage(), e);
} finally {
ClassloaderUtil.popContextClassloader(oldClassloader);
ClassloaderUtils.popContextClassloader(oldClassloader);
}
}

Expand Down
Expand Up @@ -16,16 +16,15 @@
*/
package com.alipay.sofa.ark.container.model;

import com.alipay.sofa.ark.common.util.ClassloaderUtils;
import com.alipay.sofa.ark.common.util.StringUtils;
import com.alipay.sofa.ark.container.service.classloader.ClassloaderUtil;
import com.alipay.sofa.ark.exception.ArkException;
import com.alipay.sofa.ark.spi.constant.Constants;
import com.alipay.sofa.ark.spi.model.Plugin;
import com.alipay.sofa.ark.spi.model.PluginContext;
import com.alipay.sofa.ark.spi.service.PluginActivator;

import java.net.URL;
import java.util.HashSet;
import java.util.Set;

/**
Expand Down Expand Up @@ -238,15 +237,16 @@ public void start() throws ArkException {
return;
}

ClassLoader oldClassloader = ClassloaderUtil.pushContextClassloader(this.pluginClassLoader);
ClassLoader oldClassloader = ClassloaderUtils
.pushContextClassloader(this.pluginClassLoader);
try {
pluginActivator = (PluginActivator) pluginClassLoader.loadClass(activator)
.newInstance();
pluginActivator.start(pluginContext);
} catch (Throwable ex) {
throw new ArkException(ex.getMessage(), ex);
} finally {
ClassloaderUtil.pushContextClassloader(oldClassloader);
ClassloaderUtils.pushContextClassloader(oldClassloader);
}
}

Expand Down
Expand Up @@ -19,7 +19,7 @@
import com.alipay.sofa.ark.common.guice.AbstractArkGuiceModule;
import com.alipay.sofa.ark.common.log.ArkLogger;
import com.alipay.sofa.ark.common.log.ArkLoggerFactory;
import com.alipay.sofa.ark.container.service.classloader.ClassloaderUtil;
import com.alipay.sofa.ark.common.util.ClassloaderUtils;
import com.alipay.sofa.ark.exception.ArkException;
import com.alipay.sofa.ark.spi.service.ArkService;
import com.google.inject.Binding;
Expand Down Expand Up @@ -56,7 +56,7 @@ public class ArkServiceContainer {
*/
public void start() throws ArkException {
if (started.compareAndSet(false, true)) {
ClassLoader oldClassloader = ClassloaderUtil.pushContextClassloader(getClass()
ClassLoader oldClassloader = ClassloaderUtils.pushContextClassloader(getClass()
.getClassLoader());
try {
LOGGER.info("Begin to start ArkServiceContainer");
Expand All @@ -75,7 +75,7 @@ public void start() throws ArkException {
ArkServiceContainerHolder.setContainer(this);
LOGGER.info("Finish to start ArkServiceContainer");
} finally {
ClassloaderUtil.popContextClassloader(oldClassloader);
ClassloaderUtils.popContextClassloader(oldClassloader);
}

}
Expand Down Expand Up @@ -114,7 +114,7 @@ public void stop() throws ArkException {
if (stopped.compareAndSet(false, true)) {
LOGGER.info("Begin to stop ArkServiceContainer");

ClassLoader oldClassloader = ClassloaderUtil.pushContextClassloader(getClass()
ClassLoader oldClassloader = ClassloaderUtils.pushContextClassloader(getClass()
.getClassLoader());
try {
for (ArkService arkService : arkServiceList) {
Expand All @@ -125,7 +125,7 @@ public void stop() throws ArkException {

LOGGER.info("Finish to stop ArkServiceContainer");
} finally {
ClassloaderUtil.popContextClassloader(oldClassloader);
ClassloaderUtils.popContextClassloader(oldClassloader);
}
}
}
Expand Down
Expand Up @@ -14,15 +14,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.ark.container.service.classloader;
package com.alipay.sofa.ark.common.util;

/**
* Classloader Util
*
* @author ruoshan
* @since 0.1.0
*/
public class ClassloaderUtil {
public class ClassloaderUtils {

/**
* push ContextClassloader
Expand Down
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.ark.container.service.classloader;
package com.alipay.sofa.ark.common.util;

import org.junit.Assert;
import org.junit.Test;
Expand All @@ -32,14 +32,14 @@ public class ClassloaderUtilTest {
@Test
public void testPushContextClassloader() {
ClassLoader classLoader = new URLClassLoader(new URL[] {});
ClassloaderUtil.pushContextClassloader(classLoader);
ClassloaderUtils.pushContextClassloader(classLoader);
Assert.assertEquals(classLoader, Thread.currentThread().getContextClassLoader());
}

@Test
public void testPopContextClassloader() {
ClassLoader classLoader = new URLClassLoader(new URL[] {});
ClassloaderUtil.popContextClassloader(classLoader);
ClassloaderUtils.popContextClassloader(classLoader);
Assert.assertEquals(classLoader, Thread.currentThread().getContextClassLoader());
}

Expand Down
Expand Up @@ -17,6 +17,7 @@
package com.alipay.sofa.ark.support.common;

import com.alipay.sofa.ark.common.util.AssertUtils;
import com.alipay.sofa.ark.common.util.ClassloaderUtils;
import com.alipay.sofa.ark.support.startup.SofaArkBootstrap;

import java.lang.reflect.Method;
Expand Down Expand Up @@ -53,7 +54,7 @@ public static void launch() {
}
}

Thread.currentThread().setContextClassLoader(DelegateArkContainer.getTestClassLoader());
ClassloaderUtils.pushContextClassloader(DelegateArkContainer.getTestClassLoader());
}

/**
Expand Down
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.sofa.ark.support.listener;

import com.alipay.sofa.ark.common.util.ClassloaderUtils;
import com.alipay.sofa.ark.exception.ArkException;
import com.alipay.sofa.ark.support.common.DelegateArkContainer;
import org.testng.IAlterSuiteListener;
Expand Down Expand Up @@ -66,8 +67,7 @@ protected void resetSingleXmlSuite(XmlSuite suite) {
if (testClass.getAnnotation(TestNGOnArk.class) != null) {
if (!DelegateArkContainer.isStarted()) {
DelegateArkContainer.launch();
Thread.currentThread().setContextClassLoader(
ClassLoader.getSystemClassLoader());
ClassloaderUtils.pushContextClassloader(ClassLoader.getSystemClassLoader());
}

try {
Expand Down
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.sofa.ark.support.listener;

import com.alipay.sofa.ark.common.util.ClassloaderUtils;
import com.alipay.sofa.ark.support.common.DelegateArkContainer;
import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
Expand All @@ -32,15 +33,15 @@ public class ArkTestNGInvokedMethodListener implements IInvokedMethodListener {
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
Class testClass = method.getTestMethod().getInstance().getClass();
if (isTestOnArk(testClass)) {
Thread.currentThread().setContextClassLoader(DelegateArkContainer.getTestClassLoader());
ClassloaderUtils.pushContextClassloader(DelegateArkContainer.getTestClassLoader());
} else {
Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
ClassloaderUtils.pushContextClassloader(ClassLoader.getSystemClassLoader());
}
}

@Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
ClassloaderUtils.pushContextClassloader(ClassLoader.getSystemClassLoader());
}

protected boolean isTestOnArk(Class testClass) {
Expand Down
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.sofa.ark.support.runner;

import com.alipay.sofa.ark.common.util.ClassloaderUtils;
import com.alipay.sofa.ark.support.common.DelegateArkContainer;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
Expand Down Expand Up @@ -45,7 +46,10 @@ protected TestClass createTestClass(Class<?> testClass) {
DelegateArkContainer.launch();
}
ClassLoader testClassLoader = DelegateArkContainer.getTestClassLoader();
return super.createTestClass(testClassLoader.loadClass(testClass.getName()));
TestClass testKlazz = super.createTestClass(testClassLoader.loadClass(testClass
.getName()));
ClassloaderUtils.pushContextClassloader(ClassLoader.getSystemClassLoader());
return testKlazz;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
Expand Down
Expand Up @@ -16,9 +16,9 @@
*/
package com.alipay.sofa.ark.support.runner;

import com.alipay.sofa.ark.common.util.ClassloaderUtils;
import com.alipay.sofa.ark.support.common.DelegateArkContainer;
import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.RunWith;
import org.junit.runner.notification.RunListener;

Expand All @@ -36,32 +36,20 @@ public class JUnitExecutionListener extends RunListener {
private JUnitExecutionListener() {
}

@Override
public void testRunStarted(Description description) throws Exception {
Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
super.testRunStarted(description);
}

@Override
public void testRunFinished(Result result) throws Exception {
super.testRunFinished(result);
Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
}

@Override
public void testStarted(Description description) throws Exception {
if (isTestOnArkContainer(description)) {
Thread.currentThread().setContextClassLoader(DelegateArkContainer.getTestClassLoader());
ClassloaderUtils.pushContextClassloader(DelegateArkContainer.getTestClassLoader());
} else {
Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
ClassloaderUtils.pushContextClassloader(ClassLoader.getSystemClassLoader());
}
super.testStarted(description);
}

@Override
public void testFinished(Description description) throws Exception {
super.testStarted(description);
Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
ClassloaderUtils.pushContextClassloader(ClassLoader.getSystemClassLoader());
}

protected boolean isTestOnArkContainer(Description description) {
Expand Down

0 comments on commit e426068

Please sign in to comment.