KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > wsdl > filegen > FileGenTestCase


1 /*
2  * Copyright 2001-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  * This tests the file generation of only the items that are referenced in WSDL
19  *
20  * @author Tom Jordahl (tomj@macromedia.com)
21  */

22 package test.wsdl.filegen;
23
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.Vector JavaDoc;
30
31
32 public class FileGenTestCase extends junit.framework.TestCase {
33     public FileGenTestCase(String JavaDoc name) {
34         super(name);
35     }
36
37     /**
38      * List of files which should be generated.
39      */

40     protected Set JavaDoc shouldExist() {
41         HashSet JavaDoc set = new HashSet JavaDoc();
42         set.add("AllOptionTestCase.java");
43         set.add("FileGenTestCase.java");
44         set.add("OpFault.java");
45         set.add("PortTypeSoap.java");
46         set.add("ReferenceService.java");
47         set.add("ReferenceServiceLocator.java");
48         set.add("ReferenceSoapBindingStub.java");
49         return set;
50     }
51     
52     /**
53      * List of files which may or may not be generated.
54      */

55     protected Set JavaDoc mayExist() {
56         HashSet JavaDoc set = new HashSet JavaDoc();
57         return set;
58     }
59
60     /**
61      * The directory containing the files that should exist.
62      */

63     protected String JavaDoc rootDir() {
64         return "build" + File.separator + "work" + File.separator +
65                 "test" + File.separator + "wsdl" + File.separator +
66                 "filegen";
67     }
68     
69     protected String JavaDoc getPrefix(String JavaDoc parent) {
70         if (parent == null || parent.length() == 0) {
71             return "";
72         }
73         else {
74             return parent + File.separator;
75         }
76     }
77
78     /** This method returns a array of String file paths, located within the
79      * supplied root directory. The string values are created relative to the
80      * specified parent so that the names get returned in the form of
81      * "file.java", "dir/file.java", "dir/dir/file.java", etc. This feature
82      * asslows the various file specs to include files in sub-directories as
83      * well as the root directory.
84      */

85     protected String JavaDoc[] getPaths(File JavaDoc root, String JavaDoc parent) {
86         File JavaDoc files[] = root.listFiles();
87         Set JavaDoc filePaths = new HashSet JavaDoc();
88         for(int i=0; i<files.length; i++) {
89             if (files[i].isDirectory()) {
90                 String JavaDoc children[] = getPaths(files[i],
91                             getPrefix(parent) + files[i].getName());
92                 filePaths.addAll(Arrays.asList(children));
93             }
94             else {
95                 filePaths.add(getPrefix(parent) + files[i].getName());
96             }
97         }
98         String JavaDoc paths[] = new String JavaDoc[filePaths.size()];
99         return (String JavaDoc[]) filePaths.toArray(paths);
100     }
101
102     
103     public void testFileGen() throws IOException JavaDoc {
104         String JavaDoc rootDir = rootDir();
105         Set JavaDoc shouldExist = shouldExist();
106         Set JavaDoc mayExist = mayExist();
107
108         // open up the output directory and check what files exist.
109
File JavaDoc outputDir = new File JavaDoc(rootDir);
110         
111         String JavaDoc[] files = getPaths(outputDir, null);
112
113         Vector JavaDoc shouldNotExist = new Vector JavaDoc();
114
115         for (int i = 0; i < files.length; ++i) {
116             if (shouldExist.contains(files[i])) {
117                 shouldExist.remove(files[i]);
118             }
119             else if (mayExist.contains(files[i])) {
120                 mayExist.remove(files[i]);
121             }
122             else {
123                 shouldNotExist.add(files[i]);
124             }
125         }
126
127         if (shouldExist.size() > 0) {
128             fail("The following files should exist in " + rootDir +
129                 ", but do not: " + shouldExist);
130         }
131
132         if (shouldNotExist.size() > 0) {
133             fail("The following files should NOT exist in " + rootDir +
134                 ", but do: " + shouldNotExist);
135         }
136     }
137 }
138
139
Popular Tags