【问题标题】:Maven JBehave : encoding stories UTF8Maven JBehave:编码故事 UTF8
【发布时间】:2012-05-03 10:04:02
【问题描述】:

我们设法在 Eclipse 中使用 JBehave 创建和运行具有国际化故事的测试。 一切顺利。

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

有人找到一种方法让 JBehave 使用 maven 插件以 UTF8 格式阅读故事吗?

我们已经尝试过的:

  • 添加 -Dfile.encoding=UTF-8 选项
  • 使用 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>

【问题讨论】:

    标签: maven encoding utf-8 jbehave scenarios


    【解决方案1】:

    我在子类化 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 执行日志时,像 è、à、é 等重音的法语字符被替换为 ?,但是,jbehave 仍然正确地匹配到步骤使用常规的 RegexStoryParser。我没有花时间调查为什么会这样,但我对我的故事现在可以正常工作感到满意。

    我还在插件配置中添加了 file.encoding 系统属性,以表明我打算使用 UTF-8 编码。

    【讨论】:

    • 谢谢,这也帮助我在 Eclipse 中正确运行测试!
    【解决方案2】:

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

    export MAVEN_OPTS="-Xmx1024m"
    

    发生的事情是 MAVEN_OPTS 变量在执行 JVM 之前被重置。

    将 ~/.mavenrc 文件更改为:

    export MAVEN_OPTS="$MAVEN_OPTS -Xmx1024m"
    

    问题解决了。运行时文件编码设置正确:

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

    【讨论】:

      【解决方案3】:

      由于您的故事是在“测试”上下文而不是“主要”(在另一个模块中) - 我认为当故事被复制到目标/测试类时可能会发生一些事情。

      【讨论】:

      • 我想到了,这就是为什么我在 POM 中指定:target/classes 并检查,类在那里
      • 那么,您的故事在那个目录中是 UTF-8 吗?
      【解决方案4】:

      我遇到了完全相同的问题。默认情况下,JBehave 不支持平台编码。为了解决这个问题,您可以使用此自定义 StoryLoader,它尊重 file.encoding 系统属性:

      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(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>
      

      【讨论】:

        猜你喜欢
        • 2011-05-02
        • 1970-01-01
        • 1970-01-01
        • 2023-03-17
        • 2011-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多