Skip to content

Commit

Permalink
[PLXCOMP-207] support ls date formats with inversed day-month order
Browse files Browse the repository at this point in the history
On MacOSX and FreeBSD, the date format of the 
'ls -l' output can have reversed month-day order 
for most non-US locales ($LANG) [1,2].
Add two more SimpleDateFormats (for
<= 6 months and > 6 months ls date formats)
with order day-month.

[1]
http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/ls.1.html
[2] http://www.freebsd.org/cgi/man.cgi?query=ls
  • Loading branch information
Jan Sievers committed Jun 3, 2012
1 parent 001f268 commit 686ee16
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ abstract class AttributeParser
implements StreamConsumer
{
protected static final Pattern LINE_SPLITTER = Pattern.compile( "\\s+" );
protected static final int[] LS_LAST_DATE_PART_INDICES = { 7, 7, 6 };
protected static final int[] LS_LAST_DATE_PART_INDICES = { 7, 7, 6, 7, 7 };

protected final StreamConsumer delegate;

Expand All @@ -56,7 +56,11 @@ public AttributeParser( StreamConsumer delegate, Logger logger )
this.logger = logger;
LS_DATE_FORMATS =
new SimpleDateFormat[]{ new SimpleDateFormat( "MMM dd yyyy" ), new SimpleDateFormat( "MMM dd HH:mm" ),
new SimpleDateFormat( "yyyy-MM-dd HH:mm" ), };
new SimpleDateFormat( "yyyy-MM-dd HH:mm" ),
// month-day order is reversed for most non-US locales on MacOSX and FreeBSD
new SimpleDateFormat( "dd MMM HH:mm" ),
new SimpleDateFormat( "dd MMM yyyy" )
};
}

public void consumeLine( String line )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/

import junit.framework.TestCase;

import org.codehaus.plexus.components.io.attributes.AttributeParser.NumericUserIDAttributeParser;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.logging.console.ConsoleLogger;
import org.codehaus.plexus.util.Os;
Expand All @@ -33,6 +35,25 @@ public class PlexusIoResourceAttributeUtilsTest
extends TestCase
{

private Locale origSystemLocale;

@Override
protected void setUp()
throws Exception
{
this.origSystemLocale = Locale.getDefault();
// sample ls output files have US date format and we use SimpleDateFormt with system locale for ls date format parsing
// otherwise test could fail on systems with non-US locales
Locale.setDefault( Locale.US );
}

@Override
protected void tearDown()
throws Exception
{
Locale.setDefault( origSystemLocale );
}

public void testGetAttributesForThisTestClass()
throws IOException
{
Expand Down Expand Up @@ -147,6 +168,32 @@ public void testSingleLine()
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( output.getBytes() );
AttributeParser parser = getNumericParser();
parse( byteArrayInputStream, parser );
}

public void testReversedMonthDayOrder()
throws Exception
{
String output = //
"-rw-r--r-- 1 501 80 7683 31 May 10:06 pom_newer.xml\n" + //
"-rwxr--r-- 1 502 81 7683 1 Jun 2010 pom_older.xml";
InputStream byteArrayInputStream = new ByteArrayInputStream( output.getBytes() );
NumericUserIDAttributeParser parser = getNumericParser();
parse( byteArrayInputStream, parser );
Map<String, PlexusIoResourceAttributes> map = parser.getAttributesByPath();

// 6 months or newer ls date format
FileAttributes newerFileAttr = (FileAttributes) map.get( "pom_newer.xml" );
assertNotNull( newerFileAttr );
assertEquals( "-rw-r--r--", new String( newerFileAttr.getLsModeParts() ) );
assertEquals( 501, newerFileAttr.getUserId().intValue() );
assertEquals( 80, newerFileAttr.getGroupId().intValue() );

// older than 6 months ls date format
FileAttributes olderFileAttr = (FileAttributes) map.get( "pom_older.xml" );
assertNotNull( olderFileAttr );
assertEquals( "-rwxr--r--", new String( olderFileAttr.getLsModeParts() ) );
assertEquals( 502, olderFileAttr.getUserId().intValue() );
assertEquals( 81, olderFileAttr.getGroupId().intValue() );
}

public void testOddLinuxFormatWithExtermelyLargeNumericsSingleLine()
Expand Down

0 comments on commit 686ee16

Please sign in to comment.