5

Eclipse内でJBehaveを使用して、国際化されたストーリーでテストを作成して実行することができました。すべてがうまくいった。

しかし、Mavenプラグインを使用してそれらを実行しようとすると、エンコーディングの問題を理解できません(たとえば、ストーリーから「scénario」を読み取る代わりに、「Scénario」を取得します。明らかにUTF8エンコーディングの問題です) 。

誰かが、Mavenプラグインを使用してJBehaveに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は、これを通常の手順に正しく一致させます。 RegexStoryParser。なぜそうなのかを調べるのに時間がかかりませんでしたが、私のストーリーが正しく機能するようになったことに満足しています。

また、プラグイン構成にfile.encodingシステムプロパティを追加して、UTF-8エンコーディングを使用する予定であることを明確にしました。

于 2012-11-14T16:25:58.863 に答える
1

ここでも同じ問題。「-Dfile.encoding=UTF-8」パラメーターをMAVEN_OPTSに追加した後でも、問題は解決しませんでした。〜/.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
于 2012-06-12T16:25:09.117 に答える
0

ストーリーは(別のモジュールの)「メイン」ではなく「テスト」コンテキストにあるため、ストーリーがターゲット/テストクラスにコピーされると、おそらく何かが起こっていると思います。

于 2012-05-09T21:16:19.703 に答える
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());

すべてのrespectfullプラグインでUTF-8を使用するようにPOMを構成します(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 に答える