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    
018    package org.apache.commons.pipeline.testFramework;
019    
020    import org.apache.commons.logging.Log;
021    import org.apache.commons.logging.LogFactory;
022    import org.apache.commons.pipeline.*;
023    import org.apache.commons.pipeline.validation.ConsumedTypes;
024    import org.apache.commons.pipeline.validation.ProducesConsumed;
025    
026    /**
027     * This stage will generate {@link StageException}s for every other object this
028     * stage processes. By design, the even numbered objects will cause a <CODE>StageException</CODE>
029     * to be thrown (counting the first object as 1).
030     */
031    @ConsumedTypes(Object.class)
032    @ProducesConsumed
033    public class FaultingTestStage extends TestStage {
034        private Log log = LogFactory.getLog(FaultingTestStage.class);
035        private int counter = 0;
036        
037        public FaultingTestStage(int index) {
038            super(index);
039        }
040        
041        public void process(Object obj) throws StageException {
042            if (++counter % 2 == 0) {
043                log.error("Planned fault in stage " + this + ".");
044                throw new StageException(this, "Planned fault in stage " + super.getIndex() + ".");
045            }
046            
047            super.process(obj);
048        }
049    }