gpt4 book ai didi

Maven JBehave : encoding stories UTF8

转载 作者:行者123 更新时间:2023-12-04 20:17:30 26 4
gpt4 key购买 nike

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

但是当我们尝试使用 maven 插件运行它们时,我们无法解决编码问题(例如,不是从故事中读取“场景”,而是得到“场景”:显然是 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>

    最佳答案

    我在继承 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 执行的日志时,重音的法语字符(如 è、à、é 等)被替换为? RegexStoryParser。我没有花时间去调查为什么会这样,但我很满意我的故事现在可以正常工作。

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

    关于Maven JBehave : encoding stories UTF8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10429163/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com