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.loganalysis.item; 17 18 import junit.framework.TestCase; 19 20 import org.json.JSONArray; 21 import org.json.JSONException; 22 import org.json.JSONObject; 23 24 /** 25 * Unit test for {@link MonkeyLogItem}. 26 */ 27 public class MonkeyLogItemTest extends TestCase { 28 /** 29 * Test that {@link MonkeyLogItem#toJson()} returns correctly. 30 */ testToJson()31 public void testToJson() throws JSONException { 32 MonkeyLogItem item = new MonkeyLogItem(); 33 item.addCategory("category1"); 34 item.addCategory("category2"); 35 item.addPackage("package1"); 36 item.addPackage("package2"); 37 item.addPackage("package3"); 38 39 // Convert to JSON string and back again 40 JSONObject output = new JSONObject(item.toJson().toString()); 41 42 assertTrue(output.has(MonkeyLogItem.CATEGORIES)); 43 assertTrue(output.get(MonkeyLogItem.CATEGORIES) instanceof JSONArray); 44 45 JSONArray categories = output.getJSONArray(MonkeyLogItem.CATEGORIES); 46 47 assertEquals(2, categories.length()); 48 assertTrue(in("category1", categories)); 49 assertTrue(in("category2", categories)); 50 51 JSONArray packages = output.getJSONArray(MonkeyLogItem.PACKAGES); 52 53 assertEquals(3, packages.length()); 54 assertTrue(in("package1", packages)); 55 assertTrue(in("package2", packages)); 56 assertTrue(in("package3", packages)); 57 } 58 in(String value, JSONArray array)59 private boolean in(String value, JSONArray array) throws JSONException { 60 for (int i = 0; i < array.length(); i++) { 61 if (value.equals(array.get(i))) { 62 return true; 63 } 64 } 65 return false; 66 } 67 } 68