“中文问题没商量”之Spring2.0项目中的Bug一例

news/2023/12/9 16:34:49
  Spring是一个非常优秀的开源项目,然而,跟其它任何优秀的系统产品一样,也存在着这样那样的问题,我们喜欢称为Bug。Spring中的Bug确实不少,今天为了充实“中文问题没商量”主题,举一个不算很重要,也比较简单理解的一个Bug示例。
  这里提前申明,这个话题不是针对Spring项目,因此请“ 春迷”们自重、没事勿扰,文中不足之处欢迎大家批评指教。
  我们知道,一个开源软件项目,给用户的单元测试最基本的要求是能全部通过测试,在Java中就是在运行单元测试的时候应该要看见一个绿条。Spring项目的单元测试写得非常好,也非常全面。然而,单元测试中却有一些问题,在中文路径上无法完全通过测试,必须放到英文路径下才能完全通过测试,因此,这属于一种“没商量”的中文问题。
   单元测试:
   包:org.springframework.beans.factory.xml
   类:XmlBeanFactoryTests
   方法:testFileSystemResourceWithImport
   错误图示:
 


  详细错误信息:
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from file [C:/Documents%20and%20Settings/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%20Settings/Administrator/%e6%a1%8c%e9%9d%a2/spring/spring-framework-2.0-rc2/bin/org/springframework/beans/factory/xml/resource.xml (系统找不到指定的路径。)
Caused by: java.io.FileNotFoundException: C:/Documents%20and%20Settings/Administrator/%e6%a1%8c%e9%9d%a2/spring/spring-framework-2.0-rc2/bin/org/springframework/beans/factory/xml/resource.xml (系统找不到指定的路径。)
 at java.io.FileInputStream.open(Native Method)
 at java.io.FileInputStream. (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. (XmlBeanFactory.java:73)
 at org.springframework.beans.factory.xml.XmlBeanFactory. (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)

测试代码:
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");
 }
 
出错原因:
  这个问题是笔者在参与开发EasyJWeb及EasyDBO框架中遇到过的问题,因此很容易就找到了问题的所在。Java的Class.getResource(name)返回的是一个URL,而URL.getFile默认情况下返回的是经过URL编码后的字符,会把中文等特殊字符变成类似%e6的形式。而一般io构造路径是没有自动解码功能的,所以在中文路径下要出现错误。
 
解决办法:
  在使用URL.getFile返回的路径时,使用前需要使用java.net.URLDecoder对路径进行一次解码操作。修改后的且能通过测试的方法如下:
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");
 }
 
小结:
  由于Spring开源项目的开发团队中,除了一些喜欢跟在Rod大叔的屁股后面唱中文版赞歌的“ 春迷”以外,当前似乎还没有中国人参与到正式的Spring开发小组中。因此,也许没有在中文路径下运行过测试用例,导致我这样的Spring初学者一不小心就遇上这样的问题。
  问题是解决了,但是却是一种比较罕见的方式,而且Spring开发小组事先也许没有预想到或是故意忽略掉的问题,因此,可以称得上是“没商量”的中文问题。
   (注:本文作者, EasyJF开源团队  大峡,转载请保留作者声明!)
 


  Spring是一个非常优秀的开源项目,然而,跟其它任何优秀的系统产品一样,也存在着这样那样的问题,我们喜欢称为Bug。Spring中的Bug确实不少,今天为了充实“中文问题没商量”主题,举一个不算很重要,也比较简单理解的一个Bug示例。
  这里提前申明,这个话题不是针对Spring项目,因此请“ 春迷”们自重、没事勿扰,文中不足之处欢迎大家批评指教。
  我们知道,一个开源软件项目,给用户的单元测试最基本的要求是能全部通过测试,在Java中就是在运行单元测试的时候应该要看见一个绿条。Spring项目的单元测试写得非常好,也非常全面。然而,单元测试中却有一些问题,在中文路径上无法完全通过测试,必须放到英文路径下才能完全通过测试,因此,这属于一种“没商量”的中文问题。
   单元测试:
   包:org.springframework.beans.factory.xml
   类:XmlBeanFactoryTests
   方法:testFileSystemResourceWithImport
   错误图示:
 


  详细错误信息:
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from file [C:/Documents%20and%20Settings/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%20Settings/Administrator/%e6%a1%8c%e9%9d%a2/spring/spring-framework-2.0-rc2/bin/org/springframework/beans/factory/xml/resource.xml (系统找不到指定的路径。)
Caused by: java.io.FileNotFoundException: C:/Documents%20and%20Settings/Administrator/%e6%a1%8c%e9%9d%a2/spring/spring-framework-2.0-rc2/bin/org/springframework/beans/factory/xml/resource.xml (系统找不到指定的路径。)
 at java.io.FileInputStream.open(Native Method)
 at java.io.FileInputStream. (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. (XmlBeanFactory.java:73)
 at org.springframework.beans.factory.xml.XmlBeanFactory. (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)

测试代码:
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");
 }
 
出错原因:
  这个问题是笔者在参与开发EasyJWeb及EasyDBO框架中遇到过的问题,因此很容易就找到了问题的所在。Java的Class.getResource(name)返回的是一个URL,而URL.getFile默认情况下返回的是经过URL编码后的字符,会把中文等特殊字符变成类似%e6的形式。而一般io构造路径是没有自动解码功能的,所以在中文路径下要出现错误。
 
解决办法:
  在使用URL.getFile返回的路径时,使用前需要使用java.net.URLDecoder对路径进行一次解码操作。修改后的且能通过测试的方法如下:
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");
 }
 
小结:
  由于Spring开源项目的开发团队中,除了一些喜欢跟在Rod大叔的屁股后面唱中文版赞歌的“ 春迷”以外,当前似乎还没有中国人参与到正式的Spring开发小组中。因此,也许没有在中文路径下运行过测试用例,导致我这样的Spring初学者一不小心就遇上这样的问题。
  问题是解决了,但是却是一种比较罕见的方式,而且Spring开发小组事先也许没有预想到或是故意忽略掉的问题,因此,可以称得上是“没商量”的中文问题。
   (注:本文作者, EasyJF开源团队  大峡,转载请保留作者声明!)
 


  Spring是一个非常优秀的开源项目,然而,跟其它任何优秀的系统产品一样,也存在着这样那样的问题,我们喜欢称为Bug。Spring中的Bug确实不少,今天为了充实“中文问题没商量”主题,举一个不算很重要,也比较简单理解的一个Bug示例。
  这里提前申明,这个话题不是针对Spring项目,因此请“ 春迷”们自重、没事勿扰,文中不足之处欢迎大家批评指教。
  我们知道,一个开源软件项目,给用户的单元测试最基本的要求是能全部通过测试,在Java中就是在运行单元测试的时候应该要看见一个绿条。Spring项目的单元测试写得非常好,也非常全面。然而,单元测试中却有一些问题,在中文路径上无法完全通过测试,必须放到英文路径下才能完全通过测试,因此,这属于一种“没商量”的中文问题。
   单元测试:
   包:org.springframework.beans.factory.xml
   类:XmlBeanFactoryTests
   方法:testFileSystemResourceWithImport
   错误图示:
 


  详细错误信息:
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from file [C:/Documents%20and%20Settings/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%20Settings/Administrator/%e6%a1%8c%e9%9d%a2/spring/spring-framework-2.0-rc2/bin/org/springframework/beans/factory/xml/resource.xml (系统找不到指定的路径。)
Caused by: java.io.FileNotFoundException: C:/Documents%20and%20Settings/Administrator/%e6%a1%8c%e9%9d%a2/spring/spring-framework-2.0-rc2/bin/org/springframework/beans/factory/xml/resource.xml (系统找不到指定的路径。)
 at java.io.FileInputStream.open(Native Method)
 at java.io.FileInputStream. (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. (XmlBeanFactory.java:73)
 at org.springframework.beans.factory.xml.XmlBeanFactory. (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)

测试代码:
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");
 }
 
出错原因:
  这个问题是笔者在参与开发EasyJWeb及EasyDBO框架中遇到过的问题,因此很容易就找到了问题的所在。Java的Class.getResource(name)返回的是一个URL,而URL.getFile默认情况下返回的是经过URL编码后的字符,会把中文等特殊字符变成类似%e6的形式。而一般io构造路径是没有自动解码功能的,所以在中文路径下要出现错误。
 
解决办法:
  在使用URL.getFile返回的路径时,使用前需要使用java.net.URLDecoder对路径进行一次解码操作。修改后的且能通过测试的方法如下:
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");
 }
 
小结:
  由于Spring开源项目的开发团队中,除了一些喜欢跟在Rod大叔的屁股后面唱中文版赞歌的“ 春迷”以外,当前似乎还没有中国人参与到正式的Spring开发小组中。因此,也许没有在中文路径下运行过测试用例,导致我这样的Spring初学者一不小心就遇上这样的问题。
  问题是解决了,但是却是一种比较罕见的方式,而且Spring开发小组事先也许没有预想到或是故意忽略掉的问题,因此,可以称得上是“没商量”的中文问题。
   (注:本文作者, EasyJF开源团队  大峡,转载请保留作者声明!)
 



http://www.niftyadmin.cn/n/3647942.html

相关文章

如何在Ubuntu 18.04上安装ERPNext堆栈

The author selected Software in the Public Interest to receive a donation as part of the Write for DOnations program. 作者选择了符合公共利益的软件来接受捐赠,这是Write for DOnations计划的一部分。 介绍 (Introduction) ERPNext is an Enterprise Reso…

在一个开源项目的开发中遇到的问题

记得以前曾经有网友跟我们讨论有关EasyJWeb的效率问题,大致意思是EasyJWeb用得少,没有经过成熟项目的考验,而struts应用案例比较多,所以用起更放心。我的看法是,EasyJWeb只是一个MVC框架,在一个J2EE应用中&…

如何在Ubuntu 18.04上连接到托管数据库

介绍 (Introduction) Managed databases have a number of benefits over self-managed databases, including automated updates, simplified scaling, and high availability. If you’re new to working with managed databases, though, the best way to perform certain t…

easyjweb-0.7.0版本发布

Easyjweb-0.7在easyjweb-0.6的版本上做了一些改进:主要的功能改变有:1, 去掉了0.6版本比较难于使用的intercpetor,增加了在执行Action前后的intercptor;2, 增加了主题功能的支持;3&#xff0c…

apache 配置错误信息_Apache配置错误AH00526:语法错误

apache 配置错误信息Common Apache Errors 常见的Apache错误This tutorial series explains how to troubleshoot and fix some of the most common errors that you may encounter when using the Apache web server. 本教程系列说明了如何解决和修复使用Apache Web服务器时可…

简易Java框架开源论坛系统0.5.0版本发布

在stef_wu、大峡、williamRaym、天意等成员的努力下,在中秋国庆国际来临之际,EasyJF对开源论坛系统作了比较大的调整,并推出了一个专用于测试该论坛系统的网站“中国程序员八卦娱乐”,欢迎广大的开源爱好者下载交流。本次论坛调整…

javascript 模板_了解JavaScript中的模板文字

javascript 模板The author selected the COVID-19 Relief Fund to receive a donation as part of the Write for DOnations program. 作者选择了COVID-19救济基金来接受捐赠,这是Write for DOnations计划的一部分。 介绍 (Introduction) The 2015 edition of the…

“Spring轮子血案”续集1-“回应”与“反回应”

前段时间写了《一个Spring轮子的血案》的故事,引起很多“春迷”的反响,并积极对此事件作出了回应。这里大峡根据一些回应,作一个简单的反回应。 回应: 大峡把这样的ID引为知己,加为好友,实在..... http:/…

angular 渐进_如何使用Angular构建渐进式Web应用

angular 渐进介绍 (Introduction) Progressive web apps are web applications built with technologies that make them behave like native apps. A benefit of progressive web apps is the ability to work smoothly when network coverage is unreliable. Also, unlike na…

国内开源环境的感悟-Spring轮子血案总结

电影无间道系列中的《终极无间》里面陈道明说了一句话:“从来都是事情改变人,人改变不了事。”换个说法就是“只有环境改变人,人改变不了环境,环境可以造就一个人”。我想,通过此次“Spring轮子血案”事件,…

react 自定义控件使用_如何使用React构建自定义的拨动开关

react 自定义控件使用介绍 (Introduction) Building web applications usually involves making provisions for user interactions. One of the significant ways of making provision for user interactions is through forms. Different form components exist for taking d…

Spring MVC的流程图,欢迎指正

最近在写Spring MVC的介绍,下面是一本人绘制的一幅图,欢迎“春迷”及Spring高手来指正,其中省略了MultipartResolver。最近在写Spring MVC的介绍,下面是一本人绘制的一幅图,欢迎“春迷”及Spring高手来指正&#xff0c…

如何使用HTML将Favicon添加到您的网站

How To Build a Website With HTML 如何使用HTML构建网站This tutorial series will guide you through creating and further customizing this website using HTML, the standard markup language used to display documents in a web browser. No prior coding experience i…

Spring+EasyJWeb+iBatis版j2ee在线购物jpetstore系统源码发布

SpringEasyJWebiBatis版j2ee在线购物jpetstore系统由EasyJF团队开发,业务层及持久层使用springframework上的jpetsotre源码,表示层使用EasyJWeb,该系统是一个简单的J2EE网上商店系统,包括商品列表、购物车、用户管理、在线订单等功…

在DigitalOcean上创建经过身份验证的API

视频 (Video) 关于谈话 (About the Talk) We create an API that has authentication and authorization so that certain resources are only accessible by certain users. 我们创建具有身份验证和授权的API,以便某些资源只能由某些用户访问。 您将学到什么 (Wha…

超轻量级ORM系统EasyDBO-0.6.0版本发布

EasyDBO是一个超轻量级对象-关系映射(Object/Relation Mapping,简称ORM)系统,由国内的EasyJF开源团队中的EasyDBO项目组开发及维护,主要解决关系数据库系统中表数据与对象的自动映射,当前支持My SQL、MS SQ…
最新文章