Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.compatibility.common.util;
     18 
     19 import static org.junit.Assert.assertTrue;
     20 
     21 import org.xmlpull.v1.XmlPullParser;
     22 import org.xmlpull.v1.XmlPullParserException;
     23 import org.xmlpull.v1.XmlPullParserFactory;
     24 
     25 import java.io.File;
     26 import java.io.FileInputStream;
     27 import java.io.FileNotFoundException;
     28 import java.io.IOException;
     29 import java.io.InputStreamReader;
     30 import java.util.ArrayList;
     31 import java.util.HashMap;
     32 import java.util.List;
     33 import java.util.Map;
     34 import java.util.Set;
     35 
     36 /**
     37  * Load dynamic config for test cases
     38  */
     39 public class DynamicConfig {
     40 
     41     //XML constant
     42     public static final String NS = null;
     43     public static final String CONFIG_TAG = "dynamicConfig";
     44     public static final String ENTRY_TAG = "entry";
     45     public static final String VALUE_TAG = "value";
     46     public static final String KEY_ATTR = "key";
     47 
     48     public static final String REMOTE_CONFIG_REQUIRED_KEY = "remote_config_required";
     49     public static final String REMOTE_CONFIG_RETRIEVED_KEY = "remote_config_retrieved";
     50     public static final String CONFIG_FOLDER_ON_DEVICE = "/sdcard/dynamic-config-files/";
     51 
     52     protected Map<String, List<String>> mDynamicConfigMap = new HashMap<String, List<String>>();
     53 
     54     public void initializeConfig(File file) throws XmlPullParserException, IOException {
     55         mDynamicConfigMap = createConfigMap(file);
     56     }
     57 
     58     public String getValue(String key) {
     59         assertRemoteConfigRequirementMet();
     60         List<String> singleValue = mDynamicConfigMap.get(key);
     61         if (singleValue == null || singleValue.size() == 0 || singleValue.size() > 1) {
     62             // key must exist in the map, and map to a list containing exactly one string
     63             return null;
     64         }
     65         return singleValue.get(0);
     66     }
     67 
     68     public List<String> getValues(String key) {
     69         assertRemoteConfigRequirementMet();
     70         return mDynamicConfigMap.get(key);
     71     }
     72 
     73     public Set<String> keySet() {
     74         assertRemoteConfigRequirementMet();
     75         return mDynamicConfigMap.keySet();
     76     }
     77 
     78     public boolean remoteConfigRequired() {
     79         if (mDynamicConfigMap.containsKey(REMOTE_CONFIG_REQUIRED_KEY)) {
     80             String val = mDynamicConfigMap.get(REMOTE_CONFIG_REQUIRED_KEY).get(0);
     81             return Boolean.parseBoolean(val);
     82         }
     83         return false;
     84     }
     85 
     86     public boolean remoteConfigRetrieved() {
     87         // assume config will always contain exactly one value, populated by DynamicConfigHandler
     88         String val = mDynamicConfigMap.get(REMOTE_CONFIG_RETRIEVED_KEY).get(0);
     89         return Boolean.parseBoolean(val);
     90     }
     91 
     92     public void assertRemoteConfigRequirementMet() {
     93         assertTrue("Remote connection to DynamicConfigService required for this test",
     94                 !remoteConfigRequired() || remoteConfigRetrieved());
     95     }
     96 
     97     public static File getConfigFile(File configFolder, String moduleName)
     98             throws FileNotFoundException {
     99         File config = getConfigFileUnchecked(configFolder, moduleName);
    100         if (!config.exists()) {
    101             throw new FileNotFoundException(String.format("Cannot find %s.dynamic", moduleName));
    102         }
    103         return config;
    104     }
    105 
    106     public static File getConfigFileUnchecked(File configFolder, String moduleName) {
    107         return new File(configFolder, String.format("%s.dynamic", moduleName));
    108     }
    109 
    110     public static Map<String, List<String>> createConfigMap(File file)
    111             throws XmlPullParserException, IOException {
    112 
    113         Map<String, List<String>> dynamicConfigMap = new HashMap<String, List<String>>();
    114         XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
    115         parser.setInput(new InputStreamReader(new FileInputStream(file)));
    116         parser.nextTag();
    117         parser.require(XmlPullParser.START_TAG, NS, CONFIG_TAG);
    118 
    119         while (parser.nextTag() == XmlPullParser.START_TAG) {
    120             parser.require(XmlPullParser.START_TAG, NS, ENTRY_TAG);
    121             String key = parser.getAttributeValue(NS, KEY_ATTR);
    122             List<String> valueList = new ArrayList<String>();
    123             while (parser.nextTag() == XmlPullParser.START_TAG) {
    124                 parser.require(XmlPullParser.START_TAG, NS, VALUE_TAG);
    125                 valueList.add(parser.nextText());
    126                 parser.require(XmlPullParser.END_TAG, NS, VALUE_TAG);
    127             }
    128             parser.require(XmlPullParser.END_TAG, NS, ENTRY_TAG);
    129             if (key != null && !key.isEmpty()) {
    130                 dynamicConfigMap.put(key, valueList);
    131             }
    132         }
    133 
    134         parser.require(XmlPullParser.END_TAG, NS, CONFIG_TAG);
    135         return dynamicConfigMap;
    136     }
    137 }
    138