[jboss-svn-commits] JBL Code SVN: r30082 - in labs/jbossrules/trunk/drools-doc/src: test/java/org/drools/doc and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Nov 10 04:56:00 EST 2009


Author: Rikkola
Date: 2009-11-10 04:55:59 -0500 (Tue, 10 Nov 2009)
New Revision: 30082

Added:
   labs/jbossrules/trunk/drools-doc/src/test/java/org/drools/doc/StandaloneDocBuilder.java
Modified:
   labs/jbossrules/trunk/drools-doc/src/main/java/org/drools/doc/DrlRuleData.java
   labs/jbossrules/trunk/drools-doc/src/main/java/org/drools/doc/DroolsDocsBuilder.java
   labs/jbossrules/trunk/drools-doc/src/main/java/org/drools/doc/DroolsDocsComponentFactory.java
   labs/jbossrules/trunk/drools-doc/src/test/java/org/drools/doc/DrlPackageDataTest.java
   labs/jbossrules/trunk/drools-doc/src/test/java/org/drools/doc/DrlRuleDataTest.java
Log:
GUVNOR-96 : Documentation generation for all rules in a package

Modified: labs/jbossrules/trunk/drools-doc/src/main/java/org/drools/doc/DrlRuleData.java
===================================================================
--- labs/jbossrules/trunk/drools-doc/src/main/java/org/drools/doc/DrlRuleData.java	2009-11-10 08:40:51 UTC (rev 30081)
+++ labs/jbossrules/trunk/drools-doc/src/main/java/org/drools/doc/DrlRuleData.java	2009-11-10 09:55:59 UTC (rev 30082)
@@ -2,7 +2,6 @@
 
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -62,19 +61,19 @@
 
         while ( m.find() ) {
 
-            //            System.out.println( "1 " + m.group( 1 ) );
-            //            System.out.println( "2 " + m.group( 2 ) );
-            //            System.out.println( "3 " + m.group( 3 ) );
-            //            System.out.println( "4 " + m.group( 4 ) );
-            //            System.out.println( "5 " + m.group( 5 ) );
-            //            System.out.println( "6 " + m.group( 6 ) );
-            //            System.out.println( "7 " + m.group( 7 ) );
+            // System.out.println( "1 " + m.group( 1 ) );
+            // System.out.println( "2 " + m.group( 2 ) );
+            // System.out.println( "3 " + m.group( 3 ) );
+            // System.out.println( "4 " + m.group( 4 ) );
+            // System.out.println( "5 " + m.group( 5 ) );
+            // System.out.println( "6 " + m.group( 6 ) );
+            // System.out.println( "7 " + m.group( 7 ) );
 
             Comment comment = processComment( m.group( 1 ) );
 
             String ruleName = m.group( 2 );
 
-            ruleName = ruleName.substring( ruleName.indexOf( "rule" ) + "rule".length() ).trim();
+            ruleName = trimRuleName( ruleName );
 
             list.add( new DrlRuleData( ruleName,
                                        m.group( 3 ),
@@ -88,6 +87,21 @@
         return list;
     }
 
+    private static String trimRuleName(String ruleName) {
+        ruleName = ruleName.substring( ruleName.indexOf( "rule" ) + "rule".length() ).trim();
+
+        if ( ruleName.indexOf( "\"" ) == 0 ) {
+            ruleName = ruleName.substring( 1 );
+        }
+
+        if ( ruleName.lastIndexOf( "\"" ) == (ruleName.length() - 1) ) {
+            ruleName = ruleName.substring( 0,
+                                           ruleName.length() - 1 );
+        }
+
+        return ruleName;
+    }
+
     static Comment processComment(String text) {
         Comment comment = new Comment();
 
@@ -98,8 +112,34 @@
             return comment;
         }
 
-        comment.description = text.replaceAll( "#",
-                                               "" ).trim();
+        // Sometimes the first rule in a file gets the package imports and
+        // comments included.
+        // Checking for that and fixing the description if this did happen.
+
+        StringBuilder description = new StringBuilder();
+        String[] commentLines = text.split( "\n" );
+        for ( int i = 0; i < commentLines.length; i++ ) {
+            String line = commentLines[i].trim();
+
+            if ( line.startsWith( "#" ) ) {
+                while ( line.startsWith( "#" ) ) {
+                    line = line.substring( 1 );
+                }
+
+                line = line.trim();
+
+                if ( !line.startsWith( "@" ) ) {
+                    description.append( line );
+                    description.append( "\n" );
+                }
+            } else {
+                description.delete( 0,
+                                    description.length() );
+            }
+
+        }
+        comment.description = description.toString();
+
         comment.metadata = findMetaData( text );
 
         return comment;

Modified: labs/jbossrules/trunk/drools-doc/src/main/java/org/drools/doc/DroolsDocsBuilder.java
===================================================================
--- labs/jbossrules/trunk/drools-doc/src/main/java/org/drools/doc/DroolsDocsBuilder.java	2009-11-10 08:40:51 UTC (rev 30081)
+++ labs/jbossrules/trunk/drools-doc/src/main/java/org/drools/doc/DroolsDocsBuilder.java	2009-11-10 09:55:59 UTC (rev 30082)
@@ -7,6 +7,7 @@
 
 import com.lowagie.text.Document;
 import com.lowagie.text.DocumentException;
+import com.lowagie.text.Font;
 import com.lowagie.text.HeaderFooter;
 import com.lowagie.text.Phrase;
 import com.lowagie.text.pdf.PdfWriter;
@@ -67,13 +68,13 @@
             document.newPage();
 
             // List index of the rules            
-            document.add( new Phrase( "Contents" ) );
+            document.add( new Phrase( "Table of Contents" ) );
             document.add( DroolsDocsComponentFactory.createContents( packageData.rules ) );
 
             document.newPage();
 
             for ( DrlRuleData ruleData : packageData.rules ) {
-                DroolsDocsComponentFactory.createRulePage( document,
+                DroolsDocsComponentFactory.newRulePage( document,
                                                            packageData.packageName,
                                                            ruleData );
             }

Modified: labs/jbossrules/trunk/drools-doc/src/main/java/org/drools/doc/DroolsDocsComponentFactory.java
===================================================================
--- labs/jbossrules/trunk/drools-doc/src/main/java/org/drools/doc/DroolsDocsComponentFactory.java	2009-11-10 08:40:51 UTC (rev 30081)
+++ labs/jbossrules/trunk/drools-doc/src/main/java/org/drools/doc/DroolsDocsComponentFactory.java	2009-11-10 09:55:59 UTC (rev 30082)
@@ -48,25 +48,33 @@
     static final Font           HEADER_FOOTER_TEXT = FontFactory.getFont( FontFactory.TIMES,
                                                                           8 );
 
-    public static Table createDescription(String description) throws DocumentException {
-        Table table = createEmptyTable();
+    public static Table newDescription(String description) throws DocumentException {
+        if ( description == null || "".equals( description ) ) {
+            description = " - ";
+        }
 
-        Cell headerCell = newEmptyHeaderCell( "Description" );
+        Table table = newTable();
+
+        Cell headerCell = newHeaderCell( "Description",
+                                         CATEGORIES_TEXT );
         table.addCell( headerCell );
 
-        table.addCell( newEmptyCell( INDENT + description ) );
+        table.addCell( newCell( description ) );
 
         return table;
     }
 
-    private static Table createEmptyTable() throws BadElementException {
+    private static Table newTable() throws BadElementException {
         Table table = new Table( 1 );
+
         table.setBorderWidthTop( 1 );
         table.setBorderWidthLeft( 1 );
         table.setBorderWidthRight( 1 );
         table.setBorderWidthBottom( 0 );
+        table.setWidth( 100 );
         table.setPadding( 3 );
         table.setAlignment( Table.ALIGN_LEFT );
+
         return table;
     }
 
@@ -74,102 +82,90 @@
                                         Map<String, java.util.List<String>> other) throws DocumentException {
 
         for ( String key : other.keySet() ) {
-            document.add( createTable( key,
-                                       other.get( key ) ) );
+            document.add( newTable( key,
+                                    other.get( key ) ) );
         }
     }
 
-    public static Table createRuleTable(DrlRuleData drl) throws BadElementException,
-                                                        DocumentException {
-        Table table = createEmptyTable();
+    public static Table newRuleTable(DrlRuleData drl) throws BadElementException,
+                                                     DocumentException {
+        Table table = newTable();
 
-        Cell headerCell = newEmptyWhenThenCell( "Attributes" );
+        Cell headerCell = newHeaderCell( "Attributes",
+                                         CATEGORIES_TEXT );
         table.addCell( headerCell );
 
         for ( String s : drl.header ) {
-            table.addCell( newEmptyCell( INDENT + s.trim() ) );
+            table.addCell( newCell( INDENT + s.trim() ) );
         }
 
-        table.addCell( newEmptyWhenThenCell( INDENT + "WHEN" ) );
+        table.addCell( newHeaderCell( INDENT + "WHEN",
+                                      BODY_TEXT ) );
         for ( String s : drl.lhs ) {
-            table.addCell( newEmptyCell( INDENT + INDENT + s.trim() ) );
+            table.addCell( newCell( INDENT + INDENT + s.trim() ) );
         }
 
-        table.addCell( newEmptyWhenThenCell( INDENT + "THEN" ) );
+        table.addCell( newHeaderCell( INDENT + "THEN",
+                                      BODY_TEXT ) );
         for ( String s : drl.rhs ) {
-            table.addCell( newEmptyCell( INDENT + INDENT + s.trim() ) );
+            table.addCell( newCell( INDENT + INDENT + s.trim() ) );
         }
-        //        table.addCell( newEmptyWhenThenCell( "END" ) );
+        // table.addCell( newEmptyWhenThenCell( "END" ) );
 
         return table;
     }
 
-    public static Table createTable(final String topic,
-                                    Collection<String> items) throws BadElementException,
-                                                             DocumentException {
-        Table metadata = createEmptyTable();
+    public static Table newTable(final String topic,
+                                 Collection<String> items) throws BadElementException,
+                                                          DocumentException {
+        Table table = newTable();
 
-        Cell headerCell = newEmptyHeaderCell( topic );
-        metadata.addCell( headerCell );
+        Cell headerCell = newHeaderCell( topic,
+                                         CATEGORIES_TEXT );
+        table.addCell( headerCell );
 
-        for ( String s : items ) {
-            metadata.addCell( newEmptyCell( s ) );
+        if ( items.isEmpty() ) {
+            table.addCell( newCell( " - " ) );
+        } else {
+            for ( String s : items ) {
+                table.addCell( newCell( s ) );
+            }
         }
 
-        return metadata;
+        return table;
     }
 
-    public static Table createMetaDataTable(Collection<String> metadatas) throws BadElementException,
-                                                                         DocumentException {
-        Table metadata = createEmptyTable();
-
-        Cell headerCell = newEmptyHeaderCell( "META DATA" );
-        metadata.addCell( headerCell );
-
-        for ( String s : metadatas ) {
-            metadata.addCell( newEmptyCell( INDENT + s ) );
-        }
-
-        return metadata;
-    }
-
     public static List createContents(java.util.List<DrlRuleData> rules) {
         List index = new List( true );
 
         for ( DrlRuleData drlRuleData : rules ) {
             Chunk chunk = new Chunk( drlRuleData.ruleName );
-            //                chunk.setLocalGoto( item.getName() );
+            // chunk.setLocalGoto( item.getName() );
             ListItem listItem = new ListItem( chunk );
             index.add( listItem );
         }
+
         return index;
     }
 
-    private static Cell newEmptyWhenThenCell(String text) throws BadElementException {
+    private static Cell newHeaderCell(String text,
+                                      Font font) throws BadElementException {
         Cell c = new Cell( new Phrase( text,
-                                       BODY_TEXT ) );
+                                       font ) );
         c.setBackgroundColor( Color.decode( "#CCCCFF" ) );
         c.setLeading( 10 );
         c.setBorder( 1 );
-        return c;
-    }
 
-    private static Cell newEmptyHeaderCell(String text) throws BadElementException {
-        Cell c = new Cell( new Phrase( text,
-                                       CATEGORIES_TEXT ) );
-        c.setBackgroundColor( Color.decode( "#CCCCFF" ) );
-        c.setLeading( 10 );
-        c.setBorder( 0 );
-        c.setBorderWidthBottom( 1 );
         return c;
     }
 
-    private static Cell newEmptyCell(String text) throws BadElementException {
+    private static Cell newCell(String text) throws BadElementException {
         Cell c = new Cell( new Phrase( text,
                                        BODY_TEXT ) );
         c.setLeading( 10 );
         c.setBorder( 0 );
         c.setBorderWidthBottom( 1 );
+
         return c;
     }
 
@@ -183,9 +179,9 @@
         return footer;
     }
 
-    public static void createRulePage(Document document,
-                                      String packageName,
-                                      DrlRuleData drlData) throws DocumentException {
+    public static void newRulePage(Document document,
+                                   String packageName,
+                                   DrlRuleData drlData) throws DocumentException {
 
         document.add( new Paragraph( packageName,
                                      PACKAGE_NAME ) );
@@ -205,13 +201,14 @@
         }
 
         // Description
-        document.add( createDescription( drlData.description ) );
+        document.add( newDescription( drlData.description ) );
 
         // DRL
-        document.add( createRuleTable( drlData ) );
+        document.add( newRuleTable( drlData ) );
 
         // Meta data
-        document.add( createMetaDataTable( drlData.metadata ) );
+        document.add( newTable( "Meta Data",
+                                drlData.metadata ) );
 
         // Other
         createOtherItems( document,
@@ -233,12 +230,12 @@
         date.setAlignment( Element.ALIGN_CENTER );
         document.add( date );
 
-        document.add( new Paragraph( "\n\n\n\n\nDescription: " + packageData.description,
+        document.add( new Paragraph( "\n\n\n\n\n" + packageData.description,
                                      BODY_TEXT ) );
-        document.add( createTable( "Metadata: ",
-                                   packageData.metadata ) );
-        document.add( createTable( "Globals: ",
-                                   packageData.globals ) );
+        document.add( newTable( "Meta Data ",
+                                packageData.metadata ) );
+        document.add( newTable( "Globals ",
+                                packageData.globals ) );
         createOtherItems( document,
                           packageData.otherInformation );
     }

Modified: labs/jbossrules/trunk/drools-doc/src/test/java/org/drools/doc/DrlPackageDataTest.java
===================================================================
--- labs/jbossrules/trunk/drools-doc/src/test/java/org/drools/doc/DrlPackageDataTest.java	2009-11-10 08:40:51 UTC (rev 30081)
+++ labs/jbossrules/trunk/drools-doc/src/test/java/org/drools/doc/DrlPackageDataTest.java	2009-11-10 09:55:59 UTC (rev 30082)
@@ -5,80 +5,76 @@
 import junit.framework.TestCase;
 
 public class DrlPackageDataTest extends TestCase {
-    public void testHandleDrl() {
+	public void testHandleDrl() {
 
-        String rule1 = "";
-        rule1 += "package org.drools.test\n";
-        rule1 += "global java.util.List list\n";
-        rule1 += "rule rule1\n";
-        rule1 += "when\n";
-        rule1 += "then\n";
-        rule1 += "list.add( drools.getRule().getName() );\n";
-        rule1 += "end\n";
-        rule1 += "rule rule2\n";
-        rule1 += "when\n";
-        rule1 += "then\n";
-        rule1 += "list.add( drools.getRule().getName() );\n";
-        rule1 += "end\n";
+		String rule1 = "";
+		rule1 += "package org.drools.test\n";
+		rule1 += "global java.util.List list\n";
+		rule1 += "rule rule1\n";
+		rule1 += "when\n";
+		rule1 += "then\n";
+		rule1 += "list.add( drools.getRule().getName() );\n";
+		rule1 += "end\n";
+		rule1 += "rule rule2\n";
+		rule1 += "when\n";
+		rule1 += "then\n";
+		rule1 += "list.add( drools.getRule().getName() );\n";
+		rule1 += "end\n";
 
-        DrlPackageData s = DrlPackageData.findPackageDataFromDrl( rule1 );
+		DrlPackageData s = DrlPackageData.findPackageDataFromDrl(rule1);
 
-        assertEquals( "org.drools.test",
-                      s.packageName );
-        assertEquals( 2,
-                      s.rules.size() );
-        assertEquals( "",
-                      s.description );
+		assertEquals("org.drools.test", s.packageName);
+		assertEquals(2, s.rules.size());
+		assertEquals("", s.description);
 
-    }
+	}
 
-    public void testHandleDrlWithComments() {
+	public void testHandleDrlWithComments() {
 
-        String rule1 = "";
-        rule1 += "# important information\n";
-        rule1 += "# about this package\n";
-        rule1 += "# it contains some rules\n";
-        rule1 += "package org.drools.test\n";
-        rule1 += "global java.util.List list\n";
-        rule1 += "rule rule1\n";
-        rule1 += "when\n";
-        rule1 += "then\n";
-        rule1 += "list.add( drools.getRule().getName() );\n";
-        rule1 += "end\n";
-        rule1 += "rule rule2\n";
-        rule1 += "when\n";
-        rule1 += "then\n";
-        rule1 += "list.add( drools.getRule().getName() );\n";
-        rule1 += "end\n";
+		String rule1 = "";
+		rule1 += "# important information\n";
+		rule1 += "# about this package\n";
+		rule1 += "# it contains some rules\n";
+		rule1 += "package org.drools.test\n";
+		rule1 += "global java.util.List list\n";
+		rule1 += "rule rule1\n";
+		rule1 += "	when\n";
+		rule1 += "	then\n";
+		rule1 += "		list.add( drools.getRule().getName() );\n";
+		rule1 += "end\n";
+		rule1 += "rule rule2\n";
+		rule1 += "	when\n";
+		rule1 += "	then\n";
+		rule1 += "		list.add( drools.getRule().getName() );\n";
+		rule1 += "end\n";
 
-        DrlPackageData data = DrlPackageData.findPackageDataFromDrl( rule1 );
+		DrlPackageData data = DrlPackageData.findPackageDataFromDrl(rule1);
 
-        assertEquals( "org.drools.test",
-                      data.packageName );
-        assertEquals( 2,
-                      data.rules.size() );
-        assertEquals( "rule1",
-                      data.rules.get( 0 ).ruleName );
-        assertEquals( "rule2",
-                      data.rules.get( 1 ).ruleName );
-        assertEquals( 1,
-                      data.globals.size() );
-        assertEquals( "java.util.List list",
-                      data.globals.get( 0 ) );
-        assertEquals( "important information\n about this package\n it contains some rules",
-                      data.description );
-    }
+		assertEquals("org.drools.test", data.packageName);
+		assertEquals(2, data.rules.size());
+		assertEquals(1, data.globals.size());
+		assertEquals("java.util.List list", data.globals.get(0));
+		assertEquals(
+				"important information\nabout this package\nit contains some rules\n",
+				data.description);
 
-    public void testfindGlobals() {
+		DrlRuleData rd1 = data.rules.get(0);
+		assertEquals("rule1", rd1.ruleName);
+		assertEquals("", rd1.description);
 
-        String header = "global LoanApplication gg";
+		DrlRuleData rd2 = data.rules.get(1);
+		assertEquals("rule2", rd2.ruleName);
+		assertEquals("", rd2.description);
+	}
 
-        List<String> globals = DrlPackageData.findGlobals( header );
+	public void testfindGlobals() {
 
-        assertEquals( 1,
-                      globals.size() );
-        assertEquals( "LoanApplication gg",
-                      globals.get( 0 ) );
-    }
+		String header = "global LoanApplication gg";
 
+		List<String> globals = DrlPackageData.findGlobals(header);
+
+		assertEquals(1, globals.size());
+		assertEquals("LoanApplication gg", globals.get(0));
+	}
+
 }

Modified: labs/jbossrules/trunk/drools-doc/src/test/java/org/drools/doc/DrlRuleDataTest.java
===================================================================
--- labs/jbossrules/trunk/drools-doc/src/test/java/org/drools/doc/DrlRuleDataTest.java	2009-11-10 08:40:51 UTC (rev 30081)
+++ labs/jbossrules/trunk/drools-doc/src/test/java/org/drools/doc/DrlRuleDataTest.java	2009-11-10 09:55:59 UTC (rev 30082)
@@ -5,151 +5,129 @@
 import junit.framework.TestCase;
 
 public class DrlRuleDataTest extends TestCase {
-    public void testHandleDrl() {
-        String drl = 
-            "rule \"Something\" \n " + 
-            "dialect \"Java\" \n " + 
-            "when \n " + 
-            "Person() \n " + 
-            "Cheesery() \n " + 
-            "then \n " + 
-            "insert( new Person()) \n " + 
-            "insert( new Car()) \n " + 
-            "insert( new Cheese()) \n " + 
-            "end ";
-        
-        DrlRuleData s = DrlRuleData.findRulesDataFromDrl( drl ).get( 0 );
+	public void testHandleDrl() {
+		String drl = "rule \"Something\" \n ";
+		drl += "dialect \"Java\" \n ";
+		drl += "	when \n ";
+		drl += "		Person() \n ";
+		drl += "		Cheesery() \n ";
+		drl += "	then \n ";
+		drl += "		insert( new Person()) \n ";
+		drl += "		insert( new Car()) \n ";
+		drl += "		insert( new Cheese()) \n ";
+		drl += "end ";
 
-        assertEquals( 1,
-                      s.header.size() );
-        assertEquals( 2,
-                      s.lhs.size() );
-        assertEquals( 3,
-                      s.rhs.size() );
-        assertEquals( "",
-                      s.description );
-        
-    }
-    
-    public void testHandleDrlNoLineBreaks() {
-        String drl = 
-            "rule \"CreditScoreApproval\" \n" + 
-               "dialect \"mvel\" \n" +
-               "when    then" +
-                   "applicant.setApproved(true) \n" +
-                   "applicant.setName( \"Toni\" ) \n" +
-                   "applicant.setAge( 10 ) \n" +
-            "end";
-        DrlRuleData s = DrlRuleData.findRulesDataFromDrl( drl ).get( 0 );
+		DrlRuleData s = DrlRuleData.findRulesDataFromDrl(drl).get(0);
 
-        assertNotNull( s );
+		assertEquals(1, s.header.size());
+		assertEquals(2, s.lhs.size());
+		assertEquals(3, s.rhs.size());
+		assertEquals("", s.description);
 
-        assertEquals( 1,
-                      s.header.size() );
-        assertEquals( 0,
-                      s.lhs.size() );
-        assertEquals( 3,
-                      s.rhs.size() );
-        assertEquals( "",
-                      s.description );
-        
-    }
+	}
 
-    public void testHandleDrlWithComment() {
-        String drl = 
-            "# Really important information about this rule \n" + 
-            "# Another line because one was not enough \n" + 
-            "#  \n" + 
-            "# @author: trikkola \n" + 
-            "rule \"First\" \n" + 
-            "dialect \"mvel\" \n" +
-            "when \n " + 
-            "Person() \n " + 
-            "Cheesery() \n " + 
-            "then \n " + 
-            "applicant.setApproved(true) \n" +
-            "applicant.setName( \"Toni\" ) \n" +
-            "applicant.setAge( 10 ) \n" +
-            "end \n" +
-            "\n" +
-            "# Really important information about this rule \n" + 
-            "# Another line because one was not enough \n" + 
-            "#  \n" + 
-            "# @author: trikkola \n" + 
-            "# @created: 29.12.2001 \n" + 
-            "# @edited: 5.5.2005 \n" + 
-            "rule \"Second\" \n" + 
-            "dialect \"mvel\" \n" +
-            "when \n " + 
-            "Person() \n " + 
-            "Cheesery() \n " + 
-            "then \n " + 
-            "applicant.setApproved(true) \n" +
-            "applicant.setName( \"Toni\" ) \n" +
-            "applicant.setAge( 10 ) \n" +
-            "end" +
-            "\n" +
-            "rule \"Third\" \n" + 
-            "dialect \"mvel\" \n" +
-            "when \n " + 
-            "Person() \n " + 
-            "Cheesery() \n " + 
-            "then \n " + 
-            "applicant.setApproved(true) \n" +
-            "applicant.setName( \"Toni\" ) \n" +
-            "applicant.setAge( 10 ) \n" +
-            "end";
-        
-        
-        List<DrlRuleData> list = DrlRuleData.findRulesDataFromDrl( drl );
+	public void testHandleDrlNoLineBreaks() {
+		String drl = "rule \"CreditScoreApproval\" \n";
+		drl += "	dialect \"mvel\" \n";
+		drl += "	when    then";
+		drl += "		applicant.setApproved(true) \n";
+		drl += "		applicant.setName( \"Toni\" ) \n";
+		drl += "		applicant.setAge( 10 ) \n";
+		drl += "end";
+		DrlRuleData s = DrlRuleData.findRulesDataFromDrl(drl).get(0);
 
-        assertEquals( 3,
-                      list.size() );
+		assertNotNull(s);
 
-        DrlRuleData rd = list.get( 0 );
+		assertEquals(1, s.header.size());
+		assertEquals(0, s.lhs.size());
+		assertEquals(3, s.rhs.size());
+		assertEquals("", s.description);
 
-        assertNotNull( rd );
+	}
 
-        assertEquals( 1,
-                      rd.header.size() );
-        assertEquals( 2,
-                      rd.lhs.size() );
-        assertEquals( 3,
-                      rd.rhs.size() );
-        assertEquals( 1,
-                      rd.metadata.size() );
-        assertNotNull( rd.description );
-        assertNotSame( "",
-                       rd.description );
-        
-        DrlRuleData rd2 = list.get( 1 );
+	public void testHandleDrlWithComment() {
+		String drl = "# Really important information about this rule \n";
+		drl += "# Another line because one was not enough \n";
+		drl += "#  \n";
+		drl += "# @author: trikkola \n";
+		drl += "rule \"First\" \n";
+		drl += "	dialect \"mvel\" \n";
+		drl += "	when \n ";
+		drl += "		Person() \n ";
+		drl += "		Cheesery() \n ";
+		drl += "	then \n ";
+		drl += "		applicant.setApproved(true) \n";
+		drl += "		applicant.setName( \"Toni\" ) \n";
+		drl += "		applicant.setAge( 10 ) \n";
+		drl += "end \n";
+		drl += "\n";
+		drl += "# Really important information about this rule \n";
+		drl += "# Another line because one was not enough \n";
+		drl += "#  \n";
+		drl += "# @author: trikkola \n";
+		drl += "# @created: 29.12.2001 \n";
+		drl += "# @edited: 5.5.2005 \n";
+		drl += "rule \"Second\" \n";
+		drl += "	dialect \"mvel\" \n";
+		drl += "	when \n ";
+		drl += "		Person() \n ";
+		drl += "		Cheesery() \n ";
+		drl += "	then \n ";
+		drl += "		applicant.setApproved(true) \n";
+		drl += "		applicant.setName( \"Toni\" ) \n";
+		drl += "		applicant.setAge( 10 ) \n";
+		drl += "end";
+		drl += "\n";
+		drl += "rule \"Third\" \n";
+		drl += "	dialect \"mvel\" \n";
+		drl += "	when \n ";
+		drl += "		Person() \n ";
+		drl += "		Cheesery() \n ";
+		drl += "	then \n ";
+		drl += "		applicant.setApproved(true) \n";
+		drl += "		applicant.setName( \"Toni\" ) \n";
+		drl += "		applicant.setAge( 10 ) \n";
+		drl += "end";
 
-        assertNotNull( rd2 );
+		List<DrlRuleData> list = DrlRuleData.findRulesDataFromDrl(drl);
 
-        assertEquals( 1,
-                      rd2.header.size() );
-        assertEquals( 2,
-                      rd2.lhs.size() );
-        assertEquals( 3,
-                      rd2.rhs.size() );
-        assertEquals( 3,
-                      rd2.metadata.size() );
-        assertNotNull( rd2.description );
-        assertNotSame( "",
-                       rd2.description );
+		assertEquals(3, list.size());
 
-        DrlRuleData rd3 = list.get( 2 );
+		DrlRuleData rd = list.get(0);
 
-        assertNotNull( rd3 );
+		assertNotNull(rd);
 
-        assertEquals( 1,
-                      rd3.header.size() );
-        assertEquals( 2,
-                      rd3.lhs.size() );
-        assertEquals( 3,
-                      rd3.rhs.size() );
-        assertNotNull( rd3.description );
-        assertEquals( "",
-                       rd3.description );
-    }
+		assertEquals(1, rd.header.size());
+		assertEquals(2, rd.lhs.size());
+		assertEquals(3, rd.rhs.size());
+		assertEquals(1, rd.metadata.size());
+		assertNotNull(rd.description);
+		assertNotSame("", rd.description);
+
+		DrlRuleData rd2 = list.get(1);
+
+		assertNotNull(rd2);
+
+		assertEquals(1, rd2.header.size());
+		assertEquals(2, rd2.lhs.size());
+		assertEquals(3, rd2.rhs.size());
+		assertEquals(3, rd2.metadata.size());
+		assertNotNull(rd2.description);
+
+		String description = "Really important information about this rule\n";
+		description += "Another line because one was not enough\n\n";
+
+		assertEquals(description, rd2.description);
+		assertNotSame("", rd2.description);
+
+		DrlRuleData rd3 = list.get(2);
+
+		assertNotNull(rd3);
+
+		assertEquals(1, rd3.header.size());
+		assertEquals(2, rd3.lhs.size());
+		assertEquals(3, rd3.rhs.size());
+		assertNotNull(rd3.description);
+		assertEquals("", rd3.description);
+	}
 }

Added: labs/jbossrules/trunk/drools-doc/src/test/java/org/drools/doc/StandaloneDocBuilder.java
===================================================================
--- labs/jbossrules/trunk/drools-doc/src/test/java/org/drools/doc/StandaloneDocBuilder.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-doc/src/test/java/org/drools/doc/StandaloneDocBuilder.java	2009-11-10 09:55:59 UTC (rev 30082)
@@ -0,0 +1,74 @@
+package org.drools.doc;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+
+/**
+ * Stand alone to test writing to a file.
+ * 
+ * @author rikkola
+ * 
+ */
+public class StandaloneDocBuilder {
+
+	public static void main(String[] args) throws FileNotFoundException {
+
+		String drl = "";
+		drl += "# important information\n";
+		drl += "# about this package\n";
+		drl += "# it contains some rules\n";
+		drl += "package org.drools.test\n";
+		drl += "global java.util.List list\n";
+		drl += "# Really important information about this rule \n";
+		drl += "# Another line because one was not enough \n";
+		drl += "#  \n";
+		drl += "# @author: trikkola \n";
+		drl += "rule \"First\" \n";
+		drl += "	dialect \"mvel\" \n";
+		drl += "	when \n ";
+		drl += "		Person() \n ";
+		drl += "		Cheesery() \n ";
+		drl += "	then \n ";
+		drl += "		applicant.setApproved(true) \n";
+		drl += "		applicant.setName( \"Toni\" ) \n";
+		drl += "		applicant.setAge( 10 ) \n";
+		drl += "end \n";
+		drl += "\n";
+		drl += "# Really important information about this rule \n";
+		drl += "# Another line because one was not enough \n";
+		drl += "#  \n";
+		drl += "# @author: trikkola \n";
+		drl += "# @created: 29.12.2001 \n";
+		drl += "# @edited: 5.5.2005 \n";
+		drl += "rule \"Second\" \n";
+		drl += "	dialect \"mvel\" \n";
+		drl += "	when \n ";
+		drl += "		Person() \n ";
+		drl += "		Cheesery() \n ";
+		drl += "	then \n ";
+		drl += "		applicant.setApproved(true) \n";
+		drl += "		applicant.setName( \"Toni\" ) \n";
+		drl += "		applicant.setAge( 10 ) \n";
+		drl += "end";
+		drl += "\n";
+		drl += "rule \"Third\" \n";
+		drl += "	dialect \"mvel\" \n";
+		drl += "	when \n ";
+		drl += "		Person() \n ";
+		drl += "		Cheesery() \n ";
+		drl += "	then \n ";
+		drl += "		applicant.setApproved(true) \n";
+		drl += "		applicant.setName( \"Toni\" ) \n";
+		drl += "		applicant.setAge( 10 ) \n";
+		drl += "end";
+
+		DroolsDocsBuilder ddBuilder = DroolsDocsBuilder.getInstance(drl);
+
+		File file = new File("/Users/rikkola/Desktop/DroolsDoc.pdf");
+		OutputStream out = new FileOutputStream(file);
+
+		ddBuilder.writePDF(out);
+	}
+}



More information about the jboss-svn-commits mailing list