cvs commit: spice/sandbox/repository/componenthaus/src/tests/org/componenthaus/ant ClassAbbreviatorImplTestCase.java JavadocFormatterImplTestCase.java LineIndenterImplTestCase.java ComponentMetadataTaskTestCase.java

Mike Hogan <[email protected]> Sun, 09 Nov 2003 06:00:50 -0800
Newsgroups gmane.comp.java.spice.cvs
Message-ID <[email protected]>
hogie       03/11/09 06:00:50

  Modified:    sandbox/repository/componenthaus/src/tests/org/componenthaus/ant
                        ComponentMetadataTaskTestCase.java
  Added:       sandbox/repository/componenthaus/src/tests/org/componenthaus/ant
                        ClassAbbreviatorImplTestCase.java
                        JavadocFormatterImplTestCase.java
                        LineIndenterImplTestCase.java
  Log:
  If components are classes, show as little of the class as necessary to show service offering
  
  Revision  Changes    Path
  1.3       +2 -19     spice/sandbox/repository/componenthaus/src/tests/org/componenthaus/ant/ComponentMetadataTaskTestCase.java
  
  Index: ComponentMetadataTaskTestCase.java
  ===================================================================
  RCS file: /cvsroot/spice/spice/sandbox/repository/componenthaus/src/tests/org/componenthaus/ant/ComponentMetadataTaskTestCase.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ComponentMetadataTaskTestCase.java	5 Nov 2003 19:35:50 -0000	1.2
  +++ ComponentMetadataTaskTestCase.java	9 Nov 2003 14:00:49 -0000	1.3
  @@ -178,25 +178,6 @@
           assertEquals(fullyQualifiedInterfaceName, expected.getInterfaceName());
       }
   
  -/*
  -    public void testThrowsAnExceptionWhenInterfaceIsAClass() throws IOException {
  -        createJavaSourceFile(sourceDirectory, new Javaclass(
  -                packageName,
  -                interfaceName,
  -                someJavadoc,
  -                null), createdFiles);
  -        final ComponentMetadataTask task = new ComponentMetadataTask(fullyQualifiedInterfaceName,sourceDirectory,targetDirectory);
  -        NotAPublicInterfaceBuildException expected = null;
  -        try {
  -            task.execute();
  -            fail("Did not get expected exception");
  -        } catch (NotAPublicInterfaceBuildException e) {
  -            expected = e;
  -        }
  -        assertEquals(fullyQualifiedInterfaceName, expected.getInterfaceName());
  -    }
  -*/
  -
       public void testCanGenerateCorrectMetadataForOneInterface() throws IOException, ParserConfigurationException, SAXException {
           createJavaSourceFile(sourceDirectory, new Interface(
                   packageName,
  @@ -243,6 +224,7 @@
           assertEquals(fullyQualifiedInterfaceName, interfaceMetadata.getFullyQualifiedName());
           assertEquals(someJavadoc,interfaceMetadata.getJavadoc());
           assertEquals(firstLineOfJavadoc, interfaceMetadata.getShortDescription());
  +        assertFalse(interfaceMetadata.isClass());
   
           interfaceMetadata = componentMetadata.getInterfaceMetadataForName(secondFullyQualifiedInterfaceName);
           assertEquals(packageName,interfaceMetadata.getPackageName());
  @@ -250,6 +232,7 @@
           assertEquals(secondFullyQualifiedInterfaceName, interfaceMetadata.getFullyQualifiedName());
           assertEquals(secondJavadoc,interfaceMetadata.getJavadoc());
           assertEquals(firstLineOfJavadocForSecondInterface, interfaceMetadata.getShortDescription());
  +        assertTrue(interfaceMetadata.isClass());
       }
   
       private void createJavaSourceFile(String targetDirectory, JavaSourceFile javaSourceFile, final Collection collectingParameter) throws IOException {
  
  
  
  1.1                  spice/sandbox/repository/componenthaus/src/tests/org/componenthaus/ant/ClassAbbreviatorImplTestCase.java
  
  Index: ClassAbbreviatorImplTestCase.java
  ===================================================================
  package org.componenthaus.ant;
  
  import com.thoughtworks.qdox.JavaDocBuilder;
  import com.thoughtworks.qdox.model.JavaClass;
  import com.thoughtworks.qdox.model.JavaMethod;
  import junit.framework.TestCase;
  
  import java.io.StringReader;
  import java.io.BufferedReader;
  import java.io.IOException;
  
  public class ClassAbbreviatorImplTestCase extends TestCase {
      private ClassAbbreviator abbreviator = null;
      private JavadocFormatter javadocFormatter = null;
  
      protected void setUp() throws Exception {
          javadocFormatter = new JavadocFormatterImpl(80);
          abbreviator = new ClassAbbreviatorImpl(javadocFormatter, new LineIndenterImpl());
      }
  
      public void testThrowsExceptionIfNotAClass() {
          final String interfaceName = "AnInterface";
          final String packageName = "com.acme";
          final MockJavaClass anInterface = new MockJavaClass(packageName,interfaceName);
          anInterface.setInterface(true);
          NotAClassException expected = null;
          try {
              abbreviator.abbreviate(anInterface, new StringBuffer());
              fail("Did not get expected exception");
          } catch (NotAClassException e) {
              expected = e;
          }
          assertEquals(packageName + "." + interfaceName, expected.getName());
      }
  
      public void testWillIgnoreNonPublicMembers() {
          final String interfaceName = "AnInterface";
          final String packageName = "com.acme";
          final MockJavaClass aClass = new MockJavaClass(packageName,interfaceName);
  
          final JavaMethod privateMethod = new JavaMethod();
          privateMethod.setModifiers(new String[]{"private"});
          final JavaMethod protectedMethod = new JavaMethod();
          protectedMethod.setModifiers(new String[]{"protected"});
          final JavaMethod packageMethod = new JavaMethod();
          aClass.addMethod(privateMethod);
          aClass.addMethod(protectedMethod);
          aClass.addMethod(packageMethod);
          final StringBuffer collector = new StringBuffer();
          assertEquals(0, abbreviator.abbreviate(aClass, collector));
      }
  
      public void testWillFindPublicMethods() throws IOException {
  
          final String sourceCode =
                  "package com.acme.thing;\n" +
                  "\n" +
                  "import com.acme.OtherThing;\n" +
                  "import com.acme.AcmeException;\n" +
                  "import java.io.File;\n" +
                  "\n" +
                  "/**\n" +
                  " * This class does something.  \n" +
                  " * Explained in more detail.\n" +
                  " * Explained in even more detail.\n" +
                  " * Explained in yet more detail again.\n" +
                  " **/\n" +
                  "public final class AClassName {\n" +
                  "   /**\n" +
                  "   * Returns the basename of the supplied File.  The basename \n" +
                  "   * is whats left when you take the directory away \n" +
                  "   * \n" +
                  "   * @param file the file to get the basename from" +
                  "   * @return the basename" +
                  "   **/" +
                  "   public static final String basename(File file) throws AcmeException{\n" +
                  "       int i = 0;\n" +
                  "   }\n" +
                  "   public String instanceBasename(File file){\n" +
                  "       int i = 0;\n" +
                  "   }\n" +
                  "   private static final String privateBasename(File file) throws AcmeException{\n" +
                  "       int i = 0;\n" +
                  "   }\n" +
                  "}";
          JavaDocBuilder builder = new JavaDocBuilder();
          builder.addSource(new StringReader(sourceCode));
          final JavaClass aClass = builder.getClassByName("com.acme.thing.AClassName");
          final StringBuffer bodyCollector = new StringBuffer();
  
          assertEquals(2, abbreviator.abbreviate(aClass, bodyCollector));
          final String formatted = bodyCollector.toString();
          assertEquals(1,countMatches(formatted,"package com.acme.thing;"));
          assertEquals(1,countMatches(formatted,"import java.io.File;"));
          assertEquals(1,countMatches(formatted,"import com.acme.AcmeException;"));
          assertEquals(2,countMatches(formatted,"import .*")); //Only two import expected, those actually referenced
          assertEquals(1,countMatches(formatted,"public final class AClassName \\{"));
          assertEquals(1,countMatches(formatted," \\* This class does something. Explained in more detail.*"));
          assertEquals(1,countMatches(formatted, "\\s*public final static String basename\\(File file\\) throws AcmeException;"));
          assertEquals(1,countMatches(formatted, "\\s*public String instanceBasename\\(File file\\);"));
          assertEquals(1,countMatches(formatted, ".*\\* Returns the basename of the supplied File\\..*"));
          assertEquals(1,countMatches(formatted, ".*\\* @param file the file to get the basename from"));
          assertEquals(1,countMatches(formatted, ".*\\* @return the basename"));
  
  
      }
  
      private int countMatches(String string, String regexp) throws IOException {
          int matches = 0;
          final BufferedReader reader = new BufferedReader(new StringReader(string));
          String line = reader.readLine();
          while ( line != null ) {
              if ( line.matches(regexp) ) {
                  matches++;
              }
              line = reader.readLine();
          }
          reader.close();
          return matches;
      }
  
      private static final class MockJavaClass extends JavaClass {
          private final String packageName;
          private final String className;
  
          public MockJavaClass(String packageName, String className) {
              //super(null);
              this.packageName = packageName;
              this.className = className;
              setName(className);
          }
  
          public String getFullyQualifiedName() {
              return packageName + "." + className;
          }
  
          public boolean isPublic() {
              return true;
          }
  
          public String getPackage() {
              return packageName;
          }
      }
  }
  
  
  
  1.1                  spice/sandbox/repository/componenthaus/src/tests/org/componenthaus/ant/JavadocFormatterImplTestCase.java
  
  Index: JavadocFormatterImplTestCase.java
  ===================================================================
  package org.componenthaus.ant;
  
  import junit.framework.TestCase;
  
  public class JavadocFormatterImplTestCase extends TestCase {
      private static final int MAX_LINE_LENGTH = 80;
      private JavadocFormatter formatter = null;
  
      protected void setUp() throws Exception {
          formatter = new JavadocFormatterImpl(MAX_LINE_LENGTH);
      }
  
      public void testCanFormatEmptyString() {
          assertEquals("",formatter.format(""));
      }
  
      public void testWillIgnoreNull() {
          assertNull(formatter.format(null));
      }
  
      public void testCanHandleSingleShortLine() {
          String input = "This is a short javadoc comment";
          String expected =
                  "/**\n" +
                  " * " + input + "\n" +
                  " **/";
          final String actual = formatter.format(input);
          assertEquals(expected,actual);
      }
  
      public void testWillConcatenateTwoShortLines() {
          String input = "This is a short javadoc comment.";
          String input2 = "So is this.";
          String expected =
                  "/**\n" +
                  " * " + input + " " + input2 + "\n" +
                  " **/";
          final String actual = formatter.format(input + "\n" + input2);
          assertEquals(expected,actual);
      }
  
      public void testCanHandleAWordLongerThanTheLineLimit() {
          formatter = new JavadocFormatterImpl(10);
          String input = "This is a short javadoc comment.";
          String input2 = "So is this.";
          String longInput = "AndThisWordIsLongerThanTheMaxLineLength.";
          String expected =
                  "/**\n" +
                  " * This\n" +
                  " * is a\n" +
                  " * short\n" +
                  " * javadoc\n" +
                  " * comment.\n" +
                  " * So is\n" +
                  " * this.\n" +
                  " * AndThisWordIsLongerThanTheMaxLineLength.\n" +
                  " **/";
          final String actual = formatter.format(input + "\n" + input2 + "\n" + longInput);
          assertEquals(expected,actual);
      }
  
      public void testAlwaysPutsParamTagOnNewLine() {
          String input = "This is a short line. @param this is a param. @return this is return";
          final String actual = formatter.format(input);
          assertEquals(
                  "/**\n" +
                  " * This is a short line.\n" +
                  " * \n" +
                  " * @param this is a param.\n" +
                  " * @return this is return\n" +
                  " **/", actual);
      }
  }
  
  
  
  1.1                  spice/sandbox/repository/componenthaus/src/tests/org/componenthaus/ant/LineIndenterImplTestCase.java
  
  Index: LineIndenterImplTestCase.java
  ===================================================================
  package org.componenthaus.ant;
  
  import junit.framework.TestCase;
  
  public class LineIndenterImplTestCase extends TestCase {
      private LineIndenter indenter = null;
  
      protected void setUp() throws Exception {
          indenter = new LineIndenterImpl();
      }
  
      public void testCanIndentEmptyString() {
          assertEquals("",indenter.indent("",0));
      }
  
      public void testCanHandleNullInput() {
          assertNull(indenter.indent(null,0));
      }
  
      public void testCanIndentSingleLine() {
          final String line = "This is a single line";
          assertEquals(line, indenter.indent(line,0));
          assertEquals("\t" + line, indenter.indent(line,1));
          assertEquals("\t\t" + line, indenter.indent(line,2));
      }
  
      public void testCanIndentTwoLines() {
          final String twoLines =
                  "This is line one.\n" +
                  "This is line two.";
          assertEquals(twoLines,indenter.indent(twoLines,0));
  
          assertEquals(
                  "\tThis is line one.\n" +
                  "\tThis is line two.",indenter.indent(twoLines,1));
  
          assertEquals(
                  "\t\tThis is line one.\n" +
                  "\t\tThis is line two.",indenter.indent(twoLines,2));
  
      }
  
  }
  
  
  


-------------------------------------------------------
This SF.Net email sponsored by: ApacheCon 2003,
16-19 November in Las Vegas. Learn firsthand the latest
developments in Apache, PHP, Perl, XML, Java, MySQL,
WebDAV, and more! http://www.apachecon.com/