1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.functor.example.lines;
18  
19  import java.io.Reader;
20  import java.io.StringReader;
21  import java.util.Collection;
22  
23  import junit.framework.Test;
24  import junit.framework.TestCase;
25  import junit.framework.TestSuite;
26  
27  import org.apache.commons.functor.adapter.ProcedureUnaryProcedure;
28  import org.apache.commons.functor.core.Offset;
29  import org.apache.commons.functor.core.algorithm.FoldLeft;
30  import org.apache.commons.functor.core.collection.Size;
31  import org.apache.commons.functor.core.composite.UnaryAnd;
32  import org.apache.commons.functor.core.composite.UnaryNot;
33  import org.apache.commons.functor.generator.FilteredGenerator;
34  import org.apache.commons.functor.generator.TransformedGenerator;
35  
36  /**
37   * @version $Revision: 665786 $ $Date: 2008-06-09 19:17:39 +0200 (Mon, 09 Jun 2008) $
38   * @author Rodney Waldhoff
39   */
40  public class TestLines extends TestCase {
41  
42      public TestLines(String testName) {
43          super(testName);
44      }
45  
46      public static Test suite() {
47          return new TestSuite(TestLines.class);
48      }
49  
50      private Reader reader = null;
51  
52      public void setUp() throws Exception {
53          super.setUp();
54          reader = new StringReader(DOCUMENT);
55      }
56  
57      public void tearDown() throws Exception {
58          super.tearDown();
59          reader = null;
60      }
61  
62      public void testCountCharacters() throws Exception {
63          Object result = new FoldLeft<Integer>(Sum.instance()).evaluate(
64                  new TransformedGenerator<String, Integer>(Lines.from(reader), new Size<String>()));
65  
66          assertEquals("Expected 990 characters",new Integer(990),result);
67      }
68  
69      public void testCountWords() throws Exception {
70          Object result = new FoldLeft<Integer>(Sum.instance()).evaluate(
71                  new TransformedGenerator<String, Integer>(Lines.from(reader),WordCount.instance()));
72  
73          assertEquals("Expected 157 words",new Integer(157),result);
74      }
75  
76      public void testCountLines() throws Exception {
77          Count count = new Count();
78          Lines
79              .from(reader)
80                  .run(ProcedureUnaryProcedure.adapt(count));
81  
82          assertEquals("Expected 16 lines",16,count.getCount());
83      }
84  
85      public void testCountWordsExcludingComments() throws Exception {
86          Object result = new FoldLeft<Integer>(Sum.instance()).evaluate(new TransformedGenerator<String, Integer>(
87                  new FilteredGenerator<String>(Lines.from(reader), UnaryNot.not(new StartsWith<String>("#"))), WordCount
88                          .instance()));
89  
90          assertEquals("Expected 90 words",new Integer(90),result);
91      }
92  
93      public void testCountCommentLines() throws Exception {
94          Count count = new Count();
95          new FilteredGenerator<String>(Lines.from(reader), new StartsWith<String>("#"))
96                      .run(ProcedureUnaryProcedure.<String>adapt(count));
97  
98          assertEquals("Expected 6 lines",6,count.getCount());
99      }
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 }