Maven JBehave:编码故事UTF8

时间:2012-05-03 10:04:02

标签: maven encoding utf-8 jbehave scenarios

我们设法在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>

4 个答案:

答案 0 :(得分: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执行日志时,è,à,é等重音法语字符被替换为?,但是,jbehave仍然将其正确地与步骤匹配使用常规的RegexStoryParser。我没有花时间调查这是为什么,但我很满意我的故事现在正常运作。

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

答案 1 :(得分:1)

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

export MAVEN_OPTS="-Xmx1024m"

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

将〜/ .mavenrc文件更改为:

export MAVEN_OPTS="$MAVEN_OPTS -Xmx1024m"

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

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

答案 2 :(得分:0)

因为你在&#34;测试&#34;上下文而不是&#34; main&#34; (在另一个模块中) - 我认为当故事被复制到目标/测试类时可能会发生一些事情。

答案 3 :(得分:0)

我有完全相同的问题。默认情况下,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>