KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > wsdl > groups > GroupsTestCase


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 Steve Green (steve.green@epok.net)
21  */

22 package test.wsdl.groups;
23
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Set JavaDoc;
30
31
32 public class GroupsTestCase extends junit.framework.TestCase {
33     public GroupsTestCase(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("GroupsTestCase.java");
43         set.add("SomeType.java");
44         return set;
45     }
46     
47     /**
48      * List of files which may or may not be generated.
49      */

50     protected Set JavaDoc shouldNotExist() {
51         HashSet JavaDoc set = new HashSet JavaDoc();
52         set.add("SomeGroup.java");
53         return set;
54     }
55
56     /**
57      * The directory containing the files that should exist.
58      */

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

81     protected String JavaDoc[] getPaths(File JavaDoc root, String JavaDoc parent) {
82         File JavaDoc files[] = root.listFiles();
83         if (files == null)
84             fail("Unable to get a list of files from " + root.getPath());
85
86         Set JavaDoc filePaths = new HashSet JavaDoc();
87         for(int i=0; i<files.length; i++) {
88             if (files[i].isDirectory()) {
89                 String JavaDoc children[] = getPaths(files[i],
90                             getPrefix(parent) + files[i].getName());
91                 filePaths.addAll(Arrays.asList(children));
92             }
93             else {
94                 filePaths.add(getPrefix(parent) + files[i].getName());
95             }
96         }
97         String JavaDoc paths[] = new String JavaDoc[filePaths.size()];
98         return (String JavaDoc[]) filePaths.toArray(paths);
99     }
100
101     
102     public void testGroups() throws IOException JavaDoc, ClassNotFoundException JavaDoc, SecurityException JavaDoc, NoSuchMethodException JavaDoc {
103
104         // Test for the proper files
105

106         String JavaDoc rootDir = rootDir();
107         Set JavaDoc shouldExist = shouldExist();
108         Set JavaDoc shouldNotExist = shouldNotExist();
109
110         // open up the output directory and check what files exist.
111
File JavaDoc outputDir = new File JavaDoc(rootDir);
112         
113         String JavaDoc[] files = getPaths(outputDir, null);
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 (shouldNotExist.contains(files[i])) {
120                 fail("The following file should not exist in " + rootDir +
121             ", but does: " + files[i]);
122             }
123         }
124
125         if (shouldExist.size() > 0) {
126             fail("The following files should exist in " + rootDir +
127                 ", but do not: " + shouldExist);
128         }
129
130         // Test for the proper members
131

132         Class JavaDoc ourClass = Class.forName("test.wsdl.groups.SomeType");
133         ourClass.getDeclaredMethod("getA", null);
134         ourClass.getDeclaredMethod("getB", null);
135         ourClass.getDeclaredMethod("getZ", null);
136
137         return;
138     }
139 }
140
141
Popular Tags