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:
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 }}
|
Reference in New Issue
Block a user