java - 编码测试在 Ant 中失败但在 Eclipse 中有效

标签 java ant utf-8 junit

我在 UTF-8 编码文件中有以下 junit 测试。

public class EncodingTest {

 private static byte[] utf8Bytes;

 private static byte[] utf8Bytes2;

 private static byte[] utf8Bytes3;

 static {
try {
    utf8Bytes = "åæø".getBytes("UTF-8");
    utf8Bytes2 = new byte[] { (byte) 0xc3, (byte) 0xa5, (byte) 0xc3, (byte) 0xa6, (byte) 0xc3, (byte) 0xb8 };
    utf8Bytes3 = "\u00E5\u00E6\u00F8".getBytes("UTF-8");
} catch (final UnsupportedEncodingException e) {
    assertTrue(false);
}

 }

 @Test
 public void testUTF8Encoding1() throws UnsupportedEncodingException {
assertTrue(Arrays.equals(utf8Bytes, utf8Bytes2));
 }

 @Test
 public void testUTF8Encoding2() throws UnsupportedEncodingException {
 assertTrue(Arrays.equals(utf8Bytes2, utf8Bytes3));
 }

 @Test
 public void testUTF8Encoding3() throws UnsupportedEncodingException {
 assertEquals(new String(utf8Bytes), new String(utf8Bytes2));
 }

 @Test
 public void testUTF8Encoding4() throws UnsupportedEncodingException {
 assertEquals(new String(utf8Bytes2), new String(utf8Bytes3));
 }
}

测试在 ant 中失败,但在 eclipse 中有效。我正在使用的 ANT 任务如下:

<target
    name="test"
    depends="compile-tests"
    description="Test the full company tests" >

    <junit fork="yes" haltonfailure="yes" >
        <jvmarg value="-Dfile.encoding=UTF-8"/>
        <classpath>

            <path refid="test.classpath" />

            <fileset dir="war/WEB-INF/lib/" >

                <include name="*.jar" />
            </fileset>
        </classpath>

        <formatter
            type="plain"
            usefile="false" />
        <!-- to screen -->
        <!-- formatter type="plain" / -->
        <!-- to file -->

        <batchtest>

            <fileset
                dir="test/classes/"
                includes="**/*Test.class" />
        </batchtest>
    </junit>
</target>

输出如下:

test:
[junit] Testsuite: com.company.appengine.EncodingTest
[junit] Tests run: 4, Failures: 2, Errors: 0, Time elapsed: 0,028 sec
[junit]
[junit] Testcase: testUTF8Encoding1 took 0,004 sec
[junit]     FAILED
[junit]
[junit] junit.framework.AssertionFailedError:
[junit]     at com.company.appengine.EncodingTest.testUTF8Encoding1(EncodingTest.java:42)
[junit]
[junit] Testcase: testUTF8Encoding2 took 0 sec
[junit] Testcase: testUTF8Encoding3 took 0,001 sec
[junit]     FAILED
[junit] expected:<[åæø]> but was:<[åæø]>
[junit] junit.framework.AssertionFailedError: expected:<[åæø]> but was:<[åæø]>
[junit]     at com.company.appengine.EncodingTest.testUTF8Encoding3(EncodingTest.java:52)
[junit]
[junit] Testcase: testUTF8Encoding4 took 0 sec

有什么想法吗?

最佳答案

请首先确保 javac 知道您的源文件是用 utf-8 编码的。如果使用 javac task 编译, 提供 encoding 属性。

关于java - 编码测试在 Ant 中失败但在 Eclipse 中有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13140834/

相关文章:

瑞典语字符的 Python 编码

java - Android Studio下调试时能否修改或指定Java应用程序的执行点

java - 使用 Apache Ant 时部署的 jar 找不到主类

java - xml中替换字符时如何防止ant构建改变国家字符

ant - Gradle的AntBuilder文档在哪里?

php - UTF-8 排序数组不适用于 PHP 中的 foreach

java - 将 PBKDF2 盐重新用于 AES/GCM 作为 IV : dangerous?

java - Eclipse 插件不会从 RESOLVED 变为 ACTIVE

java - 测试post请求

perl - 为什么 CGI.pm 在处理 "application/x-www-form-urlencoded"表单时不能正确处理 UTF-8?