1
2
3
4
5 package com.jguild.jrpm.test;
6
7 import java.io.*;
8 import java.net.URL;
9
10 import junit.framework.TestCase;
11
12 import org.apache.log4j.ConsoleAppender;
13 import org.apache.log4j.Level;
14 import org.apache.log4j.Logger;
15 import org.apache.log4j.SimpleLayout;
16
17 import com.jguild.jrpm.io.Header;
18 import com.jguild.jrpm.io.RPMHeader;
19 import com.jguild.jrpm.io.RPMFile;
20
21 /***
22 * Unit Test for parsing RPM files.
23 */
24 public class HeaderFileParsingTest extends TestCase {
25
26 private static final Logger logger = Logger
27 .getLogger(RPMFileParsingTest.class);
28
29 /***
30 * Try to retrieve some tags of the defined test.rpm rpm file.
31 */
32 public void testRPM() {
33 logger.setLevel(Level.INFO);
34 try {
35
36 String rpmFile = "glibc-2.3.3-47.i686.hdr";
37 File rpmFileReference = getTestRPMFile(rpmFile);
38 assertNotNull(rpmFileReference);
39
40 InputStream in = new BufferedInputStream ( new FileInputStream(rpmFileReference));
41 RPMHeader hdr = new RPMHeader(new DataInputStream(in), true);
42 assertEquals("glibc", hdr.getTag("name").toString());
43
44 } catch (Throwable e) {
45 logger.error("Unexpected error", e);
46 fail(e.getLocalizedMessage());
47 }
48 }
49
50 protected void setUp() throws Exception {
51 if (!Logger.getLogger("com.jguild.jrpm").getAllAppenders()
52 .hasMoreElements()) {
53 Logger.getLogger("com.jguild.jrpm").addAppender(
54 new ConsoleAppender(new SimpleLayout()));
55 }
56 super.setUp();
57 }
58
59 protected void tearDown() throws Exception {
60 super.tearDown();
61 }
62
63 /***
64 * Find a resource in the classpath.
65 *
66 * @param name
67 * The resource name
68 * @return An input stream pointing to the resource
69 * @see java.lang.ClassLoader#getResourceAsStream(String)
70 */
71 private File getTestRPMFile(String name) {
72 URL rpmUrl = HeaderFileParsingTest.class.getClassLoader().getResource(name);
73 System.out.println(rpmUrl);
74 return new File(rpmUrl.getFile());
75 }
76 }