2012-05-03 68 views
4

我们设法在eclipse中使用JBehave创建并运行带有国际化故事的测试。 一切都很好。Maven JBehave:编码故事UTF8

但是,当我们试图使用maven插件运行它们时,我们无法得到编码问题(例如,而不是从故事中读取“scénario”,它会得到“Scénario”:显然是UTF8编码问题)。

有人找到一种方法让JBehave使用maven插件读取UTF8中的故事吗?

我们已经尝试过:

  • 加入-Dfile.encoding = UTF8选项
  • 改变关键字文件使用UTF8
  • 在ISO改变整个项目编码=>这工作但是,这不是”吨适于生产部分需要在UTF8显示消息

我们的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
... 

<properties> 
    <jbehave.version>3.6.5</jbehave.version> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <resource.encoding>UTF-8</resource.encoding> 
</properties> 

<build> 

    <testOutputDirectory>target/classes</testOutputDirectory> 
    <testResources> 
     <testResource> 
      <directory>src/test/resources</directory> 
     </testResource> 
     <testResource> 
      <directory>src/test/story</directory> 
     </testResource> 
    </testResources> 
    <plugins> 
     ... 
     <plugin> 
      <artifactId>maven-eclipse-plugin</artifactId> 
      <version>2.8</version> 
      <configuration> 
       <downloadSources>true</downloadSources> 
       <downloadJavadocs>true</downloadJavadocs> 
       <additionalBuildcommands> 
        <buildcommand>com.google.gdt.eclipse.core.webAppProjectValidator</buildcommand> 
       </additionalBuildcommands> 
       <additionalProjectnatures> 
        <projectnature>com.google.gwt.eclipse.core.gwtNature</projectnature> 
       </additionalProjectnatures> 
       <classpathContainers> 
        <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer> 
       </classpathContainers> 
       <additionalConfig> 
        <file> 
         <name>.settings/org.eclipse.core.resources.prefs</name> 
         <content> 
          <![CDATA[eclipse.preferences.version=1 
          encoding/<project>=UTF-8]]> 
         </content> 
        </file> 
       </additionalConfig> 
      </configuration> 
     </plugin> 
     <plugin> 
      <groupId>org.jbehave</groupId> 
      <artifactId>jbehave-maven-plugin</artifactId> 
      <version>${jbehave.version}</version> 
      <executions> 
       <execution> 
        <id>run-stories-as-embeddables</id> 
        <phase>test</phase> 
        <configuration> 
         <scope>test</scope> 
         <includes> 
          <include>**/*Story.java</include> 
         </includes> 
         <ignoreFailureInStories>true</ignoreFailureInStories> 
         <ignoreFailureInView>false</ignoreFailureInView> 
        </configuration> 
        <goals> 
         <goal>run-stories-as-embeddables</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 

     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-dependency-plugin</artifactId> 
      <executions> 
       <execution> 
        <id>unpack-jbehave-site-resources</id> 
        <phase>generate-resources</phase> 
        <goals> 
         <goal>unpack</goal> 
        </goals> 
        <configuration> 
         <overwriteReleases>false</overwriteReleases> 
         <overwriteSnapshots>true</overwriteSnapshots> 
         <artifactItems> 
          <artifactItem> 
           <groupId>org.jbehave.site</groupId> 
           <artifactId>jbehave-site-resources</artifactId> 
           <version>3.1.1</version> 
           <type>zip</type> 
           <outputDirectory> 
            ${project.build.directory}/jbehave/view 
           </outputDirectory> 
          </artifactItem> 
         </artifactItems> 
        </configuration> 
       </execution> 
       <execution> 
        <id>unpack-jbehave-reports-resources</id> 
        <phase>generate-resources</phase> 
        <goals> 
         <goal>unpack</goal> 
        </goals> 
        <configuration> 
         <overwriteReleases>false</overwriteReleases> 
         <overwriteSnapshots>true</overwriteSnapshots> 
         <artifactItems> 
          <artifactItem> 
           <groupId>org.jbehave</groupId> 
           <artifactId>jbehave-core</artifactId> 
           <version>${jbehave.version}</version> 
           <outputDirectory> 
            ${project.build.directory}/jbehave/view 
           </outputDirectory> 
           <includes> 
            **\/*.css,**\/*.ftl,**\/*.js 
           </includes> 
          </artifactItem> 
         </artifactItems> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 

<dependencies> 
    ... 

    <!-- JBehave Dependencies --> 
    <dependency> 
     <groupId>org.jbehave</groupId> 
     <artifactId>jbehave-core</artifactId> 
     <version>${jbehave.version}</version> 
    </dependency> 

    <!-- Test Frameworks Dependencies --> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.8.2</version> 
     <scope>test</scope> 
    </dependency> 

    <dependency> 
     <groupId>org.mockito</groupId> 
     <artifactId>mockito-all</artifactId> 
     <version>1.8.4</version> 
     <scope>test</scope> 
    </dependency> 
</dependencies> 

回答

3

我已经取得了一些成功的子类org.jbehave.core.io.LoadFromClasspath类,我在我的配置使用作为故事装载机,即

MostUsefulConfiguration().useStoryLoader(new LoadFromClasspathUtf8()); 

这里是我的子类,用适当的方法重写:

import java.io.IOException; 
import java.io.InputStream; 

import org.apache.commons.io.IOUtils; 
import org.jbehave.core.io.InvalidStoryResource; 
import org.jbehave.core.io.LoadFromClasspath; 

public class LoadFromClasspathUtf8 extends LoadFromClasspath { 

    public LoadFromClasspathUtf8(Class<?> loadFromClass) { 
     super(loadFromClass); 
    } 

    public LoadFromClasspathUtf8(ClassLoader classLoader) { 
     super(classLoader); 
    } 

    @Override 
    public String loadResourceAsText(String resourcePath) { 
     InputStream stream = resourceAsStream(resourcePath); 
     try { 
      return IOUtils.toString(stream, "UTF-8"); 
     } catch (IOException e) { 
      throw new InvalidStoryResource(resourcePath, stream, e); 
     } 
    } 
} 

我说:“我有一些成功”,因为当我看着我的jbehave执行日志,重音法语字符,如E,A ,é等被替换为?,但是,然后,jbehave仍然可以正确匹配使用常规RegexStoryParser的步骤。我没有花时间调查为什么这样,但我很满意我的故事现在正确运行。

我还将file.encoding系统属性添加到我的插件配置中,以清楚说明我打算使用UTF-8编码。

+0

谢谢,这也帮助我在eclipse中正确运行测试! – Santosh

0

既然你有你的故事,在“测试”上下文,而不是“主”(在其他模块) - 我认为这有可能是一些事情,当故事复制到目标/测试类上。

+0

我想过,这就是为什么我在POM中指定:目标/类和我检查,类有 – antoine

+0

所以,你的故事是UTF-8在那个目录? –

0

我有完全相同的问题。默认情况下,JBehave不支持平台编码。为了解决这个问题,你可以使用荣誉的file.encoding系统属性这一习俗StoryLoader:

import java.io.IOException; 
import java.io.InputStream; 
import java.nio.charset.Charset; 

import org.apache.commons.io.IOUtils; 
import org.jbehave.core.io.InvalidStoryResource; 
import org.jbehave.core.io.LoadFromClasspath; 

/** 
* @author cedric.vidal 
* 
*/ 
public class FixedStoryLoader extends LoadFromClasspath { 

    public String loadResourceAsText(String resourcePath) { 
     InputStream stream = resourceAsStream(resourcePath); 
     try { 
      return IOUtils.toString(stream, platformCharset().name()); 
     } catch (IOException e) { 
      throw new InvalidStoryResource(resourcePath, stream, e); 
     } 
    } 

    public static Charset platformCharset() { 
     String csn = System.getProperty("file.encoding"); 
     Charset cs = Charset.forName(csn); 
     if (cs == null) { 
      cs = Charset.forName("UTF-8"); 
     } 
     return cs; 
    } 

} 

与注册它JBehave配置:

new MostUsefulConfiguration().useStoryLoader(new FixedStoryLoader()); 

配置您的POM中使用UTF-8所有respectfull插件(将m2eclipse的被使用过):

<properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
</properties> 

,并告诉JBehave Maven插件使用它也(寻找systemProperties块):

 <plugin> 
      <groupId>org.jbehave</groupId> 
      <artifactId>jbehave-maven-plugin</artifactId> 
      <version>${jbehave.core.version}</version> 
      <executions> 
       <execution> 
        <id>unpack-view-resources</id> 
        <phase>process-resources</phase> 
        <goals> 
         <goal>unpack-view-resources</goal> 
        </goals> 
       </execution> 
       <execution> 
        <id>embeddable-stories</id> 
        <phase>integration-test</phase> 
        <configuration> 
         <includes> 
          <include>${embeddables}</include> 
         </includes> 
         <excludes/> 
         <systemProperties> 
          <property> 
           <name>file.encoding</name> 
           <value>${project.build.sourceEncoding}</value> 
          </property> 
         </systemProperties> 
         <ignoreFailureInStories>true</ignoreFailureInStories> 
         <ignoreFailureInView>false</ignoreFailureInView> 
         <threads>1</threads> 
         <metaFilters> 
          <metaFilter/> 
         </metaFilters> 
        </configuration> 
        <goals> 
         <goal>run-stories-as-embeddables</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
1

同样的问题也在这里。即使在将“-Dfile.encoding = UTF-8”参数添加到MAVEN_OPTS之后,问题仍然存在。转出,我有我的〜/ .mavenrc文件中这一行:

发生了什么事是MAVEN_OPTS变量得到执行JVM前复位。

更改后的〜/ .mavenrc文件:

export MAVEN_OPTS="$MAVEN_OPTS -Xmx1024m" 

的问题得到解决。运行时,文件编码设置正确:

export MAVEN_OPTS="$MAVEN_OPTS -Dfile.encoding=UTF-8" 
mvn clean integration-test