Source Code Cross Referenced for DiffUtil.java in  » J2EE » wicket » wicket » util » diff » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » wicket » wicket.util.diff 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: DiffUtil.java 6044 2006-06-04 20:00:30 +0000 (Sun, 04 Jun 2006) jannehietamaki $ $Revision: 6044 $
003:         * $Date: 2006-06-04 20:00:30 +0000 (Sun, 04 Jun 2006) $
004:         * 
005:         * ==================================================================== Licensed
006:         * under the Apache License, Version 2.0 (the "License"); you may not use this
007:         * file except in compliance with the License. You may obtain a copy of the
008:         * License at
009:         * 
010:         * http://www.apache.org/licenses/LICENSE-2.0
011:         * 
012:         * Unless required by applicable law or agreed to in writing, software
013:         * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014:         * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015:         * License for the specific language governing permissions and limitations under
016:         * the License.
017:         */
018:        package wicket.util.diff;
019:
020:        import java.io.FileOutputStream;
021:        import java.io.IOException;
022:        import java.io.InputStream;
023:        import java.io.PrintWriter;
024:        import java.net.URL;
025:
026:        import org.apache.commons.logging.Log;
027:        import org.apache.commons.logging.LogFactory;
028:
029:        import wicket.util.io.Streams;
030:        import wicket.util.string.StringList;
031:
032:        /**
033:         * This is a utility class. It serves two purposes.
034:         * <p>
035:         * First: compare a string output generated by wicket with a file's content
036:         * (expected result).
037:         * <p>
038:         * Second: Create/replace the expected result file with the new content, if a
039:         * system property has be made available like
040:         * -Dwicket.replace.expected.results=true
041:         * 
042:         * @author Juergen Donnerstag
043:         */
044:        public final class DiffUtil {
045:            private static final Log log = LogFactory.getLog(DiffUtil.class);
046:
047:            /**
048:             * Compare the output generated by Wicket ("document") with the a previously
049:             * generated file which contains the expected result.
050:             * 
051:             * @param document
052:             *            Current output
053:             * @param file
054:             *            Expected ouput
055:             * @param clazz
056:             *            Used to load the file (relativ to clazz package)
057:             * @return true, if equal
058:             * @throws IOException
059:             */
060:            public static final boolean validatePage(String document,
061:                    final Class clazz, final String file) throws IOException {
062:                String filename = clazz.getPackage().getName();
063:                filename = filename.replace('.', '/');
064:                filename += "/" + file;
065:
066:                InputStream in = clazz.getClassLoader().getResourceAsStream(
067:                        filename);
068:                if (in == null) {
069:                    throw new IOException("File not found: " + filename);
070:                }
071:
072:                String reference = Streams.readString(in);
073:
074:                // replace all line endings with unix style line ending
075:                reference = reference.replaceAll("\n\r", "\n");
076:                reference = reference.replaceAll("\r\n", "\n");
077:
078:                // replace all line endings with unix style line ending
079:                document = document.replaceAll("\n\r", "\n");
080:                document = document.replaceAll("\r\n", "\n");
081:
082:                boolean equals = document.equals(reference);
083:                if (equals == false) {
084:                    // Change the condition to true, if you want to make the new output
085:                    // the reference output for future tests. That is, it is regarded as
086:                    // correct. It'll replace the current reference files. Thus change
087:                    // it only for one test-run.
088:                    // -Dwicket.replace.expected.results=true
089:                    final String prop = System
090:                            .getProperty("wicket.replace.expected.results");
091:                    if (prop != null) {
092:                        in.close();
093:                        in = null;
094:
095:                        replaceExpectedResultFile(document, clazz, file);
096:                        return true;
097:                    }
098:
099:                    log.error("File name: " + file);
100:                    /*  */
101:                    log.error("===================");
102:                    log.error(reference);
103:                    log.error("===================");
104:
105:                    log.error(document);
106:                    log.error("===================");
107:                    /* */
108:
109:                    String[] test1 = StringList.tokenize(reference, "\n")
110:                            .toArray();
111:                    String[] test2 = StringList.tokenize(document, "\n")
112:                            .toArray();
113:                    Diff df = new Diff(test1);
114:                    Revision r;
115:                    try {
116:                        r = df.diff(test2);
117:                    } catch (DifferentiationFailedException e) {
118:                        throw new RuntimeException(e);
119:                    }
120:
121:                    //Diff diff = new Diff(test1, test2);
122:                    //Diff.change script = diff.diff_2(false);
123:                    //DiffPrint.Base p = new DiffPrint.UnifiedPrint(test1, test2);
124:                    //p.setOutput(new PrintWriter(System.err));
125:                    //p.print_script(script);
126:                    System.out.println(r.toString());
127:                }
128:
129:                return equals;
130:            }
131:
132:            /**
133:             * Replace the expected result file with the current output.
134:             * 
135:             * @param document
136:             *            How the expected result should look like
137:             * @param clazz
138:             *            Used to load the file (relativ to clazz package)
139:             * @param file
140:             *            The name of the expected result file to be created
141:             * @throws IOException
142:             */
143:            public final static void replaceExpectedResultFile(
144:                    final String document, final Class clazz, final String file)
145:                    throws IOException {
146:                String filename = clazz.getPackage().getName();
147:                filename = filename.replace('.', '/');
148:                filename += "/" + file;
149:
150:                final URL url = clazz.getClassLoader().getResource(filename);
151:                filename = url.getFile();
152:                filename = filename.replaceAll("/target/test-classes/",
153:                        "/src/test/java/");
154:                PrintWriter out = new PrintWriter(
155:                        new FileOutputStream(filename));
156:                out.print(document);
157:                out.close();
158:            }
159:
160:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.