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 package com.android.compatibility.common.tradefed.targetprep;
17 
18 import com.android.tradefed.build.IBuildInfo;
19 import com.android.tradefed.config.Option;
20 import com.android.tradefed.config.OptionClass;
21 import com.android.tradefed.device.DeviceNotAvailableException;
22 import com.android.tradefed.device.ITestDevice;
23 import com.android.tradefed.log.LogUtil.CLog;
24 import com.android.tradefed.targetprep.BuildError;
25 import com.android.tradefed.targetprep.TargetSetupError;
26 
27 /**
28  * Checks that a device property is as expected
29  */
30 @OptionClass(alias="property-check")
31 public class PropertyCheck extends PreconditionPreparer {
32 
33     @Option(
34         name = "property-name",
35         description = "The name of the property to check",
36         mandatory = true
37     )
38     private String mPropertyName = null;
39 
40     @Option(
41         name = "expected-value",
42         description = "The expected value of the property",
43         mandatory = true
44     )
45     private String mExpectedPropertyValue = null;
46 
47     @Option(
48         name = "throw-error",
49         description = "Whether to throw an error for an unexpected property value"
50     )
51     private boolean mThrowError = false;
52 
53     @Override
run(ITestDevice device, IBuildInfo buildInfo)54     public void run(ITestDevice device, IBuildInfo buildInfo) throws TargetSetupError,
55             BuildError, DeviceNotAvailableException {
56 
57         String propertyValue = device.getProperty(mPropertyName);
58         if (propertyValue == null) {
59             CLog.w(
60                     "Property \"%s\" not found on device, cannot verify value \"%s\" ",
61                     mPropertyName, mExpectedPropertyValue);
62             return;
63         }
64 
65         if (!mExpectedPropertyValue.equalsIgnoreCase(propertyValue)) {
66             String msg = String.format("Expected \"%s\" but found \"%s\" for property: %s",
67                     mExpectedPropertyValue, propertyValue, mPropertyName);
68             // Handle unexpected property value with either exception or warning
69             if(mThrowError) {
70                 throw new TargetSetupError(msg, device.getDeviceDescriptor());
71             } else {
72                 CLog.w(msg);
73             }
74         }
75     }
76 
77 }
78