Skip to content

Commit 686ee16

Browse files
author
Jan Sievers
committed
[PLXCOMP-207] support ls date formats with inversed day-month order
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
1 parent 001f268 commit 686ee16

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

src/main/java/org/codehaus/plexus/components/io/attributes/AttributeParser.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ abstract class AttributeParser
3434
implements StreamConsumer
3535
{
3636
protected static final Pattern LINE_SPLITTER = Pattern.compile( "\\s+" );
37-
protected static final int[] LS_LAST_DATE_PART_INDICES = { 7, 7, 6 };
37+
protected static final int[] LS_LAST_DATE_PART_INDICES = { 7, 7, 6, 7, 7 };
3838

3939
protected final StreamConsumer delegate;
4040

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

6266
public void consumeLine( String line )

src/test/java/org/codehaus/plexus/components/io/attributes/PlexusIoResourceAttributeUtilsTest.java

+47
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818

1919
import junit.framework.TestCase;
20+
21+
import org.codehaus.plexus.components.io.attributes.AttributeParser.NumericUserIDAttributeParser;
2022
import org.codehaus.plexus.logging.Logger;
2123
import org.codehaus.plexus.logging.console.ConsoleLogger;
2224
import org.codehaus.plexus.util.Os;
@@ -33,6 +35,25 @@ public class PlexusIoResourceAttributeUtilsTest
3335
extends TestCase
3436
{
3537

38+
private Locale origSystemLocale;
39+
40+
@Override
41+
protected void setUp()
42+
throws Exception
43+
{
44+
this.origSystemLocale = Locale.getDefault();
45+
// sample ls output files have US date format and we use SimpleDateFormt with system locale for ls date format parsing
46+
// otherwise test could fail on systems with non-US locales
47+
Locale.setDefault( Locale.US );
48+
}
49+
50+
@Override
51+
protected void tearDown()
52+
throws Exception
53+
{
54+
Locale.setDefault( origSystemLocale );
55+
}
56+
3657
public void testGetAttributesForThisTestClass()
3758
throws IOException
3859
{
@@ -147,6 +168,32 @@ public void testSingleLine()
147168
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( output.getBytes() );
148169
AttributeParser parser = getNumericParser();
149170
parse( byteArrayInputStream, parser );
171+
}
172+
173+
public void testReversedMonthDayOrder()
174+
throws Exception
175+
{
176+
String output = //
177+
"-rw-r--r-- 1 501 80 7683 31 May 10:06 pom_newer.xml\n" + //
178+
"-rwxr--r-- 1 502 81 7683 1 Jun 2010 pom_older.xml";
179+
InputStream byteArrayInputStream = new ByteArrayInputStream( output.getBytes() );
180+
NumericUserIDAttributeParser parser = getNumericParser();
181+
parse( byteArrayInputStream, parser );
182+
Map<String, PlexusIoResourceAttributes> map = parser.getAttributesByPath();
183+
184+
// 6 months or newer ls date format
185+
FileAttributes newerFileAttr = (FileAttributes) map.get( "pom_newer.xml" );
186+
assertNotNull( newerFileAttr );
187+
assertEquals( "-rw-r--r--", new String( newerFileAttr.getLsModeParts() ) );
188+
assertEquals( 501, newerFileAttr.getUserId().intValue() );
189+
assertEquals( 80, newerFileAttr.getGroupId().intValue() );
190+
191+
// older than 6 months ls date format
192+
FileAttributes olderFileAttr = (FileAttributes) map.get( "pom_older.xml" );
193+
assertNotNull( olderFileAttr );
194+
assertEquals( "-rwxr--r--", new String( olderFileAttr.getLsModeParts() ) );
195+
assertEquals( 502, olderFileAttr.getUserId().intValue() );
196+
assertEquals( 81, olderFileAttr.getGroupId().intValue() );
150197
}
151198

152199
public void testOddLinuxFormatWithExtermelyLargeNumericsSingleLine()

0 commit comments

Comments
 (0)