chemistry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1241563 [2/2] - in /chemistry/opencmis/trunk/chemistry-opencmis-test/chemistry-opencmis-test-util/src: main/java/org/apache/chemistry/opencmis/util/content/ main/java/org/apache/chemistry/opencmis/util/content/fractal/ main/java/org/apache...
Date Tue, 07 Feb 2012 19:26:18 GMT
Modified: chemistry/opencmis/trunk/chemistry-opencmis-test/chemistry-opencmis-test-util/src/main/java/org/apache/chemistry/opencmis/util/repository/MultiThreadedObjectGenerator.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-test/chemistry-opencmis-test-util/src/main/java/org/apache/chemistry/opencmis/util/repository/MultiThreadedObjectGenerator.java?rev=1241563&r1=1241562&r2=1241563&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-test/chemistry-opencmis-test-util/src/main/java/org/apache/chemistry/opencmis/util/repository/MultiThreadedObjectGenerator.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-test/chemistry-opencmis-test-util/src/main/java/org/apache/chemistry/opencmis/util/repository/MultiThreadedObjectGenerator.java
Tue Feb  7 19:26:17 2012
@@ -25,6 +25,7 @@ import org.apache.chemistry.opencmis.com
 import org.apache.chemistry.opencmis.commons.spi.NavigationService;
 import org.apache.chemistry.opencmis.commons.spi.ObjectService;
 import org.apache.chemistry.opencmis.commons.spi.RepositoryService;
+import org.apache.chemistry.opencmis.util.repository.ObjectGenerator.CONTENT_KIND;
 
 public class MultiThreadedObjectGenerator {
 
@@ -86,14 +87,14 @@ public class MultiThreadedObjectGenerato
 
     private static ObjectGenerator createObjectGenerator(CmisBinding binding, String repoId,
int docsPerFolder,
             int foldersPerFolders, int depth, String documentType, String folderType, int
contentSizeInKB,
-            String rootFolderId, boolean doCleanup) {
+            String rootFolderId, CONTENT_KIND contentKind, boolean doCleanup) {
 
         BindingsObjectFactory objectFactory = binding.getObjectFactory();
         NavigationService navSvc = binding.getNavigationService();
         ObjectService objSvc = binding.getObjectService();
         RepositoryService repSvc = binding.getRepositoryService();
         
-        ObjectGenerator gen = new ObjectGenerator(objectFactory, navSvc, objSvc, repSvc,
repoId);
+        ObjectGenerator gen = new ObjectGenerator(objectFactory, navSvc, objSvc, repSvc,
repoId, contentKind);
         gen.setUseUuidsForNames(true);
         gen.setNumberOfDocumentsToCreatePerFolder(docsPerFolder);
         // Set the type id for all created documents:
@@ -120,10 +121,10 @@ public class MultiThreadedObjectGenerato
 
     public static ObjectGeneratorRunner prepareForCreateTree(CmisBinding binding, String
repoId, int docsPerFolder,
             int foldersPerFolders, int depth, String documentType, String folderType, int
contentSizeInKB,
-            String rootFolderId, boolean doCleanup) {
+            String rootFolderId, CONTENT_KIND contentKind, boolean doCleanup) {
 
         ObjectGenerator objGen = createObjectGenerator(binding, repoId, docsPerFolder, foldersPerFolders,
depth,
-                documentType, folderType, contentSizeInKB, rootFolderId, doCleanup);
+                documentType, folderType, contentSizeInKB, rootFolderId, contentKind, doCleanup);
 
         ObjectGeneratorRunner gen = new ObjectGeneratorRunner(objGen, Action.CreateTree);
         gen.fFoldersPerFolders = foldersPerFolders;
@@ -134,12 +135,13 @@ public class MultiThreadedObjectGenerato
 
     public static ObjectGeneratorRunner[] prepareForCreateTreeMT(CmisBinding provider, String
repoId,
             int docsPerFolder, int foldersPerFolders, int depth, String documentType, String
folderType,
-            int contentSizeInKB, String[] rootFolderIds, boolean doCleanup) {
+            int contentSizeInKB, String[] rootFolderIds, CONTENT_KIND contentKind, boolean
doCleanup) {
 
         ObjectGeneratorRunner[] runners = new ObjectGeneratorRunner[rootFolderIds.length];
         for (int i = 0; i < rootFolderIds.length; i++) {
             ObjectGenerator objGen = createObjectGenerator(provider, repoId, docsPerFolder,
foldersPerFolders, depth,
-                    documentType, folderType, contentSizeInKB, rootFolderIds[i], doCleanup);
+                    documentType, folderType, contentSizeInKB, rootFolderIds[i], contentKind,
+                    doCleanup);
 
             ObjectGeneratorRunner gen = new ObjectGeneratorRunner(objGen, Action.CreateTree);
             gen.fFoldersPerFolders = foldersPerFolders;
@@ -151,10 +153,11 @@ public class MultiThreadedObjectGenerato
     }
 
     public static ObjectGeneratorRunner prepareForCreateDocument(CmisBinding provider, String
repoId,
-            String documentType, int contentSizeInKB, String rootFolderId, int noDocuments,
boolean doCleanup) {
+            String documentType, int contentSizeInKB, String rootFolderId, int noDocuments,

+            CONTENT_KIND contentKind, boolean doCleanup) {
 
         ObjectGenerator objGen = createObjectGenerator(provider, repoId, 0, 0, 0, documentType,
null, contentSizeInKB,
-                rootFolderId, doCleanup);
+                rootFolderId, contentKind, doCleanup);
 
         ObjectGeneratorRunner gen = new ObjectGeneratorRunner(objGen, Action.CreateDocument);
         gen.fRootFolderId = getRootFolderId(provider, repoId, rootFolderId);
@@ -164,12 +167,12 @@ public class MultiThreadedObjectGenerato
 
     public static ObjectGeneratorRunner[] prepareForCreateDocumentMT(int threadCount, CmisBinding
binding,
             String repoId, String documentType, int contentSizeInKB, String rootFolderId,
int noDocuments,
-            boolean doCleanup) {
+            CONTENT_KIND contentKind, boolean doCleanup) {
 
         ObjectGeneratorRunner[] runners = new ObjectGeneratorRunner[threadCount];
         for (int i = 0; i < threadCount; i++) {
             ObjectGenerator objGen = createObjectGenerator(binding, repoId, 0, 0, 0, documentType,
null,
-                    contentSizeInKB, rootFolderId, doCleanup);
+                    contentSizeInKB, rootFolderId, contentKind, doCleanup);
 
             ObjectGeneratorRunner gen = new ObjectGeneratorRunner(objGen, Action.CreateDocument);
             gen.fRootFolderId = getRootFolderId(binding, repoId, rootFolderId);
@@ -183,7 +186,7 @@ public class MultiThreadedObjectGenerato
             String rootFolderId, int noFolders, boolean doCleanup) {
 
         ObjectGenerator objGen = createObjectGenerator(provider, repoId, 0, 0, 0, null, folderType,
0, rootFolderId,
-                doCleanup);
+                null, doCleanup);
 
         ObjectGeneratorRunner gen = new ObjectGeneratorRunner(objGen, Action.CreateFolders);
         gen.fRootFolderId = getRootFolderId(provider, repoId, rootFolderId);
@@ -197,7 +200,7 @@ public class MultiThreadedObjectGenerato
         ObjectGeneratorRunner[] runners = new ObjectGeneratorRunner[threadCount];
         for (int i = 0; i < threadCount; i++) {
             ObjectGenerator objGen = createObjectGenerator(binding, repoId, 0, 0, 0, null,
folderType, 0, rootFolderId,
-                    doCleanup);
+                    null, doCleanup);
 
             ObjectGeneratorRunner gen = new ObjectGeneratorRunner(objGen, Action.CreateFolders);
             gen.fRootFolderId = getRootFolderId(binding, repoId, rootFolderId);
@@ -210,7 +213,7 @@ public class MultiThreadedObjectGenerato
     public static ObjectGeneratorRunner prepareForCreateTypes(CmisBinding provider, String
repoId, TypeDefinitionList typeDefList) {
 
         ObjectGenerator objGen = createObjectGenerator(provider, repoId, 0, 0, 0, null, null,
0, null,
-                false);
+                null, false);
 
         ObjectGeneratorRunner gen = new ObjectGeneratorRunner(objGen, Action.CreateTypes);
         gen.fTypeDefList = typeDefList;

Modified: chemistry/opencmis/trunk/chemistry-opencmis-test/chemistry-opencmis-test-util/src/main/java/org/apache/chemistry/opencmis/util/repository/ObjGenApp.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-test/chemistry-opencmis-test-util/src/main/java/org/apache/chemistry/opencmis/util/repository/ObjGenApp.java?rev=1241563&r1=1241562&r2=1241563&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-test/chemistry-opencmis-test-util/src/main/java/org/apache/chemistry/opencmis/util/repository/ObjGenApp.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-test/chemistry-opencmis-test-util/src/main/java/org/apache/chemistry/opencmis/util/repository/ObjGenApp.java
Tue Feb  7 19:26:17 2012
@@ -19,7 +19,6 @@
 package org.apache.chemistry.opencmis.util.repository;
 
 import java.io.BufferedReader;
-import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -28,9 +27,6 @@ import java.net.URL;
 import java.util.HashMap;
 import java.util.Map;
 
-import javax.xml.bind.JAXBElement;
-import javax.xml.bind.Unmarshaller;
-
 import joptsimple.OptionParser;
 import joptsimple.OptionSet;
 import joptsimple.OptionSpec;
@@ -38,16 +34,11 @@ import joptsimple.OptionSpec;
 import org.apache.chemistry.opencmis.client.bindings.CmisBindingFactory;
 import org.apache.chemistry.opencmis.commons.SessionParameter;
 import org.apache.chemistry.opencmis.commons.data.RepositoryInfo;
-import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
-import org.apache.chemistry.opencmis.commons.definitions.TypeDefinitionList;
 import org.apache.chemistry.opencmis.commons.enums.BaseTypeId;
 import org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException;
-import org.apache.chemistry.opencmis.commons.impl.Converter;
-import org.apache.chemistry.opencmis.commons.impl.JaxBHelper;
-import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisTypeDefinitionListType;
-import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisTypeDefinitionType;
 import org.apache.chemistry.opencmis.commons.spi.CmisBinding;
 import org.apache.chemistry.opencmis.commons.spi.RepositoryService;
+import org.apache.chemistry.opencmis.util.repository.ObjectGenerator.CONTENT_KIND;
 
 public class ObjGenApp {
 
@@ -72,6 +63,7 @@ public class ObjGenApp {
     private static final String CLEANUP = "Cleanup";
     private static final String ROOTFOLDER = "RootFolder";
     private static final String THREADS = "Threads";
+    private static final String CONTENT_KIND = "ContentKind";
 //    private static final String FILE = "File";
 
     private static final String BINDING_ATOM = "AtomPub";
@@ -80,6 +72,7 @@ public class ObjGenApp {
     private CmisBinding binding;
     private boolean fUsingAtom;
     private String fUrlStr;
+    private CONTENT_KIND fContentKind;
 
     OptionSpec<String> fCmd;
     OptionSpec<Integer> fDepth;
@@ -95,6 +88,7 @@ public class ObjGenApp {
     OptionSpec<String> fRootFolder;
     OptionSpec<Integer> fThreads;
     OptionSpec<String> fFileName;
+    OptionSpec<String> fContentKindStr;
     
     public static void main(String[] args) {
 
@@ -139,7 +133,9 @@ public class ObjGenApp {
         fThreads = parser.accepts(THREADS).withOptionalArg().ofType(Integer.class).defaultsTo(1).describedAs(
                 "Number of threads to start in parallel");
 //        fFileName = parser.accepts(FILE).withRequiredArg().ofType(String.class).describedAs("Input
File");
-
+        fContentKindStr = parser.accepts(CONTENT_KIND).withOptionalArg().ofType(String.class).defaultsTo("lorem/text")
+                .describedAs("kind of content: static/text, lorem/text, lorem/html, fractal/jpeg");
+        
         OptionSet options = parser.parse(args);
 
         if (options.valueOf(fCmd) == null || options.has("?")) {
@@ -156,6 +152,26 @@ public class ObjGenApp {
             return;
         }
 
+        String kind = options.valueOf(fContentKindStr);
+        if (null == kind) {
+            if (options.valueOf(fContentSize) > 0)
+                fContentKind = ObjectGenerator.CONTENT_KIND.StaticText;
+            else
+                fContentKind = null;
+        } if (kind.equals("static/text"))
+            fContentKind = ObjectGenerator.CONTENT_KIND.StaticText;
+        else if (kind.equals("lorem/text"))
+            fContentKind = ObjectGenerator.CONTENT_KIND.LoremIpsumText;
+        else if (kind.equals("lorem/html"))
+            fContentKind = ObjectGenerator.CONTENT_KIND.LoremIpsumHtml;
+        else if (kind.equals("fractal/jpeg"))
+            fContentKind = ObjectGenerator.CONTENT_KIND.ImageFractalJpeg;
+        else {
+            System.out.println("Unknown content kind: " + options.valueOf(fContentKindStr));
+            System.out.println("  must be one of static/text, lorem/text, lorem/html, fractal/jpeg");
+            usage(parser);
+        }
+
         if (options.valueOf(fCmd).equals("FillRepository")) {
             fillRepository(options);
         } else if (options.valueOf(fCmd).equals("CreateDocument")) {
@@ -204,7 +220,7 @@ public class ObjGenApp {
                     + PROP_ATOMPUB_URL
                     + "=http://localhost:8080/opencmis/atom -cp ... "
                     + "org.apache.chemistry.opencmis.util.repository.ObjGenApp --Binding=AtomPub
--Command=CreateDocument "
-                    + "--RepositoryId=A1 --ContentSizeInKB=25");
+                    + "--RepositoryId=A1 --ContentSizeInKB=25 --ContentKind=lorem/text");
             return;
         } catch (IOException e) {
             e.printStackTrace();
@@ -216,7 +232,7 @@ public class ObjGenApp {
 
         MultiThreadedObjectGenerator.ObjectGeneratorRunner runner = MultiThreadedObjectGenerator.prepareForCreateTree(
                 getBinding(), repoId, docsPerFolder, foldersPerFolders, depth, documentType,
folderType,
-                contentSizeInKB, rootFolderId, doCleanup);
+                contentSizeInKB, rootFolderId, fContentKind, doCleanup);
         ObjectGenerator gen = runner.getObjectGenerator();
         runner.doCreateTree();
 
@@ -240,7 +256,7 @@ public class ObjGenApp {
         // Step 2: fill each root folder with an object tree
         MultiThreadedObjectGenerator.ObjectGeneratorRunner[] runners = MultiThreadedObjectGenerator
                 .prepareForCreateTreeMT(getBinding(), repoId, docsPerFolder, foldersPerFolders,
depth, documentType,
-                        folderType, contentSizeInKB, folderIds, doCleanup);
+                        folderType, contentSizeInKB, folderIds, fContentKind, doCleanup);
 
         MultiThreadedObjectGenerator.runMultiThreaded(runners);
         System.out.println("Filling repository succeeded.");
@@ -260,6 +276,7 @@ public class ObjGenApp {
         System.out.println("Delete all objects after creation: " + options.valueOf(fCleanup));
         System.out.println("Number of actions to perform: " + options.valueOf(fCount));
         System.out.println("Number of threads to start: " + options.valueOf(fThreads));
+        System.out.println("Kind of created content: " + options.valueOf(fContentKindStr));
     }
 
     private void createSingleDocument(OptionSet options) {
@@ -321,7 +338,7 @@ public class ObjGenApp {
 
         MultiThreadedObjectGenerator.ObjectGeneratorRunner runner = MultiThreadedObjectGenerator
                 .prepareForCreateDocument(getBinding(), repoId, documentType, contentSizeInKB,
rootFolderId, docCount,
-                        doCleanup);
+                        fContentKind, doCleanup);
         ObjectGenerator gen = runner.getObjectGenerator();
         String[] ids = runner.doCreateDocument();
         System.out.println();
@@ -345,7 +362,7 @@ public class ObjGenApp {
 
         MultiThreadedObjectGenerator.ObjectGeneratorRunner[] runners = MultiThreadedObjectGenerator
                 .prepareForCreateDocumentMT(noThreads, getBinding(), repoId, documentType,
contentSizeInKB,
-                        rootFolderId, docCount, doCleanup);
+                        rootFolderId, docCount, fContentKind, doCleanup);
 
         MultiThreadedObjectGenerator.runMultiThreaded(runners);
         System.out.println("Document creation succeeded. All threads terminated.");

Modified: chemistry/opencmis/trunk/chemistry-opencmis-test/chemistry-opencmis-test-util/src/main/java/org/apache/chemistry/opencmis/util/repository/ObjectGenerator.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-test/chemistry-opencmis-test-util/src/main/java/org/apache/chemistry/opencmis/util/repository/ObjectGenerator.java?rev=1241563&r1=1241562&r2=1241563&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-test/chemistry-opencmis-test-util/src/main/java/org/apache/chemistry/opencmis/util/repository/ObjectGenerator.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-test/chemistry-opencmis-test-util/src/main/java/org/apache/chemistry/opencmis/util/repository/ObjectGenerator.java
Tue Feb  7 19:26:17 2012
@@ -49,7 +49,8 @@ import org.apache.chemistry.opencmis.com
 import org.apache.chemistry.opencmis.commons.spi.NavigationService;
 import org.apache.chemistry.opencmis.commons.spi.ObjectService;
 import org.apache.chemistry.opencmis.commons.spi.RepositoryService;
-import org.apache.chemistry.opencmis.util.content.LoreIpsum;
+import org.apache.chemistry.opencmis.util.content.fractal.FractalGenerator;
+import org.apache.chemistry.opencmis.util.content.loremipsum.LoremIpsum;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -77,6 +78,12 @@ public class ObjectGenerator {
     List<String> fTopLevelFoldersCreated; // list of ids created on first level
 
     /**
+     * supported kinds of content
+     *
+     */
+    public enum CONTENT_KIND {StaticText, LoremIpsumText, LoremIpsumHtml, ImageFractalJpeg};
+
+    /**
      * Indicates if / how many documents are created in each folder
      */
     private int fNoDocumentsToCreate;
@@ -121,9 +128,10 @@ public class ObjectGenerator {
     private int fContentSizeInK = 0;
     
     /**
-     * true: use Lore Ipsum generator, false: use static text
+     * Kind of content to create
      */
-    private boolean useLoreIpsum = true;
+    private CONTENT_KIND fContentKind;
+    
 
     private static final String NAMEPROPVALPREFIXDOC = "My_Document-";
     private static final String NAMEPROPVALPREFIXFOLDER = "My_Folder-";
@@ -135,9 +143,14 @@ public class ObjectGenerator {
      * use UUIDs to generate folder and document names
      */
     private boolean fUseUuids;
+    
+    /**
+     * generator for images
+     */
+    private FractalGenerator fractalGenerator;
 
     public ObjectGenerator(BindingsObjectFactory factory, NavigationService navSvc, ObjectService
objSvc,
-            RepositoryService repSvc, String repositoryId) {
+            RepositoryService repSvc, String repositoryId, CONTENT_KIND contentKind) {
         super();
         fFactory = factory;
         fNavSvc = navSvc;
@@ -157,6 +170,8 @@ public class ObjectGenerator {
         fCleanup = false;
         fTopLevelDocsCreated = new ArrayList<String>();
         fTopLevelFoldersCreated = new ArrayList<String>();
+        fContentKind = contentKind;
+        fractalGenerator = new FractalGenerator();
     }
 
     public void setNumberOfDocumentsToCreatePerFolder(int noDocumentsToCreate) {
@@ -183,12 +198,12 @@ public class ObjectGenerator {
         fContentSizeInK = sizeInK;
     }
     
-    public boolean getLoreIpsumGenerator() {
-        return useLoreIpsum;
+    public CONTENT_KIND getContentKind() {
+        return fContentKind;
     }
     
-    public void setLoreIpsumGenerator(boolean use) {
-        useLoreIpsum = use;
+    public void setLoreIpsumGenerator(CONTENT_KIND contentKind) {
+        fContentKind = contentKind;
     }
 
     public void setCleanUpAfterCreate(boolean doCleanup) {
@@ -436,9 +451,24 @@ public class ObjectGenerator {
         // log.info("create document in folder " + folderId);
         Properties props = createDocumentProperties(no, level);
         String id = null;
+        
         if (fContentSizeInK > 0) {
-            contentStream = useLoreIpsum ? createContent() : createContentStaticText();
+            switch (fContentKind) {
+            case StaticText:
+                contentStream = createContentStaticText();
+                break;
+            case LoremIpsumText:
+                contentStream = createContentLoremIpsumText();
+                break;
+            case LoremIpsumHtml:
+                contentStream = createContentLoremIpsumHtml();
+                break;
+            case ImageFractalJpeg:
+                contentStream = createContentFractalimageJpeg();
+                break;
+            }
         }
+
         try {
             fTimeLoggerCreateDoc.start();
             id = fObjSvc.createDocument(fRepositoryId, props, folderId, contentStream, versioningState,
policies,
@@ -481,13 +511,25 @@ public class ObjectGenerator {
         }
     }
 
-    private ContentStream createContent() {
+    private ContentStream createContentLoremIpsumHtml() {
+        ContentStreamImpl content = new ContentStreamImpl();
+        content.setFileName("data.html");
+        content.setMimeType("text/html");
+        int len = fContentSizeInK * 1024; // size of document in K
+        
+        LoremIpsum ipsum = new LoremIpsum();
+        String text = ipsum.generateParagraphsFullHtml(len, true);
+        content.setStream(new ByteArrayInputStream(text.getBytes()));
+        return content;
+    }
+
+    private ContentStream createContentLoremIpsumText() {
         ContentStreamImpl content = new ContentStreamImpl();
         content.setFileName("data.txt");
         content.setMimeType("text/plain");
         int len = fContentSizeInK * 1024; // size of document in K
         
-        LoreIpsum ipsum = new LoreIpsum();
+        LoremIpsum ipsum = new LoremIpsum();
         String text = ipsum.generateParagraphsPlainText(len, 80, true);
         content.setStream(new ByteArrayInputStream(text.getBytes()));
         return content;
@@ -516,6 +558,24 @@ public class ObjectGenerator {
         return content;
     }
 
+    private ContentStream createContentFractalimageJpeg() {
+        ContentStreamImpl content = null;
+
+        try {
+            ByteArrayOutputStream bos = fractalGenerator.generateFractal();
+            content = new ContentStreamImpl();
+            content.setFileName("image.jpg");
+            content.setMimeType("image/jpeg");
+            content.setStream(new ByteArrayInputStream(bos.toByteArray()));
+            bos.close();            
+        } catch (IOException e) {
+            System.err.println("Error when generating fractal image: " + e);
+            e.printStackTrace();
+        }
+
+        return content;
+    }
+
     private Properties createFolderProperties(int no, int level) {
         List<PropertyData<?>> properties = new ArrayList<PropertyData<?>>();
         properties.add(fFactory.createPropertyStringData(PropertyIds.NAME, generateFolderNameValue(no,
level)));

Added: chemistry/opencmis/trunk/chemistry-opencmis-test/chemistry-opencmis-test-util/src/test/java/org/apache/chemistry/opencmis/util/content/loremipsum/LoremIpsumTest.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-test/chemistry-opencmis-test-util/src/test/java/org/apache/chemistry/opencmis/util/content/loremipsum/LoremIpsumTest.java?rev=1241563&view=auto
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-test/chemistry-opencmis-test-util/src/test/java/org/apache/chemistry/opencmis/util/content/loremipsum/LoremIpsumTest.java
(added)
+++ chemistry/opencmis/trunk/chemistry-opencmis-test/chemistry-opencmis-test-util/src/test/java/org/apache/chemistry/opencmis/util/content/loremipsum/LoremIpsumTest.java
Tue Feb  7 19:26:17 2012
@@ -0,0 +1,401 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.chemistry.opencmis.util.content.loremipsum;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.io.StringWriter;
+import java.io.Writer;
+
+import org.apache.chemistry.opencmis.util.content.loremipsum.LoremIpsum;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class LoremIpsumTest {
+
+    private static final Log LOG = LogFactory.getLog(LoremIpsumTest.class);
+
+    String sample = "One two three four five six. Seven eight nine ten eleven twelve. "
+		+ "\n\n"
+		+ "Thirteen fourteen fifteen sixteen. Seventeen eighteen nineteen twenty.";
+       
+    String dictionary = "a bb ccc dddd eeeee ffffff ggggggg hhhhhhhh iiiiiiiii jjjjjjjjjj
kkkkkkkkkkk llllllllllll";
+    LoremIpsum generator = new LoremIpsum(sample, dictionary);
+    
+	@Before
+	public void setUp() throws Exception {
+	    dictionary.split(" ");
+	}
+
+	@After
+	public void tearDown() throws Exception {
+	}
+
+	@Test
+	public void  test_mean() {
+		int[] ia1 = {1, 2, 3, 4};
+        assertEquals(2.5d, LoremIpsum.mean(ia1), 0.01d);
+        int[] ia2 = {6, 6, 4, 4};
+        assertEquals(5.0d, LoremIpsum.mean(ia2), 0.01d);
+	}
+	
+    @Test
+	public void  test_mean_empty() {
+		int[] ia1 = {};
+        assertEquals(0.0d, LoremIpsum.mean(ia1), 0.01d);
+    }
+    
+    @Test
+	public void  test_variance() {
+		double[] ia1 = {6.0d, 6.0d, 4.0d, 4.0d};
+        assertEquals(1.0d, LoremIpsum.variance(ia1), 0.01d);
+        double[] ia2 = {1.0d, 2.0d, 3.0d, 4.0d};
+        assertEquals(1.25d, LoremIpsum.variance(ia2), 0.01d);
+    }
+    
+    @Test
+	public void  test_sigma() {
+		double[] ia1 = {6.0d, 6.0d, 4.0d, 4.0d};
+        double[] ia2 = {1.0d, 2.0d, 3.0d, 4.0d};
+        assertEquals(1.0d, LoremIpsum.sigma(ia1), 0.01d);
+        assertEquals(Math.sqrt(1.25), LoremIpsum.sigma(ia2), 0.01d);
+    }
+    
+    @Test
+	public void  test_sigma_empty() {
+		int[] ia1 = {};
+        assertEquals(0.0d, LoremIpsum.sigma(ia1), 0.01d);
+    }
+    
+    @Test
+	public void test_split_sentences() {
+    	String[] sentences1 = {"Hello", "Hi"};
+    	assertArrayEquals (sentences1, LoremIpsum.splitSentences("Hello. Hi."));
+        String[] sentences2 = {"One two three four five six", 
+                                 "Seven eight nine ten eleven twelve", 
+                                 "Thirteen fourteen fifteen sixteen", 
+                                 "Seventeen eighteen nineteen twenty"}; 
+        assertArrayEquals(sentences2, LoremIpsum.splitSentences(sample));
+    }
+    
+    @Test
+	public void test_split_sentences_empty() {
+    	String[] sentences = {};
+    	assertArrayEquals(sentences, LoremIpsum.splitSentences(""));
+    }
+    
+    @Test
+	public void test_split_sentences_trailing() {
+    	String[] sentences1 = {"Hello", "Hi", "Hello"};    	
+    	assertArrayEquals(sentences1, LoremIpsum.splitSentences("Hello. Hi. Hello"));
+    	String[] sentences2 = {"Hello", "Hi", "Hello"};
+    	assertArrayEquals(sentences2, LoremIpsum.splitSentences("  Hello. Hi. Hello  "));
+        String[] sentences3 = {"Hello", "Hi", "Hello"};
+        assertArrayEquals(sentences3, LoremIpsum.splitSentences("..  Hello... Hi.... Hello
 ")); 
+    }
+
+    @Test
+	public void test_split_paragraphs() {
+    	String[] paragraphs = {"One two three four five six. Seven eight nine ten eleven twelve.",
+    			"Thirteen fourteen fifteen sixteen. Seventeen eighteen nineteen twenty."};
+    	assertArrayEquals(paragraphs, LoremIpsum.splitParagraphs(sample));
+    }
+    
+    @Test
+	public void test_split_paragraphs_empty() {
+    	String[] paragraphs = {};
+    	assertArrayEquals(paragraphs, LoremIpsum.splitParagraphs(""));
+    }
+    
+    @Test
+	public void test_split_paragraphs_trailing() {
+    	String[] paragraphs = {"Hello", "Hi"};
+    	assertArrayEquals(paragraphs, LoremIpsum.splitParagraphs("Hello\n\nHi"));
+    	assertArrayEquals(paragraphs, LoremIpsum.splitParagraphs("Hello\n\nHi\n"));
+    	assertArrayEquals(paragraphs, LoremIpsum.splitParagraphs("Hello\n\nHi\n\n"));
+    	assertArrayEquals(paragraphs, LoremIpsum.splitParagraphs("Hello\n\nHi\n\n\n"));
+    	assertArrayEquals(paragraphs, LoremIpsum.splitParagraphs("Hello\n\nHi\n\n\n\n\n\n"));
+    	assertArrayEquals(paragraphs, LoremIpsum.splitParagraphs("\nHello\n\nHi"));
+    	assertArrayEquals(paragraphs, LoremIpsum.splitParagraphs("\n\nHello\n\nHi"));
+    	assertArrayEquals(paragraphs, LoremIpsum.splitParagraphs("\n\n\nHello\n\nHi"));
+    	assertArrayEquals(paragraphs, LoremIpsum.splitParagraphs("\n\n\n\n\n\nHello\n\nHi"));
+    }
+    
+    @Test
+	public void test_split_words() {
+    	String[] words = {"One", "two", "three", "four"};
+    	assertArrayEquals(words, LoremIpsum.splitWords("One two three four"));    	
+    	assertArrayEquals(words, LoremIpsum.splitWords("  One    two  three  four   ")); 
+    }
+                
+    @Test
+	public void test_split_words_empty() {
+    	String[] words = {};
+    	assertArrayEquals(words, LoremIpsum.splitWords(""));
+	}
+    
+    @Test
+	public void test_choose_closest() {
+    	Integer[] intArray1 ={1,2,3,4};
+        assertEquals(1, LoremIpsum.chooseClosest(intArray1, 1));
+        Integer[] intArray2 ={1,2,3,4};
+        assertEquals(4, LoremIpsum.chooseClosest(intArray2, 4));
+        assertEquals(4, LoremIpsum.chooseClosest(intArray2, 20));
+        assertEquals(1, LoremIpsum.chooseClosest(intArray2, -10));
+        Integer[] intArray3 ={1,4};
+        assertEquals(1, LoremIpsum.chooseClosest(intArray3, 2));
+        assertEquals(4, LoremIpsum.chooseClosest(intArray3, 3));
+        Integer[] intArray4 ={1,3};
+        assertEquals(1, LoremIpsum.chooseClosest(intArray4, 2));
+        Integer[] intArray5 ={3,1};
+        assertEquals(3, LoremIpsum.chooseClosest(intArray5, 2));
+        Integer[] intArray6 ={1};
+        assertEquals(1, LoremIpsum.chooseClosest(intArray6, 200));
+    }
+
+    @Test
+	public void test_sentence_mean() {
+        assertEquals(5.0d, generator.getSentenceMean(), 0.01d);
+	}
+   
+    @Test
+	public void test_paragraph_mean() {
+    	assertEquals(2.0d, generator.getParagraphMean(), 0.01d);
+    }
+        
+    @Test
+	public void test_sentence_sigma() {
+        assertEquals(1.0d, generator.getSentenceSigma(), 0.01d);
+    }
+        
+    @Test
+	public void test_paragraph_sigma() {
+        assertEquals(0.0d, generator.getParagraphSigma(), 0.01d);
+    }
+        
+    @Test
+	public void test_sample() {
+        assertEquals(generator.getSample(), sample);
+    }
+
+    @Test
+	public void test_dictionary() {
+        assertEquals(generator.getDictionary(), dictionary);
+    }
+
+    @Test
+	public void test_set_dictionary() {
+        String newdict = "a b c";
+        generator.setDictionary(newdict);
+        assertEquals(generator.getDictionary(), newdict);
+	}
+    
+    @Test 
+    public void test_init_no_sample() {
+    	doGenerate("");
+    	doGenerate(" ");
+    	doGenerate("\n\n");
+    	doGenerate("  \n\n  ");
+    	doGenerate(" .\n\n .");
+    }
+    
+    private void doGenerate(String text) {
+    	try {
+    		generator = new LoremIpsum(text, dictionary);
+    		generator.generateParagraph(false);
+    		fail("Sample text " + text + " should generate exception.");
+    	} catch (RuntimeException e) {
+    		assertTrue(e.getMessage().contains("Invalid sample text"));
+    	}
+    }
+    
+    @Test 
+    public void test_init_no_dict() {
+    	doGenerateNoDict("");
+    	doGenerateNoDict(" ");
+    	doGenerateNoDict("\n\n");
+    	doGenerateNoDict("  \n\n  ");
+    }
+    
+    private void doGenerateNoDict(String dict) {
+    	try {
+    		generator = new LoremIpsum(sample, dict);
+    		generator.generateParagraph(false);
+    		fail("Dictionary " + dict + " should generate exception.");
+    	} catch (RuntimeException e) {
+    		assertEquals(e.getMessage(), "Invalid dictionary.");
+    	}
+    }
+
+    @Test 
+    public void testGenerate() {
+    	LOG.debug("Generate new text: ");
+    	String newDict = "me you he the One two three four five six Seven eight nine ten eleven
twelve "
+       		+ "Thirteen fourteen fifteen sixteen Seventeen eighteen nineteen twenty joe fred
some";
+    	String[] newParagraphs = new String[4];
+    	generator.setDictionary(newDict);
+    	for (int i=0; i<newParagraphs.length; i++) {
+    		newParagraphs[i] = generator.generateParagraph(false);
+    		LOG.debug(newParagraphs[i]);
+    		LOG.debug("");
+    	}
+    	assertFalse(newParagraphs[0].equals(newParagraphs[1]));
+    	assertFalse(newParagraphs[0].equals(newParagraphs[2]));
+    	assertFalse(newParagraphs[0].equals(newParagraphs[3]));
+    	assertFalse(newParagraphs[1].equals(newParagraphs[2]));
+    	assertFalse(newParagraphs[1].equals(newParagraphs[3]));
+    	assertFalse(newParagraphs[2].equals(newParagraphs[3]));
+    }
+    
+    @Test 
+    public void testGenerateLoreIpsum() {
+    	LOG.debug("Generate new Lore Ipsum text: ");
+    	LoremIpsum ipsum = new LoremIpsum();
+    	String[] newParagraphs = new String[4];
+    	for (int i=0; i<newParagraphs.length; i++) {
+    		newParagraphs[i] = ipsum.generateParagraph(false);
+    		LOG.debug(newParagraphs[i]);
+    		LOG.debug("");
+    		LOG.debug("");
+    	}
+    }
+    
+    @Test 
+    public void testGenerateLoreIpsumHtml1() {
+    	LOG.debug("Generate new Lore Ipsum as html paragraphs:");
+    	LoremIpsum ipsum = new LoremIpsum();
+    	String output = ipsum.generateParagraphsHtml(2048, true);
+    	LOG.debug(output);
+    	LOG.debug("");
+    }
+    
+    @Test 
+    public void testGenerateLoreIpsumHtml2() {
+    	LOG.debug("Generate new Lore Ipsum as one html paragraph:");
+    	LoremIpsum ipsum = new LoremIpsum();
+    	String output = ipsum.generateOneParagraphHtml(2048, true);
+    	LOG.debug(output);
+    	LOG.debug("");
+    }
+    
+    @Test 
+    public void testGenerateLoreIpsumHtml3() {
+        LOG.debug("Generate new Lore Ipsum as full html document: ");
+    	LoremIpsum ipsum = new LoremIpsum();
+    	String output = ipsum.generateParagraphsFullHtml(2048, true);
+    	LOG.debug(output);
+    	LOG.debug("");
+    }
+    
+    @Test 
+    public void testGenerateLoreIpsumPlainText() {
+    	LOG.debug("Generate new Lore Ipsum as plain text: ");
+    	LoremIpsum ipsum = new LoremIpsum();
+    	String output = ipsum.generateParagraphsPlainText(2048, true);
+    	LOG.debug(output);
+    	LOG.debug("");
+    }
+    
+    @Test 
+    public void testGenerateLoreIpsumPlainTextFormatted() {
+    	LOG.debug("Generate new Lore Ipsum as plain text with 60 columns: ");
+    	LoremIpsum ipsum = new LoremIpsum();
+    	String output = ipsum.generateParagraphsPlainText(256, 60, false);
+    	LOG.debug(output);
+    	LOG.debug("");
+    }
+        
+    @Test 
+    public void testGenerateLoreIpsumHtml1Writer() throws IOException {
+        LOG.debug("Generate new Lore Ipsum as html paragraphs with PrintWriter:");
+        LoremIpsum ipsum = new LoremIpsum();
+        StringWriter writer = new StringWriter();
+        ipsum.generateParagraphsHtml(writer, 2048, true);
+        LOG.debug(writer.toString());
+        LOG.debug("End Test.");
+    }
+    
+    @Test 
+    public void testGenerateLoreIpsumHtml2Writer() throws IOException  {
+        LOG.debug("Generate new Lore Ipsum as full html paragraph with PrintWriter:");
+        LoremIpsum ipsum = new LoremIpsum();
+        StringWriter writer = new StringWriter();
+        ipsum.generateParagraphsFullHtml(writer, 2048, true);
+        LOG.debug(writer.toString());
+        LOG.debug("End Test.");
+    }
+    
+    @Test 
+    public void testGenerateLoreIpsumPlainTextWriter() throws IOException  {
+        LOG.debug("Generate new Lore Ipsum as plain text with PrintWriter: ");
+        LoremIpsum ipsum = new LoremIpsum();
+        StringWriter writer = new StringWriter();
+        ipsum.generateParagraphsPlainText(writer, 2048, true);
+        LOG.debug(writer.toString());
+        LOG.debug("End Test.");
+    }
+    
+    @Test 
+    public void testGenerateLoreIpsumPlainTextFormattedWriter() throws IOException {
+        LOG.debug("Generate new Lore Ipsum as plain text with 60 columns with PrintWriter:
");
+        LoremIpsum ipsum = new LoremIpsum();
+        StringWriter writer = new StringWriter();
+        ipsum.generateParagraphsPlainText(writer, 256, 60, false);
+        LOG.debug(writer.toString());
+        LOG.debug("End Test.");
+    }
+    
+    @Test 
+    public void testGenerateLoreIpsumGerman() throws Exception {
+    	LOG.debug("Generate new Lore Ipsum Ferry Tale: ");
+    	InputStream is = this.getClass().getResourceAsStream("/HaenselUndGretel.txt");
+    	
+    	// read stream into a string
+    	final char[] buffer = new char[0x10000];
+    	StringBuilder sample = new StringBuilder();
+    	Reader in = new InputStreamReader(is, "ISO-8859-1");
+    	int read;
+    	do {
+    	  read = in.read(buffer, 0, buffer.length);
+    	  if (read>0) {
+    	    sample.append(buffer, 0, read);
+    	  }
+    	} while (read>=0);
+
+    	
+    	LoremIpsum ipsum = new LoremIpsum(sample.toString());
+    	String output = ipsum.generateParagraphsPlainText(4096, 80, false);
+    	LOG.debug(output);
+        LOG.debug("End Test.");
+    }
+    
+}



Mime
View raw message