|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iotdb.db.i18n; |
| 21 | + |
| 22 | +import org.junit.Assert; |
| 23 | +import org.junit.Test; |
| 24 | +import org.slf4j.helpers.MessageFormatter; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.net.URISyntaxException; |
| 28 | +import java.nio.charset.StandardCharsets; |
| 29 | +import java.nio.file.Files; |
| 30 | +import java.nio.file.Path; |
| 31 | +import java.nio.file.Paths; |
| 32 | +import java.util.regex.Matcher; |
| 33 | +import java.util.regex.Pattern; |
| 34 | + |
| 35 | +public class DataNodeSchemaMessagesZhFormatTest { |
| 36 | + |
| 37 | + private static final String ZH_MESSAGES_RELATIVE_PATH = |
| 38 | + "src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeSchemaMessages.java"; |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testZhSlf4jTemplatesPreserveLoggerArgumentOrder() throws IOException { |
| 42 | + Assert.assertEquals( |
| 43 | + "从 mlog.bin 反序列化 MTree 耗时 12 ms,对应路径 root.sg", |
| 44 | + formatSlf4j(readZhMessage("SPEND_TIME_DESERIALIZE_MTREE"), 12, "root.sg")); |
| 45 | + Assert.assertEquals( |
| 46 | + "跳过 128 失败,schemaRegion 目录为 C:/schema", |
| 47 | + formatSlf4j(readZhMessage("FAILED_TO_SKIP_MLOG"), 128, "C:/schema")); |
| 48 | + Assert.assertEquals( |
| 49 | + "恢复 root.sg.d.s 的 tagIndex 失败,schemaRegion 为 SchemaRegionId{1}。", |
| 50 | + formatSlf4j( |
| 51 | + readZhMessage("FAILED_TO_RECOVER_TAG_INDEX"), "root.sg.d.s", "SchemaRegionId{1}")); |
| 52 | + Assert.assertEquals( |
| 53 | + "MTree 刷写耗时 321ms,SchemaRegion 为 SchemaRegionId{1}", |
| 54 | + formatSlf4j(readZhMessage("MTREE_FLUSH_COST"), 321, "SchemaRegionId{1}")); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testZhStringFormatTemplatePreservesArgumentTypesAndOrder() throws IOException { |
| 59 | + Assert.assertEquals( |
| 60 | + "SchemaRegion [7] 在 StorageGroup [root.sg] 中恢复失败。", |
| 61 | + String.format(readZhMessage("SCHEMA_REGION_FAILED_TO_RECOVER"), 7, "root.sg")); |
| 62 | + } |
| 63 | + |
| 64 | + private static String formatSlf4j(String template, Object... arguments) { |
| 65 | + return MessageFormatter.arrayFormat(template, arguments).getMessage(); |
| 66 | + } |
| 67 | + |
| 68 | + private static String readZhMessage(String constantName) throws IOException { |
| 69 | + Path basePath = resolveZhMessagesPath(); |
| 70 | + Assert.assertTrue("Missing zh messages file: " + basePath, Files.exists(basePath)); |
| 71 | + |
| 72 | + String content = new String(Files.readAllBytes(basePath), StandardCharsets.UTF_8); |
| 73 | + Matcher matcher = |
| 74 | + Pattern.compile( |
| 75 | + "public static final String\\s+" |
| 76 | + + Pattern.quote(constantName) |
| 77 | + + "\\s*=\\s*\"([^\"]*)\";", |
| 78 | + Pattern.DOTALL) |
| 79 | + .matcher(content); |
| 80 | + Assert.assertTrue("Missing zh message constant: " + constantName, matcher.find()); |
| 81 | + return matcher.group(1); |
| 82 | + } |
| 83 | + |
| 84 | + private static Path resolveZhMessagesPath() throws IOException { |
| 85 | + try { |
| 86 | + Path testClassesDir = |
| 87 | + Paths.get( |
| 88 | + DataNodeSchemaMessagesZhFormatTest.class |
| 89 | + .getProtectionDomain() |
| 90 | + .getCodeSource() |
| 91 | + .getLocation() |
| 92 | + .toURI()); |
| 93 | + Path moduleBaseDir = testClassesDir.getParent().getParent(); |
| 94 | + return moduleBaseDir.resolve(ZH_MESSAGES_RELATIVE_PATH); |
| 95 | + } catch (URISyntaxException e) { |
| 96 | + throw new IOException("Failed to resolve zh messages path", e); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments