You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ki...@apache.org on 2013/07/31 00:48:08 UTC
svn commit: r1508677 [8/9] - in /commons/proper/functor/trunk:
api/src/main/java/org/apache/commons/functor/
core/src/main/java/org/apache/commons/functor/adapter/
core/src/main/java/org/apache/commons/functor/aggregator/
core/src/main/java/org/apache/...
Modified: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestConditional.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestConditional.java?rev=1508677&r1=1508676&r2=1508677&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestConditional.java (original)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestConditional.java Tue Jul 30 22:48:02 2013
@@ -18,7 +18,7 @@ package org.apache.commons.functor.core.
import static org.junit.Assert.assertNotNull;
-import org.apache.commons.functor.Predicate;
+import org.apache.commons.functor.NullaryPredicate;
import org.apache.commons.functor.core.Constant;
import org.apache.commons.functor.core.Identity;
import org.apache.commons.functor.core.IsEqual;
@@ -42,16 +42,19 @@ public class TestConditional {
}
@Test
+ public void testNullaryMethods() {
+ assertNotNull(Conditional.procedure(new NullaryNot(Constant.TRUE),new NullarySequence()));
+ assertNotNull(Conditional.procedure(new NullaryNot(Constant.TRUE),new NullarySequence(),new NullarySequence()));
+ assertNotNull(Conditional.function(new NullaryNot(Constant.TRUE),Constant.FALSE,Constant.FALSE));
+ assertNotNull(Conditional.predicate((NullaryPredicate)Constant.truePredicate(),Constant.truePredicate(),Constant.truePredicate()));
+ }
+
+ @Test
public void testUnaryMethods() {
- assertNotNull(Conditional.procedure(new Not(Constant.TRUE),new Sequence()));
- assertNotNull(Conditional.procedure(new Not(Constant.TRUE),new Sequence(),new Sequence()));
- assertNotNull(Conditional.procedure(IsNull.instance(),NoOp.instance()));
- assertNotNull(Conditional.procedure(IsNull.instance(),NoOp.instance(),NoOp.instance()));
- assertNotNull(Conditional.procedure(new IsEqual<Object, Object>(),NoOp.INSTANCE));
- assertNotNull(Conditional.function(IsNull.instance(),Identity.instance(),Identity.instance()));
- assertNotNull(Conditional.function(new Not(Constant.TRUE),Constant.FALSE,Constant.FALSE));
- assertNotNull(Conditional.predicate((Predicate)Constant.truePredicate(),Constant.truePredicate(),Constant.truePredicate()));
- assertNotNull(Conditional.predicate(IsNull.instance(),Constant.truePredicate(),Constant.truePredicate()));
+ assertNotNull(Conditional.procedure(IsNull.instance(),NoOp.instance()));
+ assertNotNull(Conditional.procedure(IsNull.instance(),NoOp.instance(),NoOp.instance()));
+ assertNotNull(Conditional.function(IsNull.instance(),Identity.instance(),Identity.instance()));
+ assertNotNull(Conditional.predicate(IsNull.instance(),Constant.truePredicate(),Constant.truePredicate()));
}
@Test
@@ -59,5 +62,6 @@ public class TestConditional {
assertNotNull(Conditional.procedure(IsGreaterThan.instance(),NoOp.instance(),NoOp.instance()));
assertNotNull(Conditional.function(IsGreaterThan.instance(),Max.instance(),Max.instance()));
assertNotNull(Conditional.predicate(IsGreaterThan.instance(),Constant.truePredicate(),Constant.truePredicate()));
+ assertNotNull(Conditional.procedure(new IsEqual<Object, Object>(),NoOp.INSTANCE));
}
}
Modified: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestConditionalFunction.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestConditionalFunction.java?rev=1508677&r1=1508676&r2=1508677&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestConditionalFunction.java (original)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestConditionalFunction.java Tue Jul 30 22:48:02 2013
@@ -21,6 +21,7 @@ import static org.junit.Assert.assertTru
import org.apache.commons.functor.BaseFunctorTest;
import org.apache.commons.functor.core.Constant;
+import org.apache.commons.functor.core.Identity;
import org.junit.Test;
/**
@@ -33,7 +34,7 @@ public class TestConditionalFunction ext
@Override
protected Object makeFunctor() {
- return new ConditionalFunction<Object>(
+ return new ConditionalFunction<Object, Object>(
Constant.TRUE,
Constant.of("left"),
Constant.of("right"));
@@ -44,45 +45,37 @@ public class TestConditionalFunction ext
@Test
public void testEvaluate() throws Exception {
- {
- ConditionalFunction<Object> f = new ConditionalFunction<Object>(
- Constant.TRUE,
- Constant.of("left"),
- Constant.of("right"));
- assertEquals("left",f.evaluate());
- }
- {
- ConditionalFunction<Object> f = new ConditionalFunction<Object>(
- Constant.FALSE,
- Constant.of("left"),
- Constant.of("right"));
- assertEquals("right",f.evaluate());
- }
+ ConditionalFunction<Object, Object> f = new ConditionalFunction<Object, Object>(
+ Identity.INSTANCE,
+ Constant.of("left"),
+ Constant.of("right"));
+ assertEquals("left",f.evaluate(Boolean.TRUE));
+ assertEquals("right",f.evaluate(Boolean.FALSE));
}
@Test
public void testEquals() throws Exception {
- ConditionalFunction<Object> f = new ConditionalFunction<Object>(
- Constant.TRUE,
+ ConditionalFunction<Object, Object> f = new ConditionalFunction<Object, Object>(
+ Identity.INSTANCE,
Constant.of("left"),
Constant.of("right"));
assertEquals(f,f);
- assertObjectsAreEqual(f,new ConditionalFunction<Object>(
- Constant.TRUE,
+ assertObjectsAreEqual(f,new ConditionalFunction<Object, Object>(
+ Identity.INSTANCE,
Constant.of("left"),
Constant.of("right")));
- assertObjectsAreNotEqual(f,new ConditionalFunction<Object>(
- Constant.TRUE,
+ assertObjectsAreNotEqual(f,new ConditionalFunction<Object, Object>(
+ Identity.INSTANCE,
Constant.of(null),
Constant.of("right")));
- assertObjectsAreNotEqual(f,new ConditionalFunction<Object>(
+ assertObjectsAreNotEqual(f,new ConditionalFunction<Object, Object>(
Constant.TRUE,
Constant.of("left"),
- Constant.of(null)));
- assertObjectsAreNotEqual(f,new ConditionalFunction<Object>(
- Constant.FALSE,
- Constant.of("left"),
Constant.of("right")));
+ assertObjectsAreNotEqual(f,new ConditionalFunction<Object, Object>(
+ Identity.INSTANCE,
+ Constant.of("left"),
+ Constant.of(null)));
assertTrue(!f.equals(null));
}
}
Added: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestConditionalNullaryFunction.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestConditionalNullaryFunction.java?rev=1508677&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestConditionalNullaryFunction.java (added)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestConditionalNullaryFunction.java Tue Jul 30 22:48:02 2013
@@ -0,0 +1,88 @@
+/*
+ * 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.commons.functor.core.composite;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.core.Constant;
+import org.junit.Test;
+
+/**
+ * @version $Revision: 1365329 $ $Date: 2012-07-24 19:34:23 -0300 (Tue, 24 Jul 2012) $
+ */
+public class TestConditionalNullaryFunction extends BaseFunctorTest {
+
+ // Functor Testing Framework
+ // ------------------------------------------------------------------------
+
+ @Override
+ protected Object makeFunctor() {
+ return new ConditionalNullaryFunction<Object>(
+ Constant.TRUE,
+ Constant.of("left"),
+ Constant.of("right"));
+ }
+
+ // Tests
+ // ------------------------------------------------------------------------
+
+ @Test
+ public void testEvaluate() throws Exception {
+ {
+ ConditionalNullaryFunction<Object> f = new ConditionalNullaryFunction<Object>(
+ Constant.TRUE,
+ Constant.of("left"),
+ Constant.of("right"));
+ assertEquals("left",f.evaluate());
+ }
+ {
+ ConditionalNullaryFunction<Object> f = new ConditionalNullaryFunction<Object>(
+ Constant.FALSE,
+ Constant.of("left"),
+ Constant.of("right"));
+ assertEquals("right",f.evaluate());
+ }
+ }
+
+ @Test
+ public void testEquals() throws Exception {
+ ConditionalNullaryFunction<Object> f = new ConditionalNullaryFunction<Object>(
+ Constant.TRUE,
+ Constant.of("left"),
+ Constant.of("right"));
+ assertEquals(f,f);
+ assertObjectsAreEqual(f,new ConditionalNullaryFunction<Object>(
+ Constant.TRUE,
+ Constant.of("left"),
+ Constant.of("right")));
+ assertObjectsAreNotEqual(f,new ConditionalNullaryFunction<Object>(
+ Constant.TRUE,
+ Constant.of(null),
+ Constant.of("right")));
+ assertObjectsAreNotEqual(f,new ConditionalNullaryFunction<Object>(
+ Constant.TRUE,
+ Constant.of("left"),
+ Constant.of(null)));
+ assertObjectsAreNotEqual(f,new ConditionalNullaryFunction<Object>(
+ Constant.FALSE,
+ Constant.of("left"),
+ Constant.of("right")));
+ assertTrue(!f.equals(null));
+ }
+}
Added: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestConditionalNullaryPredicate.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestConditionalNullaryPredicate.java?rev=1508677&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestConditionalNullaryPredicate.java (added)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestConditionalNullaryPredicate.java Tue Jul 30 22:48:02 2013
@@ -0,0 +1,88 @@
+/*
+ * 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.commons.functor.core.composite;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.core.Constant;
+import org.junit.Test;
+
+/**
+ * @version $Revision: 1365329 $ $Date: 2012-07-24 19:34:23 -0300 (Tue, 24 Jul 2012) $
+ */
+public class TestConditionalNullaryPredicate extends BaseFunctorTest {
+
+ // Functor Testing Framework
+ // ------------------------------------------------------------------------
+
+ @Override
+ protected Object makeFunctor() {
+ return new ConditionalNullaryPredicate(
+ Constant.TRUE,
+ Constant.FALSE,
+ Constant.TRUE);
+ }
+
+ // Tests
+ // ------------------------------------------------------------------------
+
+ @Test
+ public void testTest() throws Exception {
+ {
+ ConditionalNullaryPredicate p = new ConditionalNullaryPredicate(
+ Constant.TRUE,
+ Constant.TRUE,
+ Constant.FALSE);
+ assertTrue(p.test());
+ }
+ {
+ ConditionalNullaryPredicate p = new ConditionalNullaryPredicate(
+ Constant.FALSE,
+ Constant.TRUE,
+ Constant.FALSE);
+ assertTrue(!p.test());
+ }
+ }
+
+ @Test
+ public void testEquals() throws Exception {
+ ConditionalNullaryPredicate p = new ConditionalNullaryPredicate(
+ Constant.TRUE,
+ Constant.TRUE,
+ Constant.FALSE);
+ assertEquals(p,p);
+ assertObjectsAreEqual(p,new ConditionalNullaryPredicate(
+ Constant.TRUE,
+ Constant.TRUE,
+ Constant.FALSE));
+ assertObjectsAreNotEqual(p,new ConditionalNullaryPredicate(
+ Constant.TRUE,
+ Constant.FALSE,
+ Constant.TRUE));
+ assertObjectsAreNotEqual(p,new ConditionalNullaryPredicate(
+ Constant.TRUE,
+ Constant.TRUE,
+ Constant.TRUE));
+ assertObjectsAreNotEqual(p,new ConditionalNullaryPredicate(
+ Constant.FALSE,
+ Constant.TRUE,
+ Constant.FALSE));
+ assertTrue(!p.equals(null));
+ }
+}
Added: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestConditionalNullaryProcedure.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestConditionalNullaryProcedure.java?rev=1508677&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestConditionalNullaryProcedure.java (added)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestConditionalNullaryProcedure.java Tue Jul 30 22:48:02 2013
@@ -0,0 +1,131 @@
+/*
+ * 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.commons.functor.core.composite;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.NullaryProcedure;
+import org.apache.commons.functor.core.Constant;
+import org.apache.commons.functor.core.NoOp;
+import org.junit.Test;
+
+/**
+ * @version $Revision: 1365329 $ $Date: 2012-07-24 19:34:23 -0300 (Tue, 24 Jul 2012) $
+ */
+public class TestConditionalNullaryProcedure extends BaseFunctorTest {
+
+ // Functor Testing Framework
+ // ------------------------------------------------------------------------
+
+ @Override
+ protected Object makeFunctor() {
+ return new ConditionalNullaryProcedure(
+ Constant.TRUE,
+ NoOp.INSTANCE,
+ NoOp.INSTANCE);
+ }
+
+ // Tests
+ // ------------------------------------------------------------------------
+
+ @Test
+ public void testRun() throws Exception {
+ {
+ RunCounter left = new RunCounter();
+ RunCounter right = new RunCounter();
+ ConditionalNullaryProcedure p = new ConditionalNullaryProcedure(
+ Constant.TRUE,
+ left,
+ right);
+ assertEquals(0,left.count);
+ assertEquals(0,right.count);
+ p.run();
+ assertEquals(1,left.count);
+ assertEquals(0,right.count);
+ p.run();
+ assertEquals(2,left.count);
+ assertEquals(0,right.count);
+ p.run();
+ assertEquals(3,left.count);
+ assertEquals(0,right.count);
+ }
+ {
+ RunCounter left = new RunCounter();
+ RunCounter right = new RunCounter();
+ ConditionalNullaryProcedure p = new ConditionalNullaryProcedure(
+ Constant.FALSE,
+ left,
+ right);
+ assertEquals(0,left.count);
+ assertEquals(0,right.count);
+ p.run();
+ assertEquals(0,left.count);
+ assertEquals(1,right.count);
+ p.run();
+ assertEquals(0,left.count);
+ assertEquals(2,right.count);
+ p.run();
+ assertEquals(0,left.count);
+ assertEquals(3,right.count);
+ }
+ }
+
+ @Test
+ public void testEquals() throws Exception {
+ ConditionalNullaryProcedure p = new ConditionalNullaryProcedure(
+ Constant.FALSE,
+ NoOp.INSTANCE,
+ NoOp.INSTANCE);
+ assertEquals(p,p);
+ assertObjectsAreEqual(p,new ConditionalNullaryProcedure(
+ Constant.FALSE,
+ NoOp.INSTANCE,
+ NoOp.INSTANCE));
+ assertObjectsAreNotEqual(p,new ConditionalNullaryProcedure(
+ Constant.TRUE,
+ NoOp.INSTANCE,
+ NoOp.INSTANCE));
+ assertObjectsAreNotEqual(p,new ConditionalNullaryProcedure(
+ Constant.TRUE,
+ NoOp.INSTANCE,
+ NoOp.INSTANCE));
+ assertObjectsAreNotEqual(p,new ConditionalNullaryProcedure(
+ Constant.FALSE,
+ new RunCounter(),
+ NoOp.INSTANCE));
+ assertObjectsAreNotEqual(p,new ConditionalNullaryProcedure(
+ Constant.FALSE,
+ NoOp.INSTANCE,
+ new RunCounter()));
+ assertObjectsAreNotEqual(p,new ConditionalNullaryProcedure(
+ Constant.TRUE,
+ NoOp.INSTANCE));
+ assertTrue(!p.equals(null));
+ }
+
+ // Classes
+ // ------------------------------------------------------------------------
+
+ static class RunCounter implements NullaryProcedure {
+ public void run() {
+ count++;
+ }
+ public int count = 0;
+ }
+}
Modified: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestConditionalPredicate.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestConditionalPredicate.java?rev=1508677&r1=1508676&r2=1508677&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestConditionalPredicate.java (original)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestConditionalPredicate.java Tue Jul 30 22:48:02 2013
@@ -21,6 +21,7 @@ import static org.junit.Assert.assertTru
import org.apache.commons.functor.BaseFunctorTest;
import org.apache.commons.functor.core.Constant;
+import org.apache.commons.functor.core.Identity;
import org.junit.Test;
/**
@@ -33,7 +34,7 @@ public class TestConditionalPredicate ex
@Override
protected Object makeFunctor() {
- return new ConditionalPredicate(
+ return new ConditionalPredicate<Object>(
Constant.TRUE,
Constant.FALSE,
Constant.TRUE);
@@ -44,43 +45,35 @@ public class TestConditionalPredicate ex
@Test
public void testTest() throws Exception {
- {
- ConditionalPredicate p = new ConditionalPredicate(
- Constant.TRUE,
- Constant.TRUE,
- Constant.FALSE);
- assertTrue(p.test());
- }
- {
- ConditionalPredicate p = new ConditionalPredicate(
- Constant.FALSE,
- Constant.TRUE,
- Constant.FALSE);
- assertTrue(!p.test());
- }
+ ConditionalPredicate<Object> p = new ConditionalPredicate<Object>(
+ Identity.INSTANCE,
+ Constant.TRUE,
+ Constant.FALSE);
+ assertTrue(p.test(Boolean.TRUE));
+ assertTrue(!p.test(Boolean.FALSE));
}
@Test
public void testEquals() throws Exception {
- ConditionalPredicate p = new ConditionalPredicate(
- Constant.TRUE,
+ ConditionalPredicate<Object> p = new ConditionalPredicate<Object>(
+ Identity.INSTANCE,
Constant.TRUE,
- Constant.FALSE);
+ Constant.TRUE);
assertEquals(p,p);
- assertObjectsAreEqual(p,new ConditionalPredicate(
- Constant.TRUE,
- Constant.TRUE,
- Constant.FALSE));
- assertObjectsAreNotEqual(p,new ConditionalPredicate(
+ assertObjectsAreEqual(p,new ConditionalPredicate<Object>(
+ Identity.INSTANCE,
Constant.TRUE,
+ Constant.TRUE));
+ assertObjectsAreNotEqual(p,new ConditionalPredicate<Object>(
+ Identity.INSTANCE,
Constant.FALSE,
Constant.TRUE));
- assertObjectsAreNotEqual(p,new ConditionalPredicate(
+ assertObjectsAreNotEqual(p,new ConditionalPredicate<Object>(
Constant.TRUE,
Constant.TRUE,
Constant.TRUE));
- assertObjectsAreNotEqual(p,new ConditionalPredicate(
- Constant.FALSE,
+ assertObjectsAreNotEqual(p,new ConditionalPredicate<Object>(
+ Identity.INSTANCE,
Constant.TRUE,
Constant.FALSE));
assertTrue(!p.equals(null));
Modified: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestConditionalProcedure.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestConditionalProcedure.java?rev=1508677&r1=1508676&r2=1508677&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestConditionalProcedure.java (original)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestConditionalProcedure.java Tue Jul 30 22:48:02 2013
@@ -22,6 +22,7 @@ import static org.junit.Assert.assertTru
import org.apache.commons.functor.BaseFunctorTest;
import org.apache.commons.functor.Procedure;
import org.apache.commons.functor.core.Constant;
+import org.apache.commons.functor.core.Identity;
import org.apache.commons.functor.core.NoOp;
import org.junit.Test;
@@ -35,7 +36,7 @@ public class TestConditionalProcedure ex
@Override
protected Object makeFunctor() {
- return new ConditionalProcedure(
+ return new ConditionalProcedure<Object>(
Constant.TRUE,
NoOp.INSTANCE,
NoOp.INSTANCE);
@@ -46,84 +47,59 @@ public class TestConditionalProcedure ex
@Test
public void testRun() throws Exception {
- {
- RunCounter left = new RunCounter();
- RunCounter right = new RunCounter();
- ConditionalProcedure p = new ConditionalProcedure(
- Constant.TRUE,
- left,
- right);
- assertEquals(0,left.count);
- assertEquals(0,right.count);
- p.run();
- assertEquals(1,left.count);
- assertEquals(0,right.count);
- p.run();
- assertEquals(2,left.count);
- assertEquals(0,right.count);
- p.run();
- assertEquals(3,left.count);
- assertEquals(0,right.count);
- }
- {
- RunCounter left = new RunCounter();
- RunCounter right = new RunCounter();
- ConditionalProcedure p = new ConditionalProcedure(
- Constant.FALSE,
- left,
- right);
- assertEquals(0,left.count);
- assertEquals(0,right.count);
- p.run();
- assertEquals(0,left.count);
- assertEquals(1,right.count);
- p.run();
- assertEquals(0,left.count);
- assertEquals(2,right.count);
- p.run();
- assertEquals(0,left.count);
- assertEquals(3,right.count);
- }
+ RunCounter left = new RunCounter();
+ RunCounter right = new RunCounter();
+ ConditionalProcedure<Object> p = new ConditionalProcedure<Object>(
+ Identity.INSTANCE,
+ left,
+ right);
+ assertEquals(0,left.count);
+ assertEquals(0,right.count);
+ p.run(Boolean.TRUE);
+ assertEquals(1,left.count);
+ assertEquals(0,right.count);
+ p.run(Boolean.FALSE);
+ assertEquals(1,left.count);
+ assertEquals(1,right.count);
+ p.run(Boolean.TRUE);
+ assertEquals(2,left.count);
+ assertEquals(1,right.count);
}
@Test
public void testEquals() throws Exception {
- ConditionalProcedure p = new ConditionalProcedure(
- Constant.FALSE,
+ ConditionalProcedure<Object> p = new ConditionalProcedure<Object>(
+ Identity.INSTANCE,
NoOp.INSTANCE,
NoOp.INSTANCE);
assertEquals(p,p);
- assertObjectsAreEqual(p,new ConditionalProcedure(
- Constant.FALSE,
+ assertObjectsAreEqual(p,new ConditionalProcedure<Object>(
+ Identity.INSTANCE,
NoOp.INSTANCE,
NoOp.INSTANCE));
- assertObjectsAreNotEqual(p,new ConditionalProcedure(
+ assertObjectsAreNotEqual(p,new ConditionalProcedure<Object>(
Constant.TRUE,
NoOp.INSTANCE,
NoOp.INSTANCE));
- assertObjectsAreNotEqual(p,new ConditionalProcedure(
- Constant.TRUE,
- NoOp.INSTANCE,
- NoOp.INSTANCE));
- assertObjectsAreNotEqual(p,new ConditionalProcedure(
- Constant.FALSE,
+ assertObjectsAreNotEqual(p,new ConditionalProcedure<Object>(
+ Identity.INSTANCE,
new RunCounter(),
NoOp.INSTANCE));
- assertObjectsAreNotEqual(p,new ConditionalProcedure(
- Constant.FALSE,
+ assertObjectsAreNotEqual(p,new ConditionalProcedure<Object>(
+ Identity.INSTANCE,
NoOp.INSTANCE,
new RunCounter()));
- assertObjectsAreNotEqual(p,new ConditionalProcedure(
+ assertObjectsAreNotEqual(p, new ConditionalProcedure<Object>(
Constant.TRUE,
- NoOp.INSTANCE));
+ new RunCounter()));
assertTrue(!p.equals(null));
}
// Classes
// ------------------------------------------------------------------------
- static class RunCounter implements Procedure {
- public void run() {
+ static class RunCounter implements Procedure<Object> {
+ public void run(Object obj) {
count++;
}
public int count = 0;
Added: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestDoWhileNullaryProcedure.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestDoWhileNullaryProcedure.java?rev=1508677&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestDoWhileNullaryProcedure.java (added)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestDoWhileNullaryProcedure.java Tue Jul 30 22:48:02 2013
@@ -0,0 +1,114 @@
+/*
+ * 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.commons.functor.core.composite;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.NullaryPredicate;
+import org.apache.commons.functor.NullaryProcedure;
+import org.apache.commons.functor.adapter.BoundNullaryPredicate;
+import org.apache.commons.functor.core.Constant;
+import org.apache.commons.functor.core.NoOp;
+import org.apache.commons.functor.core.collection.IsEmpty;
+import org.junit.Test;
+
+/**
+ * @version $Revision: 1345136 $ $Date: 2012-06-01 09:47:06 -0300 (Fri, 01 Jun 2012) $
+ */
+public class TestDoWhileNullaryProcedure extends BaseFunctorTest {
+
+ // Functor Testing Framework
+ // ------------------------------------------------------------------------
+
+ @Override
+ protected Object makeFunctor() {
+ return new DoWhileNullaryProcedure(NoOp.INSTANCE, Constant.FALSE);
+ }
+
+ // Tests
+ // ------------------------------------------------------------------------
+ public class ListRemoveFirstProcedure implements NullaryProcedure {
+ protected List<Object> list;
+
+
+ public ListRemoveFirstProcedure(List<Object> list) {
+ this.list=list;
+ }
+
+
+ public void run() {
+ list.remove(0);
+ }
+ }
+
+
+ private List<Object> getList() {
+ List<Object> list = new LinkedList<Object>();
+ list.add("a");
+ list.add("b");
+ list.add("c");
+ list.add("d");
+ return list;
+ }
+
+
+ @Test
+ public void testLoopWithAction() throws Exception {
+ List<Object> list=getList();
+
+ NullaryProcedure action=new ListRemoveFirstProcedure(list);
+ NullaryPredicate condition=new NullaryNot(new BoundNullaryPredicate(new IsEmpty<List<Object>>(), list));
+ NullaryProcedure procedure=new DoWhileNullaryProcedure(action, condition);
+
+ assertTrue("The condition should be true before running the loop", condition.test());
+ assertFalse("The list should not be empty then", list.isEmpty());
+ procedure.run();
+ assertFalse("The condition should be false after running the loop", condition.test());
+ assertTrue("The list should be empty then", list.isEmpty());
+
+ list=getList();
+ action=new ListRemoveFirstProcedure(list);
+ condition=new NullaryPredicate() {
+ private int count=2;
+
+ public boolean test() {
+ return count-- > 0;
+ }
+ };
+ procedure=new DoWhileNullaryProcedure(action, condition);
+ procedure.run();
+ assertFalse("The list should not contain \"a\" anymore", list.contains("a"));
+ assertFalse("The list should not contain \"b\" anymore", list.contains("b"));
+ assertFalse("The list should not contain \"c\" anymore", list.contains("c"));
+ assertTrue("The list should still contain \"d\"", list.contains("d"));
+ }
+
+ @Test
+ public void testLoopForNothing() {
+ List<Object> list=getList();
+ NullaryProcedure action=new ListRemoveFirstProcedure(list);
+ NullaryProcedure procedure=new DoWhileNullaryProcedure(action, Constant.FALSE);
+ assertTrue("The list should contain 4 elements before runnning the loop", list.size()==4);
+ procedure.run();
+ assertTrue("The list should contain 3 elements after runnning the loop", list.size()==3);
+ }
+}
Modified: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestNot.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestNot.java?rev=1508677&r1=1508676&r2=1508677&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestNot.java (original)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestNot.java Tue Jul 30 22:48:02 2013
@@ -17,7 +17,6 @@
package org.apache.commons.functor.core.composite;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
@@ -37,7 +36,7 @@ public class TestNot extends BaseFunctor
@Override
protected Object makeFunctor() {
- return new Not(Constant.TRUE);
+ return new Not<Object>(Constant.TRUE);
}
// Tests
@@ -45,19 +44,19 @@ public class TestNot extends BaseFunctor
@Test
public void testTest() throws Exception {
- Predicate truePred = new Not(Constant.FALSE);
- assertTrue(truePred.test());
- Predicate falsePred = new Not(Constant.TRUE);
- assertFalse(falsePred.test());
+ Predicate<Object> truePred = new Not<Object>(Constant.FALSE);
+ assertTrue(truePred.test(null));
+ assertTrue(truePred.test("xyzzy"));
+ assertTrue(truePred.test(new Integer(3)));
}
@Test
public void testEquals() throws Exception {
- Not p = new Not(Constant.TRUE);
+ Not<Object> p = new Not<Object>(Constant.TRUE);
assertEquals(p,p);
- assertObjectsAreEqual(p,new Not(Constant.TRUE));
+ assertObjectsAreEqual(p,new Not<Object>(Constant.TRUE));
assertObjectsAreEqual(p,Not.not(Constant.TRUE));
- assertObjectsAreNotEqual(p,new Not(Constant.FALSE));
+ assertObjectsAreNotEqual(p,new Not<Object>(Constant.FALSE));
assertObjectsAreNotEqual(p,Constant.TRUE);
assertTrue(!p.equals(null));
}
@@ -69,6 +68,6 @@ public class TestNot extends BaseFunctor
@Test
public void testNotNotNull() throws Exception {
- assertNotNull(Not.not(Constant.TRUE));
+ assertNotNull(Not.not(Constant.truePredicate()));
}
}
Added: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestNullaryAnd.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestNullaryAnd.java?rev=1508677&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestNullaryAnd.java (added)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestNullaryAnd.java Tue Jul 30 22:48:02 2013
@@ -0,0 +1,140 @@
+/*
+ * 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.commons.functor.core.composite;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Arrays;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.NullaryPredicate;
+import org.apache.commons.functor.core.Constant;
+import org.junit.Test;
+
+/**
+ * @version $Revision: 1365329 $ $Date: 2012-07-24 19:34:23 -0300 (Tue, 24 Jul 2012) $
+ */
+public class TestNullaryAnd extends BaseFunctorTest {
+
+ // Functor Testing Framework
+ // ------------------------------------------------------------------------
+
+ @Override
+ protected Object makeFunctor() {
+ return new NullaryAnd(Constant.TRUE, Constant.TRUE);
+ }
+
+ // Tests
+ // ------------------------------------------------------------------------
+
+ @Test
+ public void testTrue() throws Exception {
+ assertTrue((new NullaryAnd()).test());
+ assertTrue((new NullaryAnd(Constant.TRUE)).test());
+ assertTrue((new NullaryAnd(Constant.TRUE,Constant.TRUE)).test());
+ assertTrue((new NullaryAnd(Constant.TRUE,Constant.TRUE,Constant.TRUE)).test());
+
+ NullaryAnd p = new NullaryAnd(Constant.TRUE);
+ assertTrue(p.test());
+ for (int i=0;i<10;i++) {
+ p.and(Constant.TRUE);
+ assertTrue(p.test());
+ }
+
+ NullaryAnd q = new NullaryAnd(Constant.TRUE);
+ assertTrue(q.test());
+ for (int i=0;i<10;i++) {
+ q.and(Constant.TRUE);
+ assertTrue(q.test());
+ }
+
+ NullaryAnd r = new NullaryAnd(p,q);
+ assertTrue(r.test());
+ }
+
+ @Test
+ public void testFalse() throws Exception {
+ assertTrue(!(new NullaryAnd(Constant.FALSE)).test());
+ assertTrue(!(new NullaryAnd(Constant.TRUE,Constant.FALSE)).test());
+ assertTrue(!(new NullaryAnd(Constant.TRUE,Constant.TRUE,Constant.FALSE)).test());
+
+ NullaryAnd p = new NullaryAnd(Constant.FALSE);
+ assertTrue(!p.test());
+ for (int i=0;i<10;i++) {
+ p.and(Constant.FALSE);
+ assertTrue(!p.test());
+ }
+
+ NullaryAnd q = new NullaryAnd(Constant.TRUE);
+ assertTrue(q.test());
+ for (int i=0;i<10;i++) {
+ q.and(Constant.TRUE);
+ assertTrue(q.test());
+ }
+
+ NullaryAnd r = new NullaryAnd(p,q);
+ assertTrue(!r.test());
+ }
+
+ @Test
+ public void testDuplicateAdd() throws Exception {
+ NullaryPredicate p = Constant.TRUE;
+ NullaryAnd q = new NullaryAnd(p,p);
+ assertTrue(q.test());
+ for (int i=0;i<10;i++) {
+ q.and(p);
+ assertTrue(q.test());
+ }
+ }
+
+ @Test
+ public void testEquals() throws Exception {
+ NullaryAnd p = new NullaryAnd();
+ assertEquals(p,p);
+ NullaryAnd q = new NullaryAnd();
+ assertObjectsAreEqual(p,q);
+
+ for (int i=0;i<3;i++) {
+ p.and(Constant.TRUE);
+ assertObjectsAreNotEqual(p,q);
+ q.and(Constant.truePredicate());
+ assertObjectsAreEqual(p,q);
+ p.and(new NullaryAnd(Constant.TRUE,Constant.FALSE));
+ assertObjectsAreNotEqual(p,q);
+ q.and(new NullaryAnd(Constant.TRUE,Constant.FALSE));
+ assertObjectsAreEqual(p,q);
+ }
+
+ assertObjectsAreNotEqual(p,Constant.TRUE);
+ Iterable<NullaryPredicate> iterable = Arrays.<NullaryPredicate>asList(
+ Constant.TRUE,
+ new NullaryAnd(Constant.TRUE, Constant.FALSE),
+ Constant.TRUE,
+ new NullaryAnd(Constant.TRUE, Constant.FALSE),
+ Constant.TRUE,
+ new NullaryAnd(Constant.TRUE, Constant.FALSE));
+ assertObjectsAreEqual(p,new NullaryAnd(iterable));
+
+ assertObjectsAreNotEqual(p,new NullaryAnd((Iterable<NullaryPredicate>)null));
+ assertObjectsAreNotEqual(p,new NullaryAnd((NullaryPredicate[])null));
+ assertObjectsAreNotEqual(p,new NullaryAnd((NullaryPredicate)null));
+
+ assertTrue(!p.equals(null));
+ }
+
+}
Added: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestNullaryNot.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestNullaryNot.java?rev=1508677&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestNullaryNot.java (added)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestNullaryNot.java Tue Jul 30 22:48:02 2013
@@ -0,0 +1,74 @@
+/*
+ * 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.commons.functor.core.composite;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.NullaryPredicate;
+import org.apache.commons.functor.core.Constant;
+import org.junit.Test;
+
+/**
+ * @version $Revision: 1365329 $ $Date: 2012-07-24 19:34:23 -0300 (Tue, 24 Jul 2012) $
+ */
+public class TestNullaryNot extends BaseFunctorTest {
+
+ // Functor Testing Framework
+ // ------------------------------------------------------------------------
+
+ @Override
+ protected Object makeFunctor() {
+ return new NullaryNot(Constant.TRUE);
+ }
+
+ // Tests
+ // ------------------------------------------------------------------------
+
+ @Test
+ public void testTest() throws Exception {
+ NullaryPredicate truePred = new NullaryNot(Constant.FALSE);
+ assertTrue(truePred.test());
+ NullaryPredicate falsePred = new NullaryNot(Constant.TRUE);
+ assertFalse(falsePred.test());
+ }
+
+ @Test
+ public void testEquals() throws Exception {
+ NullaryNot p = new NullaryNot(Constant.TRUE);
+ assertEquals(p,p);
+ assertObjectsAreEqual(p,new NullaryNot(Constant.TRUE));
+ assertObjectsAreEqual(p,NullaryNot.not(Constant.TRUE));
+ assertObjectsAreNotEqual(p,new NullaryNot(Constant.FALSE));
+ assertObjectsAreNotEqual(p,Constant.TRUE);
+ assertTrue(!p.equals(null));
+ }
+
+ @Test
+ public void testNotNull() throws Exception {
+ assertNull(NullaryNot.not(null));
+ }
+
+ @Test
+ public void testNotNotNull() throws Exception {
+ assertNotNull(NullaryNot.not(Constant.TRUE));
+ }
+}
Added: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestNullaryOr.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestNullaryOr.java?rev=1508677&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestNullaryOr.java (added)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestNullaryOr.java Tue Jul 30 22:48:02 2013
@@ -0,0 +1,154 @@
+/*
+ * 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.commons.functor.core.composite;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Arrays;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.NullaryPredicate;
+import org.apache.commons.functor.core.Constant;
+import org.junit.Test;
+
+/**
+ * @version $Revision: 1365329 $ $Date: 2012-07-24 19:34:23 -0300 (Tue, 24 Jul 2012) $
+ */
+public class TestNullaryOr extends BaseFunctorTest {
+
+ // Functor Testing Framework
+ // ------------------------------------------------------------------------
+
+ @Override
+ protected Object makeFunctor() {
+ return new NullaryOr(Constant.FALSE, Constant.TRUE);
+ }
+
+ // Tests
+ // ------------------------------------------------------------------------
+
+ @Test
+ public void testConstructors() {
+ NullaryOr or = new NullaryOr(Constant.FALSE);
+ assertEquals(Boolean.FALSE, or.test());
+ NullaryOr or2 = new NullaryOr((Iterable<NullaryPredicate>)Arrays.asList((NullaryPredicate)Constant.truePredicate()));
+ assertEquals(Boolean.TRUE, or2.test());
+ NullaryOr or3 = new NullaryOr((NullaryPredicate)null);
+ assertEquals(Boolean.FALSE, or3.test());
+ NullaryOr or4 = new NullaryOr((Iterable<NullaryPredicate>)null);
+ assertEquals(Boolean.FALSE, or4.test());
+ }
+
+ @Test
+ public void testTrue() throws Exception {
+ assertTrue((new NullaryOr(Constant.TRUE)).test());
+ assertTrue((new NullaryOr(Constant.FALSE, Constant.TRUE)).test());
+ assertTrue((new NullaryOr(Constant.FALSE, Constant.FALSE, Constant.TRUE)).test());
+
+ NullaryOr p = new NullaryOr(Constant.TRUE);
+ assertTrue(p.test());
+ for (int i=0;i<10;i++) {
+ p.or(Constant.of(i%2==0));
+ assertTrue(p.test());
+ }
+
+ NullaryOr q = new NullaryOr(Constant.TRUE);
+ assertTrue(q.test());
+ for (int i=0;i<10;i++) {
+ q.or(Constant.of(i%2==0));
+ assertTrue(q.test());
+ }
+
+ NullaryOr r = new NullaryOr(p,q);
+ assertTrue(r.test());
+ }
+
+ @Test
+ public void testFalse() throws Exception {
+ assertFalse(new NullaryOr().test());
+ assertFalse(new NullaryOr(Constant.FALSE).test());
+ assertFalse(new NullaryOr(Constant.FALSE,Constant.FALSE).test());
+ assertFalse(new NullaryOr(Constant.FALSE,Constant.FALSE,Constant.FALSE).test());
+
+ NullaryOr p = new NullaryOr(Constant.FALSE);
+ assertFalse(p.test());
+ for (int i=0;i<10;i++) {
+ p.or(Constant.FALSE);
+ assertFalse(p.test());
+ }
+
+ NullaryOr q = new NullaryOr(Constant.FALSE);
+ assertFalse(q.test());
+ for (int i=0;i<10;i++) {
+ q.or(Constant.FALSE);
+ assertFalse(q.test());
+ }
+
+ NullaryOr r = new NullaryOr(p,q);
+ assertTrue(!r.test());
+ }
+
+ @Test
+ public void testDuplicateAdd() throws Exception {
+ NullaryPredicate p = Constant.TRUE;
+ NullaryOr q = new NullaryOr(p,p);
+ assertTrue(q.test());
+ for (int i=0;i<10;i++) {
+ q.or(p);
+ assertTrue(q.test());
+ }
+ }
+
+ @Test
+ public void testEquals() throws Exception {
+ NullaryOr p = new NullaryOr();
+ assertEquals(p,p);
+
+ NullaryOr q = new NullaryOr();
+ assertObjectsAreEqual(p,q);
+
+ NullaryAnd r = new NullaryAnd();
+ assertObjectsAreNotEqual(p,r);
+
+ for (int i=0;i<3;i++) {
+ p.or(Constant.TRUE);
+ assertObjectsAreNotEqual(p,q);
+ q.or(Constant.TRUE);
+ assertObjectsAreEqual(p,q);
+ r.and(Constant.TRUE);
+ assertObjectsAreNotEqual(p,r);
+
+ p.or(new NullaryOr(Constant.TRUE,Constant.FALSE));
+ assertObjectsAreNotEqual(p,q);
+ q.or(new NullaryOr(Constant.TRUE,Constant.FALSE));
+ assertObjectsAreEqual(p,q);
+ r.and(new NullaryOr(Constant.TRUE,Constant.FALSE));
+ assertObjectsAreNotEqual(p,r);
+ }
+
+ assertObjectsAreNotEqual(p,Constant.TRUE);
+
+ assertObjectsAreNotEqual(p,new NullaryOr((Iterable<NullaryPredicate>)null));
+ assertObjectsAreNotEqual(p,new NullaryOr((NullaryPredicate[])null));
+ assertObjectsAreNotEqual(p,new NullaryOr((NullaryPredicate)null));
+
+ assertTrue(!p.equals(null));
+ }
+
+}
Added: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestNullarySequence.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestNullarySequence.java?rev=1508677&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestNullarySequence.java (added)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestNullarySequence.java Tue Jul 30 22:48:02 2013
@@ -0,0 +1,139 @@
+/*
+ * 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.commons.functor.core.composite;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.NullaryProcedure;
+import org.apache.commons.functor.core.NoOp;
+import org.junit.Test;
+
+/**
+ * @version $Revision: 1365329 $ $Date: 2012-07-24 19:34:23 -0300 (Tue, 24 Jul 2012) $
+ */
+public class TestNullarySequence extends BaseFunctorTest {
+
+ // Functor Testing Framework
+ // ------------------------------------------------------------------------
+
+ @Override
+ protected Object makeFunctor() {
+ return new NullarySequence(new NoOp(),new NoOp());
+ }
+
+ // Tests
+ // ------------------------------------------------------------------------
+
+ @Test
+ public void testConstructors() throws Exception {
+ NullarySequence seq1 = new NullarySequence((NullaryProcedure)null);
+ NullarySequence seq2 = new NullarySequence();
+ assertObjectsAreEqual(seq1, seq2);
+
+ RunCounter p1 = new RunCounter();
+ RunCounter p2 = new RunCounter();
+ List<NullaryProcedure> iterable = new ArrayList<NullaryProcedure>();
+ iterable.add(p1);
+ iterable.add(p2);
+ NullarySequence seq3 = new NullarySequence(iterable);
+ NullarySequence seq4 = new NullarySequence(p1, p2);
+ assertObjectsAreEqual(seq3, seq4);
+
+ NullarySequence seq5 = new NullarySequence((Iterable<NullaryProcedure>)null);
+ NullarySequence seq6 = new NullarySequence((NullaryProcedure[])null);
+ assertObjectsAreEqual(seq5, seq6);
+ }
+
+ @Test
+ public void testRunZero() throws Exception {
+ NullarySequence seq = new NullarySequence();
+ seq.run();
+ }
+
+ @Test
+ public void testRunOne() throws Exception {
+ RunCounter counter = new RunCounter();
+ NullarySequence seq = new NullarySequence(counter);
+ assertEquals(0,counter.count);
+ seq.run();
+ assertEquals(1,counter.count);
+ }
+
+ @Test
+ public void testRunTwo() throws Exception {
+ RunCounter[] counter = { new RunCounter(), new RunCounter() };
+ NullarySequence seq = new NullarySequence(counter[0],counter[1]);
+ assertEquals(0,counter[0].count);
+ assertEquals(0,counter[1].count);
+ seq.run();
+ assertEquals(1,counter[0].count);
+ assertEquals(1,counter[1].count);
+ }
+
+ @Test
+ public void testThen() throws Exception {
+ List<RunCounter> list = new ArrayList<RunCounter>();
+ NullarySequence seq = new NullarySequence();
+ seq.run();
+ for (int i=0;i<10;i++) {
+ RunCounter counter = new RunCounter();
+ seq.then(counter);
+ list.add(counter);
+ seq.run();
+ for (int j=0;j<list.size();j++) {
+ assertEquals(list.size()-j,(((RunCounter)(list.get(j))).count));
+ }
+ }
+ }
+
+ @Test
+ public void testEquals() throws Exception {
+ NullarySequence p = new NullarySequence();
+ assertEquals(p,p);
+ NullarySequence q = new NullarySequence();
+ assertObjectsAreEqual(p,q);
+
+ for (int i=0;i<3;i++) {
+ p.then(new NoOp());
+ assertObjectsAreNotEqual(p,q);
+ q.then(new NoOp());
+ assertObjectsAreEqual(p,q);
+ p.then(new NullarySequence(new NoOp(),new NoOp()));
+ assertObjectsAreNotEqual(p,q);
+ q.then(new NullarySequence(new NoOp(),new NoOp()));
+ assertObjectsAreEqual(p,q);
+ }
+
+ assertObjectsAreNotEqual(p,new NoOp());
+ assertTrue(!p.equals(null));
+ }
+
+ // Classes
+ // ------------------------------------------------------------------------
+
+ static class RunCounter implements NullaryProcedure {
+ public void run() {
+ count++;
+ }
+ public int count = 0;
+ }
+}
Modified: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestOr.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestOr.java?rev=1508677&r1=1508676&r2=1508677&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestOr.java (original)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestOr.java Tue Jul 30 22:48:02 2013
@@ -17,11 +17,8 @@
package org.apache.commons.functor.core.composite;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
-import java.util.Arrays;
-
import org.apache.commons.functor.BaseFunctorTest;
import org.apache.commons.functor.Predicate;
import org.apache.commons.functor.core.Constant;
@@ -30,6 +27,7 @@ import org.junit.Test;
/**
* @version $Revision$ $Date$
*/
+@SuppressWarnings("unchecked")
public class TestOr extends BaseFunctorTest {
// Functor Testing Framework
@@ -37,118 +35,104 @@ public class TestOr extends BaseFunctorT
@Override
protected Object makeFunctor() {
- return new Or(Constant.FALSE, Constant.TRUE);
+ return new Or<Object>(Constant.FALSE,Constant.TRUE);
}
// Tests
// ------------------------------------------------------------------------
@Test
- public void testConstructors() {
- Or or = new Or(Constant.FALSE);
- assertEquals(Boolean.FALSE, or.test());
- Or or2 = new Or((Iterable<Predicate>)Arrays.asList((Predicate)Constant.truePredicate()));
- assertEquals(Boolean.TRUE, or2.test());
- Or or3 = new Or((Predicate)null);
- assertEquals(Boolean.FALSE, or3.test());
- Or or4 = new Or((Iterable<Predicate>)null);
- assertEquals(Boolean.FALSE, or4.test());
- }
-
- @Test
public void testTrue() throws Exception {
- assertTrue((new Or(Constant.TRUE)).test());
- assertTrue((new Or(Constant.FALSE, Constant.TRUE)).test());
- assertTrue((new Or(Constant.FALSE, Constant.FALSE, Constant.TRUE)).test());
+ assertTrue((new Or<Object>(Constant.TRUE)).test("xyzzy"));
+ assertTrue((new Or<Object>(Constant.FALSE,Constant.TRUE)).test("xyzzy"));
+ assertTrue((new Or<Object>(Constant.FALSE,Constant.FALSE,Constant.TRUE)).test("xyzzy"));
- Or p = new Or(Constant.TRUE);
- assertTrue(p.test());
+ Or<Object> p = new Or<Object>(Constant.TRUE);
+ assertTrue(p.test("xyzzy"));
for (int i=0;i<10;i++) {
p.or(Constant.of(i%2==0));
- assertTrue(p.test());
+ assertTrue(p.test("xyzzy"));
}
- Or q = new Or(Constant.TRUE);
- assertTrue(q.test());
+ Or<Object> q = new Or<Object>(Constant.TRUE);
+ assertTrue(q.test("xyzzy"));
for (int i=0;i<10;i++) {
q.or(Constant.of(i%2==0));
- assertTrue(q.test());
+ assertTrue(q.test("xyzzy"));
}
- Or r = new Or(p,q);
- assertTrue(r.test());
+ Or<Object> r = new Or<Object>(p,q);
+ assertTrue(r.test("xyzzy"));
}
@Test
public void testFalse() throws Exception {
- assertFalse(new Or().test());
- assertFalse(new Or(Constant.FALSE).test());
- assertFalse(new Or(Constant.FALSE,Constant.FALSE).test());
- assertFalse(new Or(Constant.FALSE,Constant.FALSE,Constant.FALSE).test());
+ assertTrue(!(new Or<Object>()).test("xyzzy"));
+ assertTrue(!(new Or<Object>(Constant.FALSE)).test("xyzzy"));
+ assertTrue(!(new Or<Object>(Constant.FALSE,Constant.FALSE)).test("xyzzy"));
+ assertTrue(!(new Or<Object>(Constant.FALSE,Constant.FALSE,Constant.FALSE)).test("xyzzy"));
- Or p = new Or(Constant.FALSE);
- assertFalse(p.test());
+ Or<Object> p = new Or<Object>(Constant.FALSE);
+ assertTrue(!p.test("xyzzy"));
for (int i=0;i<10;i++) {
p.or(Constant.FALSE);
- assertFalse(p.test());
+ assertTrue(!p.test("xyzzy"));
}
- Or q = new Or(Constant.FALSE);
- assertFalse(q.test());
+ Or<Object> q = new Or<Object>(Constant.FALSE);
+ assertTrue(!q.test("xyzzy"));
for (int i=0;i<10;i++) {
q.or(Constant.FALSE);
- assertFalse(q.test());
+ assertTrue(!q.test("xyzzy"));
}
- Or r = new Or(p,q);
- assertTrue(!r.test());
+ Or<Object> r = new Or<Object>(p,q);
+ assertTrue(!r.test("xyzzy"));
}
@Test
public void testDuplicateAdd() throws Exception {
- Predicate p = Constant.TRUE;
- Or q = new Or(p,p);
- assertTrue(q.test());
+ Predicate<Object> p = Constant.TRUE;
+ Or<Object> q = new Or<Object>(p,p);
+ assertTrue(q.test("xyzzy"));
for (int i=0;i<10;i++) {
q.or(p);
- assertTrue(q.test());
+ assertTrue(q.test("xyzzy"));
}
}
@Test
public void testEquals() throws Exception {
- Or p = new Or();
+ Or<Object> p = new Or<Object>();
assertEquals(p,p);
- Or q = new Or();
+ Or<Object> q = new Or<Object>();
assertObjectsAreEqual(p,q);
- And r = new And();
+ And<Object> r = new And<Object>();
assertObjectsAreNotEqual(p,r);
for (int i=0;i<3;i++) {
- p.or(Constant.TRUE);
+ p.or(Constant.truePredicate());
assertObjectsAreNotEqual(p,q);
- q.or(Constant.TRUE);
+ q.or(Constant.truePredicate());
assertObjectsAreEqual(p,q);
- r.and(Constant.TRUE);
+ r.and(Constant.truePredicate());
assertObjectsAreNotEqual(p,r);
- p.or(new Or(Constant.TRUE,Constant.FALSE));
+ p.or(new Or<Object>(Constant.truePredicate(),Constant.falsePredicate()));
assertObjectsAreNotEqual(p,q);
- q.or(new Or(Constant.TRUE,Constant.FALSE));
+ q.or(new Or<Object>(Constant.truePredicate(),Constant.falsePredicate()));
assertObjectsAreEqual(p,q);
- r.and(new Or(Constant.TRUE,Constant.FALSE));
+ r.and(new Or<Object>(Constant.truePredicate(),Constant.falsePredicate()));
assertObjectsAreNotEqual(p,r);
}
- assertObjectsAreNotEqual(p,Constant.TRUE);
-
- assertObjectsAreNotEqual(p,new Or((Iterable<Predicate>)null));
- assertObjectsAreNotEqual(p,new Or((Predicate[])null));
- assertObjectsAreNotEqual(p,new Or((Predicate)null));
-
- assertTrue(!p.equals(null));
+ assertObjectsAreNotEqual(p,Constant.truePredicate());
+ Or<Object> s = new Or<Object>();
+ s.or(Constant.truePredicate());
+ s.or(new Or<Object>(Constant.truePredicate(),Constant.falsePredicate()));
+ assertObjectsAreEqual(s, new Or<Object>(s.getPredicateList()));
}
}
Modified: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestSequence.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestSequence.java?rev=1508677&r1=1508676&r2=1508677&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestSequence.java (original)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestSequence.java Tue Jul 30 22:48:02 2013
@@ -17,7 +17,7 @@
package org.apache.commons.functor.core.composite;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
import java.util.ArrayList;
import java.util.List;
@@ -30,6 +30,7 @@ import org.junit.Test;
/**
* @version $Revision$ $Date$
*/
+@SuppressWarnings("unchecked")
public class TestSequence extends BaseFunctorTest {
// Functor Testing Framework
@@ -37,7 +38,7 @@ public class TestSequence extends BaseFu
@Override
protected Object makeFunctor() {
- return new Sequence(new NoOp(),new NoOp());
+ return new Sequence<Object>(new NoOp(),new NoOp());
}
// Tests
@@ -45,71 +46,77 @@ public class TestSequence extends BaseFu
@Test
public void testConstructors() throws Exception {
- Sequence seq1 = new Sequence((Procedure)null);
- Sequence seq2 = new Sequence();
+ Sequence<Object> seq1 = new Sequence<Object>((Procedure<? super Object>)null);
+ Sequence<Object> seq2 = new Sequence<Object>();
assertObjectsAreEqual(seq1, seq2);
RunCounter p1 = new RunCounter();
RunCounter p2 = new RunCounter();
- List<Procedure> iterable = new ArrayList<Procedure>();
+ List<Procedure<? super Object>> iterable = new ArrayList<Procedure<? super Object>>();
iterable.add(p1);
iterable.add(p2);
- Sequence seq3 = new Sequence(iterable);
- Sequence seq4 = new Sequence(p1, p2);
+ Sequence<Object> seq3 = new Sequence<Object>(iterable);
+ Sequence<Object> seq4 = new Sequence<Object>(p1, p2);
assertObjectsAreEqual(seq3, seq4);
- Sequence seq5 = new Sequence((Iterable<Procedure>)null);
- Sequence seq6 = new Sequence((Procedure[])null);
+ Sequence<Object> seq5 = new Sequence<Object>((Iterable<Procedure<? super Object>>)null);
+ Sequence<Object> seq6 = new Sequence<Object>((Procedure<? super Object>[])null);
assertObjectsAreEqual(seq5, seq6);
}
-
+
@Test
public void testRunZero() throws Exception {
- Sequence seq = new Sequence();
- seq.run();
+ Sequence<String> seq = new Sequence<String>();
+ seq.run(null);
+ seq.run("xyzzy");
}
@Test
public void testRunOne() throws Exception {
RunCounter counter = new RunCounter();
- Sequence seq = new Sequence(counter);
+ Sequence<String> seq = new Sequence<String>(counter);
assertEquals(0,counter.count);
- seq.run();
+ seq.run(null);
assertEquals(1,counter.count);
+ seq.run("xyzzy");
+ assertEquals(2,counter.count);
}
@Test
public void testRunTwo() throws Exception {
RunCounter[] counter = { new RunCounter(), new RunCounter() };
- Sequence seq = new Sequence(counter[0],counter[1]);
+ Sequence<String> seq = new Sequence<String>(counter[0],counter[1]);
assertEquals(0,counter[0].count);
assertEquals(0,counter[1].count);
- seq.run();
+ seq.run(null);
assertEquals(1,counter[0].count);
assertEquals(1,counter[1].count);
+ seq.run("xyzzy");
+ assertEquals(2,counter[0].count);
+ assertEquals(2,counter[1].count);
}
@Test
public void testThen() throws Exception {
List<RunCounter> list = new ArrayList<RunCounter>();
- Sequence seq = new Sequence();
- seq.run();
+ Sequence<String> seq = new Sequence<String>();
+ seq.run(null);
for (int i=0;i<10;i++) {
RunCounter counter = new RunCounter();
seq.then(counter);
list.add(counter);
- seq.run();
+ seq.run("xyzzy");
for (int j=0;j<list.size();j++) {
- assertEquals(list.size()-j,(((RunCounter)(list.get(j))).count));
+ assertEquals(list.size()-j,((list.get(j)).count));
}
}
}
@Test
public void testEquals() throws Exception {
- Sequence p = new Sequence();
+ Sequence<?> p = new Sequence<Object>();
assertEquals(p,p);
- Sequence q = new Sequence();
+ Sequence<?> q = new Sequence<Object>();
assertObjectsAreEqual(p,q);
for (int i=0;i<3;i++) {
@@ -117,21 +124,21 @@ public class TestSequence extends BaseFu
assertObjectsAreNotEqual(p,q);
q.then(new NoOp());
assertObjectsAreEqual(p,q);
- p.then(new Sequence(new NoOp(),new NoOp()));
+ p.then(new Sequence<Object>(new NoOp(),new NoOp()));
assertObjectsAreNotEqual(p,q);
- q.then(new Sequence(new NoOp(),new NoOp()));
+ q.then(new Sequence<Object>(new NoOp(),new NoOp()));
assertObjectsAreEqual(p,q);
}
assertObjectsAreNotEqual(p,new NoOp());
- assertTrue(!p.equals(null));
+ assertFalse(p.equals(null));
}
// Classes
// ------------------------------------------------------------------------
- static class RunCounter implements Procedure {
- public void run() {
+ static class RunCounter implements Procedure<Object> {
+ public void run(Object that) {
count++;
}
public int count = 0;
Modified: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestTransformedBinaryFunction.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestTransformedBinaryFunction.java?rev=1508677&r1=1508676&r2=1508677&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestTransformedBinaryFunction.java (original)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestTransformedBinaryFunction.java Tue Jul 30 22:48:02 2013
@@ -23,7 +23,7 @@ import java.io.Serializable;
import org.apache.commons.functor.BaseFunctorTest;
import org.apache.commons.functor.BinaryFunction;
-import org.apache.commons.functor.UnaryFunction;
+import org.apache.commons.functor.Function;
import org.junit.Test;
@@ -48,7 +48,7 @@ public class TestTransformedBinaryFuncti
}
}
- private static class AddOne implements UnaryFunction<Integer, Integer>, Serializable {
+ private static class AddOne implements Function<Integer, Integer>, Serializable {
private static final long serialVersionUID = 8759620198239402369L;
public Integer evaluate(Integer obj) {
return obj + 1;
@@ -82,7 +82,7 @@ public class TestTransformedBinaryFuncti
return left-right;
}
};
- UnaryFunction<Integer, Integer> p = new UnaryFunction<Integer, Integer>() {
+ Function<Integer, Integer> p = new Function<Integer, Integer>() {
public Integer evaluate(Integer obj) {
return obj-1;
}
Modified: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestTransformedBinaryProcedure.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestTransformedBinaryProcedure.java?rev=1508677&r1=1508676&r2=1508677&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestTransformedBinaryProcedure.java (original)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestTransformedBinaryProcedure.java Tue Jul 30 22:48:02 2013
@@ -23,7 +23,7 @@ import java.io.Serializable;
import org.apache.commons.functor.BaseFunctorTest;
import org.apache.commons.functor.BinaryFunction;
-import org.apache.commons.functor.UnaryProcedure;
+import org.apache.commons.functor.Procedure;
import org.junit.Test;
@@ -48,7 +48,7 @@ public class TestTransformedBinaryProced
}
}
- private static class Total implements UnaryProcedure<Integer>, Serializable {
+ private static class Total implements Procedure<Integer>, Serializable {
private static final long serialVersionUID = 8532164308852418241L;
private int total = 0;
public void run(Integer obj) {
@@ -88,7 +88,7 @@ public class TestTransformedBinaryProced
return left-right;
}
};
- UnaryProcedure<Integer> p = new UnaryProcedure<Integer>() {
+ Procedure<Integer> p = new Procedure<Integer>() {
public void run(Integer obj) {
// Do nothing
}
Added: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestTransformedNullaryFunction.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestTransformedNullaryFunction.java?rev=1508677&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestTransformedNullaryFunction.java (added)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestTransformedNullaryFunction.java Tue Jul 30 22:48:02 2013
@@ -0,0 +1,99 @@
+/*
+ * 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.commons.functor.core.composite;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.Serializable;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.NullaryFunction;
+import org.apache.commons.functor.Function;
+import org.junit.Test;
+
+/**
+ * Tests for TransformedNullaryFunction.
+ * @version $Revision: $ $Date: $
+ */
+public class TestTransformedNullaryFunction extends BaseFunctorTest {
+
+ private static class One implements NullaryFunction<Integer>, Serializable {
+ private static final long serialVersionUID = 8160546546365936087L;
+ public Integer evaluate() {
+ return new Integer(1);
+ }
+ @Override
+ public boolean equals(Object obj) {
+ return obj == this || obj != null && obj instanceof One;
+ }
+ @Override
+ public int hashCode() {
+ return "One".hashCode();
+ }
+ };
+
+ private static class AddOne implements Function<Integer, Integer>, Serializable {
+ private static final long serialVersionUID = 2005155787293129721L;
+ public Integer evaluate(Integer obj) {
+ return obj + 1;
+ }
+ @Override
+ public boolean equals(Object obj) {
+ return obj == this || obj != null && obj instanceof AddOne;
+ }
+ @Override
+ public int hashCode() {
+ return "AddOne".hashCode();
+ }
+ };
+
+ private One one = new One();
+ private AddOne addOne = new AddOne();
+
+ @Override
+ protected Object makeFunctor() throws Exception {
+ return new TransformedNullaryFunction<Integer>(one, addOne);
+ }
+
+ @Test
+ public void testRun() {
+ TransformedNullaryFunction<Integer> p = new TransformedNullaryFunction<Integer>(one, addOne);
+ assertEquals(Integer.valueOf(2),p.evaluate());
+ }
+
+ @Test
+ public void testEquals() {
+ TransformedNullaryFunction<Integer> t = new TransformedNullaryFunction<Integer>(one, addOne);
+ NullaryFunction<Integer> f = new NullaryFunction<Integer>() {
+ public Integer evaluate() {
+ return new Integer(2);
+ }
+ };
+ Function<Integer, Integer> p = new Function<Integer, Integer>() {
+ public Integer evaluate(Integer obj) {
+ return obj + 2;
+ }
+ };
+ assertEquals(t,t);
+ assertObjectsAreEqual(t,new TransformedNullaryFunction<Integer>(one, addOne));
+ assertObjectsAreNotEqual(t,new TransformedNullaryFunction<Integer>(f, addOne));
+ assertObjectsAreNotEqual(t,new TransformedNullaryFunction<Integer>(one, p));
+ assertTrue(!t.equals(null));
+ }
+
+}
Added: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestTransformedNullaryProcedure.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestTransformedNullaryProcedure.java?rev=1508677&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestTransformedNullaryProcedure.java (added)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestTransformedNullaryProcedure.java Tue Jul 30 22:48:02 2013
@@ -0,0 +1,103 @@
+/*
+ * 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.commons.functor.core.composite;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.Serializable;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.NullaryFunction;
+import org.apache.commons.functor.Procedure;
+import org.junit.Test;
+
+/**
+ * Tests for TransformedNullaryProcedure.
+ * @version $Revision: $ $Date: $
+ */
+public class TestTransformedNullaryProcedure extends BaseFunctorTest{
+
+ private static class One implements NullaryFunction<Integer>, Serializable {
+ private static final long serialVersionUID = 7385852113529459456L;
+ public Integer evaluate() {
+ return new Integer(1);
+ }
+ @Override
+ public boolean equals(Object obj) {
+ return obj == this || obj != null && obj instanceof One;
+ }
+ @Override
+ public int hashCode() {
+ return "One".hashCode();
+ }
+ };
+
+ private static class AggregatorProcedure implements Procedure<Integer>, Serializable {
+ private static final long serialVersionUID = -2744193737701268327L;
+ private int total = 0;
+ public void run(Integer obj) {
+ total += obj;
+ }
+ public int getTotal() {
+ return total;
+ }
+ @Override
+ public boolean equals(Object obj) {
+ return obj == this || obj != null && obj instanceof AggregatorProcedure;
+ }
+ @Override
+ public int hashCode() {
+ return "AggregatorProcedure".hashCode();
+ }
+ };
+
+ private One one = new One();
+ private AggregatorProcedure aggregator = new AggregatorProcedure();
+
+ @Override
+ protected Object makeFunctor() throws Exception {
+ return new TransformedNullaryProcedure(one, aggregator);
+ }
+
+ @Test
+ public void testRun() {
+ TransformedNullaryProcedure p = new TransformedNullaryProcedure(one, aggregator);
+ p.run();
+ assertEquals(1,aggregator.getTotal());
+ }
+
+ @Test
+ public void testEquals() {
+ TransformedNullaryProcedure t = new TransformedNullaryProcedure(one, aggregator);
+ NullaryFunction<Integer> f = new NullaryFunction<Integer>() {
+ public Integer evaluate() {
+ return new Integer(2);
+ }
+ };
+ Procedure<Integer> p = new Procedure<Integer>() {
+ public void run(Integer obj) {
+ // Do nothing
+ }
+ };
+ assertEquals(t,t);
+ assertObjectsAreEqual(t,new TransformedNullaryProcedure(one, aggregator));
+ assertObjectsAreNotEqual(t,new TransformedNullaryProcedure(f, aggregator));
+ assertObjectsAreNotEqual(t,new TransformedNullaryProcedure(one, p));
+ assertTrue(!t.equals(null));
+ }
+}
Added: commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestWhileDoNullaryProcedure.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestWhileDoNullaryProcedure.java?rev=1508677&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestWhileDoNullaryProcedure.java (added)
+++ commons/proper/functor/trunk/core/src/test/java/org/apache/commons/functor/core/composite/TestWhileDoNullaryProcedure.java Tue Jul 30 22:48:02 2013
@@ -0,0 +1,115 @@
+/*
+ * 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.commons.functor.core.composite;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.NullaryPredicate;
+import org.apache.commons.functor.NullaryProcedure;
+import org.apache.commons.functor.adapter.BoundNullaryPredicate;
+import org.apache.commons.functor.core.Constant;
+import org.apache.commons.functor.core.NoOp;
+import org.apache.commons.functor.core.collection.IsEmpty;
+import org.junit.Test;
+
+/**
+ * @version $Revision: 1345136 $ $Date: 2012-06-01 09:47:06 -0300 (Fri, 01 Jun 2012) $
+ */
+public class TestWhileDoNullaryProcedure extends BaseFunctorTest {
+
+ // Functor Testing Framework
+ // ------------------------------------------------------------------------
+
+ @Override
+ protected Object makeFunctor() {
+ return new WhileDoNullaryProcedure(Constant.FALSE, NoOp.INSTANCE);
+ }
+
+ // Tests
+ // ------------------------------------------------------------------------
+ public class ListRemoveFirstProcedure implements NullaryProcedure {
+ protected List<Object> list;
+
+
+ public ListRemoveFirstProcedure(List<Object> list) {
+ this.list=list;
+ }
+
+
+ public void run() {
+ list.remove(0);
+ }
+ }
+
+
+ private List<Object> getList() {
+ List<Object> list=new LinkedList<Object>();
+ list.add("a");
+ list.add("b");
+ list.add("c");
+ list.add("d");
+ return list;
+ }
+
+
+ @Test
+ public void testLoopWithAction() throws Exception {
+ List<Object> list=getList();
+
+ NullaryProcedure action=new ListRemoveFirstProcedure(list);
+ NullaryPredicate condition=new NullaryNot(new BoundNullaryPredicate(new IsEmpty<List<Object>>(), list));
+ NullaryProcedure procedure=new WhileDoNullaryProcedure(condition, action);
+
+ assertTrue("The condition should be true before running the loop", condition.test());
+ assertFalse("The list should not be empty then", list.isEmpty());
+ procedure.run();
+ assertFalse("The condition should be false after running the loop", condition.test());
+ assertTrue("The list should be empty then", list.isEmpty());
+
+ list=getList();
+ action=new ListRemoveFirstProcedure(list);
+ condition=new NullaryPredicate() {
+ private int count=2;
+
+ public boolean test() {
+ return count-- > 0;
+ }
+ };
+ procedure=new WhileDoNullaryProcedure(condition, action);
+ procedure.run();
+ assertFalse("The list should not contain \"a\" anymore", list.contains("a"));
+ assertFalse("The list should not contain \"b\" anymore", list.contains("b"));
+ assertTrue("The list should still contain \"c\"", list.contains("c"));
+ assertTrue("The list should still contain \"d\"", list.contains("d"));
+ }
+
+ @Test
+ public void testLoopForNothing() {
+ List<Object> list=getList();
+ NullaryProcedure action=new ListRemoveFirstProcedure(list);
+ NullaryProcedure procedure=new WhileDoNullaryProcedure(Constant.FALSE, action);
+ assertTrue("The list should contain 4 elements before runnning the loop", list.size()==4);
+ procedure.run();
+ assertTrue("The list should contain 4 elements after runnning the loop", list.size()==4);
+ }
+}
+