http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/cc186b52/modules/aop/src/main/java/org/gridgain/grid/compute/gridify/aop/spring/GridifySpringAspect.java ---------------------------------------------------------------------- diff --git a/modules/aop/src/main/java/org/gridgain/grid/compute/gridify/aop/spring/GridifySpringAspect.java b/modules/aop/src/main/java/org/gridgain/grid/compute/gridify/aop/spring/GridifySpringAspect.java deleted file mode 100644 index 75fcb66..0000000 --- a/modules/aop/src/main/java/org/gridgain/grid/compute/gridify/aop/spring/GridifySpringAspect.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * 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.gridgain.grid.compute.gridify.aop.spring; - -import org.aopalliance.intercept.*; -import org.apache.ignite.*; -import org.apache.ignite.compute.*; -import org.apache.ignite.compute.gridify.*; -import org.apache.ignite.compute.gridify.aop.*; -import org.apache.ignite.internal.util.typedef.*; - -import java.lang.reflect.*; - -import static org.apache.ignite.IgniteState.*; - -/** - * Spring aspect that cross-cuts on all methods grid-enabled with - * {@link org.apache.ignite.compute.gridify.Gridify} annotation and potentially executes them on - * remote node. - * <p> - * Note that Spring uses proxy-based AOP, so in order to be properly - * cross-cut, all methods need to be enhanced with {@link GridifySpringEnhancer} - * helper. - * <p> - * See {@link org.apache.ignite.compute.gridify.Gridify} documentation for more information about execution of - * {@code gridified} methods. - * @see org.apache.ignite.compute.gridify.Gridify - */ -public class GridifySpringAspect implements MethodInterceptor { - /** - * Aspect implementation which executes grid-enabled methods on remote - * nodes. - * - * {@inheritDoc} - */ - @SuppressWarnings({"ProhibitedExceptionDeclared", "ProhibitedExceptionThrown", "CatchGenericClass", "unchecked"}) - @Override public Object invoke(MethodInvocation invoc) throws Throwable { - Method mtd = invoc.getMethod(); - - Gridify ann = mtd.getAnnotation(Gridify.class); - - assert ann != null : "Intercepted method does not have gridify annotation."; - - // Since annotations in Java don't allow 'null' as default value - // we have accept an empty string and convert it here. - // NOTE: there's unintended behavior when user specifies an empty - // string as intended grid name. - // NOTE: the 'ann.gridName() == null' check is added to mitigate - // annotation bugs in some scripting languages (e.g. Groovy). - String gridName = F.isEmpty(ann.gridName()) ? null : ann.gridName(); - - if (G.state(gridName) != STARTED) - throw new IgniteCheckedException("Grid is not locally started: " + gridName); - - // Initialize defaults. - GridifyArgument arg = new GridifyArgumentAdapter(mtd.getDeclaringClass(), mtd.getName(), - mtd.getParameterTypes(), invoc.getArguments(), invoc.getThis()); - - if (!ann.interceptor().equals(GridifyInterceptor.class)) { - // Check interceptor first. - if (!ann.interceptor().newInstance().isGridify(ann, arg)) - return invoc.proceed(); - } - - if (!ann.taskClass().equals(GridifyDefaultTask.class) && !ann.taskName().isEmpty()) - throw new IgniteCheckedException("Gridify annotation must specify either Gridify.taskName() or " + - "Gridify.taskClass(), but not both: " + ann); - - try { - Ignite ignite = G.ignite(gridName); - - if (!ann.taskClass().equals(GridifyDefaultTask.class)) - return ignite.compute().withTimeout(ann.timeout()).execute( - (Class<? extends ComputeTask<GridifyArgument, Object>>)ann.taskClass(), arg); - - // If task name was not specified. - if (ann.taskName().isEmpty()) - return ignite.compute().withTimeout(ann.timeout()).execute( - new GridifyDefaultTask(invoc.getMethod().getDeclaringClass()), arg); - - // If task name was specified. - return ignite.compute().withTimeout(ann.timeout()).execute(ann.taskName(), arg); - } - catch (Throwable e) { - for (Class<?> ex : invoc.getMethod().getExceptionTypes()) { - // Descend all levels down. - Throwable cause = e.getCause(); - - while (cause != null) { - if (ex.isAssignableFrom(cause.getClass())) - throw cause; - - cause = cause.getCause(); - } - - if (ex.isAssignableFrom(e.getClass())) - throw e; - } - - throw new GridifyRuntimeException("Undeclared exception thrown: " + e.getMessage(), e); - } - } -}
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/cc186b52/modules/aop/src/main/java/org/gridgain/grid/compute/gridify/aop/spring/GridifySpringEnhancer.java ---------------------------------------------------------------------- diff --git a/modules/aop/src/main/java/org/gridgain/grid/compute/gridify/aop/spring/GridifySpringEnhancer.java b/modules/aop/src/main/java/org/gridgain/grid/compute/gridify/aop/spring/GridifySpringEnhancer.java deleted file mode 100644 index 2e6e1bf..0000000 --- a/modules/aop/src/main/java/org/gridgain/grid/compute/gridify/aop/spring/GridifySpringEnhancer.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * 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.gridgain.grid.compute.gridify.aop.spring; - -import org.gridgain.grid.compute.gridify.aop.spring.GridifySpringPointcut.*; -import org.springframework.aop.framework.*; -import org.springframework.aop.support.*; - -/** - * Spring AOP enhancer. Use it to grid-enable methods annotated with - * {@link org.apache.ignite.compute.gridify.Gridify}, {@link org.apache.ignite.compute.gridify.GridifySetToValue} and {@link org.apache.ignite.compute.gridify.GridifySetToSet} annotations. - * <p> - * Note, that Spring AOP requires that all grid-enabled methods must - * be {@code enhanced} because it is proxy-based. Other AOP implementations, - * such as JBoss or AspectJ don't require special handling. - * <p> - * See {@link org.apache.ignite.compute.gridify.Gridify} documentation for more information about execution of - * {@code gridified} methods. - * @see org.apache.ignite.compute.gridify.Gridify - * @see org.apache.ignite.compute.gridify.GridifySetToValue - * @see org.apache.ignite.compute.gridify.GridifySetToSet - */ -public final class GridifySpringEnhancer { - /** Spring aspect. */ - private static final GridifySpringAspect dfltAsp = new GridifySpringAspect(); - - /** Spring aspect. */ - private static final GridifySetToSetSpringAspect setToSetAsp = new GridifySetToSetSpringAspect(); - - /** Spring aspect. */ - private static final GridifySetToValueSpringAspect setToValAsp = new GridifySetToValueSpringAspect(); - - /** - * Enforces singleton. - */ - private GridifySpringEnhancer() { - // No-op. - } - - /** - * Enhances the object on load. - * - * @param <T> Type of the object to enhance. - * @param obj Object to augment/enhance. - * @return Enhanced object. - */ - @SuppressWarnings({"unchecked"}) - public static <T> T enhance(T obj) { - ProxyFactory proxyFac = new ProxyFactory(obj); - - proxyFac.addAdvice(dfltAsp); - proxyFac.addAdvice(setToValAsp); - proxyFac.addAdvice(setToSetAsp); - - while (proxyFac.getAdvisors().length > 0) - proxyFac.removeAdvisor(0); - - proxyFac.addAdvisor(new DefaultPointcutAdvisor( - new GridifySpringPointcut(GridifySpringPointcutType.DFLT), dfltAsp)); - proxyFac.addAdvisor(new DefaultPointcutAdvisor( - new GridifySpringPointcut(GridifySpringPointcutType.SET_TO_VALUE), setToValAsp)); - proxyFac.addAdvisor(new DefaultPointcutAdvisor( - new GridifySpringPointcut(GridifySpringPointcutType.SET_TO_SET), setToSetAsp)); - - return (T)proxyFac.getProxy(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/cc186b52/modules/aop/src/main/java/org/gridgain/grid/compute/gridify/aop/spring/GridifySpringPointcut.java ---------------------------------------------------------------------- diff --git a/modules/aop/src/main/java/org/gridgain/grid/compute/gridify/aop/spring/GridifySpringPointcut.java b/modules/aop/src/main/java/org/gridgain/grid/compute/gridify/aop/spring/GridifySpringPointcut.java deleted file mode 100644 index a001300..0000000 --- a/modules/aop/src/main/java/org/gridgain/grid/compute/gridify/aop/spring/GridifySpringPointcut.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * 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.gridgain.grid.compute.gridify.aop.spring; - -import org.apache.ignite.compute.gridify.*; -import org.springframework.aop.*; -import java.lang.reflect.*; - -/** - * Pointcut used by gridified aspects to find methods - * annotated with {@link org.apache.ignite.compute.gridify.Gridify}, {@link GridifySetToValue} and - * {@link GridifySetToSet} annotations. - */ -public class GridifySpringPointcut implements Pointcut { - /** - * Class filter. - */ - private static final ClassFilter filter = new ClassFilter() { - @SuppressWarnings({"unchecked", "RawUseOfParameterizedType"}) - @Override public boolean matches(Class cls) { - return true; - } - }; - - /** Method matcher. */ - private static final MethodMatcher dfltMatcher = new GridifyMethodMatcher() { - // Warning suppression is due to Spring... - @SuppressWarnings("unchecked") - @Override public boolean matches(Method method, Class cls) { - return cls.isAnnotationPresent(Gridify.class) || method.isAnnotationPresent(Gridify.class); - } - }; - - /** Method matcher. */ - private static final MethodMatcher setToValueMatcher = new GridifyMethodMatcher() { - // Warning suppression is due to Spring... - @SuppressWarnings("unchecked") - @Override public boolean matches(Method method, Class cls) { - return cls.isAnnotationPresent(GridifySetToValue.class) || method.isAnnotationPresent(GridifySetToValue.class); - } - }; - - /** Method matcher. */ - private static final MethodMatcher setToSetMatcher = new GridifyMethodMatcher() { - // Warning suppression is due to Spring... - @SuppressWarnings("unchecked") - @Override public boolean matches(Method method, Class cls) { - return cls.isAnnotationPresent(GridifySetToSet.class) || method.isAnnotationPresent(GridifySetToSet.class); - } - }; - - /** */ - private final GridifySpringPointcutType type; - - /** - * Creates pointcut associated with specific aspect. - * - * @param type Type. - */ - public GridifySpringPointcut(GridifySpringPointcutType type) { - assert type != null; - - this.type = type; - } - - /** {@inheritDoc} */ - @Override public ClassFilter getClassFilter() { - return filter; - } - - /** {@inheritDoc} */ - @Override public MethodMatcher getMethodMatcher() { - switch (type) { - case DFLT: return dfltMatcher; - case SET_TO_VALUE: return setToValueMatcher; - case SET_TO_SET: return setToSetMatcher; - - default: - assert false : "Unknown pointcut type: " + type; - } - - return null; - } - - /** - * Method matcher. - */ - private abstract static class GridifyMethodMatcher implements MethodMatcher { - /** {@inheritDoc} */ - @SuppressWarnings("unchecked") - @Override public abstract boolean matches(Method method, Class cls); - - /** {@inheritDoc} */ - @Override public boolean isRuntime() { - return false; - } - - /** {@inheritDoc} */ - // Warning suppression is due to Spring... - @SuppressWarnings({"unchecked", "RawUseOfParameterizedType"}) - @Override public boolean matches(Method method, Class aClass, Object[] objs) { - // No-op. - return false; - } - } - - /** - * Pointcut type. - */ - @SuppressWarnings({"PublicInnerClass"}) - public enum GridifySpringPointcutType { - /** */ - DFLT, - - /** */ - SET_TO_VALUE, - - /** */ - SET_TO_SET - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/cc186b52/modules/aop/src/main/java/org/gridgain/grid/compute/gridify/aop/spring/package.html ---------------------------------------------------------------------- diff --git a/modules/aop/src/main/java/org/gridgain/grid/compute/gridify/aop/spring/package.html b/modules/aop/src/main/java/org/gridgain/grid/compute/gridify/aop/spring/package.html deleted file mode 100644 index 3162446..0000000 --- a/modules/aop/src/main/java/org/gridgain/grid/compute/gridify/aop/spring/package.html +++ /dev/null @@ -1,23 +0,0 @@ -<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> -<!-- - 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. - --> -<html> -<body> - <!-- Package description. --> - Contains all classes used by Spring AOP implementation. -</body> -</html> http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/cc186b52/modules/aop/src/test/config/aop/aspectj/META-INF/aop.xml ---------------------------------------------------------------------- diff --git a/modules/aop/src/test/config/aop/aspectj/META-INF/aop.xml b/modules/aop/src/test/config/aop/aspectj/META-INF/aop.xml index 6a89ba0..650fe72 100644 --- a/modules/aop/src/test/config/aop/aspectj/META-INF/aop.xml +++ b/modules/aop/src/test/config/aop/aspectj/META-INF/aop.xml @@ -162,9 +162,9 @@ <aspects> <!-- Declare @Gridify aspect to the weaver. --> - <aspect name="org.gridgain.grid.compute.gridify.aop.aspectj.GridifyAspectJAspect"/> - <aspect name="org.gridgain.grid.compute.gridify.aop.aspectj.GridifySetToValueAspectJAspect"/> - <aspect name="org.gridgain.grid.compute.gridify.aop.aspectj.GridifySetToSetAspectJAspect"/> + <aspect name="org.apache.ignite.compute.gridify.aop.aspectj.GridifyAspectJAspect"/> + <aspect name="org.apache.ignite.compute.gridify.aop.aspectj.GridifySetToValueAspectJAspect"/> + <aspect name="org.apache.ignite.compute.gridify.aop.aspectj.GridifySetToSetAspectJAspect"/> <!-- Declare folder to search for aspects. --> <include within="org.gridgain.grid.compute.gridify.aop.aspectj.*"/> http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/cc186b52/modules/aop/src/test/java/org/apache/ignite/gridify/GridAbstractAopTest.java ---------------------------------------------------------------------- diff --git a/modules/aop/src/test/java/org/apache/ignite/gridify/GridAbstractAopTest.java b/modules/aop/src/test/java/org/apache/ignite/gridify/GridAbstractAopTest.java new file mode 100644 index 0000000..1c0fc31 --- /dev/null +++ b/modules/aop/src/test/java/org/apache/ignite/gridify/GridAbstractAopTest.java @@ -0,0 +1,734 @@ +/* + * 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.gridify; + +import org.apache.ignite.*; +import org.apache.ignite.cluster.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.events.*; +import org.apache.ignite.lang.*; +import org.apache.ignite.spi.deployment.local.*; +import org.apache.ignite.spi.discovery.tcp.*; +import org.apache.ignite.internal.util.typedef.*; +import org.gridgain.testframework.*; +import org.gridgain.testframework.junits.common.*; + +import java.lang.reflect.*; +import java.util.*; +import java.util.concurrent.atomic.*; + +import static org.apache.ignite.events.IgniteEventType.*; + +/** + * Abstract AOP test. + */ +@SuppressWarnings( {"OverlyStrongTypeCast", "JUnitAbstractTestClassNamingConvention", "ProhibitedExceptionDeclared", "IfMayBeConditional"}) +public abstract class GridAbstractAopTest extends GridCommonAbstractTest { + /** */ + private IgniteDeploymentMode depMode = IgniteDeploymentMode.PRIVATE; + + /** + * @throws Exception If test failed. + */ + public void testDefaultPrivate() throws Exception { + checkDefault(IgniteDeploymentMode.PRIVATE); + } + + /** + * @throws Exception If test failed. + */ + public void testDefaultIsolated() throws Exception { + checkDefault(IgniteDeploymentMode.ISOLATED); + } + + /** + * @throws Exception If test failed. + */ + public void testDefaultContinuous() throws Exception { + checkDefault(IgniteDeploymentMode.CONTINUOUS); + } + + /** + * @throws Exception If test failed. + */ + public void testDefaultShared() throws Exception { + checkDefault(IgniteDeploymentMode.SHARED); + } + + /** + * @throws Exception If test failed. + */ + public void testDefaultWithUserClassLoaderPrivate() throws Exception { + checkDefaultWithUserClassLoader(IgniteDeploymentMode.PRIVATE); + } + + /** + * @throws Exception If test failed. + */ + public void testDefaultWithUserClassLoaderIsolated() throws Exception { + checkDefaultWithUserClassLoader(IgniteDeploymentMode.ISOLATED); + } + + /** + * @throws Exception If test failed. + */ + public void testDefaultWithUserClassLoaderContinuous() throws Exception { + checkDefaultWithUserClassLoader(IgniteDeploymentMode.CONTINUOUS); + } + + /** + * @throws Exception If test failed. + */ + public void testDefaultWithUserClassLoaderShared() throws Exception { + checkDefaultWithUserClassLoader(IgniteDeploymentMode.SHARED); + } + + /** + * @throws Exception If test failed. + */ + public void testSingleDeploymentWithUserClassLoaderPrivate() throws Exception { + checkSingleDeploymentWithUserClassLoader(IgniteDeploymentMode.PRIVATE); + } + + /** + * @throws Exception If test failed. + */ + public void testSingleDeploymentWithUserClassLoaderIsolated() throws Exception { + checkSingleDeploymentWithUserClassLoader(IgniteDeploymentMode.ISOLATED); + } + + /** + * @throws Exception If test failed. + */ + public void testSingleDeploymentWithUserClassLoaderContinuous() throws Exception { + checkSingleDeploymentWithUserClassLoader(IgniteDeploymentMode.CONTINUOUS); + } + + /** + * @throws Exception If test failed. + */ + public void testSingleDeploymentWithUserClassLoaderShared() throws Exception { + checkSingleDeploymentWithUserClassLoader(IgniteDeploymentMode.SHARED); + } + + /** + * @throws Exception If test failed. + */ + public void testDefaultResourceWithUserClassLoaderPrivate() throws Exception { + checkDefaultResourceWithUserClassLoader(IgniteDeploymentMode.PRIVATE); + } + + /** + * @throws Exception If test failed. + */ + public void testDefaultResourceWithUserClassLoaderIsolated() throws Exception { + checkDefaultResourceWithUserClassLoader(IgniteDeploymentMode.ISOLATED); + } + + /** + * @throws Exception If test failed. + */ + public void testDefaultResourceWithUserClassLoaderContinuous() throws Exception { + checkDefaultResourceWithUserClassLoader(IgniteDeploymentMode.CONTINUOUS); + } + + /** + * @throws Exception If test failed. + */ + public void testDefaultResourceWithUserClassLoaderShared() throws Exception { + checkDefaultResourceWithUserClassLoader(IgniteDeploymentMode.SHARED); + } + + /** + * @throws Exception If test failed. + */ + public void testNonDefaultClassPrivate() throws Exception { + checkNonDefaultClass(IgniteDeploymentMode.PRIVATE); + } + + /** + * @throws Exception If test failed. + */ + public void testNonDefaultClassIsolated() throws Exception { + checkNonDefaultClass(IgniteDeploymentMode.ISOLATED); + } + + /** + * @throws Exception If test failed. + */ + public void testNonDefaultClassContinuous() throws Exception { + checkNonDefaultClass(IgniteDeploymentMode.CONTINUOUS); + } + + /** + * @throws Exception If test failed. + */ + public void testNonDefaultClassShared() throws Exception { + checkNonDefaultClass(IgniteDeploymentMode.SHARED); + } + + /** + * @throws Exception If test failed. + */ + public void testNonDefaultNamePrivate() throws Exception { + checkNonDefaultName(IgniteDeploymentMode.PRIVATE); + } + + /** + * @throws Exception If test failed. + */ + public void testNonDefaultNameIsolated() throws Exception { + checkNonDefaultName(IgniteDeploymentMode.ISOLATED); + } + + /** + * @throws Exception If test failed. + */ + public void testNonDefaultNameContinuous() throws Exception { + checkNonDefaultName(IgniteDeploymentMode.CONTINUOUS); + } + + /** + * @throws Exception If test failed. + */ + public void testNonDefaultNameShared() throws Exception { + checkNonDefaultName(IgniteDeploymentMode.SHARED); + } + + /** + * @throws Exception If test failed. + */ + public void testDefaultExceptionPrivate() throws Exception { + checkDefaultException(IgniteDeploymentMode.PRIVATE); + } + + /** + * @throws Exception If test failed. + */ + public void testDefaultExceptionIsolated() throws Exception { + checkDefaultException(IgniteDeploymentMode.ISOLATED); + } + + /** + * @throws Exception If test failed. + */ + public void testDefaultExceptionContinuous() throws Exception { + checkDefaultException(IgniteDeploymentMode.CONTINUOUS); + } + + /** + * @throws Exception If test failed. + */ + public void testDefaultExceptionShared() throws Exception { + checkDefaultException(IgniteDeploymentMode.SHARED); + } + + /** + * @throws Exception If test failed. + */ + public void testDefaultResourcePrivate() throws Exception { + checkDefaultResource(IgniteDeploymentMode.PRIVATE); + } + + /** + * @throws Exception If test failed. + */ + public void testDefaultResourceIsolated() throws Exception { + checkDefaultResource(IgniteDeploymentMode.ISOLATED); + } + + /** + * @throws Exception If test failed. + */ + public void testDefaultResourceContinuous() throws Exception { + checkDefaultResource(IgniteDeploymentMode.CONTINUOUS); + } + + /** + * @throws Exception If test failed. + */ + public void testDefaultResourceShared() throws Exception { + checkDefaultResource(IgniteDeploymentMode.SHARED); + } + + /** + * @throws Exception If test failed. + */ + public void testNonDefaultClassResourcePrivate() throws Exception { + checkNonDefaultClassResource(IgniteDeploymentMode.PRIVATE); + } + + /** + * @throws Exception If test failed. + */ + public void testNonDefaultClassResourceIsolated() throws Exception { + checkNonDefaultClassResource(IgniteDeploymentMode.ISOLATED); + } + + /** + * @throws Exception If test failed. + */ + public void testNonDefaultClassResourceContinuous() throws Exception { + checkNonDefaultClassResource(IgniteDeploymentMode.CONTINUOUS); + } + + /** + * @throws Exception If test failed. + */ + public void testNonDefaultClassResourceShared() throws Exception { + checkNonDefaultClassResource(IgniteDeploymentMode.SHARED); + } + + /** + * @throws Exception If test failed. + */ + public void testNonDefaultNameResourcePrivate() throws Exception { + checkNonDefaultNameResource(IgniteDeploymentMode.PRIVATE); + } + + /** + * @throws Exception If test failed. + */ + public void testNonDefaultNameResourceIsolated() throws Exception { + checkNonDefaultNameResource(IgniteDeploymentMode.ISOLATED); + } + + /** + * @throws Exception If test failed. + */ + public void testNonDefaultNameResourceContinuous() throws Exception { + checkNonDefaultNameResource(IgniteDeploymentMode.CONTINUOUS); + } + + /** + * @throws Exception If test failed. + */ + public void testNonDefaultNameResourceShared() throws Exception { + checkNonDefaultNameResource(IgniteDeploymentMode.SHARED); + } + + /** + * @param depMode Deployment mode to use. + * @throws Exception If failed. + */ + private void checkDefault(IgniteDeploymentMode depMode) throws Exception { + this.depMode = depMode; + + info("Start Gridify test with Default AOP Task in Deployment Mode : " + depMode); + + startGrid(); + + try { + int res; + + Object targetObj = target(); + + if (targetObj instanceof GridTestAopTarget) + res = ((GridTestAopTarget)targetObj).gridifyDefault("1"); + else + res = ((GridTestAopTargetInterface) targetObj).gridifyDefault("1"); + + assert res == 1 : "Invalid gridifyDefault result: " + res; + } + finally { + stopGrid(); + } + } + + /** + * @param depMode Deployment mode to use. + * @throws Exception If failed. + */ + private void checkDefaultWithUserClassLoader(IgniteDeploymentMode depMode) throws Exception { + this.depMode = depMode; + + info("Start Gridify test with Default AOP Task in Deployment Mode : " + depMode); + + startGrid(); + + int res; + + try { + res = -1; + + Object targetObj = targetWithUserClassLoader(); + + Method gridifyMtd = targetObj.getClass().getDeclaredMethod("gridifyDefault", String.class); + + res = (Integer) gridifyMtd.invoke(targetObj, "1"); + + if (res != 1) + fail("Method gridifyDefault returned wrong value [result=" + res + ", expect=1]"); + } + finally { + stopGrid(); + } + + info("Executed @Gridify method gridifyDefault(1) [result=" + res + ']'); + } + + /** + * @param depMode Deployment mode to use. + * @throws Exception If failed. + */ + private void checkSingleDeploymentWithUserClassLoader(IgniteDeploymentMode depMode) throws Exception { + this.depMode = depMode; + + // Create remote grid to execute test on. + Ignite locIgnite = startGrid(); + + Ignite rmtIgnite = startGrid(getTestGridName() + "Remote"); + + try { + AtomicInteger locDepCnt = new AtomicInteger(0); + AtomicInteger rmtDepCnt = new AtomicInteger(0); + + locIgnite.events().localListen(new TestEventListener(locDepCnt), EVT_TASK_DEPLOYED, EVT_CLASS_DEPLOYED); + rmtIgnite.events().localListen(new TestEventListener(rmtDepCnt), EVT_TASK_DEPLOYED, EVT_CLASS_DEPLOYED); + + assertEquals(2, locIgnite.cluster().forPredicate(F.<ClusterNode>alwaysTrue()).nodes().size()); + + Object targetObj = targetWithUserClassLoader(); + + Method gridifyMtd = targetObj.getClass().getDeclaredMethod("gridifyDefault", String.class); + + info("First invocation."); + + int res = (Integer)gridifyMtd.invoke(targetObj, "1"); + + assert res == 1 : "Method gridifyDefault returned wrong value [result=" + res + ", expected=1]"; + + info("Second invocation."); + + res = (Integer)gridifyMtd.invoke(targetObj, "1"); + + assert res == 1 : "Method gridifyDefault returned wrong value [result=" + res + ", expected=1]"; + + assert locDepCnt.get() == 1 : "Invalid local deployment count [expected=1, got=" + locDepCnt.get() + ']'; + assert rmtDepCnt.get() == 1 : "Invalid remote deployment count [expected=1, got=" + rmtDepCnt.get() + ']'; + } + finally { + stopAllGrids(); + } + } + + /** + * @param depMode Deployment mode to use. + * @throws Exception If failed. + */ + private void checkDefaultResourceWithUserClassLoader(IgniteDeploymentMode depMode) throws Exception { + this.depMode = depMode; + + info("Start Gridify test with Default AOP Task."); + + startGrid(); + + int res; + + try { + res = -1; + + Object targetObj = targetWithUserClassLoader(); + + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + + // Set context classloader as user class loader. + Thread.currentThread().setContextClassLoader(targetObj.getClass().getClassLoader()); + + Method gridifyMtd = targetObj.getClass().getDeclaredMethod("gridifyDefaultResource", String.class); + + res = (Integer) gridifyMtd.invoke(targetObj, "2"); + + if (res != 2) + fail("Method gridifyDefaultResource returned wrong value [result=" + res + ", expect=2]"); + + // Set old classloader back. + Thread.currentThread().setContextClassLoader(cl); + } + finally { + stopGrid(); + } + + info("Executed @Gridify method gridifyDefaultResource(2) [result=" + res + ']'); + } + + /** + * @param depMode Deployment mode to use. + * @throws Exception If failed. + */ + private void checkNonDefaultClass(IgniteDeploymentMode depMode) throws Exception { + this.depMode = depMode; + + info("Start Gridify test with Test AOP Task in Deployment Mode : " + depMode); + + startGrid(); + + int res; + + try { + res = -1; + + Object targetObj = target(); + + if (targetObj instanceof GridTestAopTarget) + res = ((GridTestAopTarget) targetObj).gridifyNonDefaultClass("1"); + else + res = ((GridTestAopTargetInterface) targetObj).gridifyNonDefaultClass("1"); + + if (res != 10) + fail("Method gridifyNonDefault returned wrong value [result=" + res + ", expect=10]"); + } + finally { + stopGrid(); + } + + info("Executed @Gridify method gridifyNonDefaultClass(0) [result=" + res + ']'); + } + + /** + * @param depMode Deployment mode to use. + * @throws Exception If failed. + */ + private void checkNonDefaultName(IgniteDeploymentMode depMode) throws Exception { + this.depMode = depMode; + + info("Start Gridify test with Test AOP Task in Deployment Mode : " + depMode); + + startGrid(); + + int res; + + try { + res = -1; + + Object targetObj = target(); + + if (targetObj instanceof GridTestAopTarget) + res = ((GridTestAopTarget) targetObj).gridifyNonDefaultName("2"); + else + res = ((GridTestAopTargetInterface) targetObj).gridifyNonDefaultName("2"); + + if (res != 20) + fail("Method gridifyNonDefault returned wrong value [result=" + res + ", expect=2]"); + } + finally { + stopGrid(); + } + + info("Executed @Gridify method gridifyNonDefaultName(2) [result=" + res + ']'); + } + + /** + * @param depMode Deployment mode to use. + * @throws Exception If failed. + */ + @SuppressWarnings({"CatchGenericClass"}) + private void checkDefaultException(IgniteDeploymentMode depMode) throws Exception { + this.depMode = depMode; + + info("Start Gridify test with Default AOP Task and exception in Deployment Mode : " + depMode); + + startGrid(); + + try { + Object targetObj = target(); + + boolean isE = false; + + try { + if (targetObj instanceof GridTestAopTarget) + ((GridTestAopTarget) targetObj).gridifyDefaultException("1"); + else + ((GridTestAopTargetInterface) targetObj).gridifyDefaultException("1"); + } + catch (GridTestGridifyException e) { + info("@Gridify method gridifyDefaultException(0) returns exception: " + e); + + isE = true; + } + catch (Exception e) { + e.printStackTrace(); + + fail("@Gridify method gridifyDefaultException(0) returns exception [exception" + e + + ", expect=" + GridTestGridifyException.class.getName() + ']'); + } + + if (!isE) + fail("@Gridify method gridifyDefaultException(0) does not return any exception."); + } + finally { + stopGrid(); + } + } + + /** + * @param depMode Deployment mode to use. + * @throws Exception If failed. + */ + private void checkDefaultResource(IgniteDeploymentMode depMode) throws Exception { + this.depMode = depMode; + + info("Start Gridify test with Default AOP Task in Deploy Mode : " + depMode); + + startGrid(); + + int res; + + try { + res = -1; + + Object targetObj = target(); + + if (targetObj instanceof GridTestAopTarget) + res = ((GridTestAopTarget)targetObj).gridifyDefaultResource("1"); + else + res = ((GridTestAopTargetInterface)targetObj).gridifyDefaultResource("1"); + + if (res != 1) + fail("Method gridifyDefaultResource returned wrong value [result=" + res + ", expect=1]"); + } + finally { + stopGrid(); + } + + info("Executed @Gridify method gridifyDefaultResource(0) [result=" + res + ']'); + } + + /** + * @param depMode Deployment mode to use. + * @throws Exception If failed. + */ + private void checkNonDefaultClassResource(IgniteDeploymentMode depMode) throws Exception { + this.depMode = depMode; + + info("Start Gridify test with Test AOP Task in Deploy Mode : " + depMode); + + startGrid(); + + int res; + + try { + res = -1; + + Object targetObj = target(); + + if (targetObj instanceof GridTestAopTarget) + res = ((GridTestAopTarget) targetObj).gridifyNonDefaultClassResource("3"); + else + res = ((GridTestAopTargetInterface) targetObj).gridifyNonDefaultClassResource("3"); + + if (res != 30) + fail("Method gridifyNonDefaultClassResource returned wrong value [result=" + res + ", expect=3]"); + } + finally { + stopGrid(); + } + + info("Executed @Gridify method gridifyNonDefaultClassResource(3) [result=" + res + ']'); + } + + /** + * @param depMode Deployment mode to use. + * @throws Exception If failed. + */ + private void checkNonDefaultNameResource(IgniteDeploymentMode depMode) throws Exception { + this.depMode = depMode; + + info("Start Gridify test with Test AOP Task in Deployment Mode : " + depMode); + + startGrid(); + + int res; + + try { + res = -1; + + Object targetObj = target(); + + if (targetObj instanceof GridTestAopTarget) + res = ((GridTestAopTarget)targetObj).gridifyNonDefaultNameResource("4"); + else + res = ((GridTestAopTargetInterface)targetObj).gridifyNonDefaultNameResource("4"); + + if (res != 40) + fail("Method gridifyNonDefaultNameResource returned wrong value [result=" + res + ", expect=4]"); + } + finally { + stopGrid(); + } + + info("Executed @Gridify method gridifyNonDefaultNameResource(4) [result=" + res + ']'); + } + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(gridName); + + cfg.setDeploymentSpi(new LocalDeploymentSpi()); + + ((TcpDiscoverySpi)cfg.getDiscoverySpi()).setHeartbeatFrequency(500); + + cfg.setDeploymentMode(depMode); + + return cfg; + } + + /** + * @return Test target. + */ + protected abstract Object target(); + + /** + * @return Target. + * @throws Exception If failed. + */ + protected Object targetWithUserClassLoader() throws Exception { + // Notice that we use another resource naming because file has path. + ClassLoader tstClsLdr = new GridTestClassLoader( + Collections.singletonMap("org/apache/ignite/gridify/test_resource.properties", "param1=2"), + getClass().getClassLoader(), + GridTestAopTarget.class.getName(), GridTestAopTargetInterface.class.getName()); + + return tstClsLdr.loadClass("GridTestAopTarget").newInstance(); + } + + /** + * Event listener. + */ + private static final class TestEventListener implements IgnitePredicate<IgniteEvent> { + /** */ + private static final long serialVersionUID = 0L; + + /** Counter. */ + private final AtomicInteger cnt; + + /** + * @param cnt Deploy counter. + */ + private TestEventListener(AtomicInteger cnt) { this.cnt = cnt; } + + /** {@inheritDoc} */ + @Override public boolean apply(IgniteEvent evt) { + if ((evt.type() == EVT_TASK_DEPLOYED || evt.type() == EVT_CLASS_DEPLOYED) && + evt.message() != null && !evt.message().contains("GridTopic")) + cnt.addAndGet(1); + + return true; + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/cc186b52/modules/aop/src/test/java/org/apache/ignite/gridify/GridBasicAopSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/aop/src/test/java/org/apache/ignite/gridify/GridBasicAopSelfTest.java b/modules/aop/src/test/java/org/apache/ignite/gridify/GridBasicAopSelfTest.java new file mode 100644 index 0000000..f3b97e7 --- /dev/null +++ b/modules/aop/src/test/java/org/apache/ignite/gridify/GridBasicAopSelfTest.java @@ -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 org.apache.ignite.gridify; + +import org.apache.ignite.*; +import org.apache.ignite.compute.*; +import org.apache.ignite.compute.gridify.*; +import org.gridgain.testframework.*; +import org.gridgain.testframework.junits.common.*; + +import java.util.*; +import java.util.concurrent.*; + +/** + * Tries to execute dummy gridified task. It should fail because grid is not started. + * <p> + * The main purpose of this test is to check that AOP is properly configured. It should + * be included in all suites that require AOP. + */ +public class GridBasicAopSelfTest extends GridCommonAbstractTest { + /** + * @throws Exception If failed. + */ + public void testAop() throws Exception { + GridTestUtils.assertThrows( + log, + new Callable<Object>() { + @Override public Object call() throws Exception { + gridify(); + + return null; + } + }, + IgniteCheckedException.class, + "Grid is not locally started: null" + ); + } + + /** + * Gridified method. + */ + @Gridify(taskClass = TestTask.class) + private void gridify() { + // No-op + } + + /** + * Test task. + */ + private static class TestTask extends GridifyTaskSplitAdapter<Void> { + /** {@inheritDoc} */ + @Override protected Collection<? extends ComputeJob> split(int gridSize, + GridifyArgument arg) throws IgniteCheckedException { + return null; + } + + /** {@inheritDoc} */ + @Override public Void reduce(List<ComputeJobResult> results) throws IgniteCheckedException { + return null; + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/cc186b52/modules/aop/src/test/java/org/apache/ignite/gridify/GridNonSpringAopSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/aop/src/test/java/org/apache/ignite/gridify/GridNonSpringAopSelfTest.java b/modules/aop/src/test/java/org/apache/ignite/gridify/GridNonSpringAopSelfTest.java new file mode 100644 index 0000000..49290ff --- /dev/null +++ b/modules/aop/src/test/java/org/apache/ignite/gridify/GridNonSpringAopSelfTest.java @@ -0,0 +1,57 @@ +/* + * 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.gridify; + +import org.gridgain.testframework.junits.common.*; + +/** + * To run this test with JBoss AOP make sure of the following: + * + * 1. The JVM is started with following parameters to enable jboss online weaving + * (replace ${GRIDGAIN_HOME} to you $GRIDGAIN_HOME): + * -javaagent:${GRIDGAIN_HOME}libs/jboss-aop-jdk50-4.0.4.jar + * -Djboss.aop.class.path=[path to grid compiled classes (Idea out folder) or path to gridgain.jar] + * -Djboss.aop.exclude=org,com -Djboss.aop.include=org.gridgain + * + * 2. The following jars should be in a classpath: + * ${GRIDGAIN_HOME}libs/javassist-3.x.x.jar + * ${GRIDGAIN_HOME}libs/jboss-aop-jdk50-4.0.4.jar + * ${GRIDGAIN_HOME}libs/jboss-aspect-library-jdk50-4.0.4.jar + * ${GRIDGAIN_HOME}libs/jboss-common-4.2.2.jar + * ${GRIDGAIN_HOME}libs/trove-1.0.2.jar + * + * To run this test with AspectJ AOP make sure of the following: + * + * 1. The JVM is started with following parameters for enable AspectJ online weaving + * (replace ${GRIDGAIN_HOME} to you $GRIDGAIN_HOME): + * -javaagent:${GRIDGAIN_HOME}/libs/optional/gridgain-aop/aspectjweaver-1.7.2.jar + * + * 2. Classpath should contains the ${GRIDGAIN_HOME}/modules/tests/config/aop/aspectj folder. + */ +@GridCommonTest(group="AOP") +public class GridNonSpringAopSelfTest extends GridAbstractAopTest { + /** {@inheritDoc} */ + @Override protected Object target() { + return new GridTestAopTarget(); + } + + /** {@inheritDoc} */ + @Override public String getTestGridName() { + return "GridTestAopTarget"; + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/cc186b52/modules/aop/src/test/java/org/apache/ignite/gridify/GridSpringAopSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/aop/src/test/java/org/apache/ignite/gridify/GridSpringAopSelfTest.java b/modules/aop/src/test/java/org/apache/ignite/gridify/GridSpringAopSelfTest.java new file mode 100644 index 0000000..a34f50e --- /dev/null +++ b/modules/aop/src/test/java/org/apache/ignite/gridify/GridSpringAopSelfTest.java @@ -0,0 +1,52 @@ +/* + * 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.gridify; + +import org.apache.ignite.compute.gridify.aop.spring.*; +import org.gridgain.testframework.junits.common.*; + +/** + * Spring AOP test. + */ +@GridCommonTest(group="AOP") +public class GridSpringAopSelfTest extends GridAbstractAopTest { + /** {@inheritDoc} */ + @Override protected Object target() { + return GridifySpringEnhancer.enhance(new GridTestAopTarget()); + } + + /** {@inheritDoc} */ + @Override public String getTestGridName() { + return "GridTestAopTargetInterface"; + } + + /** {@inheritDoc} */ + @Override protected Object targetWithUserClassLoader() throws Exception { + Object res = super.targetWithUserClassLoader(); + + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + + Thread.currentThread().setContextClassLoader(res.getClass().getClassLoader()); + + res = GridifySpringEnhancer.enhance(res); + + Thread.currentThread().setContextClassLoader(cl); + + return res; + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/cc186b52/modules/aop/src/test/java/org/apache/ignite/gridify/GridTestAopTarget.java ---------------------------------------------------------------------- diff --git a/modules/aop/src/test/java/org/apache/ignite/gridify/GridTestAopTarget.java b/modules/aop/src/test/java/org/apache/ignite/gridify/GridTestAopTarget.java new file mode 100644 index 0000000..3c76ac7 --- /dev/null +++ b/modules/aop/src/test/java/org/apache/ignite/gridify/GridTestAopTarget.java @@ -0,0 +1,138 @@ +/* + * 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.gridify; + +import org.apache.ignite.compute.gridify.*; + +import java.io.*; +import java.util.*; + +/** + * AOP test. + */ +public class GridTestAopTarget implements GridTestAopTargetInterface { + /** + * @param arg Argument. + * @return Result. + */ + @Gridify(gridName="GridTestAopTarget") + @Override public int gridifyDefault(String arg) { + return Integer.parseInt(arg); + } + + /** + * @param arg Argument. + * @return Result. + */ + @Gridify(gridName="GridTestAopTarget", taskClass = GridTestGridifyTask.class) + @Override public int gridifyNonDefaultClass(String arg) { + return Integer.parseInt(arg); + } + + + /** + * @param arg Argument. + * @return Result. + */ + @Gridify(gridName="GridTestAopTarget", taskName = GridTestGridifyTask.TASK_NAME) + @Override public int gridifyNonDefaultName(String arg) { + return Integer.parseInt(arg); + } + + /** + * @param arg Argument. + * @return Result. + */ + @Gridify(gridName="GridTestAopTarget", taskName = "") + @Override public int gridifyNoName(String arg) { + return 0; + } + + /** + * @param arg Argument. + * @return Result. + * @throws GridTestGridifyException If failed. + */ + @Gridify(gridName="GridTestAopTarget") + @Override public int gridifyDefaultException(String arg) throws GridTestGridifyException { + throw new GridTestGridifyException(arg); + } + + /** + * @param arg Argument. + * @return Result. + * @throws GridTestGridifyException If failed. + */ + @Gridify(gridName="GridTestAopTarget") + @Override public int gridifyDefaultResource(String arg) throws GridTestGridifyException { + int res = Integer.parseInt(arg); + + Integer rsrcVal = getResource(); + + assert rsrcVal != null; + assert rsrcVal == res : "Invalid result [res=" + res + ", rsrc=" + rsrcVal + ']'; + + return res; + } + + /** + * @param arg Argument. + * @return Result. + * @throws GridTestGridifyException If failed. + */ + @Gridify(gridName="GridTestAopTarget", taskClass = GridTestGridifyTask.class) + @Override public int gridifyNonDefaultClassResource(String arg) throws GridTestGridifyException { + assert getResource() != null; + + return Integer.parseInt(arg); + } + + + /** + * @param arg Argument. + * @return Result. + * @throws GridTestGridifyException If failed. + */ + @Gridify(gridName="GridTestAopTarget", taskName = GridTestGridifyTask.TASK_NAME) + @Override public int gridifyNonDefaultNameResource(String arg) throws GridTestGridifyException { + assert getResource() != null; + + return Integer.parseInt(arg); + } + + /** + * @return Result. + * @throws GridTestGridifyException If failed. + */ + private Integer getResource() throws GridTestGridifyException { + try (InputStream in = getClass().getResourceAsStream("test_resource.properties")) { + assert in != null; + + Properties prop = new Properties(); + + prop.load(in); + + String val = prop.getProperty("param1"); + + return Integer.parseInt(val); + } + catch (IOException e) { + throw new GridTestGridifyException("Failed to test load properties file.", e); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/cc186b52/modules/aop/src/test/java/org/apache/ignite/gridify/GridTestAopTargetInterface.java ---------------------------------------------------------------------- diff --git a/modules/aop/src/test/java/org/apache/ignite/gridify/GridTestAopTargetInterface.java b/modules/aop/src/test/java/org/apache/ignite/gridify/GridTestAopTargetInterface.java new file mode 100644 index 0000000..8fb70f1 --- /dev/null +++ b/modules/aop/src/test/java/org/apache/ignite/gridify/GridTestAopTargetInterface.java @@ -0,0 +1,87 @@ +/* + * 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.gridify; + +import org.apache.ignite.compute.gridify.*; + +import java.io.*; + +/** + * Test AOP target interface. + */ +public interface GridTestAopTargetInterface extends Serializable { + /** + * @param arg Argument. + * @return Result. + */ + @Gridify(gridName="GridTestAopTargetInterface") + public int gridifyDefault(String arg); + + /** + * @param arg Argument. + * @return Result. + */ + @Gridify(gridName="GridTestAopTargetInterface", taskName = GridTestGridifyTask.TASK_NAME) + public int gridifyNonDefaultName(String arg); + + /** + * @param arg Argument. + * @return Result. + */ + @Gridify(gridName="GridTestAopTargetInterface", taskClass = GridTestGridifyTask.class) + public int gridifyNonDefaultClass(String arg); + + /** + * @param arg Argument. + * @return Result. + */ + @Gridify(gridName="GridTestAopTargetInterface", taskName = "") + public int gridifyNoName(String arg); + + /** + * @param arg Argument. + * @return Result. + * @throws GridTestGridifyException If failed. + */ + @Gridify(gridName="GridTestAopTargetInterface") + public int gridifyDefaultException(String arg) throws GridTestGridifyException; + + /** + * @param arg Argument. + * @return Result. + * @throws GridTestGridifyException If failed. + */ + @Gridify(gridName="GridTestAopTargetInterface") + public int gridifyDefaultResource(String arg) throws GridTestGridifyException; + + /** + * @param arg Argument. + * @return Result. + * @throws GridTestGridifyException If failed. + */ + @Gridify(gridName="GridTestAopTargetInterface", taskClass = GridTestGridifyTask.class) + public int gridifyNonDefaultClassResource(String arg) throws GridTestGridifyException; + + /** + * @param arg Argument. + * @return Result. + * @throws GridTestGridifyException If failed. + */ + @Gridify(gridName="GridTestAopTargetInterface", taskName = GridTestGridifyTask.TASK_NAME) + public int gridifyNonDefaultNameResource(String arg) throws GridTestGridifyException; +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/cc186b52/modules/aop/src/test/java/org/apache/ignite/gridify/GridTestGridifyException.java ---------------------------------------------------------------------- diff --git a/modules/aop/src/test/java/org/apache/ignite/gridify/GridTestGridifyException.java b/modules/aop/src/test/java/org/apache/ignite/gridify/GridTestGridifyException.java new file mode 100644 index 0000000..8f1a883 --- /dev/null +++ b/modules/aop/src/test/java/org/apache/ignite/gridify/GridTestGridifyException.java @@ -0,0 +1,38 @@ +/* + * 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.gridify; + +/** + * Test gridify exception. + */ +public class GridTestGridifyException extends Exception { + /** + * @param msg Message. + */ + public GridTestGridifyException(String msg) { + super(msg); + } + + /** + * @param msg Message. + * @param cause Exception cause. + */ + public GridTestGridifyException(String msg, Throwable cause) { + super(msg, cause); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/cc186b52/modules/aop/src/test/java/org/apache/ignite/gridify/GridTestGridifyJob.java ---------------------------------------------------------------------- diff --git a/modules/aop/src/test/java/org/apache/ignite/gridify/GridTestGridifyJob.java b/modules/aop/src/test/java/org/apache/ignite/gridify/GridTestGridifyJob.java new file mode 100644 index 0000000..75cc674 --- /dev/null +++ b/modules/aop/src/test/java/org/apache/ignite/gridify/GridTestGridifyJob.java @@ -0,0 +1,67 @@ +/* + * 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.gridify; + +import org.apache.ignite.*; +import org.apache.ignite.compute.*; +import org.apache.ignite.resources.*; + +import java.io.*; + +/** + * Test gridify job. + */ +public class GridTestGridifyJob extends ComputeJobAdapter { + /** */ + @IgniteLoggerResource + private IgniteLogger log; + + /** + * @param arg Argument. + */ + public GridTestGridifyJob(String arg) { + super(arg); + } + + /** {@inheritDoc} */ + @Override public Serializable execute() throws IgniteCheckedException { + if (log.isInfoEnabled()) + log.info("Execute GridTestGridifyJob.execute(" + argument(0) + ')'); + + GridTestAopTarget target = new GridTestAopTarget(); + + try { + if ("1".equals(argument(0))) + return target.gridifyNonDefaultClass("10"); + else if ("2".equals(argument(0))) + return target.gridifyNonDefaultName("20"); + else if ("3".equals(argument(0))) + return target.gridifyNonDefaultClassResource("30"); + else if ("4".equals(argument(0))) + return target.gridifyNonDefaultNameResource("40"); + } + catch (GridTestGridifyException e) { + throw new RuntimeException("Failed to execute target method.", e); + } + + assert false : "Argument must be equals to \"0\" [gridifyArg=" + argument(0) + ']'; + + // Never reached. + return null; + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/cc186b52/modules/aop/src/test/java/org/apache/ignite/gridify/GridTestGridifyTask.java ---------------------------------------------------------------------- diff --git a/modules/aop/src/test/java/org/apache/ignite/gridify/GridTestGridifyTask.java b/modules/aop/src/test/java/org/apache/ignite/gridify/GridTestGridifyTask.java new file mode 100644 index 0000000..a780362 --- /dev/null +++ b/modules/aop/src/test/java/org/apache/ignite/gridify/GridTestGridifyTask.java @@ -0,0 +1,47 @@ +/* + * 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.gridify; + +import org.apache.ignite.*; +import org.apache.ignite.compute.*; +import org.apache.ignite.compute.gridify.*; + +import java.util.*; + +/** + * Test gridify task. + */ +@ComputeTaskName(GridTestGridifyTask.TASK_NAME) +public class GridTestGridifyTask extends ComputeTaskSplitAdapter<GridifyArgument, Object> { + /** */ + public static final String TASK_NAME = "GridTestGridifyTask"; + + /** {@inheritDoc} */ + @Override public Collection<? extends ComputeJob> split(int gridSize, GridifyArgument arg) throws IgniteCheckedException { + assert arg.getMethodParameters().length == 1; + + return Collections.singletonList(new GridTestGridifyJob((String)arg.getMethodParameters()[0])); + } + + /** {@inheritDoc} */ + @Override public Object reduce(List<ComputeJobResult> results) throws IgniteCheckedException { + assert results.size() == 1; + + return results.get(0).getData(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/cc186b52/modules/aop/src/test/java/org/apache/ignite/gridify/GridifySetToSetTarget.java ---------------------------------------------------------------------- diff --git a/modules/aop/src/test/java/org/apache/ignite/gridify/GridifySetToSetTarget.java b/modules/aop/src/test/java/org/apache/ignite/gridify/GridifySetToSetTarget.java new file mode 100644 index 0000000..ede07f9 --- /dev/null +++ b/modules/aop/src/test/java/org/apache/ignite/gridify/GridifySetToSetTarget.java @@ -0,0 +1,372 @@ +/* + * 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.gridify; + +import org.apache.ignite.compute.gridify.*; + +import java.io.*; +import java.util.*; + +/** + * Test set-to-set target. + */ +public class GridifySetToSetTarget implements GridifySetToSetTargetInterface, Serializable { + /** + * Find prime numbers in collection. + * + * @param input Input collection. + * @return Prime numbers. + */ + @GridifySetToSet(gridName = "GridifySetToSetTarget", threshold = 2, splitSize = 2) + @Override public Collection<Long> findPrimes(Collection<Long> input) { + return findPrimes0(input); + } + + /** + * Find prime numbers in collection. + * + * @param input Input collection. + * @return Prime numbers. + */ + @GridifySetToSet(gridName = "GridifySetToSetTarget", threshold = 2) + @Override public Collection<Long> findPrimesWithoutSplitSize(Collection<Long> input) { + return findPrimes0(input); + } + + /** + * Find prime numbers in collection. + * + * @param input Input collection. + * @return Prime numbers. + */ + @GridifySetToSet(gridName = "GridifySetToSetTarget") + @Override public Collection<Long> findPrimesWithoutSplitSizeAndThreshold(Collection<Long> input) { + return findPrimes0(input); + } + + /** + * Find prime numbers in collection. + * + * @param input Input collection. + * @return Prime numbers. + */ + @GridifySetToSet(gridName = "GridifySetToSetTarget") + @Override public Collection<Long> findPrimesInListWithoutSplitSizeAndThreshold(List<Long> input) { + return findPrimes0(input); + } + + /** + * Find prime numbers in collection. + * + * @param input Input collection. + * @return Prime numbers. + */ + @SuppressWarnings({"CollectionDeclaredAsConcreteClass"}) + @GridifySetToSet(gridName = "GridifySetToSetTarget") + @Override public Collection<Long> findPrimesInArrayListWithoutSplitSizeAndThreshold(ArrayList<Long> input) { + return findPrimes0(input); + } + + /** + * Find prime numbers in array. + * + * @param input Input collection. + * @return Prime numbers. + */ + @GridifySetToSet(gridName = "GridifySetToSetTarget", threshold = 2, splitSize = 2) + @Override public Long[] findPrimesInArray(Long[] input) { + return findPrimesInArray0(input); + } + + /** + * Find prime numbers in primitive array. + * + * @param input Input collection. + * @return Prime numbers. + */ + @GridifySetToSet(gridName = "GridifySetToSetTarget", threshold = 2, splitSize = 2) + @Override public long[] findPrimesInPrimitiveArray(long[] input) { + return findPrimesInPrimitiveArray0(input); + } + + /** + * Find prime numbers in collection. + * + * @param input Input collection. + * @return Prime numbers. + */ + private Collection<Long> findPrimes0(Iterable<Long> input) { + System.out.println(">>>"); + System.out.println("Find primes in: " + input); + System.out.println(">>>"); + + Collection<Long> res = new ArrayList<>(); + + for (Long val : input) { + Long divisor = checkPrime(val, 2, val); + + if (divisor == null) + res.add(val); + } + + return res; + } + + /** + * Find prime numbers in collection. + * + * @param input Input collection. + * @return Prime numbers. + */ + private Long[] findPrimesInArray0(Long[] input) { + System.out.println(">>>"); + System.out.println("Find primes in array: " + Arrays.asList(input)); + System.out.println(">>>"); + + Collection<Long> res = new ArrayList<>(); + + for (Long val : input) { + Long divisor = checkPrime(val, 2, val); + + if (divisor == null) + res.add(val); + } + + return res.toArray(new Long[res.size()]); + } + + /** + * Find prime numbers in collection. + * + * @param input Input collection. + * @return Prime numbers. + */ + private long[] findPrimesInPrimitiveArray0(long[] input) { + System.out.println(">>>"); + System.out.println("Find primes in primitive array: " + Arrays.toString(input)); + System.out.println(">>>"); + + Collection<Long> res = new ArrayList<>(); + + for (Long val : input) { + Long divisor = checkPrime(val, 2, val); + + if (divisor == null) + res.add(val); + } + + long[] arr = new long[res.size()]; + + int i = 0; + + for (Long element : res) { + arr[i] = element; + i++; + } + + return arr; + } + + /** + * Find prime numbers in iterator. + * + * @param input Input collection. + * @return Prime numbers. + */ + @GridifySetToSet(gridName = "GridifySetToSetTarget", threshold = 2, splitSize = 2) + @Override public Iterator<Long> findPrimesWithIterator(Iterator<Long> input) { + System.out.println(">>>"); + System.out.println("Find primes in iterator: " + input); + System.out.println(">>>"); + + Collection<Long> res = new ArrayList<>(); + + + while (input.hasNext()) { + Long val = input.next(); + + Long divisor = checkPrime(val, 2, val); + + if (divisor == null) + res.add(val); + } + + return new MathIteratorAdapter<>(res); + } + + /** + * Find prime numbers in enumeration. + * + * @param input Input collection. + * @return Prime numbers. + */ + @GridifySetToSet(gridName = "GridifySetToSetTarget", threshold = 2, splitSize = 2) + @Override public Enumeration<Long> findPrimesWithEnumeration(Enumeration<Long> input) { + System.out.println(">>>"); + System.out.println("Find primes in enumeration: " + input); + System.out.println(">>>"); + + Collection<Long> res = new ArrayList<>(); + + while (input.hasMoreElements()) { + Long val = input.nextElement(); + + Long divisor = checkPrime(val, 2, val); + + if (divisor == null) + res.add(val); + } + + return new MathEnumerationAdapter<>(res); + } + + /** + * Method to check value for prime. + * Returns first divisor found or {@code null} if no divisor was found. + * + * @param val Value to check for prime. + * @param minRage Lower boundary of divisors range. + * @param maxRange Upper boundary of divisors range. + * @return First divisor found or {@code null} if no divisor was found. + */ + private Long checkPrime(long val, long minRage, long maxRange) { + // Loop through all divisors in the range and check if the value passed + // in is divisible by any of these divisors. + // Note that we also check for thread interruption which may happen + // if the job was cancelled from the grid task. + for (long divisor = minRage; divisor <= maxRange && !Thread.currentThread().isInterrupted(); divisor++) { + if (divisor != 1 && divisor != val && val % divisor == 0) + return divisor; + } + + return null; + } + + /** + * Serializable {@link Enumeration} implementation based on {@link Collection}. + */ + private static class MathEnumerationAdapter<T> implements Enumeration<T>, Serializable { + /** */ + private Collection<T> col; + + /** */ + private transient Iterator<T> iter; + + /** + * Creates enumeration. + * + * @param col Collection. + */ + private MathEnumerationAdapter(Collection<T> col) { + this.col = col; + + iter = col.iterator(); + } + + /** {@inheritDoc} */ + @Override public boolean hasMoreElements() { + return iter.hasNext(); + } + + /** {@inheritDoc} */ + @Override public T nextElement() { + return iter.next(); + } + + /** + * Recreate inner state for object after deserialization. + * + * @param in Input stream. + * @throws ClassNotFoundException Thrown in case of error. + * @throws IOException Thrown in case of error. + */ + private void readObject(ObjectInputStream in) throws ClassNotFoundException, IOException { + // Always perform the default de-serialization first. + in.defaultReadObject(); + + iter = col.iterator(); + } + + /** + * @param out Output stream + * @throws IOException Thrown in case of error. + */ + private void writeObject(ObjectOutputStream out) throws IOException { + // Perform the default serialization for all non-transient, non-static fields. + out.defaultWriteObject(); + } + } + + /** + * Serializable {@link Iterator} implementation based on {@link Collection}. + */ + private static class MathIteratorAdapter<T> implements Iterator<T>, Serializable { + /** */ + private Collection<T> col; + + /** */ + private transient Iterator<T> iter; + + /** + * @param col Collection. + */ + MathIteratorAdapter(Collection<T> col) { + this.col = col; + + iter = col.iterator(); + } + + /** {@inheritDoc} */ + @Override public boolean hasNext() { + return iter.hasNext(); + } + + /** {@inheritDoc} */ + @Override public T next() { + return iter.next(); + } + + /** {@inheritDoc} */ + @Override public void remove() { + iter.remove(); + } + + /** + * Recreate inner state for object after deserialization. + * + * @param in Input stream. + * @throws ClassNotFoundException Thrown in case of error. + * @throws IOException Thrown in case of error. + */ + private void readObject(ObjectInputStream in) throws ClassNotFoundException, IOException { + // Always perform the default de-serialization first. + in.defaultReadObject(); + + iter = col.iterator(); + } + + /** + * @param out Output stream + * @throws IOException Thrown in case of error. + */ + private void writeObject(ObjectOutputStream out) throws IOException { + // Perform the default serialization for all non-transient, non-static fields. + out.defaultWriteObject(); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/cc186b52/modules/aop/src/test/java/org/apache/ignite/gridify/GridifySetToSetTargetInterface.java ---------------------------------------------------------------------- diff --git a/modules/aop/src/test/java/org/apache/ignite/gridify/GridifySetToSetTargetInterface.java b/modules/aop/src/test/java/org/apache/ignite/gridify/GridifySetToSetTargetInterface.java new file mode 100644 index 0000000..82e1da0 --- /dev/null +++ b/modules/aop/src/test/java/org/apache/ignite/gridify/GridifySetToSetTargetInterface.java @@ -0,0 +1,109 @@ +/* + * 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.gridify; + +import org.apache.ignite.compute.gridify.*; + +import java.util.*; + +/** + * Test set-to-set target interface. + */ +public interface GridifySetToSetTargetInterface { + /** + * Find prime numbers in collection. + * + * @param input Input collection. + * @return Prime numbers. + */ + @GridifySetToSet(gridName = "GridifySetToSetTarget", threshold = 2, splitSize = 2) + public Collection<Long> findPrimes(Collection<Long> input); + + /** + * Find prime numbers in collection. + * + * @param input Input collection. + * @return Prime numbers. + */ + @GridifySetToSet(gridName = "GridifySetToSetTarget", threshold = 2) + public Collection<Long> findPrimesWithoutSplitSize(Collection<Long> input); + + /** + * Find prime numbers in collection. + * + * @param input Input collection. + * @return Prime numbers. + */ + @GridifySetToSet(gridName = "GridifySetToSetTarget") + public Collection<Long> findPrimesWithoutSplitSizeAndThreshold(Collection<Long> input); + + /** + * Find prime numbers in collection. + * + * @param input Input collection. + * @return Prime numbers. + */ + @GridifySetToSet(gridName = "GridifySetToSetTarget") + public Collection<Long> findPrimesInListWithoutSplitSizeAndThreshold(List<Long> input); + + /** + * Find prime numbers in collection. + * + * @param input Input collection. + * @return Prime numbers. + */ + @SuppressWarnings({"CollectionDeclaredAsConcreteClass"}) + @GridifySetToSet(gridName = "GridifySetToSetTarget") + public Collection<Long> findPrimesInArrayListWithoutSplitSizeAndThreshold(ArrayList<Long> input); + + /** + * Find prime numbers in array. + * + * @param input Input collection. + * @return Prime numbers. + */ + @GridifySetToSet(gridName = "GridifySetToSetTarget", threshold = 2, splitSize = 2) + public Long[] findPrimesInArray(Long[] input); + + /** + * Find prime numbers in primitive array. + * + * @param input Input collection. + * @return Prime numbers. + */ + @GridifySetToSet(gridName = "GridifySetToSetTarget", threshold = 2, splitSize = 2) + public long[] findPrimesInPrimitiveArray(long[] input); + + /** + * Find prime numbers in iterator. + * + * @param input Input collection. + * @return Prime numbers. + */ + @GridifySetToSet(gridName = "GridifySetToSetTarget", threshold = 2, splitSize = 2) + public Iterator<Long> findPrimesWithIterator(Iterator<Long> input); + + /** + * Find prime numbers in enumeration. + * + * @param input Input collection. + * @return Prime numbers. + */ + @GridifySetToSet(gridName = "GridifySetToSetTarget", threshold = 2, splitSize = 2) + public Enumeration<Long> findPrimesWithEnumeration(Enumeration<Long> input); +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/cc186b52/modules/aop/src/test/java/org/apache/ignite/gridify/GridifySetToValueTarget.java ---------------------------------------------------------------------- diff --git a/modules/aop/src/test/java/org/apache/ignite/gridify/GridifySetToValueTarget.java b/modules/aop/src/test/java/org/apache/ignite/gridify/GridifySetToValueTarget.java new file mode 100644 index 0000000..59fdddc --- /dev/null +++ b/modules/aop/src/test/java/org/apache/ignite/gridify/GridifySetToValueTarget.java @@ -0,0 +1,199 @@ +/* + * 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.gridify; + +import org.apache.ignite.compute.gridify.*; + +import java.io.*; +import java.util.*; + +/** + * Test set-to-value target. + */ +public class GridifySetToValueTarget implements GridifySetToValueTargetInterface, Serializable { + /** + * Find maximum value in collection. + * + * @param input Input collection. + * @return Maximum value. + */ + @GridifySetToValue(gridName = "GridifySetToValueTarget", threshold = 2, splitSize = 2) + @Override public Long findMaximum(Collection<Long> input) { + return findMaximum0(input); + } + + /** + * Find maximum value in collection. + * + * @param input Input collection. + * @return Maximum value. + */ + @GridifySetToValue(gridName = "GridifySetToValueTarget") + @Override public Long findMaximumInList(List<Long> input) { + return findMaximum0(input); + } + + /** + * Find maximum value in collection. + * + * @param input Input collection. + * @return Maximum value. + */ + @GridifySetToValue(gridName = "GridifySetToValueTarget", threshold = 2) + @Override public Long findMaximumWithoutSplitSize(Collection<Long> input) { + return findMaximum0(input); + } + + /** + * Find maximum value in collection. + * + * @param input Input collection. + * @return Maximum value. + */ + @GridifySetToValue(gridName = "GridifySetToValueTarget") + @Override public Long findMaximumWithoutSplitSizeAndThreshold(Collection<Long> input) { + return findMaximum0(input); + } + + /** + * Find maximum in array. + * + * @param input Input collection. + * @return Maximum value. + */ + @GridifySetToValue(gridName = "GridifySetToValueTarget", threshold = 2, splitSize = 2) + @Override public Long findPrimesInArray(Long[] input) { + return findMaximumInArray0(input); + } + + /** + * Find maximum in primitive array. + * + * @param input Input collection. + * @return Maximum value. + */ + @GridifySetToValue(gridName = "GridifySetToValueTarget", threshold = 2, splitSize = 2) + @Override public long findMaximumInPrimitiveArray(long[] input) { + return findMaximumInPrimitiveArray0(input); + } + + /** + * Find maximum value in collection. + * + * @param input Input collection. + * @return Maximum value. + */ + private Long findMaximum0(Collection<Long> input) { + System.out.println(">>>"); + System.out.println("Find maximum in: " + input); + System.out.println(">>>"); + + return Collections.max(input); + } + + /** + * Find maximum value in array. + * + * @param input Input collection. + * @return Maximum value. + */ + private Long findMaximumInArray0(Long[] input) { + System.out.println(">>>"); + System.out.println("Find maximum in array: " + Arrays.asList(input)); + System.out.println(">>>"); + + return Collections.max(Arrays.asList(input)); + } + + /** + * Find maximum value in array. + * + * @param input Input collection. + * @return Maximum value. + */ + private long findMaximumInPrimitiveArray0(long[] input) { + assert input != null; + assert input.length > 0; + System.out.println(">>>"); + System.out.println("Find maximum in primitive array: " + Arrays.toString(input)); + System.out.println(">>>"); + + long maximum = input[0]; + + for (int i = 1; i < input.length; i++) { + if (input[i] > maximum) + maximum = input[i]; + } + + return maximum; + } + + /** + * Find maximum value in Iterator. + * + * @param input Input collection. + * @return Maximum value. + */ + @GridifySetToValue(gridName = "GridifySetToValueTarget", threshold = 2, splitSize = 2) + @Override public long findMaximumInIterator(Iterator<Long> input) { + assert input != null; + assert input.hasNext(); + + System.out.println(">>>"); + System.out.println("Find maximum in iterator: " + input); + System.out.println(">>>"); + + long maximum = input.next(); + + while(input.hasNext()) { + Long val = input.next(); + + if (val > maximum) + maximum = val; + } + + return maximum; + } + + /** + * Find maximum value in Enumeration. + * + * @param input Input collection. + * @return Maximum value. + */ + @GridifySetToValue(gridName = "GridifySetToValueTarget", threshold = 2, splitSize = 2) + @Override public long findMaximumInEnumeration(Enumeration<Long> input) { + assert input != null; + assert input.hasMoreElements(); + + System.out.println(">>>"); + System.out.println("Find maximum in enumeration: " + input); + System.out.println(">>>"); + + long maximum = input.nextElement(); + + while(input.hasMoreElements()) { + Long val = input.nextElement(); + + if (val > maximum) + maximum = val; + } + + return maximum; + } +}