Index: ../incubator-ignite/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/ContextClassloaderTest.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- ../incubator-ignite/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/ContextClassloaderTest.java (revision ) +++ ../incubator-ignite/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/ContextClassloaderTest.java (revision ) @@ -0,0 +1,197 @@ +/* + * 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 org.apache.ignite.internal.processors.cache; + +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import javax.cache.processor.EntryProcessorException; +import javax.cache.processor.MutableEntry; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.cache.CacheAtomicityMode; +import org.apache.ignite.cache.CacheEntryProcessor; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.configuration.DeploymentMode; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.configuration.NearCacheConfiguration; +import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi; +import org.apache.ignite.spi.discovery.tcp.ipfinder.TcpDiscoveryIpFinder; +import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder; +import org.apache.ignite.testframework.config.GridTestProperties; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; + +import static org.apache.ignite.cache.CacheAtomicityMode.ATOMIC; +import static org.apache.ignite.cache.CacheMode.PARTITIONED; +import static org.apache.ignite.cache.CacheRebalanceMode.SYNC; +import static org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC; + +/** + * Cache EntryProcessor + Deployment. + */ +public class ContextClassloaderTest extends GridCommonAbstractTest { + /** IP finder. */ + private static final TcpDiscoveryIpFinder IP_FINDER = new TcpDiscoveryVmIpFinder(true); + + /** */ + protected DeploymentMode depMode; + + /** */ + protected boolean clientMode; + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(gridName); + + cfg.setDeploymentMode(depMode); + + cfg.setCacheConfiguration(cacheConfiguration()); + + TcpDiscoverySpi disco = new TcpDiscoverySpi(); + + disco.setIpFinder(IP_FINDER); + + cfg.setDiscoverySpi(disco); + + cfg.setConnectorConfiguration(null); + + return cfg; + } + + /** + * @return Cache configuration. + * @throws Exception In case of error. + */ + protected CacheConfiguration cacheConfiguration() throws Exception { + CacheConfiguration cfg = defaultCacheConfiguration(); + + cfg.setCacheMode(PARTITIONED); + cfg.setWriteSynchronizationMode(FULL_SYNC); + cfg.setRebalanceMode(SYNC); + cfg.setAtomicityMode(atomicityMode()); + cfg.setNearConfiguration(new NearCacheConfiguration()); + cfg.setBackups(1); + + return cfg; + } + + /** {@inheritDoc} */ + protected CacheAtomicityMode atomicityMode() { + return ATOMIC; + } + + /** + * @throws Exception In case of error. + */ + public void testInvokeDeployment() throws Exception { + depMode = DeploymentMode.CONTINUOUS; + + doTestInvoke(); + } + + /** + * @throws Exception In case of error. + */ + public void testInvokeDeployment2() throws Exception { + depMode = DeploymentMode.SHARED; + + doTestInvoke(); + } + + @Override public boolean isDebug() { + return true; + } + + /** + * @throws Exception In case of error. + */ + private void doTestInvoke() throws Exception { + try { + Thread.currentThread().setContextClassLoader(createClassLoader()); + + clientMode = false; + startGrid(0); + + clientMode = true; + startGrid(1); + + IgniteCache cache = grid(1).cache(null); + + cache.invoke("key", new CacheEntryProcessor() { + @Override public Object process(MutableEntry entry, Object... arguments) + throws EntryProcessorException { + entry.setValue("test1"); + return null; + } + }); + + assertEquals("test1", cache.get("key")); + + cache.invoke("key", new NONLOAD()); + + assertEquals("test", cache.get("key")); + } + finally { + stopAllGrids(); + } + } + + /** + * + */ + public static class NONLOAD implements CacheEntryProcessor { + /** {@inheritDoc} */ + @Override public Object process(MutableEntry entry, Object... arguments) throws EntryProcessorException { + entry.setValue("test"); + + return null; + } + } + + + /** + * Gets external class loader. + * + * @return External class loader. + */ + protected static ClassLoader createClassLoader() { + String path = GridTestProperties.getProperty("p2p.uri.cls"); + + try { + return new URLClassLoader(new URL[] {new URL(path)}, null) { + @Override protected Class findClass(String name) throws ClassNotFoundException { + if (name.contains("NONLOAD")) + return ContextClassloaderTest.NONLOAD.class; + + return super.findClass(name); + } + + @Override public Class loadClass(String name) throws ClassNotFoundException { + if (name.contains("NONLOAD")) + return ContextClassloaderTest.NONLOAD.class; + + return super.loadClass(name); + } + + + }; + } + catch (MalformedURLException e) { + throw new RuntimeException("Failed to create URL: " + path, e); + } + } +}