KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > test > setup > ContentLoaderTestCase


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.test.setup;
10
11 import junit.framework.TestCase;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15
16
17
18
19
20 /**
21  * @author <a HREF="mailto:palber@novell.com">Polina Alber</a>
22  * Date: May 2, 2005; Time: 4:30:38 PM
23  * @since JBoss portal 2.0
24  * Class org.jboss.portal.test.setup.ContentLoaderTestCase
25  */

26 public class ContentLoaderTestCase extends TestCase
27 {
28    public ContentLoaderTestCase (String JavaDoc name)
29    {
30       super(name);
31    }
32
33    public void test1() throws Exception JavaDoc
34    {
35       String JavaDoc file = Thread.currentThread().getContextClassLoader().getResource("content").getFile();
36       File JavaDoc content = new File JavaDoc(file);
37       if(content.isDirectory()) {
38          loadDirectory(content, null, false);
39       }
40       else {
41          loadFile(content, "content");
42       }
43    }
44    /**
45        * Read all content enetries from a directory tree.
46        * @param dir a directory
47        */

48       public boolean loadDirectory(File JavaDoc dir, String JavaDoc parentUri, boolean add) throws IOException JavaDoc {
49
50          //crete dir node
51
String JavaDoc nodeUri = null;
52          if(add) {
53             nodeUri = parentUri == null ? File.separatorChar + dir.getName() : parentUri + File.separator + dir.getName();
54          } else {
55             nodeUri = parentUri;
56          }
57
58          File JavaDoc[] files = dir.listFiles();
59          for ( int i=0; i<files.length; i++ ) {
60             if ( files[i].isDirectory() ) {
61                loadDirectory( files[i], nodeUri, true);
62             }
63             else {
64                loadFile( files[i], nodeUri);
65             }
66          }
67          return true;
68       }
69
70       private boolean loadFile(File JavaDoc file, String JavaDoc parentUri) throws IOException JavaDoc
71       {
72
73
74           if(parentUri == null) {
75              parentUri = "/files";
76           }
77           String JavaDoc name = file.getName();
78           String JavaDoc fileUri = parentUri + File.separatorChar + name;
79
80           String JavaDoc extension = name.substring(name.lastIndexOf('.'));
81
82          return true;
83
84       }
85
86 }
87
Popular Tags