001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.tools.common;
019:
020: import java.io.BufferedReader;
021: import java.io.File;
022: import java.io.FileInputStream;
023: import java.io.InputStream;
024: import java.io.InputStreamReader;
025: import java.net.URISyntaxException;
026: import java.net.URL;
027: import java.net.URLClassLoader;
028: import java.util.ArrayList;
029: import java.util.List;
030: import java.util.StringTokenizer;
031:
032: import org.apache.cxf.helpers.FileUtils;
033: import org.junit.After;
034: import org.junit.Assert;
035: import org.junit.Before;
036:
037: public class ProcessorTestBase extends Assert {
038:
039: protected ToolContext env = new ToolContext();
040: protected File output;
041:
042: @Before
043: public void setUp() throws Exception {
044: URL url = getClass().getResource(".");
045: output = new File(url.toURI());
046: output = new File(output, "/generated");
047: FileUtils.mkDir(output);
048: }
049:
050: @After
051: public void tearDown() {
052: FileUtils.removeDir(output);
053: output = null;
054: env = null;
055: }
056:
057: protected String getClassPath() throws URISyntaxException {
058: ClassLoader loader = getClass().getClassLoader();
059: StringBuffer classPath = new StringBuffer();
060: if (loader instanceof URLClassLoader) {
061: URLClassLoader urlLoader = (URLClassLoader) loader;
062: for (URL url : urlLoader.getURLs()) {
063: File file;
064: file = new File(url.toURI());
065: String filename = file.getAbsolutePath();
066: if (filename.indexOf("junit") == -1) {
067: classPath.append(filename);
068: classPath.append(System
069: .getProperty("path.separator"));
070: }
071: }
072: }
073: return classPath.toString();
074: }
075:
076: protected String getLocation(String wsdlFile)
077: throws URISyntaxException {
078: return getClass().getResource(wsdlFile).toString();
079: }
080:
081: protected void assertFileEquals(String f1, String f2) {
082: assertFileEquals(new File(f1), new File(f2));
083: }
084:
085: protected void assertFileEquals(File location1, File location2) {
086: String str1 = getStringFromFile(location1);
087: String str2 = getStringFromFile(location2);
088:
089: StringTokenizer st1 = new StringTokenizer(str1, " \t\n\r\f(),");
090: StringTokenizer st2 = new StringTokenizer(str2, " \t\n\r\f(),");
091:
092: // namespace declarations and wsdl message parts can be ordered
093: // differently in the generated wsdl between the ibm and sun jdks.
094: // So, when we encounter a mismatch, put the unmatched token in a
095: // list and check this list when matching subsequent tokens.
096: // It would be much better to do a proper xml comparison.
097: List<String> unmatched = new ArrayList<String>();
098: while (st1.hasMoreTokens()) {
099: String tok1 = st1.nextToken();
100: String tok2 = null;
101: if (unmatched.contains(tok1)) {
102: unmatched.remove(tok1);
103: continue;
104: }
105: while (st2.hasMoreTokens()) {
106: tok2 = st2.nextToken();
107:
108: if (tok1.equals(tok2)) {
109: break;
110: } else {
111: unmatched.add(tok2);
112: }
113: }
114: assertEquals("Compare failed "
115: + location1.getAbsolutePath() + " != "
116: + location2.getAbsolutePath(), tok1, tok2);
117: }
118:
119: assertTrue(!st1.hasMoreTokens());
120: assertTrue(!st2.hasMoreTokens());
121: assertTrue("Files did not match", unmatched.isEmpty());
122: }
123:
124: public String getStringFromFile(File location) {
125: InputStream is = null;
126: String result = null;
127:
128: try {
129: is = new FileInputStream(location);
130: result = normalizeCRLF(is);
131: } catch (Exception e) {
132: e.printStackTrace();
133: } finally {
134: if (is != null) {
135: try {
136: is.close();
137: } catch (Exception e) {
138: //do nothing
139: }
140: }
141: }
142:
143: return result;
144: }
145:
146: private String normalizeCRLF(InputStream instream) {
147: BufferedReader in = new BufferedReader(new InputStreamReader(
148: instream));
149: StringBuffer result = new StringBuffer();
150: String line = null;
151:
152: try {
153: line = in.readLine();
154: while (line != null) {
155: String[] tok = line.split("\\s");
156:
157: for (int x = 0; x < tok.length; x++) {
158: String token = tok[x];
159: result.append(" " + token);
160: }
161: line = in.readLine();
162: }
163: } catch (Exception ex) {
164: ex.printStackTrace();
165: }
166:
167: String rtn = result.toString();
168:
169: rtn = ignoreTokens(rtn, "<!--", "-->");
170: rtn = ignoreTokens(rtn, "/*", "*/");
171: return rtn;
172: }
173:
174: private String ignoreTokens(final String contents,
175: final String startToken, final String endToken) {
176: String rtn = contents;
177: int headerIndexStart = rtn.indexOf(startToken);
178: int headerIndexEnd = rtn.indexOf(endToken);
179: if (headerIndexStart != -1 && headerIndexEnd != -1
180: && headerIndexStart < headerIndexEnd) {
181: rtn = rtn.substring(0, headerIndexStart - 1)
182: + rtn.substring(headerIndexEnd + endToken.length()
183: + 1);
184: }
185: return rtn;
186: }
187: }
|