KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > AxisFileGenTestBase


1 /*
2  * Copyright 2002-2004 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
17
18 package test;
19
20 import org.custommonkey.xmlunit.XMLTestCase;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 /**
30  * base class for Axis FileGen test cases.
31  */

32 public abstract class AxisFileGenTestBase extends AxisTestBase {
33
34     public AxisFileGenTestBase(String JavaDoc s) {
35         super(s);
36     }
37
38     protected String JavaDoc getPrefix(String JavaDoc parent) {
39         if (parent == null || parent.length() == 0) {
40             return "";
41         }
42         else {
43             return parent + File.separator;
44         }
45     }
46
47     abstract protected Set JavaDoc mayExist();
48     abstract protected String JavaDoc rootDir();
49     abstract protected Set JavaDoc shouldExist();
50
51     /** This method returns a array of String file paths, located within the
52      * supplied root directory. The string values are created relative to the
53      * specified parent so that the names get returned in the form of
54      * "file.java", "dir/file.java", "dir/dir/file.java", etc. This feature
55      * asslows the various file specs to include files in sub-directories as
56      * well as the root directory.
57      */

58     protected String JavaDoc[] getPaths(File JavaDoc root, String JavaDoc parent) {
59         File JavaDoc files[] = root.listFiles();
60         if (files == null)
61             fail("Unable to get a list of files from " + root.getPath());
62
63         Set JavaDoc filePaths = new HashSet JavaDoc();
64         for(int i=0; i<files.length; i++) {
65             if (files[i].isDirectory()) {
66                 String JavaDoc children[] = getPaths(files[i],
67                             getPrefix(parent) + files[i].getName());
68                 filePaths.addAll(Arrays.asList(children));
69             }
70             else {
71                 filePaths.add(getPrefix(parent) + files[i].getName());
72             }
73         }
74         String JavaDoc paths[] = new String JavaDoc[filePaths.size()];
75         return (String JavaDoc[]) filePaths.toArray(paths);
76     }
77
78
79     public void testFileGen() throws IOException JavaDoc {
80         String JavaDoc rootDir = rootDir();
81         Set JavaDoc shouldExist = shouldExist();
82         Set JavaDoc mayExist = mayExist();
83
84         // open up the output directory and check what files exist.
85
File JavaDoc outputDir = new File JavaDoc(rootDir);
86
87         String JavaDoc[] files = getPaths(outputDir, null);
88
89         Vector JavaDoc shouldNotExist = new Vector JavaDoc();
90
91         for (int i = 0; i < files.length; ++i) {
92             if (shouldExist.contains(files[i])) {
93                 shouldExist.remove(files[i]);
94             }
95             else if (mayExist.contains(files[i])) {
96                 mayExist.remove(files[i]);
97             }
98             else {
99                 shouldNotExist.add(files[i]);
100             }
101         }
102
103         if (shouldExist.size() > 0) {
104             fail("The following files should exist in " + rootDir +
105                 ", but do not: " + shouldExist);
106         }
107
108         if (shouldNotExist.size() > 0) {
109             fail("The following files should NOT exist in " + rootDir +
110                 ", but do: " + shouldNotExist);
111         }
112     }
113 }
114
Popular Tags