Generate FakeFeatureFlagsImpl in test mode
Before FeatureFlagsImpl will be used as a fake for testing. This change adds new class FakeFeatureFlagsImpl. The FeatureFlagsImpl will keep the same as production. FakeFeatureFlagsImpl can be used as fake for testing. FakeFeatureFlagsImpl, and FeatureFlagsImpl will be generated in both test and prod mode. In test mode FeatureFlagsImpl will just be a stub, and in prod mode FakeFeatureFlagsImpl will just be a stub. Bug: 280833463 Test: atest aconfig.test Change-Id: I11c1e716a9ea00d55600e5e9d5fb6442420762e6
This commit is contained in:
parent
1afb7ffec2
commit
5aaeee3749
4 changed files with 140 additions and 58 deletions
|
@ -47,9 +47,13 @@ where
|
|||
"FeatureFlags.java",
|
||||
include_str!("../templates/FeatureFlags.java.template"),
|
||||
)?;
|
||||
template.add_template(
|
||||
"FakeFeatureFlagsImpl.java",
|
||||
include_str!("../templates/FakeFeatureFlagsImpl.java.template"),
|
||||
)?;
|
||||
|
||||
let path: PathBuf = package.split('.').collect();
|
||||
["Flags.java", "FeatureFlagsImpl.java", "FeatureFlags.java"]
|
||||
["Flags.java", "FeatureFlags.java", "FeatureFlagsImpl.java", "FakeFeatureFlagsImpl.java"]
|
||||
.iter()
|
||||
.map(|file| {
|
||||
Ok(OutputFile {
|
||||
|
@ -143,6 +147,29 @@ mod tests {
|
|||
}
|
||||
"#;
|
||||
|
||||
const EXPECTED_METHOD_NOT_IMPL_COMMON_CONTENT: &str = r#"
|
||||
@Override
|
||||
public boolean disabledRo() {
|
||||
throw new UnsupportedOperationException(
|
||||
"Method is not implemented.");
|
||||
}
|
||||
@Override
|
||||
public boolean disabledRw() {
|
||||
throw new UnsupportedOperationException(
|
||||
"Method is not implemented.");
|
||||
}
|
||||
@Override
|
||||
public boolean enabledRo() {
|
||||
throw new UnsupportedOperationException(
|
||||
"Method is not implemented.");
|
||||
}
|
||||
@Override
|
||||
public boolean enabledRw() {
|
||||
throw new UnsupportedOperationException(
|
||||
"Method is not implemented.");
|
||||
}
|
||||
"#;
|
||||
|
||||
#[test]
|
||||
fn test_generate_java_code_production() {
|
||||
let parsed_flags = crate::test::parse_test_flags();
|
||||
|
@ -156,7 +183,15 @@ mod tests {
|
|||
+ r#"
|
||||
private static FeatureFlags FEATURE_FLAGS = new FeatureFlagsImpl();
|
||||
}"#;
|
||||
let expected_featureflagsimpl_content = r#"
|
||||
let expect_fakefeatureflagsimpl_content = r#"
|
||||
package com.android.aconfig.test;
|
||||
public class FakeFeatureFlagsImpl implements FeatureFlags {"#
|
||||
.to_owned()
|
||||
+ EXPECTED_METHOD_NOT_IMPL_COMMON_CONTENT
|
||||
+ r#"
|
||||
}
|
||||
"#;
|
||||
let expect_featureflagsimpl_content = r#"
|
||||
package com.android.aconfig.test;
|
||||
import android.provider.DeviceConfig;
|
||||
public final class FeatureFlagsImpl implements FeatureFlags {
|
||||
|
@ -188,8 +223,12 @@ mod tests {
|
|||
"#;
|
||||
let mut file_set = HashMap::from([
|
||||
("com/android/aconfig/test/Flags.java", expect_flags_content.as_str()),
|
||||
("com/android/aconfig/test/FeatureFlagsImpl.java", expected_featureflagsimpl_content),
|
||||
("com/android/aconfig/test/FeatureFlagsImpl.java", expect_featureflagsimpl_content),
|
||||
("com/android/aconfig/test/FeatureFlags.java", EXPECTED_FEATUREFLAGS_CONTENT),
|
||||
(
|
||||
"com/android/aconfig/test/FakeFeatureFlagsImpl.java",
|
||||
expect_fakefeatureflagsimpl_content.as_str(),
|
||||
),
|
||||
]);
|
||||
|
||||
for file in generated_files {
|
||||
|
@ -221,22 +260,30 @@ mod tests {
|
|||
.unwrap();
|
||||
let expect_flags_content = EXPECTED_FLAG_COMMON_CONTENT.to_string()
|
||||
+ r#"
|
||||
public static void setFeatureFlagsImpl(FeatureFlags featureFlags) {
|
||||
public static void setFeatureFlags(FeatureFlags featureFlags) {
|
||||
Flags.FEATURE_FLAGS = featureFlags;
|
||||
}
|
||||
public static void unsetFeatureFlagsImpl() {
|
||||
public static void unsetFeatureFlags() {
|
||||
Flags.FEATURE_FLAGS = null;
|
||||
}
|
||||
private static FeatureFlags FEATURE_FLAGS;
|
||||
}
|
||||
"#;
|
||||
let expected_featureflagsimpl_content = r#"
|
||||
let expect_featureflagsimpl_content = r#"
|
||||
package com.android.aconfig.test;
|
||||
public final class FeatureFlagsImpl implements FeatureFlags {"#
|
||||
.to_owned()
|
||||
+ EXPECTED_METHOD_NOT_IMPL_COMMON_CONTENT
|
||||
+ r#"
|
||||
}
|
||||
"#;
|
||||
let expect_fakefeatureflagsimpl_content = r#"
|
||||
package com.android.aconfig.test;
|
||||
import static java.util.stream.Collectors.toMap;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
public final class FeatureFlagsImpl implements FeatureFlags {
|
||||
public class FakeFeatureFlagsImpl implements FeatureFlags {
|
||||
@Override
|
||||
public boolean disabledRo() {
|
||||
return getFlag(Flags.FLAG_DISABLED_RO);
|
||||
|
@ -284,10 +331,18 @@ mod tests {
|
|||
);
|
||||
}
|
||||
"#;
|
||||
|
||||
let mut file_set = HashMap::from([
|
||||
("com/android/aconfig/test/Flags.java", expect_flags_content.as_str()),
|
||||
("com/android/aconfig/test/FeatureFlagsImpl.java", expected_featureflagsimpl_content),
|
||||
("com/android/aconfig/test/FeatureFlags.java", EXPECTED_FEATUREFLAGS_CONTENT),
|
||||
(
|
||||
"com/android/aconfig/test/FeatureFlagsImpl.java",
|
||||
expect_featureflagsimpl_content.as_str(),
|
||||
),
|
||||
(
|
||||
"com/android/aconfig/test/FakeFeatureFlagsImpl.java",
|
||||
expect_fakefeatureflagsimpl_content,
|
||||
),
|
||||
]);
|
||||
|
||||
for file in generated_files {
|
||||
|
|
59
tools/aconfig/templates/FakeFeatureFlagsImpl.java.template
Normal file
59
tools/aconfig/templates/FakeFeatureFlagsImpl.java.template
Normal file
|
@ -0,0 +1,59 @@
|
|||
package {package_name};
|
||||
{{ if is_test_mode }}
|
||||
import static java.util.stream.Collectors.toMap;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class FakeFeatureFlagsImpl implements FeatureFlags \{
|
||||
{{ for item in class_elements}}
|
||||
@Override
|
||||
public boolean {item.method_name}() \{
|
||||
return getFlag(Flags.FLAG_{item.flag_name_constant_suffix});
|
||||
}
|
||||
{{ endfor}}
|
||||
public void setFlag(String flagName, boolean value) \{
|
||||
if (!this.mFlagMap.containsKey(flagName)) \{
|
||||
throw new IllegalArgumentException("no such flag" + flagName);
|
||||
}
|
||||
this.mFlagMap.put(flagName, value);
|
||||
}
|
||||
|
||||
public void resetAll() \{
|
||||
for (Map.Entry entry : mFlagMap.entrySet()) \{
|
||||
entry.setValue(null);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean getFlag(String flagName) \{
|
||||
Boolean value = this.mFlagMap.get(flagName);
|
||||
if (value == null) \{
|
||||
throw new IllegalArgumentException(flagName + " is not set");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private HashMap<String, Boolean> mFlagMap = Stream.of(
|
||||
{{-for item in class_elements}}
|
||||
Flags.FLAG_{item.flag_name_constant_suffix}{{ if not @last }},{{ endif }}
|
||||
{{ -endfor }}
|
||||
)
|
||||
.collect(
|
||||
HashMap::new,
|
||||
(map, elem) -> map.put(elem, null),
|
||||
HashMap::putAll
|
||||
);
|
||||
}
|
||||
{{ else }}
|
||||
{#- Generate only stub if in prod mode #}
|
||||
public class FakeFeatureFlagsImpl implements FeatureFlags \{
|
||||
{{ for item in class_elements}}
|
||||
@Override
|
||||
public boolean {item.method_name}() \{
|
||||
throw new UnsupportedOperationException(
|
||||
"Method is not implemented.");
|
||||
}
|
||||
{{ endfor}}
|
||||
}
|
||||
{{ endif }}
|
|
@ -1,65 +1,33 @@
|
|||
package {package_name};
|
||||
{{ -if is_test_mode }}
|
||||
import static java.util.stream.Collectors.toMap;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
{{ else}}
|
||||
{{ if not is_test_mode }}
|
||||
{{ if is_read_write- }}
|
||||
import android.provider.DeviceConfig;
|
||||
{{ -endif- }}
|
||||
{{ endif }}
|
||||
public final class FeatureFlagsImpl implements FeatureFlags \{
|
||||
{{ for item in class_elements}}
|
||||
@Override
|
||||
public boolean {item.method_name}() \{
|
||||
{{ -if not is_test_mode- }}
|
||||
{{ if item.is_read_write }}
|
||||
{{ -if item.is_read_write }}
|
||||
return DeviceConfig.getBoolean(
|
||||
"{item.device_config_namespace}",
|
||||
"{item.device_config_flag}",
|
||||
{item.default_value}
|
||||
);
|
||||
{{ -else }}
|
||||
{{ else }}
|
||||
return {item.default_value};
|
||||
{{ -endif- }}
|
||||
{{ else }}
|
||||
return getFlag(Flags.FLAG_{item.flag_name_constant_suffix});
|
||||
{{ -endif }}
|
||||
{{ endif- }}
|
||||
}
|
||||
{{ endfor- }}
|
||||
{{ if is_test_mode }}
|
||||
public void setFlag(String flagName, boolean value) \{
|
||||
if (!this.mFlagMap.containsKey(flagName)) \{
|
||||
throw new IllegalArgumentException("no such flag" + flagName);
|
||||
}
|
||||
this.mFlagMap.put(flagName, value);
|
||||
}
|
||||
|
||||
public void resetAll() \{
|
||||
for (Map.Entry entry : mFlagMap.entrySet()) \{
|
||||
entry.setValue(null);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean getFlag(String flagName) \{
|
||||
Boolean value = this.mFlagMap.get(flagName);
|
||||
if (value == null) \{
|
||||
throw new IllegalArgumentException(flagName + " is not set");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private HashMap<String, Boolean> mFlagMap = Stream.of(
|
||||
{{-for item in class_elements}}
|
||||
Flags.FLAG_{item.flag_name_constant_suffix}{{ if not @last }},{{ endif }}
|
||||
{{ -endfor }}
|
||||
)
|
||||
.collect(
|
||||
HashMap::new,
|
||||
(map, elem) -> map.put(elem, null),
|
||||
HashMap::putAll
|
||||
);
|
||||
{{ -endif }}
|
||||
}
|
||||
{{ else }}
|
||||
{#- Generate only stub if in test mode #}
|
||||
public final class FeatureFlagsImpl implements FeatureFlags \{
|
||||
{{ for item in class_elements}}
|
||||
@Override
|
||||
public boolean {item.method_name}() \{
|
||||
throw new UnsupportedOperationException(
|
||||
"Method is not implemented.");
|
||||
}
|
||||
{{ endfor- }}
|
||||
}
|
||||
{{ endif }}
|
||||
|
|
|
@ -10,11 +10,11 @@ public final class Flags \{
|
|||
}
|
||||
{{ endfor }}
|
||||
{{ -if is_test_mode }}
|
||||
public static void setFeatureFlagsImpl(FeatureFlags featureFlags) \{
|
||||
public static void setFeatureFlags(FeatureFlags featureFlags) \{
|
||||
Flags.FEATURE_FLAGS = featureFlags;
|
||||
}
|
||||
|
||||
public static void unsetFeatureFlagsImpl() \{
|
||||
public static void unsetFeatureFlags() \{
|
||||
Flags.FEATURE_FLAGS = null;
|
||||
}
|
||||
{{ endif}}
|
||||
|
|
Loading…
Reference in a new issue