ProvinceInfoUtils { public static String matchAddress(Context cxt, String provinceId, String cityId, ArrayList<ProvinceModel> provinceList) { int count = (null == provinceList) ? 0 : provinceList.size(); Resources res = cxt.getResources(); String province = "其他地区"; String city = "其他"; StringBuffer sb = new StringBuffer(); ProvinceModel provinceModel = null; for (int i = 0; i < count; i++) { provinceModel = provinceList.get(i); if (null != provinceModel && provinceModel.id.equals(provinceId)) { int cityCount; CityModel cityModel = null; province = provinceModel.name; cityCount = provinceModel.getCityCount(); for (int j = 0; j < cityCount; j++) { cityModel = provinceModel.getCity(j); if (null != cityModel && cityModel.id.equals(cityId)) { city = cityModel.name; break; } } break; } } sb.append(province); sb.append(" "); sb.append(city); return sb.toString(); }  static String matchAddress(Context cxt, String provinceId, String cityId, ArrayList<ProvinceModel> provinceList);  }
@Test public void testMatchAddress() throws IOException { Context contextMock = Mockito.mock(Context.class); String validProvinceId = PROVINCE_ID + "-0"; String emptyProvinceId = PROVINCE_ID + "-1"; String validCityId = CITY_ID + "-0"; ArrayList<ProvinceModel> provinces = createProvinces(); provinces.add(null); CityModel[] cities = createCities(); ProvinceModel provinceModel = provinces.get(0); provinceModel.addCity(null); for (CityModel city : cities) { provinceModel.addCity(city); } assertEquals( VALID_RESPONSE, ProvinceInfoUtils.matchAddress(contextMock, validProvinceId, validCityId, provinces)); assertEquals( EMPTY_PROVINCE_RESPONSE, ProvinceInfoUtils.matchAddress(contextMock, emptyProvinceId, validCityId, provinces)); assertEquals( INVALID_RESPONSE, ProvinceInfoUtils.matchAddress(contextMock, PROVINCE_ID, validCityId, provinces)); assertEquals( INVALID_CITY_RESPONSE, ProvinceInfoUtils.matchAddress(contextMock, validProvinceId, CITY_ID, provinces)); assertEquals( INVALID_RESPONSE, ProvinceInfoUtils.matchAddress(contextMock, validProvinceId, validCityId, new ArrayList<ProvinceModel>())); assertEquals( INVALID_RESPONSE, ProvinceInfoUtils.matchAddress(contextMock, null, validCityId, provinces)); assertEquals( INVALID_CITY_RESPONSE, ProvinceInfoUtils.matchAddress(contextMock, validProvinceId, null, provinces)); assertEquals( INVALID_RESPONSE, ProvinceInfoUtils.matchAddress(contextMock, validProvinceId, validCityId, null)); assertEquals( INVALID_RESPONSE, ProvinceInfoUtils.matchAddress(contextMock, null, null, provinces)); assertEquals( INVALID_RESPONSE, ProvinceInfoUtils.matchAddress(contextMock, null, null, null)); }