001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.functor.core.composite; 018 019 import static org.junit.Assert.assertFalse; 020 import static org.junit.Assert.assertTrue; 021 022 import java.util.LinkedList; 023 import java.util.List; 024 025 import org.apache.commons.functor.BaseFunctorTest; 026 import org.apache.commons.functor.Predicate; 027 import org.apache.commons.functor.Procedure; 028 import org.apache.commons.functor.adapter.BoundPredicate; 029 import org.apache.commons.functor.core.Constant; 030 import org.apache.commons.functor.core.NoOp; 031 import org.apache.commons.functor.core.collection.IsEmpty; 032 import org.junit.Test; 033 034 /** 035 * @version $Revision: 1171255 $ $Date: 2011-09-15 22:27:39 +0200 (Thu, 15 Sep 2011) $ 036 * @author Herve Quiroz 037 */ 038 public class TestDoWhileProcedure extends BaseFunctorTest { 039 040 // Functor Testing Framework 041 // ------------------------------------------------------------------------ 042 043 protected Object makeFunctor() { 044 return new DoWhileProcedure(NoOp.INSTANCE, Constant.FALSE); 045 } 046 047 // Tests 048 // ------------------------------------------------------------------------ 049 public class ListRemoveFirstProcedure implements Procedure { 050 protected List<Object> list; 051 052 053 public ListRemoveFirstProcedure(List<Object> list) { 054 this.list=list; 055 } 056 057 058 public void run() { 059 list.remove(0); 060 } 061 } 062 063 064 private List<Object> getList() { 065 List<Object> list = new LinkedList<Object>(); 066 list.add("a"); 067 list.add("b"); 068 list.add("c"); 069 list.add("d"); 070 return list; 071 } 072 073 074 @Test 075 public void testLoopWithAction() throws Exception { 076 List<Object> list=getList(); 077 078 Procedure action=new ListRemoveFirstProcedure(list); 079 Predicate condition=new Not(new BoundPredicate(new IsEmpty<List<Object>>(), list)); 080 Procedure procedure=new DoWhileProcedure(action, condition); 081 082 assertTrue("The condition should be true before running the loop", condition.test()); 083 assertFalse("The list should not be empty then", list.isEmpty()); 084 procedure.run(); 085 assertFalse("The condition should be false after running the loop", condition.test()); 086 assertTrue("The list should be empty then", list.isEmpty()); 087 088 list=getList(); 089 action=new ListRemoveFirstProcedure(list); 090 condition=new Predicate() { 091 private int count=2; 092 093 public boolean test() { 094 return count-- > 0; 095 } 096 }; 097 procedure=new DoWhileProcedure(action, condition); 098 procedure.run(); 099 assertFalse("The list should not contain \"a\" anymore", list.contains("a")); 100 assertFalse("The list should not contain \"b\" anymore", list.contains("b")); 101 assertFalse("The list should not contain \"c\" anymore", list.contains("c")); 102 assertTrue("The list should still contain \"d\"", list.contains("d")); 103 } 104 105 @Test 106 public void testLoopForNothing() { 107 List<Object> list=getList(); 108 Procedure action=new ListRemoveFirstProcedure(list); 109 Procedure procedure=new DoWhileProcedure(action, Constant.FALSE); 110 assertTrue("The list should contain 4 elements before runnning the loop", list.size()==4); 111 procedure.run(); 112 assertTrue("The list should contain 3 elements after runnning the loop", list.size()==3); 113 } 114 }