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.example.lines;
018
019 import java.io.Reader;
020 import java.io.StringReader;
021 import java.util.Collection;
022
023 import junit.framework.Test;
024 import junit.framework.TestCase;
025 import junit.framework.TestSuite;
026
027 import org.apache.commons.functor.adapter.ProcedureUnaryProcedure;
028 import org.apache.commons.functor.core.Offset;
029 import org.apache.commons.functor.core.algorithm.FoldLeft;
030 import org.apache.commons.functor.core.collection.Size;
031 import org.apache.commons.functor.core.composite.UnaryAnd;
032 import org.apache.commons.functor.core.composite.UnaryNot;
033 import org.apache.commons.functor.generator.FilteredGenerator;
034 import org.apache.commons.functor.generator.TransformedGenerator;
035
036 /**
037 * @version $Revision: 665786 $ $Date: 2008-06-09 19:17:39 +0200 (Mon, 09 Jun 2008) $
038 * @author Rodney Waldhoff
039 */
040 public class TestLines extends TestCase {
041
042 public TestLines(String testName) {
043 super(testName);
044 }
045
046 public static Test suite() {
047 return new TestSuite(TestLines.class);
048 }
049
050 private Reader reader = null;
051
052 public void setUp() throws Exception {
053 super.setUp();
054 reader = new StringReader(DOCUMENT);
055 }
056
057 public void tearDown() throws Exception {
058 super.tearDown();
059 reader = null;
060 }
061
062 public void testCountCharacters() throws Exception {
063 Object result = new FoldLeft<Integer>(Sum.instance()).evaluate(
064 new TransformedGenerator<String, Integer>(Lines.from(reader), new Size<String>()));
065
066 assertEquals("Expected 990 characters",new Integer(990),result);
067 }
068
069 public void testCountWords() throws Exception {
070 Object result = new FoldLeft<Integer>(Sum.instance()).evaluate(
071 new TransformedGenerator<String, Integer>(Lines.from(reader),WordCount.instance()));
072
073 assertEquals("Expected 157 words",new Integer(157),result);
074 }
075
076 public void testCountLines() throws Exception {
077 Count count = new Count();
078 Lines
079 .from(reader)
080 .run(ProcedureUnaryProcedure.adapt(count));
081
082 assertEquals("Expected 16 lines",16,count.getCount());
083 }
084
085 public void testCountWordsExcludingComments() throws Exception {
086 Object result = new FoldLeft<Integer>(Sum.instance()).evaluate(new TransformedGenerator<String, Integer>(
087 new FilteredGenerator<String>(Lines.from(reader), UnaryNot.not(new StartsWith<String>("#"))), WordCount
088 .instance()));
089
090 assertEquals("Expected 90 words",new Integer(90),result);
091 }
092
093 public void testCountCommentLines() throws Exception {
094 Count count = new Count();
095 new FilteredGenerator<String>(Lines.from(reader), new StartsWith<String>("#"))
096 .run(ProcedureUnaryProcedure.<String>adapt(count));
097
098 assertEquals("Expected 6 lines",6,count.getCount());
099 }
100
101 public void testFindMatchingLines() throws Exception {
102 Collection<String> matches = new FilteredGenerator<String>(Lines.from(reader), new Contains<String>("lo"))
103 .toCollection();
104 assertEquals("Expected 5 lines",5,matches.size());
105 }
106
107 public void testFindMatchingFromTail() throws Exception {
108 Collection<String> matches = new FilteredGenerator<String>(Lines.from(reader), new UnaryAnd<String>(new Offset(
109 8), new Contains<String>("lo"))).toCollection();
110 assertEquals("Expected 2 lines",2,matches.size());
111 }
112
113
114 private static final String DOCUMENT =
115 "# Lorem ipsum dolor sit amet, consectetuer adipiscing elit. \n" +
116 "# Aliquam erat volutpat. Donec nec eros. Etiam eget tortor eu \n" +
117 "tortor rutrum cursus. Pellentesque ornare pretium risus. Nulla \n" +
118 "libero pede, blandit nec, rutrum ut, sodales eu, enim. Duis leo. \n" +
119 "Nunc non est. Nunc consequat lobortis nisl. Vivamus et tortor in \n" +
120 "# nunc euismod elementum. Ut ut dui. Morbi semper, pede eu cursus \n" +
121 "# tristique, diam nunc luctus nibh, id tempor justo metus eget lorem.\n" +
122 "\n" +
123 "Quisque pharetra hendrerit odio. Etiam consequat ante et dui. Etiam \n" +
124 "accumsan elit ac augue. Mauris porta pulvinar tellus. Nulla ac enim ac \n"+
125 "augue ornare pharetra. Nunc dignissim eros et nibh. Sed justo dolor, \n" +
126 "# ullamcorper non, posuere eget, tempus non, ipsum. Praesent at velit. \n" +
127 "# Mauris tempor nisl sed tortor. Vestibulum ante ipsum primis in faucibus \n" +
128 "orci luctus et ultrices posuere cubilia Curae; Integer mollis malesuada \n" +
129 "dolor. Vestibulum cursus, mi in dictum blandit, eros enim convallis wisi, \n" +
130 "id ullamcorper metus tortor interdum dui.\n";
131 }