Source Code Cross Referenced for ClasspathResourceTest.java in  » Library » Tapestry » org » apache » tapestry » ioc » internal » util » 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 » Library » Tapestry » org.apache.tapestry.ioc.internal.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // Copyright 2006 The Apache Software Foundation
002:        //
003:        // Licensed under the Apache License, Version 2.0 (the "License");
004:        // you may not use this file except in compliance with the License.
005:        // You may obtain a copy of the License at
006:        //
007:        //     http://www.apache.org/licenses/LICENSE-2.0
008:        //
009:        // Unless required by applicable law or agreed to in writing, software
010:        // distributed under the License is distributed on an "AS IS" BASIS,
011:        // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012:        // See the License for the specific language governing permissions and
013:        // limitations under the License.
014:
015:        package org.apache.tapestry.ioc.internal.util;
016:
017:        import java.io.BufferedInputStream;
018:        import java.io.InputStream;
019:        import java.io.InputStreamReader;
020:        import java.io.Reader;
021:        import java.net.URL;
022:        import java.util.Locale;
023:
024:        import org.apache.tapestry.ioc.Resource;
025:        import org.testng.Assert;
026:        import org.testng.annotations.Test;
027:
028:        public class ClasspathResourceTest extends Assert {
029:            private static final String RESOURCE_TXT_CONTENT = "content from resource.txt";
030:
031:            private static final String FOLDER = "org/apache/tapestry/ioc/internal/util";
032:
033:            private static final String PATH = FOLDER + "/resource.txt";
034:
035:            @Test
036:            public void get_resource_URL() throws Exception {
037:                Resource r = new ClasspathResource(PATH);
038:
039:                assertEquals(content(r), RESOURCE_TXT_CONTENT);
040:            }
041:
042:            @Test
043:            public void relative_to_root_resource() throws Exception {
044:                Resource r = new ClasspathResource("").forFile(PATH);
045:
046:                assertEquals(content(r), RESOURCE_TXT_CONTENT);
047:            }
048:
049:            @Test
050:            public void relative_to_root_resource_using_leading_slash()
051:                    throws Exception {
052:                Resource r = new ClasspathResource("/").forFile(PATH);
053:
054:                assertEquals(content(r), RESOURCE_TXT_CONTENT);
055:            }
056:
057:            @Test
058:            public void leading_slash_on_path_relative_to_root_doesnt_matter()
059:                    throws Exception {
060:                Resource r = new ClasspathResource("/").forFile("/" + PATH);
061:
062:                assertEquals(content(r), RESOURCE_TXT_CONTENT);
063:            }
064:
065:            @Test
066:            public void path_and_file() {
067:                Resource r = new ClasspathResource(PATH);
068:
069:                assertEquals(r.getFolder(), FOLDER);
070:                assertEquals(r.getFile(), "resource.txt");
071:
072:            }
073:
074:            @Test
075:            public void for_file_in_same_folder() throws Exception {
076:                Resource r = new ClasspathResource(PATH);
077:
078:                Resource n = r.forFile("same-folder.txt");
079:
080:                assertEquals(content(n), "content from same-folder resource");
081:            }
082:
083:            @Test
084:            public void for_file_single_dot() throws Exception {
085:                Resource r = new ClasspathResource(PATH);
086:
087:                Resource n = r.forFile("./same-folder.txt");
088:
089:                assertEquals(content(n), "content from same-folder resource");
090:            }
091:
092:            @Test
093:            public void multiple_slashes_treated_as_single_slash()
094:                    throws Exception {
095:                Resource r = new ClasspathResource(PATH);
096:
097:                Resource n = r.forFile("././/.///same-folder.txt");
098:
099:                assertEquals(content(n), "content from same-folder resource");
100:            }
101:
102:            @Test
103:            public void for_file_in_subfolder() throws Exception {
104:                Resource r = new ClasspathResource(PATH);
105:
106:                Resource n = r.forFile("sub/sub-folder.txt");
107:
108:                assertEquals(content(n), "content from sub-folder resource");
109:            }
110:
111:            @Test
112:            public void for_file_same_resource() throws Exception {
113:                Resource r = new ClasspathResource(PATH);
114:
115:                assertSame(r.forFile("../util/resource.txt"), r);
116:            }
117:
118:            @Test
119:            public void for_file_in_parent_folder() throws Exception {
120:                Resource r = new ClasspathResource(PATH);
121:
122:                Resource n = r.forFile("../parent-folder.txt");
123:
124:                assertEquals(content(n), "content from parent-folder resource");
125:            }
126:
127:            @Test
128:            public void to_string() throws Exception {
129:                Resource r = new ClasspathResource(PATH);
130:
131:                assertEquals(r.toString(), "classpath:" + PATH);
132:            }
133:
134:            @Test
135:            public void get_URL_for_missing_resource() throws Exception {
136:                Resource r = new ClasspathResource(FOLDER
137:                        + "/missing-resource.txt");
138:
139:                assertNull(r.toURL());
140:            }
141:
142:            @Test
143:            public void localization_of_resource() throws Exception {
144:                Resource r = new ClasspathResource(PATH);
145:
146:                Resource l = r.forLocale(Locale.FRENCH);
147:
148:                assertEquals(content(l), "french content");
149:            }
150:
151:            @Test
152:            public void localization_to_closest_match() throws Exception {
153:                Resource r = new ClasspathResource(PATH);
154:
155:                Resource l = r.forLocale(Locale.CANADA_FRENCH);
156:
157:                assertEquals(content(l), "french content");
158:            }
159:
160:            @Test
161:            public void localization_to_base_resource() throws Exception {
162:                Resource r = new ClasspathResource(PATH);
163:
164:                Resource l = r.forLocale(Locale.JAPANESE);
165:
166:                assertSame(l, r);
167:            }
168:
169:            @Test
170:            public void with_extension_same_extension() {
171:                Resource r = new ClasspathResource(PATH);
172:
173:                assertSame(r.withExtension("txt"), r);
174:            }
175:
176:            @Test
177:            public void with_extension() throws Exception {
178:                Resource r = new ClasspathResource(PATH);
179:                Resource e = r.withExtension("ext");
180:
181:                assertEquals(content(e), "ext content");
182:            }
183:
184:            @Test
185:            public void with_extension_adds_extension() throws Exception {
186:                Resource r = new ClasspathResource(FOLDER + "/resource");
187:                Resource e = r.withExtension("ext");
188:
189:                assertEquals(content(e), "ext content");
190:            }
191:
192:            @Test
193:            public void with_extension_missing_resource_is_null() {
194:                Resource r = new ClasspathResource(PATH);
195:                Resource e = r.withExtension("does-not-exist");
196:
197:                assertNull(e.toURL());
198:            }
199:
200:            @Test
201:            public void localization_of_missing_resource() throws Exception {
202:                Resource r = new ClasspathResource(FOLDER
203:                        + "/missing-resource.txt");
204:
205:                assertNull(r.forLocale(Locale.FRENCH));
206:            }
207:
208:            private String content(Resource resource) throws Exception {
209:                return content(resource.toURL());
210:            }
211:
212:            private String content(URL url) throws Exception {
213:                InputStream is = new BufferedInputStream(url.openStream());
214:                Reader r = new InputStreamReader(is);
215:
216:                StringBuilder builder = new StringBuilder();
217:                char[] buffer = new char[2000];
218:
219:                while (true) {
220:                    int length = r.read(buffer, 0, buffer.length);
221:
222:                    if (length < 0)
223:                        break;
224:
225:                    builder.append(buffer, 0, length);
226:                }
227:
228:                r.close();
229:
230:                return builder.toString().trim();
231:            }
232:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.