directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kayyag...@apache.org
Subject svn commit: r1565625 - in /directory/shared/trunk/ldap/model/src: main/java/org/apache/directory/api/ldap/model/ldif/LdifReader.java test/java/org/apache/directory/api/ldap/model/ldif/LdifReaderTest.java
Date Fri, 07 Feb 2014 11:51:47 GMT
Author: kayyagari
Date: Fri Feb  7 11:51:47 2014
New Revision: 1565625

URL: http://svn.apache.org/r1565625
Log:
keep track of the number of the current line being parsed (DIRAPI-174) 

Modified:
    directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifReader.java
    directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/ldif/LdifReaderTest.java

Modified: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifReader.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifReader.java?rev=1565625&r1=1565624&r2=1565625&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifReader.java
(original)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifReader.java
Fri Feb  7 11:51:47 2014
@@ -233,6 +233,9 @@ public class LdifReader implements Itera
     /** the current offset of the reader */
     private long offset = 0;
     
+    /** the current line number being parsed by the reader */
+    private int lineNumber;
+    
     /**
      * Constructors
      */
@@ -1306,6 +1309,8 @@ public class LdifReader implements Itera
         // The entry must start with a dn: or a dn::
         String line = lines.get( 0 );
 
+        lineNumber -= ( lines.size() - 1 );
+        
         String name = parseDn( line );
 
         Dn dn = new Dn( name );
@@ -1340,6 +1345,8 @@ public class LdifReader implements Itera
 
         while ( iter.hasNext() )
         {
+            lineNumber++;
+            
             // Each line could start either with an OID, an attribute type, with
             // "control:" or with "changetype:"
             line = iter.next();
@@ -1553,6 +1560,8 @@ public class LdifReader implements Itera
         {
             while ( ( line = getLine() ) != null )
             {
+                lineNumber++;
+                
                 if ( line.length() == 0 )
                 {
                     if ( isFirstLine )
@@ -1960,6 +1969,15 @@ public class LdifReader implements Itera
 
 
     /**
+     * @return the current line that is being processed by the reader
+     */
+    public int getLineNumber()
+    {
+        return lineNumber;
+    }
+
+
+    /**
      * {@inheritDoc}
      */
     public void close() throws IOException
@@ -1970,7 +1988,7 @@ public class LdifReader implements Itera
             reader.close();
             containsEntries = false;
             containsChanges = false;
-            offset = entryOffset = 0;
+            offset = entryOffset = lineNumber = 0;
         }
     }
 }

Modified: directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/ldif/LdifReaderTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/ldif/LdifReaderTest.java?rev=1565625&r1=1565624&r2=1565625&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/ldif/LdifReaderTest.java
(original)
+++ directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/ldif/LdifReaderTest.java
Fri Feb  7 11:51:47 2014
@@ -2182,4 +2182,94 @@ public class LdifReaderTest
         assertEquals( rafEntry2.getLengthBeforeParsing(), reReadeRafEntry2.getLengthBeforeParsing()
);
         reader.close();
     }
+
+    
+    @Test // for DIRAPI-174
+    public void testLineNumber() throws Exception
+    {
+        String ldif =
+            "versionN:   1\n" + // wrong tag name 'versionN'
+                "dn: dc=example,dc=com\n" +
+                "changetype: delete\n" +
+                "attr1: test";
+
+        LdifReader reader = new LdifReader();
+
+        try
+        {
+            reader.parseLdif( ldif );
+            fail();
+        }
+        catch( Exception e )
+        {
+        }
+        
+        assertEquals( 1, reader.getLineNumber() );
+
+        ldif =
+            "version:   1\n" +
+                "d n: dc=example,dc=com\n" + // wrong name "d n"
+                "changetype: delete\n" +
+                "attr1: test";
+        reader = new LdifReader();
+
+        try
+        {
+            reader.parseLdif( ldif );
+            fail();
+        }
+        catch( Exception e )
+        {
+        }
+
+        assertEquals( 2, reader.getLineNumber() );
+
+        // wrong changetype
+        ldif =
+            "version:   1\n" +
+                "dn: dc=example,dc=com\n" +
+                "changetype: delete\n" +
+                "attr1: test";
+        reader = new LdifReader();
+
+        try
+        {
+            reader.parseLdif( ldif );
+            fail();
+        }
+        catch( Exception e )
+        {
+        }
+
+        assertEquals( 4, reader.getLineNumber() );
+        
+        ldif =
+            "version:   1\n" +
+                "dn: cn=app1,ou=applications,ou=conf,dc=apache,dc=org\n" +
+                "cn: app1\n" +
+                "objectClass: top\n" +
+                "objectClass: apApplication\n" +
+                "displayName:   app1   \n" +
+                "dependencies:\n" +
+                "envVars:\n\n" + // watch out the extra newline while counting
+                "d n: cn=app2,ou=applications,ou=conf,dc=apache,dc=org\n" + // wrong start
+                "cn: app2\n" +
+                "objectClass: top\n" +
+                "objectClass: apApplication\n" +
+                "displayName:   app2   \n" +
+                "dependencies:\n" +
+                "envVars:";
+        reader = new LdifReader();
+
+        try
+        {
+            reader.parseLdif( ldif );
+            fail("shouldn't be parsed");
+        }
+        catch( Exception e )
+        {
+        }
+
+        assertEquals( 10, reader.getLineNumber() );
+    }
 }



Mime
View raw message