5

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

4 回答 4

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 仍然正确地将其与使用常规的步骤匹配正则表达式解析器。我没有花时间去调查为什么会这样,但我很满意我的故事现在可以正常工作。

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

于 2012-11-14T16:25:58.863 回答
1

这里也有同样的问题。即使将“-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
于 2012-06-12T16:25:09.117 回答
0

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

于 2012-05-09T21:16:19.703 回答
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>
于 2012-05-16T18:52:06.783 回答