英文:
Java Select which class to initialize by parameter
问题
这是一个关于测试字符串的代码,包括一个主要的 TestConstants 类,其中存储了用于测试的字符串。
public class TestConstants {
public static final STRING_TEXT;
}
然后,有一个子类 EnglishConstants。如果应用程序使用英语,则将使用这个类:
public class EnglishConstants extends TestConstants {
public static final STRING_TEXT = "Text in English.";
}
最后,还有一个子类 PortugueseConstants。如果应用程序使用葡萄牙语,则将使用这个类:
public class PortugueseConstants extends TestConstants {
public static final STRING_TEXT = "Text in Portuguese.";
}
目的是,如果在终端上使用 Maven 构建测试时提供了 "-Dlanguage=" 属性,并根据该属性选择在该语言中的 "STRING_TEXT"。
所以,如果提供了以下命令:
-Dlanguage=pt-pt
那么测试将使用 "TestConstants.STRING_TEXT",并从 "PortugueseConstants" 类中获取葡萄牙语的值。
英文:
There is a main TestConstant class where the strings for testing are stored.
public class TestConstants {
public static final STRING_TEXT;
}
Then, there is a child class EnglishConstants. This class will be used if application is using English language:
public class EnglishConstants extends TestConstants {
public static final STRING_TEXT = "Text in English.";
}
At the end, there is a child class PortugueseConstants. This class will be used if application is using Portuguese language:
public class PortugueseConstants extends TestConstants {
public static final STRING_TEXT = "Text in Portuguese.";
}
What's the point:
We want if it is possible, when running the test by Maven build from terminal with "-Dlanguage=" property provided, and based on that property "STRING_TEXT" to be selected in that language.
So, if the:
-Dlanguage=pt-pt is provided
Test has then to use "TestConstants.STRING_TEXT" and to take the value in the Portuguese language from the "PortugueseConstants" class.
Tried with factory on this question and answer:
https://stackoverflow.com/a/28602106/9074348
答案1
得分: 0
以下是您要翻译的部分:
"After all investigation I've decided to ask ChatGPT for the help. So, now we have an proper solution for selecting cross-language constants.
Solution:
public class TestConstants {
public static String STRING_TEXT;
public static String NEW_TEXT;
public static String ANOTHER_TEXT;
}
public class EnglishConstants extends TestConstants {
static {
STRING_TEXT = "String Text in English.";
NEW_TEXT = "New Text in English.";
ANOTHER_TEXT = "Another Text in English.";
}
}
public class PortugueseConstants extends TestConstants {
static {
STRING_TEXT = "String Text in Portuguese.";
NEW_TEXT = "New Text in Portuguese.";
ANOTHER_TEXT = "Another Text in Portuguese.";
}
}
public class ConstantsFactory {
public static TestConstants getLanguageConstants(String language) {
switch (language) {
case "pt-pt":
return new PortugueseConstants();
case "en-us":
default:
return new EnglishConstants();
}
}
}
// Usage example
public class MainClass {
public static void main(String[] args) {
String language = System.getProperty("language");
// Initialization of the TestConstant as language by parameter can be stored //in the BaseTest also
TestConstants languageConstants = ConstantsFactory.getLanguageConstants(language);
String stringText = languageConstants.STRING_TEXT;
String newText = languageConstants.NEW_TEXT;
String anotherText = languageConstants.ANOTHER_TEXT;
System.out.println("String Text: " + stringText);
System.out.println("New Text: " + newText);
System.out.println("Another Text: " + anotherText);
}
}
如果要运行一些测试,请使用:
mvn clean test -Dlanguage=en-en
英文:
After all investigation I've decided to ask ChatGPT for the help. So, now we have an proper solution for selecting cross-language constants.
Solution:
public class TestConstants {
public static String STRING_TEXT;
public static String NEW_TEXT;
public static String ANOTHER_TEXT;
}
public class EnglishConstants extends TestConstants {
static {
STRING_TEXT = "String Text in English.";
NEW_TEXT = "New Text in English.";
ANOTHER_TEXT = "Another Text in English.";
}
}
public class PortugueseConstants extends TestConstants {
static {
STRING_TEXT = "String Text in Portuguese.";
NEW_TEXT = "New Text in Portuguese.";
ANOTHER_TEXT = "Another Text in Portuguese.";
}
}
public class ConstantsFactory {
public static TestConstants getLanguageConstants(String language) {
switch (language) {
case "pt-pt":
return new PortugueseConstants();
case "en-us":
default:
return new EnglishConstants();
}
}
}
// Usage example
public class MainClass {
public static void main(String[] args) {
String language = System.getProperty("language");
// Initialization of the TestConstant as language by parameter can be stored //in the BaseTest also
TestConstants languageConstants = ConstantsFactory.getLanguageConstants(language);
String stringText = languageConstants.STRING_TEXT;
String newText = languageConstants.NEW_TEXT;
String anotherText = languageConstants.ANOTHER_TEXT;
System.out.println("String Text: " + stringText);
System.out.println("New Text: " + newText);
System.out.println("Another Text: " + anotherText);
}
}
If you want to run the some test use:
mvn clean test -Dlanguage=en-en
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论