Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete CityListTest.java #3

Open
wants to merge 7 commits into
base: unit-test
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions app/src/main/java/com/example/citylist/CityList.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
Expand All @@ -27,9 +28,40 @@ public void add(City city) {
* @return
* Return the sorted list of cities
*/
public List<City> getCities() {
public List<City> getCities(int param) {
List<City> cityList = cities;
Collections.sort(cityList);
if(param==1){
Collections.sort(cityList);
}
else{
Collections.sort(cityList, new Comparator<City>() {
@Override
public int compare(City city, City t1) {
return city.getProvinceName().compareTo(t1.getProvinceName());
}
});

}
//Collections.sort(cityList);
return cityList;
}

/**
*
* @param city
* if city doesnt exist,return an exception
*/
public void delete(City city){
if(!cities.contains(city)){
throw new IllegalArgumentException();

}
else{
cities.remove(city);
}
}

public int count(){
return cities.size();
}
}
56 changes: 48 additions & 8 deletions app/src/test/java/com/example/citylist/CityListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,75 @@ private City mockCity() {
@Test
public void testAdd() {
CityList cityList = mockCityList();
assertEquals(1, cityList.getCities().size());
assertEquals(1, cityList.getCities(1).size());

City city = new City("Regina", "SK");
cityList.add(city);

assertEquals(2, cityList.getCities().size());
assertTrue(cityList.getCities().contains(city));
assertEquals(2, cityList.getCities(1).size());
assertTrue(cityList.getCities(1).contains(city));
}
@Test
public void testdelete(){
CityList cityList=new CityList();
City city=new City("montreal","canada");
City city1=new City("panjab","uttorprodesh");
cityList.add(city);
cityList.add(city1);
assertEquals(2,cityList.count());
cityList.delete(city);
assertEquals(1,cityList.count());
assertTrue(!cityList.getCities(1).contains(city));

}
@Test
public void testDeleteException() {
CityList cityList=new CityList();
City city=new City("montreal","canada");
City city1=new City("panjab","uttorprodesh");
cityList.add(city);
cityList.add(city1);
cityList.delete(city);

assertThrows(IllegalArgumentException.class, () -> {
cityList.delete(city1);
});
}
@Test
public void testAddException() {
CityList cityList = new CityList();
City city = mockCity();
City city1 =mockCity();
cityList.add(city);
cityList.add(city1);

assertThrows(IllegalArgumentException.class, () -> {
cityList.add(city);
cityList.add(city1);
});
}

@Test
/* @Test
public void testGetCities() {
CityList cityList = mockCityList();
assertEquals(0, mockCity().compareTo(cityList.getCities().get(0)));
assertEquals(0, mockCity().compareTo(cityList.getCities(1).get(0)));

City city = new City("Charlottetown", "Prince Edward Island");
cityList.add(city);

assertEquals(0, city.compareTo(cityList.getCities(1).get(0)));
assertEquals(0, mockCity().compareTo(cityList.getCities(1).get(1)));
}*/
@Test
public void tesTGetCities() {
CityList cityList =new CityList() ;
City city1=new City("ZZZZ","CD");
cityList.add(city1);
//assertEquals(0, city1.compareTo(cityList.getCities(1).get(0)));

City city = new City("Charlottetown", "Prince Edward Island");
cityList.add(city);

assertEquals(0, city.compareTo(cityList.getCities().get(0)));
assertEquals(0, mockCity().compareTo(cityList.getCities().get(1)));
assertEquals(0, city.compareTo(cityList.getCities(2).get(0)));
assertEquals(0, city1.compareTo(cityList.getCities(2).get(1)));
}
}