This project has retired. For details please refer to its Attic page.
LoremIpsumTest xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.util.content.loremipsum;
20  
21  import static org.junit.Assert.assertArrayEquals;
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertTrue;
25  import static org.junit.Assert.fail;
26  
27  import java.io.FileOutputStream;
28  import java.io.IOException;
29  import java.io.InputStream;
30  import java.io.InputStreamReader;
31  import java.io.OutputStreamWriter;
32  import java.io.Reader;
33  import java.io.StringWriter;
34  import java.io.Writer;
35  
36  import org.apache.chemistry.opencmis.util.content.loremipsum.LoremIpsum;
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  import org.junit.After;
40  import org.junit.Before;
41  import org.junit.Test;
42  
43  public class LoremIpsumTest {
44  
45      private static final Log LOG = LogFactory.getLog(LoremIpsumTest.class);
46  
47      String sample = "One two three four five six. Seven eight nine ten eleven twelve. "
48  		+ "\n\n"
49  		+ "Thirteen fourteen fifteen sixteen. Seventeen eighteen nineteen twenty.";
50         
51      String dictionary = "a bb ccc dddd eeeee ffffff ggggggg hhhhhhhh iiiiiiiii jjjjjjjjjj kkkkkkkkkkk llllllllllll";
52      LoremIpsum generator = new LoremIpsum(sample, dictionary);
53      
54  	@Before
55  	public void setUp() throws Exception {
56  	    dictionary.split(" ");
57  	}
58  
59  	@After
60  	public void tearDown() throws Exception {
61  	}
62  
63  	@Test
64  	public void  test_mean() {
65  		int[] ia1 = {1, 2, 3, 4};
66          assertEquals(2.5d, LoremIpsum.mean(ia1), 0.01d);
67          int[] ia2 = {6, 6, 4, 4};
68          assertEquals(5.0d, LoremIpsum.mean(ia2), 0.01d);
69  	}
70  	
71      @Test
72  	public void  test_mean_empty() {
73  		int[] ia1 = {};
74          assertEquals(0.0d, LoremIpsum.mean(ia1), 0.01d);
75      }
76      
77      @Test
78  	public void  test_variance() {
79  		double[] ia1 = {6.0d, 6.0d, 4.0d, 4.0d};
80          assertEquals(1.0d, LoremIpsum.variance(ia1), 0.01d);
81          double[] ia2 = {1.0d, 2.0d, 3.0d, 4.0d};
82          assertEquals(1.25d, LoremIpsum.variance(ia2), 0.01d);
83      }
84      
85      @Test
86  	public void  test_sigma() {
87  		double[] ia1 = {6.0d, 6.0d, 4.0d, 4.0d};
88          double[] ia2 = {1.0d, 2.0d, 3.0d, 4.0d};
89          assertEquals(1.0d, LoremIpsum.sigma(ia1), 0.01d);
90          assertEquals(Math.sqrt(1.25), LoremIpsum.sigma(ia2), 0.01d);
91      }
92      
93      @Test
94  	public void  test_sigma_empty() {
95  		int[] ia1 = {};
96          assertEquals(0.0d, LoremIpsum.sigma(ia1), 0.01d);
97      }
98      
99      @Test
100 	public void test_split_sentences() {
101     	String[] sentences1 = {"Hello", "Hi"};
102     	assertArrayEquals (sentences1, LoremIpsum.splitSentences("Hello. Hi."));
103         String[] sentences2 = {"One two three four five six", 
104                                  "Seven eight nine ten eleven twelve", 
105                                  "Thirteen fourteen fifteen sixteen", 
106                                  "Seventeen eighteen nineteen twenty"}; 
107         assertArrayEquals(sentences2, LoremIpsum.splitSentences(sample));
108     }
109     
110     @Test
111 	public void test_split_sentences_empty() {
112     	String[] sentences = {};
113     	assertArrayEquals(sentences, LoremIpsum.splitSentences(""));
114     }
115     
116     @Test
117 	public void test_split_sentences_trailing() {
118     	String[] sentences1 = {"Hello", "Hi", "Hello"};    	
119     	assertArrayEquals(sentences1, LoremIpsum.splitSentences("Hello. Hi. Hello"));
120     	String[] sentences2 = {"Hello", "Hi", "Hello"};
121     	assertArrayEquals(sentences2, LoremIpsum.splitSentences("  Hello. Hi. Hello  "));
122         String[] sentences3 = {"Hello", "Hi", "Hello"};
123         assertArrayEquals(sentences3, LoremIpsum.splitSentences("..  Hello... Hi.... Hello  ")); 
124     }
125 
126     @Test
127 	public void test_split_paragraphs() {
128     	String[] paragraphs = {"One two three four five six. Seven eight nine ten eleven twelve.",
129     			"Thirteen fourteen fifteen sixteen. Seventeen eighteen nineteen twenty."};
130     	assertArrayEquals(paragraphs, LoremIpsum.splitParagraphs(sample));
131     }
132     
133     @Test
134 	public void test_split_paragraphs_empty() {
135     	String[] paragraphs = {};
136     	assertArrayEquals(paragraphs, LoremIpsum.splitParagraphs(""));
137     }
138     
139     @Test
140 	public void test_split_paragraphs_trailing() {
141     	String[] paragraphs = {"Hello", "Hi"};
142     	assertArrayEquals(paragraphs, LoremIpsum.splitParagraphs("Hello\n\nHi"));
143     	assertArrayEquals(paragraphs, LoremIpsum.splitParagraphs("Hello\n\nHi\n"));
144     	assertArrayEquals(paragraphs, LoremIpsum.splitParagraphs("Hello\n\nHi\n\n"));
145     	assertArrayEquals(paragraphs, LoremIpsum.splitParagraphs("Hello\n\nHi\n\n\n"));
146     	assertArrayEquals(paragraphs, LoremIpsum.splitParagraphs("Hello\n\nHi\n\n\n\n\n\n"));
147     	assertArrayEquals(paragraphs, LoremIpsum.splitParagraphs("\nHello\n\nHi"));
148     	assertArrayEquals(paragraphs, LoremIpsum.splitParagraphs("\n\nHello\n\nHi"));
149     	assertArrayEquals(paragraphs, LoremIpsum.splitParagraphs("\n\n\nHello\n\nHi"));
150     	assertArrayEquals(paragraphs, LoremIpsum.splitParagraphs("\n\n\n\n\n\nHello\n\nHi"));
151     }
152     
153     @Test
154 	public void test_split_words() {
155     	String[] words = {"One", "two", "three", "four"};
156     	assertArrayEquals(words, LoremIpsum.splitWords("One two three four"));    	
157     	assertArrayEquals(words, LoremIpsum.splitWords("  One    two  three  four   ")); 
158     }
159                 
160     @Test
161 	public void test_split_words_empty() {
162     	String[] words = {};
163     	assertArrayEquals(words, LoremIpsum.splitWords(""));
164 	}
165     
166     @Test
167 	public void test_choose_closest() {
168     	Integer[] intArray1 ={1,2,3,4};
169         assertEquals(1, LoremIpsum.chooseClosest(intArray1, 1));
170         Integer[] intArray2 ={1,2,3,4};
171         assertEquals(4, LoremIpsum.chooseClosest(intArray2, 4));
172         assertEquals(4, LoremIpsum.chooseClosest(intArray2, 20));
173         assertEquals(1, LoremIpsum.chooseClosest(intArray2, -10));
174         Integer[] intArray3 ={1,4};
175         assertEquals(1, LoremIpsum.chooseClosest(intArray3, 2));
176         assertEquals(4, LoremIpsum.chooseClosest(intArray3, 3));
177         Integer[] intArray4 ={1,3};
178         assertEquals(1, LoremIpsum.chooseClosest(intArray4, 2));
179         Integer[] intArray5 ={3,1};
180         assertEquals(3, LoremIpsum.chooseClosest(intArray5, 2));
181         Integer[] intArray6 ={1};
182         assertEquals(1, LoremIpsum.chooseClosest(intArray6, 200));
183     }
184 
185     @Test
186 	public void test_sentence_mean() {
187         assertEquals(5.0d, generator.getSentenceMean(), 0.01d);
188 	}
189    
190     @Test
191 	public void test_paragraph_mean() {
192     	assertEquals(2.0d, generator.getParagraphMean(), 0.01d);
193     }
194         
195     @Test
196 	public void test_sentence_sigma() {
197         assertEquals(1.0d, generator.getSentenceSigma(), 0.01d);
198     }
199         
200     @Test
201 	public void test_paragraph_sigma() {
202         assertEquals(0.0d, generator.getParagraphSigma(), 0.01d);
203     }
204         
205     @Test
206 	public void test_sample() {
207         assertEquals(generator.getSample(), sample);
208     }
209 
210     @Test
211 	public void test_dictionary() {
212         assertEquals(generator.getDictionary(), dictionary);
213     }
214 
215     @Test
216 	public void test_set_dictionary() {
217         String newdict = "a b c";
218         generator.setDictionary(newdict);
219         assertEquals(generator.getDictionary(), newdict);
220 	}
221     
222     @Test 
223     public void test_init_no_sample() {
224     	doGenerate("");
225     	doGenerate(" ");
226     	doGenerate("\n\n");
227     	doGenerate("  \n\n  ");
228     	doGenerate(" .\n\n .");
229     }
230     
231     private void doGenerate(String text) {
232     	try {
233     		generator = new LoremIpsum(text, dictionary);
234     		generator.generateParagraph(false);
235     		fail("Sample text " + text + " should generate exception.");
236     	} catch (RuntimeException e) {
237     		assertTrue(e.getMessage().contains("Invalid sample text"));
238     	}
239     }
240     
241     @Test 
242     public void test_init_no_dict() {
243     	doGenerateNoDict("");
244     	doGenerateNoDict(" ");
245     	doGenerateNoDict("\n\n");
246     	doGenerateNoDict("  \n\n  ");
247     }
248     
249     private void doGenerateNoDict(String dict) {
250     	try {
251     		generator = new LoremIpsum(sample, dict);
252     		generator.generateParagraph(false);
253     		fail("Dictionary " + dict + " should generate exception.");
254     	} catch (RuntimeException e) {
255     		assertEquals(e.getMessage(), "Invalid dictionary.");
256     	}
257     }
258 
259     @Test 
260     public void testGenerate() {
261     	LOG.debug("Generate new text: ");
262     	String newDict = "me you he the One two three four five six Seven eight nine ten eleven twelve "
263        		+ "Thirteen fourteen fifteen sixteen Seventeen eighteen nineteen twenty joe fred some";
264     	String[] newParagraphs = new String[4];
265     	generator.setDictionary(newDict);
266     	for (int i=0; i<newParagraphs.length; i++) {
267     		newParagraphs[i] = generator.generateParagraph(false);
268     		LOG.debug(newParagraphs[i]);
269     		LOG.debug("");
270     	}
271     	assertFalse(newParagraphs[0].equals(newParagraphs[1]));
272     	assertFalse(newParagraphs[0].equals(newParagraphs[2]));
273     	assertFalse(newParagraphs[0].equals(newParagraphs[3]));
274     	assertFalse(newParagraphs[1].equals(newParagraphs[2]));
275     	assertFalse(newParagraphs[1].equals(newParagraphs[3]));
276     	assertFalse(newParagraphs[2].equals(newParagraphs[3]));
277     }
278     
279     @Test 
280     public void testGenerateLoreIpsum() {
281     	LOG.debug("Generate new Lore Ipsum text: ");
282     	LoremIpsum ipsum = new LoremIpsum();
283     	String[] newParagraphs = new String[4];
284     	for (int i=0; i<newParagraphs.length; i++) {
285     		newParagraphs[i] = ipsum.generateParagraph(false);
286     		LOG.debug(newParagraphs[i]);
287     		LOG.debug("");
288     		LOG.debug("");
289     	}
290     }
291     
292     @Test 
293     public void testGenerateLoreIpsumHtml1() {
294     	LOG.debug("Generate new Lore Ipsum as html paragraphs:");
295     	LoremIpsum ipsum = new LoremIpsum();
296     	String output = ipsum.generateParagraphsHtml(2048, true);
297     	LOG.debug(output);
298     	LOG.debug("");
299     }
300     
301     @Test 
302     public void testGenerateLoreIpsumHtml2() {
303     	LOG.debug("Generate new Lore Ipsum as one html paragraph:");
304     	LoremIpsum ipsum = new LoremIpsum();
305     	String output = ipsum.generateOneParagraphHtml(2048, true);
306     	LOG.debug(output);
307     	LOG.debug("");
308     }
309     
310     @Test 
311     public void testGenerateLoreIpsumHtml3() {
312         LOG.debug("Generate new Lore Ipsum as full html document: ");
313     	LoremIpsum ipsum = new LoremIpsum();
314     	String output = ipsum.generateParagraphsFullHtml(2048, true);
315     	LOG.debug(output);
316     	LOG.debug("");
317     }
318     
319     @Test 
320     public void testGenerateLoreIpsumPlainText() {
321     	LOG.debug("Generate new Lore Ipsum as plain text: ");
322     	LoremIpsum ipsum = new LoremIpsum();
323     	String output = ipsum.generateParagraphsPlainText(2048, true);
324     	LOG.debug(output);
325     	LOG.debug("");
326     }
327     
328     @Test 
329     public void testGenerateLoreIpsumPlainTextFormatted() {
330     	LOG.debug("Generate new Lore Ipsum as plain text with 60 columns: ");
331     	LoremIpsum ipsum = new LoremIpsum();
332     	String output = ipsum.generateParagraphsPlainText(256, 60, false);
333     	LOG.debug(output);
334     	LOG.debug("");
335     }
336         
337     @Test 
338     public void testGenerateLoreIpsumHtml1Writer() throws IOException {
339         LOG.debug("Generate new Lore Ipsum as html paragraphs with PrintWriter:");
340         LoremIpsum ipsum = new LoremIpsum();
341         StringWriter writer = new StringWriter();
342         ipsum.generateParagraphsHtml(writer, 2048, true);
343         LOG.debug(writer.toString());
344         LOG.debug("End Test.");
345     }
346     
347     @Test 
348     public void testGenerateLoreIpsumHtml2Writer() throws IOException  {
349         LOG.debug("Generate new Lore Ipsum as full html paragraph with PrintWriter:");
350         LoremIpsum ipsum = new LoremIpsum();
351         StringWriter writer = new StringWriter();
352         ipsum.generateParagraphsFullHtml(writer, 2048, true);
353         LOG.debug(writer.toString());
354         LOG.debug("End Test.");
355     }
356     
357     @Test 
358     public void testGenerateLoreIpsumPlainTextWriter() throws IOException  {
359         LOG.debug("Generate new Lore Ipsum as plain text with PrintWriter: ");
360         LoremIpsum ipsum = new LoremIpsum();
361         StringWriter writer = new StringWriter();
362         ipsum.generateParagraphsPlainText(writer, 2048, true);
363         LOG.debug(writer.toString());
364         LOG.debug("End Test.");
365     }
366     
367     @Test 
368     public void testGenerateLoreIpsumPlainTextFormattedWriter() throws IOException {
369         LOG.debug("Generate new Lore Ipsum as plain text with 60 columns with PrintWriter: ");
370         LoremIpsum ipsum = new LoremIpsum();
371         StringWriter writer = new StringWriter();
372         ipsum.generateParagraphsPlainText(writer, 256, 60, false);
373         LOG.debug(writer.toString());
374         LOG.debug("End Test.");
375     }
376     
377     @Test 
378     public void testGenerateLoreIpsumGerman() throws Exception {
379     	LOG.debug("Generate new Lore Ipsum Ferry Tale: ");
380     	InputStream is = this.getClass().getResourceAsStream("/HaenselUndGretel.txt");
381     	
382     	// read stream into a string
383     	final char[] buffer = new char[0x10000];
384     	StringBuilder sample = new StringBuilder();
385     	Reader in = new InputStreamReader(is, "ISO-8859-1");
386     	int read;
387     	do {
388     	  read = in.read(buffer, 0, buffer.length);
389     	  if (read>0) {
390     	    sample.append(buffer, 0, read);
391     	  }
392     	} while (read>=0);
393 
394     	
395     	LoremIpsum ipsum = new LoremIpsum(sample.toString());
396     	String output = ipsum.generateParagraphsPlainText(4096, 80, false);
397     	LOG.debug(output);
398         LOG.debug("End Test.");
399     }
400     
401 }