Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2013 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 package com.android.tradefed.util;
     17 
     18 import junit.framework.TestCase;
     19 
     20 import java.io.BufferedReader;
     21 import java.io.BufferedWriter;
     22 import java.io.File;
     23 import java.io.FileReader;
     24 import java.io.FileWriter;
     25 import java.io.IOException;
     26 import java.util.HashMap;
     27 import java.util.Map;
     28 import java.util.Map.Entry;
     29 
     30 /**
     31  * Functional test for {@link PropertyChanger}
     32  */
     33 public class PropertyChangerTest extends TestCase {
     34 
     35     private File mTmpInput;
     36     private File mTmpOutput;
     37     private Map<String, String> mOriginal, mChanges, mExpected;
     38 
     39     @Override
     40     protected void setUp() throws Exception {
     41         super.setUp();
     42         mTmpInput = FileUtil.createTempFile("prop_test_in", ".prop");
     43         mOriginal = new HashMap<String, String>();
     44         mOriginal.put("foo1", "bar1");
     45         mOriginal.put("foo2", "bar2");
     46         mOriginal.put("foo3", "bar3");
     47         mOriginal.put("foo4", "bar4");
     48         writeProperties(mTmpInput, mOriginal);
     49         mChanges = new HashMap<String, String>();
     50         mChanges.put("foo2", "bar2*");
     51         mChanges.put("foo3", "bar3*");
     52         mChanges.put("foo5", "bar5-new");
     53         mExpected = new HashMap<String, String>();
     54         mExpected.put("foo1", "bar1");
     55         mExpected.put("foo2", "bar2*");
     56         mExpected.put("foo3", "bar3*");
     57         mExpected.put("foo4", "bar4");
     58         mExpected.put("foo5", "bar5-new");
     59     }
     60 
     61     public void testChangeProperty() throws Exception {
     62         mTmpOutput = PropertyChanger.changeProperties(mTmpInput, mChanges);
     63         verifyProperties(mTmpOutput, mExpected);
     64     }
     65 
     66     @Override
     67     protected void tearDown() throws Exception {
     68         if (mTmpInput != null && mTmpInput.exists()) {
     69             mTmpInput.delete();
     70         }
     71         if (mTmpOutput != null && mTmpOutput.exists()) {
     72             mTmpOutput.delete();
     73         }
     74         super.tearDown();
     75     }
     76 
     77     private void writeProperties(File output, Map<String, String> props) throws IOException {
     78         BufferedWriter bw = null;
     79         try {
     80         bw = new BufferedWriter(new FileWriter(output));
     81         for (Entry<String, String> entry : props.entrySet()) {
     82             bw.write(String.format("%s=%s\n", entry.getKey(), entry.getValue()));
     83         }
     84         } finally {
     85             if (bw != null) {
     86                 bw.close();
     87             }
     88         }
     89     }
     90 
     91     private void verifyProperties(File source, Map<String, String> props) throws IOException {
     92         BufferedReader br = null;
     93         Map<String, String> verifyProps = new HashMap<String, String>(props);
     94         try {
     95             br = new BufferedReader(new FileReader(source));
     96             String line = null;
     97             while ((line = br.readLine()) != null) {
     98                 int pos = line.indexOf('=');
     99                 String name = line.substring(0, pos);
    100                 String value = line.substring(pos + 1);
    101                 assertTrue("extra property in file: " + name, verifyProps.containsKey(name));
    102                 assertEquals("property value mismatch", verifyProps.get(name), value);
    103                 verifyProps.remove(name);
    104             }
    105             assertTrue("missing properties in file", verifyProps.isEmpty());
    106         } finally {
    107             if (br != null) {
    108                 br.close();
    109             }
    110         }
    111     }
    112 }
    113