Bug example in spring2.0 project: "Chinese questions not discussed"

Source: Internet
Author: User
Spring is a very good open-source project. However, like any other excellent system product, it also has such problems. We like to call it a bug. There are indeed a lot of bugs in spring. to enrich the topic "Chinese questions are not discussed" today, we will give a bug example that is not very important and easy to understand.
I declare in advance that this topic is not for the spring Project. Therefore, please be self-respecting and free from disturbing spring fans. You are welcome to criticize and advise any shortcomings in this article.
We know that the most basic requirement for a unit test for an open-source software project is to pass all tests. in Java, you should see a green bar when running the unit test. The spring project unit tests are well written and comprehensive. However, there are some problems in unit testing. The Chinese path cannot pass the test completely, and it must be placed in the English path to pass the test completely. Therefore, this is a Chinese Question of "no discussion.
   Unit test:
   Package:Org. springframework. Beans. Factory. xml
   Class:Xmlbeanfactorytests
   Method:Testfilesystemresourcewithimport Error illustration:
 

Detailed error message:
Org. springframework. beans. factory. beandefinitionstoreexception: ioexception parsing XML document from file [C: /Documents % 20and % 20 settings/Administrator/% E6 % a1 % 8C % E9 % 9d % A2/spring/spring-framework-2.0-rc2/bin/org/springframework/beans/Factory/XML/Resource. XML]; Nested exception is Java. io. filenotfoundexception: C:/Documents % 20and % 20 settings/Administrator/% E6 % a1 % 8C % E9 % 9d % A2/spring-framework-2.0 -RC2/bin/org/springframework/beans/Factory/XML/resource. XML (the system cannot find the specified path .)
Caused by: Java. io. filenotfoundexception: C: /Documents % 20and % 20 settings/Administrator/% E6 % a1 % 8C % E9 % 9d % A2/spring/spring-framework-2.0-rc2/bin/org/springframework/beans/Factory/XML/Resource. XML (the system cannot find the specified path .)
At java. Io. fileinputstream. Open (native method)
At java. Io. fileinputstream. <init> (fileinputstream. Java: 106)
At org. springframework. Core. Io. filesystemresource. getinputstream (filesystemresource. Java: 85)
At org. springframework. Beans. Factory. xml. xmlbeandefinitionreader. loadbeandefinitions (xmlbeandefinitionreader. Java: 334)
At org. springframework. Beans. Factory. xml. xmlbeandefinitionreader. loadbeandefinitions (xmlbeandefinitionreader. Java: 315)
At org. springframework. Beans. Factory. xml. xmlbeanfactory. <init> (xmlbeanfactory. Java: 73)
At org. springframework. Beans. Factory. xml. xmlbeanfactory. <init> (xmlbeanfactory. Java: 61)
At org. springframework. Beans. Factory. xml. xmlbeanfactorytests. testfilesystemresourcewithimport (xmlbeanfactorytests. Java: 946)
At sun. Reflect. nativemethodaccessorimpl. invoke0 (native method)
At sun. Reflect. nativemethodaccessorimpl. Invoke (nativemethodaccessorimpl. Java: 39)
At sun. Reflect. delegatingmethodaccessorimpl. Invoke (delegatingmethodaccessorimpl. Java: 25)
At java. Lang. Reflect. method. Invoke (method. Java: 585)
At JUnit. Framework. testcase. runtest (testcase. Java: 154)
At JUnit. Framework. testcase. runbare (testcase. Java: 127)
At JUnit. Framework. testresult $1. Protect (testresult. Java: 106)
At JUnit. Framework. testresult. runprotected (testresult. Java: 124)
At JUnit. Framework. testresult. Run (testresult. Java: 109)
At JUnit. Framework. testcase. Run (testcase. Java: 118)
At JUnit. Framework. testsuite. runtest (testsuite. Java: 208)
At JUnit. Framework. testsuite. Run (testsuite. Java: 203)
At org. Eclipse. jdt. Internal. JUnit. Runner. junit3.junit3testreference. Run (junit3testreference. Java: 128)
At org. Eclipse. jdt. Internal. JUnit. Runner. testexecution. Run (testexecution. Java: 38)
At org. Eclipse. jdt. Internal. JUnit. Runner. remotetestrunner. runtests (remotetestrunner. Java: 460)
At org. Eclipse. jdt. Internal. JUnit. Runner. remotetestrunner. runtests (remotetestrunner. Java: 673)
At org. Eclipse. jdt. Internal. JUnit. Runner. remotetestrunner. Run (remotetestrunner. Java: 386)
At org. Eclipse. jdt. Internal. JUnit. Runner. remotetestrunner. Main (remotetestrunner. Java: 196) test code:
Public void testfilesystemresourcewithimport (){
String file = getclass (). getresource ("resource. xml"). GetFile ();
Xmlbeanfactory xbf = new xmlbeanfactory (New filesystemresource (File ));
// Comes from "resourceimport. xml"
Resourcetestbean resource1 = (resourcetestbean) xbf. getbean ("resource1 ");
// Comes from "resource. xml"
Resourcetestbean resource2 = (resourcetestbean) xbf. getbean ("resource2 ");
} Error cause:
This problem is encountered by the author in the Development of easyjweb and easydbo frameworks, so it is easy to find the problem. Java class. getresource (name) returns a URL. by default, GetFile returns URL-encoded characters, which convert special characters such as Chinese into a format similar to % E6. Generally, Io constructor paths do not have the automatic decoding function. Therefore, an error occurs in the Chinese path. Solution:
Before using the path returned by URL. GetFile, you must use java.net. urldecoder to decode the path. The modified and tested method is as follows:
Public void testfilesystemresourcewithimport (){
String file = getclass (). getresource ("resource. xml"). GetFile ();
Try {
File = java.net. urldecoder. Decode (file, "UTF-8 ");
}
Catch (exception E)
{
E. printstacktrace ();
}
Xmlbeanfactory xbf = new xmlbeanfactory (New filesystemresource (File ));
// Comes from "resourceimport. xml"
Resourcetestbean resource1 = (resourcetestbean) xbf. getbean ("resource1 ");
// Comes from "resource. xml"
Resourcetestbean resource2 = (resourcetestbean) xbf. getbean ("resource2 ");
} Summary:
Among the development teams of the spring open-source project, apart from some "Spring fans" who like to sing the Chinese version following Uncle Rod's ass, at present, it seems that no Chinese people have been involved in the formal spring development team. Therefore, the test cases may not have been run in the Chinese path, so that spring beginners like me may encounter such problems accidentally.
The problem is solved, but it is a rare method, and the spring development team may not have previewed or intentionally ignored the problem in advance. Therefore, it can be called a "not discussed" Chinese question. (Note: The author of this article, easyjf open-source team Daxia, reposted the author's statement !)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.