1   /*
2    * jGuild Project: jRPM
3    * Released under the Apache License ( http://www.apache.org/LICENSE )
4    */
5   package com.jguild.jrpm.test;
6   
7   import java.io.File;
8   import java.net.URL;
9   
10  import junit.framework.TestCase;
11  
12  import org.apache.log4j.ConsoleAppender;
13  import org.apache.log4j.Logger;
14  import org.apache.log4j.SimpleLayout;
15  
16  import com.jguild.jrpm.io.RPMFile;
17  
18  /***
19   * Unit Test for parsing RPM files.
20   */
21  public class RPMFileParsingTest extends TestCase {
22  
23      private static final Logger logger = Logger
24              .getLogger(RPMFileParsingTest.class);
25  
26      private static final String[] RPM_FILES = { "ElectricFence-2.2.2-15.i386.rpm"};
27  
28      private static final int[] TOTAL_SIG_TAGIDS = { 7};
29  
30      private static final int[] TOTAL_SIG_TAGNAMES = { 7};
31  
32      private static final int[] TOTAL_HEAD_TAGIDS = { 75};
33  
34      private static final int[] TOTAL_HEAD_TAGNAMES = { 75};
35  
36      private static final String[] NAME = { "ElectricFence"};
37  
38      private static final String VENDOR_REDHAT = "Red Hat, Inc.";
39  
40      private static final String[] VENDOR = { VENDOR_REDHAT};
41  
42      /***
43       * Try to retrieve some tags of the defined test.rpm rpm file.
44       */
45      public void testRPM() {
46          try {
47              for (int i = 0; i < RPM_FILES.length; i++) {
48                  String rpmFile = RPM_FILES[i];
49                  File rpmFileReference = getTestRPMFile(rpmFile);
50                  assertNotNull(rpmFileReference);
51  
52                  RPMFile file = new RPMFile(rpmFileReference);
53                  try {
54                      file.getSignature();
55                      fail("Should throw an exception");
56                  } catch (Exception e) {
57                      //ignore
58                  }
59                  try {
60                      file.getHeader();
61                      fail("Should throw an exception");
62                  } catch (Exception e) {
63                      //ignore
64                  }
65                  try {
66                      file.getTag("name");
67                      fail("Should throw an exception");
68                  } catch (Exception e) {
69                      //ignore
70                  }
71                  file.parse();
72  
73                  assertEquals(TOTAL_SIG_TAGIDS[i], file.getSignature()
74                          .getTagIds().length);
75                  assertEquals(TOTAL_SIG_TAGNAMES[i], file.getSignature()
76                          .getTagNames().length);
77                  assertEquals(TOTAL_HEAD_TAGIDS[i],
78                          file.getHeader().getTagIds().length);
79                  assertEquals(TOTAL_HEAD_TAGNAMES[i], file.getHeader()
80                          .getTagNames().length);
81                  assertEquals(NAME[i], file.getTag("name").toString());
82                  assertEquals(VENDOR[i], file.getTag("vendor").toString());
83                  assertEquals("cpio", file.getTag("PAYLOADFORMAT").toString());
84                  assertEquals("gzip", file.getTag("PAYLOADCOMPRESSOR")
85                          .toString());
86              }
87          } catch (Throwable e) {
88              logger.error("Unexpected error", e);
89              fail(e.getLocalizedMessage());
90          }
91      }
92  
93      /***
94       * TODO test file extraction
95       */
96      public void testFileExtraction() {
97          try {
98              for (int i = 0; i < RPM_FILES.length; i++) {
99                  String rpmFile = RPM_FILES[i];
100                 File rpmFileReference = getTestRPMFile(rpmFile);
101                 assertNotNull(rpmFileReference);
102                 RPMFile file = new RPMFile(rpmFileReference);
103                 //                File extractDir = new
104                 // File(File.separator+"_jrpm_"+File.separator+rpmFile);
105                 File extractDir = File.createTempFile("testCPIOArchive", null);
106                 extractDir.delete();
107                 extractDir.mkdirs();
108                 if (!extractDir.exists()) {
109                     extractDir.mkdirs();
110                 }
111                 //file.extract(extractDir);
112                 delete(extractDir);
113             }
114         } catch (Throwable e) {
115             logger.error("Unexpected error", e);
116             fail(e.getLocalizedMessage());
117         }
118     }
119 
120     private void delete(File fh) {
121         File[] list = fh.listFiles();
122         for (int i = 0; i < list.length; i++) {
123             File file = list[i];
124             System.out.println("> " + file);
125             if (file.isDirectory()) {
126                 delete(file);
127             } else {
128                 file.delete();
129             }
130         }
131         fh.delete();
132     }
133 
134     protected void setUp() throws Exception {
135         if (!Logger.getLogger("com.jguild.jrpm").getAllAppenders()
136                 .hasMoreElements()) {
137             Logger.getLogger("com.jguild.jrpm").addAppender(
138                     new ConsoleAppender(new SimpleLayout()));
139         }
140         super.setUp();
141     }
142 
143     protected void tearDown() throws Exception {
144         super.tearDown();
145     }
146 
147     /***
148      * Find a resource in the classpath.
149      * 
150      * @param name
151      *           The resource name
152      * @return An input stream pointing to the resource
153      * @see java.lang.ClassLoader#getResourceAsStream(String)
154      */
155     private File getTestRPMFile(String name) {
156         URL rpmUrl = NativeRPMTest.class.getClassLoader().getResource(name);
157         return new File(rpmUrl.getFile());
158     }
159 }