Source Code Cross Referenced for StringAsserts.java in  » IDE-Eclipse » jdt » org » eclipse » jdt » testplugin » 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 » IDE Eclipse » jdt » org.eclipse.jdt.testplugin 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*******************************************************************************
002:         * Copyright (c) 2000, 2005 IBM Corporation and others.
003:         * All rights reserved. This program and the accompanying materials
004:         * are made available under the terms of the Eclipse Public License v1.0
005:         * which accompanies this distribution, and is available at
006:         * http://www.eclipse.org/legal/epl-v10.html
007:         *
008:         * Contributors:
009:         *     IBM Corporation - initial API and implementation
010:         *******************************************************************************/package org.eclipse.jdt.testplugin;
011:
012:        import java.io.BufferedReader;
013:        import java.io.IOException;
014:        import java.io.StringReader;
015:        import java.util.ArrayList;
016:        import java.util.Arrays;
017:
018:        import junit.framework.Assert;
019:
020:        /**
021:         *
022:         */
023:        public class StringAsserts {
024:            /**
025:             * 
026:             */
027:            public StringAsserts() {
028:                super ();
029:            }
030:
031:            private static int getDiffPos(String str1, String str2) {
032:                int len1 = Math.min(str1.length(), str2.length());
033:
034:                int diffPos = -1;
035:                for (int i = 0; i < len1; i++) {
036:                    if (str1.charAt(i) != str2.charAt(i)) {
037:                        diffPos = i;
038:                        break;
039:                    }
040:                }
041:                if (diffPos == -1 && str1.length() != str2.length()) {
042:                    diffPos = len1;
043:                }
044:                return diffPos;
045:            }
046:
047:            private static final int printRange = 6;
048:
049:            public static void assertEqualString(String actual, String expected) {
050:                if (actual == null || expected == null) {
051:                    if (actual == expected) {
052:                        return;
053:                    }
054:                    if (actual == null) {
055:                        Assert.assertTrue(
056:                                "Content not as expected: is 'null' expected: "
057:                                        + expected, false);
058:                    } else {
059:                        Assert.assertTrue(
060:                                "Content not as expected: expected 'null' is: "
061:                                        + actual, false);
062:                    }
063:                }
064:
065:                int diffPos = getDiffPos(actual, expected);
066:                if (diffPos != -1) {
067:                    int diffAhead = Math.max(0, diffPos - printRange);
068:                    int diffAfter = Math.min(actual.length(), diffPos
069:                            + printRange);
070:
071:                    String diffStr = actual.substring(diffAhead, diffPos) + '^'
072:                            + actual.substring(diffPos, diffAfter);
073:
074:                    // use detailed message
075:                    String message = "Content not as expected: is\n" + actual + "\nDiffers at pos " + diffPos + ": " + diffStr + "\nexpected:\n" + expected; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
076:
077:                    Assert.assertEquals(message, expected, actual);
078:                }
079:            }
080:
081:            public static void assertEqualStringIgnoreDelim(String actual,
082:                    String expected) throws IOException {
083:                if (actual == null || expected == null) {
084:                    if (actual == expected) {
085:                        return;
086:                    }
087:                    if (actual == null) {
088:                        Assert.assertTrue(
089:                                "Content not as expected: is 'null' expected: "
090:                                        + expected, false);
091:                    } else {
092:                        Assert.assertTrue(
093:                                "Content not as expected: expected 'null' is: "
094:                                        + actual, false);
095:                    }
096:                }
097:
098:                BufferedReader read1 = new BufferedReader(new StringReader(
099:                        actual));
100:                BufferedReader read2 = new BufferedReader(new StringReader(
101:                        expected));
102:
103:                int line = 1;
104:                do {
105:                    String s1 = read1.readLine();
106:                    String s2 = read2.readLine();
107:
108:                    if (s1 == null || !s1.equals(s2)) {
109:                        if (s1 == null && s2 == null) {
110:                            return;
111:                        }
112:                        String diffStr = (s1 == null) ? s2 : s1;
113:
114:                        String message = "Content not as expected: Content is: \n"
115:                                + actual
116:                                + "\nDiffers at line "
117:                                + line
118:                                + ": "
119:                                + diffStr
120:                                + "\nExpected contents: \n"
121:                                + expected;
122:                        Assert.assertEquals(message, expected, actual);
123:                    }
124:                    line++;
125:                } while (true);
126:            }
127:
128:            public static void assertEqualStringsIgnoreOrder(String[] actuals,
129:                    String[] expecteds) {
130:                ArrayList list1 = new ArrayList(Arrays.asList(actuals));
131:                ArrayList list2 = new ArrayList(Arrays.asList(expecteds));
132:
133:                for (int i = list1.size() - 1; i >= 0; i--) {
134:                    if (list2.remove(list1.get(i))) {
135:                        list1.remove(i);
136:                    }
137:                }
138:
139:                int n1 = list1.size();
140:                int n2 = list2.size();
141:
142:                if (n1 + n2 > 0) {
143:                    if (n1 == 1 && n2 == 1) {
144:                        assertEqualString((String) list1.get(0), (String) list2
145:                                .get(0));
146:                    }
147:
148:                    StringBuffer buf = new StringBuffer();
149:                    for (int i = 0; i < n1; i++) {
150:                        String s1 = (String) list1.get(i);
151:                        if (s1 != null) {
152:                            buf.append(s1);
153:                            buf.append("\n");
154:                        }
155:                    }
156:                    String actual = buf.toString();
157:
158:                    buf = new StringBuffer();
159:                    for (int i = 0; i < n2; i++) {
160:                        String s2 = (String) list2.get(i);
161:                        if (s2 != null) {
162:                            buf.append(s2);
163:                            buf.append("\n");
164:                        }
165:                    }
166:                    String expected = buf.toString();
167:
168:                    String message = "Content not as expected: Content is: \n"
169:                            + actual + "\nExpected contents: \n" + expected;
170:                    Assert.assertEquals(message, expected, actual);
171:                }
172:            }
173:
174:            public static void assertExpectedExistInProposals(String[] actuals,
175:                    String[] expecteds) {
176:                ArrayList list1 = new ArrayList(Arrays.asList(actuals));
177:                ArrayList list2 = new ArrayList(Arrays.asList(expecteds));
178:
179:                for (int i = list1.size() - 1; i >= 0; i--) {
180:                    if (list2.remove(list1.get(i))) {
181:                        list1.remove(i);
182:                    }
183:                }
184:
185:                int n1 = list1.size();
186:                int n2 = list2.size();
187:
188:                if (n2 > 0) {
189:                    if (n1 == 1 && n2 == 1) {
190:                        assertEqualString((String) list1.get(0), (String) list2
191:                                .get(0));
192:                    }
193:
194:                    StringBuffer buf = new StringBuffer();
195:                    for (int i = 0; i < n1; i++) {
196:                        String s1 = (String) list1.get(i);
197:                        if (s1 != null) {
198:                            buf.append(s1);
199:                            buf.append("\n");
200:                        }
201:                    }
202:                    String actual = buf.toString();
203:
204:                    buf = new StringBuffer();
205:                    for (int i = 0; i < n2; i++) {
206:                        String s2 = (String) list2.get(i);
207:                        if (s2 != null) {
208:                            buf.append(s2);
209:                            buf.append("\n");
210:                        }
211:                    }
212:                    String expected = buf.toString();
213:
214:                    String message = "Content not as expected: Content is: \n"
215:                            + actual + "\nExpected contents: \n" + expected;
216:                    Assert.assertEquals(message, expected, actual);
217:                }
218:            }
219:
220:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.