1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.vafer.jdeb.mapping;
17  
18  import java.io.ByteArrayInputStream;
19  
20  import junit.framework.TestCase;
21  
22  import org.apache.tools.tar.TarEntry;
23  import org.vafer.jdeb.mapping.LsMapper.ParseError;
24  
25  public final class LsMapperTestCase extends TestCase {
26  
27  	private final static String output = 
28  		"total 0\n" +
29  		"drwxr-xr-x   23 tcurdt  tcurdt   782 Jun 25 03:48 .\n" +
30  		"drwxr-xr-x    3 tcurdt  tcurdt   102 Jun 25 03:48 ..\n" +
31  		"\n" +
32  		"./trunk/target/test-classes/org/vafer/dependency:\n" +
33  		"total 176\n" +
34  		"drwxr-xr-x   23 tcurdt  tcurdt   782 Jun 25 03:48 .\n" +
35  		"drwxr-xr-x    3 tcurdt  tcurdt   102 Jun 25 03:48 ..\n" +
36  		"-rw-r--r--    1 tcurdt  tcurdt  2934 Jun 25 03:48 DependenciesTestCase.class\n" +
37  		"-rw-r--r--    1 tcurdt  tcurdt   786 Jun 25 03:48 JarCombiningTestCase$1.class\n" +
38  		"drwxr-xr-x    4 tcurdt  tcurdt   136 Jun 25 03:48 classes\n" +
39  		"\n" +
40  		"./trunk/src/test-classes/org/vafer/dependency:\n" +
41  		"total 76\n" +
42  		"drwxr-xr-x   23 tcurdt  tcurdt   782 Jun 25 03:48 .\n" +
43  		"drwxr-xr-x    3 tcurdt  tcurdt   102 Jun 25 03:48 ..\n" +
44  		"-rw-r--r--    1 tcurdt  tcurdt  2934 Jun 25 03:48 DependenciesTestCase.class\n" +
45  		"-rw-r--r--    1 tcurdt  tcurdt   786 Jun 25 03:48 JarCombiningTestCase$1.class\n" +
46  		"drwxr-xr-x    4 tcurdt  tcurdt   136 Jun 25 03:48 classes\n" +
47  		"\n";
48  	
49  	public void testModes() throws Exception {
50  		final ByteArrayInputStream is = new ByteArrayInputStream(output.getBytes("UTF-8"));
51  		
52  		final Mapper mapper = new LsMapper(is);
53  
54  		final TarEntry entry1 = mapper.map(new TarEntry("trunk/target/test-classes/org/vafer/dependency"));
55  		
56  		assertEquals(493, entry1.getMode());
57  		assertEquals("tcurdt", entry1.getUserName());
58  		assertEquals("tcurdt", entry1.getGroupName());
59  		
60  		final TarEntry entry2 = mapper.map(new TarEntry("trunk/target/test-classes/org/vafer/dependency/DependenciesTestCase.class"));
61  
62  		assertEquals(420, entry2.getMode());
63  		assertEquals("tcurdt", entry2.getUserName());
64  		assertEquals("tcurdt", entry2.getGroupName());
65  	}
66  	
67  	public void testSuccessfulParsing() throws Exception {
68  		final ByteArrayInputStream is = new ByteArrayInputStream(output.getBytes("UTF-8"));
69  		
70  		final Mapper mapper = new LsMapper(is);
71  		
72  		final TarEntry unknown = new TarEntry("xyz");
73  		assertSame(unknown, mapper.map(unknown));
74  		
75  		final TarEntry known = new TarEntry("trunk/target/test-classes/org/vafer/dependency");
76  		final TarEntry knownMapped = mapper.map(known);
77  		
78  		assertNotSame(known, knownMapped);
79  		
80  	}
81  	
82  	public void testPrematureEOF() throws Exception {
83  		final ByteArrayInputStream is = new ByteArrayInputStream(output.substring(0, 200).getBytes("UTF-8"));
84  		
85  		try {
86  			new LsMapper(is);
87  			fail("should fail to parse");
88  		} catch(ParseError e) {			
89  		}		
90  	}
91  	
92  	public void testWrongFormat() throws Exception {
93  		final ByteArrayInputStream is = new ByteArrayInputStream("asas\n".getBytes("UTF-8"));
94  		
95  		try {
96  			new LsMapper(is);
97  			fail("should fail to parse");
98  		} catch(ParseError e) {			
99  		}				
100 	}
101 }