javaextensions/location/tsrc/vipertest/src/LandmarkStoreTest.java
branchRCL_3
changeset 14 04becd199f91
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 import java.util.Enumeration;
       
    18 import javax.microedition.location.*;
       
    19 
       
    20 public class LandmarkStoreTest extends ViperUnitTest
       
    21 {
       
    22     static final int MAX_NAME_LENGTH = 255;
       
    23 
       
    24     static final int MAX_CATEGORY_LENGTH = 124;
       
    25 
       
    26     static final int MAX_DESCRIPTION_LENGTH = 4095;
       
    27 
       
    28     static final int MAX_ADDRESSFIELD_LENGTH = 255;
       
    29 
       
    30     static final String extension = "23";
       
    31 
       
    32     static final String street = "11 Mount Avenue";
       
    33 
       
    34     static final String city = "London";
       
    35 
       
    36     static final String county = "Ealing";
       
    37 
       
    38     static final String postal_code = "W5 1QB";
       
    39 
       
    40     static final String state = "England";
       
    41 
       
    42     static final String district = "Middlesex";
       
    43 
       
    44     static final String country = "United Kingdom";
       
    45 
       
    46     static final String country_code = "GB";
       
    47 
       
    48     static final String building_name = "The Castle";
       
    49 
       
    50     static final String building_floor = "3";
       
    51 
       
    52     static final String building_room = "Front Room";
       
    53 
       
    54     static final String building_zone = "Upstairs";
       
    55 
       
    56     static final String crossing1 = "Mount Avenue";
       
    57 
       
    58     static final String crossing2 = "Eaton Rise";
       
    59 
       
    60     static final String url = "http://www.upmystreet.co.uk";
       
    61 
       
    62     static final String phone_number = "+358401234567";
       
    63 
       
    64     static final String searchCategory0 = "category0";
       
    65 
       
    66     static final String searchCategory1 = "category1";
       
    67 
       
    68     static final String searchCategory2 = "category2";
       
    69 
       
    70     static final String searchName0 = "name0"; // Will not be added
       
    71 
       
    72     static final String searchName1 = "name1"; // Will be added once
       
    73 
       
    74     static final String searchName2 = "name2"; // Will be added twice
       
    75 
       
    76     static final String searchName3 = "name3"; // Will be added three times
       
    77 
       
    78     private LandmarkStore ls;
       
    79 
       
    80     private String iCategory;
       
    81 
       
    82     public LandmarkStoreTest()
       
    83     {
       
    84         super("LandmarkStoreTest");
       
    85     }
       
    86 
       
    87     // These tests assume the default Landmark store is empty to begin with
       
    88     protected void runTest() throws java.lang.Throwable
       
    89     {
       
    90         testGetInstance();
       
    91 
       
    92         testDeleteLandmarkBadArguments();
       
    93         testGetLandmarksEmptyStore();
       
    94 
       
    95         // ---------------------------------------------------
       
    96         // Test addLandmark(Landmark, String category = null),
       
    97         // deleteLandmark(Landmark), updateLandmark(Landmark)
       
    98         // and getLandmarks()
       
    99         // ---------------------------------------------------
       
   100         iCategory = null;
       
   101         testAddGetDeleteAndUpdateLandmarks();
       
   102 
       
   103         // ---------------------------------------------------
       
   104         // Test addCategory(String), deleteCategory(String)
       
   105         // and getCategories()
       
   106         // ---------------------------------------------------
       
   107         testAddCategoryBadArguments();
       
   108         testDeleteCategoryBadArguments();
       
   109         testAddDeleteCategory();
       
   110         testAddDeleteManyCategories();
       
   111         testAddCategoryTruncate();
       
   112 
       
   113         testAddLandmarkToCategory1();
       
   114         testAddLandmarkToCategory2();
       
   115         testAddLandmarkToCategory3();
       
   116         testAddLandmarkToCategoryDeleteCategory();
       
   117         testAddLandmarkSameNameDifferentCategory();
       
   118 
       
   119         // Test addLandmark(Landmark, String category = "Work")
       
   120         iCategory = "Work";
       
   121         testAddGetDeleteAndUpdateLandmarks();
       
   122 
       
   123         testRemoveLandmarkFromCategoryBadArguments();
       
   124         testRemoveLandmarkFromCategory();
       
   125         testRemoveLandmarkFromCategory2();
       
   126 
       
   127         testGetLandmarksByNameBadArguments();
       
   128         testGetLandmarksByName();
       
   129         testGetLandmarksByNameUncategorized();
       
   130         testGetLandmarksByNameWildcards();
       
   131         testGetLandmarksByCategoryBadArguments();
       
   132         testGetLandmarksByCategory();
       
   133         testGetLandmarksByNameAndCategory();
       
   134         testGetLandmarksByAreaBadArguments();
       
   135         testGetLandmarksByArea();
       
   136         testGetLandmarksByArea2();
       
   137         testGetLandmarksByAreaAndCategory();
       
   138 
       
   139         testEnumerationDeleteLandmark1();
       
   140         testEnumerationDeleteLandmark2();
       
   141 
       
   142         // Make sure the tests cleaned the store
       
   143         assertEmptyLandmarkStore();
       
   144     }
       
   145 
       
   146     void testAddGetDeleteAndUpdateLandmarks() throws Exception
       
   147     {
       
   148         if (iCategory != null)
       
   149         {
       
   150             ls = getDefaultLandmarkStore();
       
   151             ls.addCategory(iCategory);
       
   152         }
       
   153 
       
   154         testAddLandmarkBadArguments();
       
   155         testAddDeleteLandmark1();
       
   156         testAddDeleteLandmark2();
       
   157         testAddDeleteLandmark3();
       
   158         testAddDeleteLandmark4();
       
   159         testAddLandmarkTwice();
       
   160         testAddLandmarkNoUpdate();
       
   161         testAddLandmarkSameName();
       
   162 
       
   163         testAddDeleteAddLandmark();
       
   164         testAddLandmarkTruncate();
       
   165         testAddLandmarkEmptyStrings();
       
   166 
       
   167         testUpdateLandmarkBadArguments();
       
   168         testUpdateLandmark1();
       
   169         testUpdateLandmark2();
       
   170 
       
   171         testUpdateLandmarkTruncate();
       
   172         testAddDeleteUpdateLandmark();
       
   173 
       
   174         testGetAllLandmarks();
       
   175 
       
   176         if (iCategory != null)
       
   177         {
       
   178             ls = getDefaultLandmarkStore();
       
   179             ls.deleteCategory(iCategory);
       
   180         }
       
   181     }
       
   182 
       
   183     void testGetInstance()
       
   184     {
       
   185         setCurrentTest("testGetInstance()");
       
   186 
       
   187         // Get default store
       
   188         ls = LandmarkStore.getInstance(null);
       
   189         assertTrue(ls != null, "Instance was null for default store");
       
   190     }
       
   191 
       
   192     void testAddLandmarkBadArguments() throws Exception
       
   193     {
       
   194         setCurrentTest("testAddLandmarkBadArguments()");
       
   195 
       
   196         ls = getDefaultLandmarkStore();
       
   197 
       
   198         try
       
   199         {
       
   200             // Add with null Landmark parameter
       
   201             ls.addLandmark(null, iCategory);
       
   202             assertTrue(false, "NullPointerException not thrown");
       
   203         }
       
   204         catch (NullPointerException npe)
       
   205         {
       
   206             // Exception was thrown correctly
       
   207             assertNoMessage(npe);
       
   208         }
       
   209 
       
   210         Landmark lm = new Landmark("test", null, null, null);
       
   211 
       
   212         try
       
   213         {
       
   214             String badCategory = "This category does not exist";
       
   215             ls.addLandmark(lm, badCategory); // Add with nonexisting category
       
   216             assertTrue(false, "IllegalArgumentException not thrown 1");
       
   217         }
       
   218         catch (IllegalArgumentException iae)
       
   219         {
       
   220             // Exception was thrown correctly
       
   221             assertNoMessage(iae);
       
   222         }
       
   223 
       
   224         try
       
   225         {
       
   226             lm.setName(new String(new char[MAX_NAME_LENGTH + 1]));
       
   227             ls.addLandmark(lm, iCategory); // Add Landmark with too long name
       
   228             assertTrue(false, "IllegalArgumentException not thrown 2");
       
   229         }
       
   230         catch (IllegalArgumentException iae)
       
   231         {
       
   232             // Exception was thrown correctly
       
   233             assertNoMessage(iae);
       
   234         }
       
   235     }
       
   236 
       
   237     void testDeleteLandmarkBadArguments() throws Exception
       
   238     {
       
   239         setCurrentTest("testDeleteLandmarkBadArguments");
       
   240 
       
   241         ls = getDefaultLandmarkStore();
       
   242 
       
   243         try
       
   244         {
       
   245             ls.deleteLandmark(null); // Delete with null Landmark parameter
       
   246             assertTrue(false, "NullPointerException not thrown");
       
   247         }
       
   248         catch (NullPointerException npe)
       
   249         {
       
   250             // Exception was thrown correctly
       
   251             assertNoMessage(npe);
       
   252         }
       
   253 
       
   254         try
       
   255         {
       
   256             Landmark lm = new Landmark("test", null, null, null);
       
   257             ls.deleteLandmark(lm); // Try to delete a Landmark that was never
       
   258             // added
       
   259             assertTrue(false, "LandmarkException not thrown");
       
   260         }
       
   261         catch (LandmarkException le)
       
   262         {
       
   263             // Exception was thrown correctly
       
   264             assertNoMessage(le);
       
   265         }
       
   266 
       
   267     }
       
   268 
       
   269     void testGetLandmarksEmptyStore() throws Exception
       
   270     {
       
   271         setCurrentTest("testGetLandmarksEmptyStore()");
       
   272         ls = getDefaultLandmarkStore();
       
   273         Enumeration e = ls.getLandmarks();
       
   274         assertTrue(e == null, "Expected getLandmarks to return null");
       
   275     }
       
   276 
       
   277     // Create a Landmark object, store it in the LandmarkStore, check
       
   278     // that it can be found again and then delete it
       
   279     void testAddDeleteLandmark1() throws Exception
       
   280     {
       
   281         setCurrentTest("testAddDeleteLandmark1()");
       
   282 
       
   283         String name = "Nowhere";
       
   284 
       
   285         // Create a new Landmark object with only name set
       
   286         Landmark lm = new Landmark(name, null, null, null);
       
   287 
       
   288         saveRetrieveDelete(lm);
       
   289     }
       
   290 
       
   291     void testAddDeleteLandmark2() throws Exception
       
   292     {
       
   293         setCurrentTest("testAddDeleteLandmark2()");
       
   294 
       
   295         String name = "Somewhere";
       
   296         String desc = "It's somewhere but noone knows where it really is";
       
   297 
       
   298         // Create a new Landmark object with only name set
       
   299         Landmark lm = new Landmark(name, desc, null, null);
       
   300 
       
   301         saveRetrieveDelete(lm);
       
   302     }
       
   303 
       
   304     void testAddDeleteLandmark3() throws Exception
       
   305     {
       
   306         setCurrentTest("testAddDeleteLandmark3()");
       
   307 
       
   308         String name = "North pole";
       
   309         String desc = "Santas workshop, whats that address again?";
       
   310 
       
   311         QualifiedCoordinates coords = new QualifiedCoordinates(90.0f, 0.0f,
       
   312                 0.0f, 10.0f, 10.0f);
       
   313 
       
   314         // Create a new Landmark object with only name set
       
   315         Landmark lm = new Landmark(name, desc, coords, null);
       
   316 
       
   317         saveRetrieveDelete(lm);
       
   318     }
       
   319 
       
   320     void testAddDeleteLandmark4() throws Exception
       
   321     {
       
   322         setCurrentTest("testAddDeleteLandmark4()");
       
   323 
       
   324         String name = "Office";
       
   325         String desc = "Where you work";
       
   326 
       
   327         QualifiedCoordinates coords = new QualifiedCoordinates(57.0f, 17.0f,
       
   328                 34.0f, 50.0f, 80.0f);
       
   329 
       
   330         AddressInfo address = new AddressInfo();
       
   331         address.setField(AddressInfo.EXTENSION, extension);
       
   332         address.setField(AddressInfo.STREET, street);
       
   333         address.setField(AddressInfo.POSTAL_CODE, postal_code);
       
   334         address.setField(AddressInfo.CITY, city);
       
   335         address.setField(AddressInfo.COUNTY, county);
       
   336         address.setField(AddressInfo.DISTRICT, district);
       
   337         address.setField(AddressInfo.STATE, state);
       
   338         address.setField(AddressInfo.COUNTRY, country);
       
   339         address.setField(AddressInfo.COUNTRY_CODE, country_code);
       
   340         address.setField(AddressInfo.BUILDING_NAME, building_name);
       
   341         address.setField(AddressInfo.BUILDING_FLOOR, building_floor);
       
   342         address.setField(AddressInfo.BUILDING_ROOM, building_room);
       
   343         address.setField(AddressInfo.BUILDING_ZONE, building_zone);
       
   344         address.setField(AddressInfo.CROSSING1, crossing1);
       
   345         address.setField(AddressInfo.CROSSING2, crossing2);
       
   346         address.setField(AddressInfo.URL, url);
       
   347         address.setField(AddressInfo.PHONE_NUMBER, phone_number);
       
   348 
       
   349         // Create a new Landmark object
       
   350         Landmark lm = new Landmark(name, desc, coords, address);
       
   351 
       
   352         saveRetrieveDelete(lm);
       
   353     }
       
   354 
       
   355     // Tests that a Landmark can be added twice.
       
   356     void testAddLandmarkTwice() throws Exception
       
   357     {
       
   358         setCurrentTest("testAddLandmarkTwice()");
       
   359 
       
   360         String name = "Add this landmark twice";
       
   361         Landmark lm = new Landmark(name, null, null, null);
       
   362 
       
   363         ls = getDefaultLandmarkStore();
       
   364         ls.addLandmark(lm, iCategory);
       
   365         ls.addLandmark(lm, iCategory);
       
   366 
       
   367         Enumeration e = ls.getLandmarks();
       
   368         int count = findLandmarks(e, new String[] { name });
       
   369         assertTrue(count == 1, "Wrong number of landmarks found");
       
   370 
       
   371         ls.deleteLandmark(lm);
       
   372     }
       
   373 
       
   374     // Tests that addLandmark can not be used as updateLandmark.
       
   375     void testAddLandmarkNoUpdate() throws Exception
       
   376     {
       
   377         setCurrentTest("testAddLandmarkNoUpdate()");
       
   378         String name = "Add this landmark twice";
       
   379         Landmark lm = new Landmark(name, null, null, null);
       
   380 
       
   381         ls = getDefaultLandmarkStore();
       
   382         ls.addLandmark(lm, iCategory);
       
   383 
       
   384         // Check that addLandmark is not doing what updateLandmark should do
       
   385         lm.setDescription("Whatever");
       
   386         lm.setQualifiedCoordinates(new QualifiedCoordinates(0, 0, 0, 0, 0));
       
   387         lm.setAddressInfo(new AddressInfo());
       
   388         ls.addLandmark(lm, iCategory);
       
   389 
       
   390         Enumeration e = ls.getLandmarks();
       
   391         Landmark fetchedLm = findLandmark(e, name);
       
   392         checkLandmark(fetchedLm, name, null, null, null);
       
   393 
       
   394         ls.deleteLandmark(lm);
       
   395     }
       
   396 
       
   397     // Tests that Landmarks with the same name can be added.
       
   398     void testAddLandmarkSameName() throws Exception
       
   399     {
       
   400         setCurrentTest("testAddLandmarkSameName()");
       
   401 
       
   402         String name = "Glenn";
       
   403         Landmark lm1 = new Landmark(name, null, null, null);
       
   404         String desc = "Another landmark with name=Glenn";
       
   405         Landmark lm2 = new Landmark(name, desc, null, null);
       
   406 
       
   407         ls = getDefaultLandmarkStore();
       
   408         ls.addLandmark(lm1, iCategory);
       
   409         ls.addLandmark(lm2, iCategory);
       
   410 
       
   411         Enumeration e = ls.getLandmarks();
       
   412         int count = findLandmarks(e, new String[] { name, name });
       
   413         assertTrue(count == 2, "Wrong number of landmarks found");
       
   414 
       
   415         ls.deleteLandmark(lm1);
       
   416         ls.deleteLandmark(lm2);
       
   417     }
       
   418 
       
   419     // Tests that a Landmark can be added, then deleted and added again.
       
   420     void testAddDeleteAddLandmark() throws Exception
       
   421     {
       
   422         setCurrentTest("testAddDeleteAddLandmark()");
       
   423 
       
   424         ls = getDefaultLandmarkStore();
       
   425 
       
   426         String name = "LandmarkToBeAddedDeletedAndAddedAgain";
       
   427         Landmark lm = new Landmark(name, null, null, null);
       
   428         Enumeration e;
       
   429 
       
   430         ls.addLandmark(lm, iCategory);
       
   431 
       
   432         e = ls.getLandmarks();
       
   433         Landmark fetchedLm = findLandmark(e, name);
       
   434         ls.deleteLandmark(fetchedLm);
       
   435 
       
   436         ls.addLandmark(lm, iCategory);
       
   437 
       
   438         e = ls.getLandmarks();
       
   439         int count = findLandmarks(e, new String[] { name });
       
   440         assertTrue(count == 1, "Wrong number of landmarks found");
       
   441 
       
   442         ls.deleteLandmark(lm);
       
   443     }
       
   444 
       
   445     // Tests that description and address are truncated if they are too long
       
   446     void testAddLandmarkTruncate() throws Exception
       
   447     {
       
   448         setCurrentTest("testAddLandmarkTruncate()");
       
   449 
       
   450         ls = getDefaultLandmarkStore();
       
   451 
       
   452         String name = "Longest address in the world :)";
       
   453 
       
   454         String maxDescription = new String(new char[MAX_DESCRIPTION_LENGTH]);
       
   455         String tooLongDescription = maxDescription + "could be a lot more";
       
   456 
       
   457         String maxAddrField = new String(new char[MAX_ADDRESSFIELD_LENGTH]);
       
   458         String tooLongAddrField = maxAddrField + "some more";
       
   459         AddressInfo tooLongAddr = new AddressInfo();
       
   460         for (int i = 0; i < 17; ++i)
       
   461         {
       
   462             tooLongAddr.setField(i + 1, tooLongAddrField);
       
   463         }
       
   464 
       
   465         // Add the landmark that has info with too long length
       
   466         Landmark lm = new Landmark(name, tooLongDescription, null, tooLongAddr);
       
   467         ls.addLandmark(lm, iCategory);
       
   468 
       
   469         // All fields should be truncated to max length
       
   470         AddressInfo expectedAddr = new AddressInfo();
       
   471         for (int i = 0; i < 17; ++i)
       
   472         {
       
   473             expectedAddr.setField(i + 1, maxAddrField);
       
   474         }
       
   475 
       
   476         Enumeration e = ls.getLandmarks();
       
   477         Landmark fetchedLm = findLandmark(e, name);
       
   478         checkLandmark(fetchedLm, name, maxDescription, null, expectedAddr);
       
   479 
       
   480         ls.deleteLandmark(lm);
       
   481     }
       
   482 
       
   483     void testUpdateLandmarkBadArguments() throws Exception
       
   484     {
       
   485         setCurrentTest("testUpdateLandmarkBadArguments");
       
   486 
       
   487         ls = getDefaultLandmarkStore();
       
   488 
       
   489         try
       
   490         {
       
   491             ls.updateLandmark(null); // Update with null Landmark parameter
       
   492             assertTrue(false, "NullPointerException not thrown");
       
   493         }
       
   494         catch (NullPointerException npe)
       
   495         {
       
   496             // Exception was thrown correctly
       
   497         }
       
   498 
       
   499         Landmark lm = new Landmark("test", null, null, null);
       
   500         try
       
   501         {
       
   502             ls.updateLandmark(lm); // Update without adding first
       
   503             assertTrue(false, "LandmarkException not thrown 1");
       
   504         }
       
   505         catch (LandmarkException iae)
       
   506         {
       
   507             // Exception was thrown correctly
       
   508         }
       
   509 
       
   510         try
       
   511         {
       
   512             ls.addLandmark(lm, iCategory);
       
   513             ls.deleteLandmark(lm);
       
   514             ls.updateLandmark(lm); // Landmark no longer belongs to store
       
   515             assertTrue(false, "LandmarkException not thrown 2");
       
   516         }
       
   517         catch (LandmarkException iae)
       
   518         {
       
   519             // Exception was thrown correctly
       
   520         }
       
   521 
       
   522         try
       
   523         {
       
   524             ls.addLandmark(lm, iCategory);
       
   525             lm.setName(new String(new char[MAX_NAME_LENGTH + 1]));
       
   526             ls.updateLandmark(lm); // Update Landmark with too long name
       
   527             assertTrue(false, "IllegalArgumentException not thrown 3");
       
   528         }
       
   529         catch (IllegalArgumentException iae)
       
   530         {
       
   531             // Exception was thrown correctly
       
   532         }
       
   533 
       
   534         ls.deleteLandmark(lm);
       
   535     }
       
   536 
       
   537     // Add a landmark to the LandmarkStore and update with more and more
       
   538     // information.
       
   539     void testUpdateLandmark1() throws Exception
       
   540     {
       
   541         setCurrentTest("testUpdateLandmark1()");
       
   542 
       
   543         // Create a new Landmark object
       
   544         String name = "Home";
       
   545         Landmark lm = new Landmark(name, null, null, null);
       
   546 
       
   547         ls = getDefaultLandmarkStore();
       
   548 
       
   549         // Save the landmark
       
   550         ls.addLandmark(lm, iCategory);
       
   551 
       
   552         Landmark fetchedLm = null;
       
   553 
       
   554         fetchedLm = findLandmark(ls.getLandmarks(), name);
       
   555         checkLandmark(fetchedLm, name, null, null, null);
       
   556 
       
   557         // Update the landmark with a decription
       
   558         String description = "There is no place like it";
       
   559         lm.setDescription(description);
       
   560         ls.updateLandmark(lm);
       
   561 
       
   562         fetchedLm = findLandmark(ls.getLandmarks(), name);
       
   563         checkLandmark(fetchedLm, name, description, null, null);
       
   564 
       
   565         // Update the landmark with QualifiedCoordinates and change the name
       
   566         QualifiedCoordinates coords = new QualifiedCoordinates(-10.2f, -107.8f,
       
   567                 -4.5f, 11.9f, 5.1f);
       
   568 
       
   569         lm.setQualifiedCoordinates(coords);
       
   570         name = "My place";
       
   571         lm.setName(name);
       
   572 
       
   573         ls.updateLandmark(lm);
       
   574 
       
   575         fetchedLm = findLandmark(ls.getLandmarks(), name);
       
   576         checkLandmark(fetchedLm, name, description, coords, null);
       
   577 
       
   578         // Update the landmark with AddressInfo
       
   579         AddressInfo address = new AddressInfo();
       
   580         address.setField(AddressInfo.EXTENSION, extension);
       
   581         address.setField(AddressInfo.STREET, street);
       
   582         address.setField(AddressInfo.POSTAL_CODE, postal_code);
       
   583         address.setField(AddressInfo.CITY, city);
       
   584         address.setField(AddressInfo.COUNTY, county);
       
   585         address.setField(AddressInfo.DISTRICT, district);
       
   586         address.setField(AddressInfo.STATE, state);
       
   587         address.setField(AddressInfo.COUNTRY, country);
       
   588         address.setField(AddressInfo.COUNTRY_CODE, country_code);
       
   589         address.setField(AddressInfo.BUILDING_NAME, building_name);
       
   590         address.setField(AddressInfo.BUILDING_FLOOR, building_floor);
       
   591         address.setField(AddressInfo.BUILDING_ROOM, building_room);
       
   592         address.setField(AddressInfo.BUILDING_ZONE, building_zone);
       
   593         address.setField(AddressInfo.CROSSING1, crossing1);
       
   594         address.setField(AddressInfo.CROSSING2, crossing2);
       
   595         address.setField(AddressInfo.URL, url);
       
   596         address.setField(AddressInfo.PHONE_NUMBER, phone_number);
       
   597 
       
   598         lm.setAddressInfo(address);
       
   599         ls.updateLandmark(lm);
       
   600 
       
   601         fetchedLm = findLandmark(ls.getLandmarks(), name);
       
   602         checkLandmark(fetchedLm, name, description, coords, address);
       
   603 
       
   604         // Finally, remove the landmark
       
   605         ls.deleteLandmark(lm);
       
   606     }
       
   607 
       
   608     // Add a landmark to the LandmarkStore with all the information set and
       
   609     // update by removing information.
       
   610     void testUpdateLandmark2() throws Exception
       
   611     {
       
   612         setCurrentTest("testUpdateLandmark2()");
       
   613 
       
   614         // Create a new Landmark object
       
   615         String name = "Home";
       
   616         String desc = "There is no place like it";
       
   617         QualifiedCoordinates coords = new QualifiedCoordinates(-10.2f, -107.8f,
       
   618                 -4.5f, 11.9f, 5.1f);
       
   619         AddressInfo address = new AddressInfo();
       
   620         address.setField(AddressInfo.EXTENSION, extension);
       
   621         address.setField(AddressInfo.STREET, street);
       
   622         address.setField(AddressInfo.POSTAL_CODE, postal_code);
       
   623         address.setField(AddressInfo.CITY, city);
       
   624         address.setField(AddressInfo.COUNTY, county);
       
   625         address.setField(AddressInfo.DISTRICT, district);
       
   626         address.setField(AddressInfo.STATE, state);
       
   627         address.setField(AddressInfo.COUNTRY, country);
       
   628         address.setField(AddressInfo.COUNTRY_CODE, country_code);
       
   629         address.setField(AddressInfo.BUILDING_NAME, building_name);
       
   630         address.setField(AddressInfo.BUILDING_FLOOR, building_floor);
       
   631         address.setField(AddressInfo.BUILDING_ROOM, building_room);
       
   632         address.setField(AddressInfo.BUILDING_ZONE, building_zone);
       
   633         address.setField(AddressInfo.CROSSING1, crossing1);
       
   634         address.setField(AddressInfo.CROSSING2, crossing2);
       
   635         address.setField(AddressInfo.URL, url);
       
   636         address.setField(AddressInfo.PHONE_NUMBER, phone_number);
       
   637         Landmark lm = new Landmark(name, desc, coords, address);
       
   638 
       
   639         ls = getDefaultLandmarkStore();
       
   640 
       
   641         // Save the landmark
       
   642         ls.addLandmark(lm, iCategory);
       
   643 
       
   644         Landmark fetchedLm = null;
       
   645 
       
   646         fetchedLm = findLandmark(ls.getLandmarks(), name);
       
   647         checkLandmark(fetchedLm, name, desc, coords, address);
       
   648 
       
   649         // Remove decription
       
   650         lm.setDescription(null);
       
   651         ls.updateLandmark(lm);
       
   652 
       
   653         fetchedLm = findLandmark(ls.getLandmarks(), name);
       
   654         checkLandmark(fetchedLm, name, null, coords, address);
       
   655 
       
   656         // Remove QualifiedCoordinates
       
   657         lm.setQualifiedCoordinates(null);
       
   658         ls.updateLandmark(lm);
       
   659 
       
   660         fetchedLm = findLandmark(ls.getLandmarks(), name);
       
   661         checkLandmark(fetchedLm, name, null, null, address);
       
   662 
       
   663         // Remove AddressInfo
       
   664         lm.setAddressInfo(null);
       
   665         ls.updateLandmark(lm);
       
   666 
       
   667         fetchedLm = findLandmark(ls.getLandmarks(), name);
       
   668         checkLandmark(fetchedLm, name, null, null, null);
       
   669 
       
   670         // Finally, remove the landmark
       
   671         ls.deleteLandmark(lm);
       
   672     }
       
   673 
       
   674     void testUpdateLandmarkTruncate() throws Exception
       
   675     {
       
   676         setCurrentTest("testUpdateLandmarkTruncate()");
       
   677 
       
   678         ls = getDefaultLandmarkStore();
       
   679 
       
   680         String name = "Update with lots of info later";
       
   681         Landmark lm = new Landmark(name, null, null, null);
       
   682         ls.addLandmark(lm, iCategory);
       
   683 
       
   684         String maxDescription = new String(new char[MAX_DESCRIPTION_LENGTH]);
       
   685         String tooLongDescription = maxDescription + "could be a lot more";
       
   686 
       
   687         String maxAddrField = new String(new char[MAX_ADDRESSFIELD_LENGTH]);
       
   688         String tooLongAddrField = maxAddrField + "some more";
       
   689         AddressInfo tooLongAddr = new AddressInfo();
       
   690         for (int i = 0; i < 17; ++i)
       
   691         {
       
   692             tooLongAddr.setField(i + 1, tooLongAddrField);
       
   693         }
       
   694 
       
   695         // Update the landmark with info that has length longer than max
       
   696         lm.setDescription(tooLongDescription);
       
   697         lm.setAddressInfo(tooLongAddr);
       
   698         ls.updateLandmark(lm);
       
   699 
       
   700         // All fields should be truncated to max length
       
   701         AddressInfo expectedAddr = new AddressInfo();
       
   702         for (int i = 0; i < 17; ++i)
       
   703         {
       
   704             expectedAddr.setField(i + 1, maxAddrField);
       
   705         }
       
   706 
       
   707         Enumeration e = ls.getLandmarks();
       
   708         Landmark fetchedLm = findLandmark(e, name);
       
   709         checkLandmark(fetchedLm, name, maxDescription, null, expectedAddr);
       
   710 
       
   711         ls.deleteLandmark(lm);
       
   712     }
       
   713 
       
   714     void testAddDeleteUpdateLandmark() throws Exception
       
   715     {
       
   716         setCurrentTest("testAddDeleteUpdateLandmark()");
       
   717 
       
   718         ls = getDefaultLandmarkStore();
       
   719 
       
   720         String name = "AddDeleteUpdateLandmark";
       
   721         Landmark lm = new Landmark(name, null, null, null);
       
   722 
       
   723         // Add, get and delete the landmark
       
   724         ls.addLandmark(lm, iCategory);
       
   725         Enumeration e = ls.getLandmarks();
       
   726         Landmark fetchedLm = findLandmark(e, name);
       
   727         ls.deleteLandmark(lm);
       
   728 
       
   729         // Try to update the landmark we just deleted
       
   730         try
       
   731         {
       
   732             fetchedLm.setDescription("Description");
       
   733             ls.updateLandmark(fetchedLm);
       
   734             assertTrue(false, "LandmarkException not thrown");
       
   735         }
       
   736         catch (LandmarkException le)
       
   737         {
       
   738             // Exception was thrown correctly
       
   739             assertNoMessage(le);
       
   740         }
       
   741     }
       
   742 
       
   743     // List all landmarks.
       
   744     void testGetAllLandmarks() throws Exception
       
   745     {
       
   746         setCurrentTest("testGetAllLandmarks()");
       
   747 
       
   748         final String BASE_NAME = "GetLandmarksTest";
       
   749         final int NUM_LANDMARKS = 10;
       
   750         String[] names = new String[NUM_LANDMARKS];
       
   751 
       
   752         ls = getDefaultLandmarkStore();
       
   753 
       
   754         for (int i = 0; i < NUM_LANDMARKS; ++i)
       
   755         {
       
   756             names[i] = BASE_NAME + i;
       
   757             Landmark lm = new Landmark(names[i], null, null, null);
       
   758             ls.addLandmark(lm, iCategory);
       
   759         }
       
   760 
       
   761         // Retrieve the enumeration of landmarks
       
   762         Enumeration e = ls.getLandmarks();
       
   763 
       
   764         int count = findLandmarks(e, names);
       
   765         assertTrue(count == NUM_LANDMARKS, "Wrong number of Landmarks found");
       
   766 
       
   767         // Delete all the added landmarks
       
   768         e = ls.getLandmarks();
       
   769         while (e.hasMoreElements())
       
   770         {
       
   771             ls.deleteLandmark((Landmark) e.nextElement());
       
   772         }
       
   773     }
       
   774 
       
   775     void testAddCategoryBadArguments() throws Exception
       
   776     {
       
   777         setCurrentTest("testAddCategoryBadArguments()");
       
   778 
       
   779         ls = getDefaultLandmarkStore();
       
   780 
       
   781         try
       
   782         {
       
   783             ls.addCategory(null); // Add category with null parameter
       
   784             assertTrue(false, "NullPointerException not thrown");
       
   785         }
       
   786         catch (NullPointerException npe)
       
   787         {
       
   788             // Exception was thrown correctly
       
   789         }
       
   790 
       
   791         try
       
   792         {
       
   793             ls.addCategory(""); // Add category with empty string parameter
       
   794             assertTrue(false, "IllegalArgumentException not thrown");
       
   795         }
       
   796         catch (IllegalArgumentException iae)
       
   797         {
       
   798             // Exception was thrown correctly
       
   799         }
       
   800 
       
   801         String category = "Try to add this category twice";
       
   802         ls.addCategory(category);
       
   803 
       
   804         try
       
   805         {
       
   806             ls.addCategory(category); // Category already exists
       
   807             assertTrue(false, "IllegalArgumentException not thrown");
       
   808         }
       
   809         catch (IllegalArgumentException iae)
       
   810         {
       
   811             // Exception was thrown correctly
       
   812         }
       
   813 
       
   814         ls.deleteCategory(category);
       
   815     }
       
   816 
       
   817     void testDeleteCategoryBadArguments() throws Exception
       
   818     {
       
   819         setCurrentTest("testDeleteCategoryBadArguments()");
       
   820 
       
   821         ls = getDefaultLandmarkStore();
       
   822 
       
   823         try
       
   824         {
       
   825             ls.deleteCategory(null); // Delete category with null parameter
       
   826             assertTrue(false, "NullPointerException not thrown");
       
   827         }
       
   828         catch (NullPointerException npe)
       
   829         {
       
   830             // Exception was thrown correctly
       
   831         }
       
   832 
       
   833         String category = "Nonexisting category";
       
   834         ls.deleteCategory(category); // This should be a silent failure
       
   835         ls.deleteCategory(category); // Try again just to make sure :)
       
   836 
       
   837         ls.deleteCategory(""); // Empty string should fail silently
       
   838 
       
   839         // Silent failure if category name is too long
       
   840         ls.deleteCategory(new String(new char[MAX_CATEGORY_LENGTH + 1]));
       
   841     }
       
   842 
       
   843     // Tests that new categories can be added and deleted.
       
   844     void testAddDeleteCategory() throws Exception
       
   845     {
       
   846         setCurrentTest("testAddDeleteCategory()");
       
   847 
       
   848         String category = "AddCategoryTest Category";
       
   849 
       
   850         ls = getDefaultLandmarkStore();
       
   851 
       
   852         // Add category
       
   853         ls.addCategory(category);
       
   854 
       
   855         // Check that the category was added
       
   856         Enumeration e = ls.getCategories();
       
   857         findCategory(e, category);
       
   858 
       
   859         // Delete the category
       
   860         ls.deleteCategory(category);
       
   861 
       
   862         // Check that category has been removed
       
   863         e = ls.getCategories();
       
   864         while (e.hasMoreElements())
       
   865         {
       
   866             String s = (String) e.nextElement();
       
   867             assertTrue(!s.equals(category), "Category was not removed");
       
   868         }
       
   869     }
       
   870 
       
   871     void testAddDeleteManyCategories() throws Exception
       
   872     {
       
   873         setCurrentTest("testAddDeleteManyCategories()");
       
   874 
       
   875         // A variety of valid category names
       
   876         String[] categories = new String[] { "q", "Dots ...", "Exclamation!",
       
   877                                              "(parentesis)", "percentage%", "hashmark#", "at@", "et&",
       
   878                                              "commas,,,"
       
   879                                            };
       
   880 
       
   881         ls = getDefaultLandmarkStore();
       
   882 
       
   883         // Quick Add and Delete of categories
       
   884         for (int i = 0; i < categories.length; ++i)
       
   885         {
       
   886             ls.addCategory(categories[i]);
       
   887             ls.deleteCategory(categories[i]);
       
   888         }
       
   889 
       
   890         // Add many categories
       
   891         String category = "Test_Category_";
       
   892 
       
   893         // Add categories
       
   894         for (int i = 0; i < 100; ++i)
       
   895         {
       
   896             ls.addCategory(category + i);
       
   897         }
       
   898 
       
   899         // Delete categories
       
   900         for (int i = 0; i < 100; ++i)
       
   901         {
       
   902             ls.deleteCategory(category + i);
       
   903         }
       
   904     }
       
   905 
       
   906     void testAddCategoryTruncate() throws Exception
       
   907     {
       
   908         setCurrentTest("testAddCategoryTruncate()");
       
   909 
       
   910         ls = getDefaultLandmarkStore();
       
   911 
       
   912         String maxCategoryName = new String(new char[MAX_CATEGORY_LENGTH]);
       
   913 
       
   914         // Add category with too long name
       
   915         String tooLongCategoryName = maxCategoryName + "extra";
       
   916         ls.addCategory(tooLongCategoryName);
       
   917 
       
   918         // Check that the category was added
       
   919         Enumeration e = ls.getCategories();
       
   920         // Category name should be truncated to max length
       
   921         findCategory(e, maxCategoryName);
       
   922 
       
   923         // Try to delete with the too long category name
       
   924         ls.deleteCategory(tooLongCategoryName);
       
   925         // Make sure the category is still there
       
   926         e = ls.getCategories();
       
   927         findCategory(e, maxCategoryName);
       
   928 
       
   929         // Delete the category
       
   930         ls.deleteCategory(maxCategoryName);
       
   931 
       
   932         // Check that category has been removed
       
   933         e = ls.getCategories();
       
   934         while (e.hasMoreElements())
       
   935         {
       
   936             String s = (String) e.nextElement();
       
   937             assertTrue(!maxCategoryName.equals(s), "Category was not removed");
       
   938         }
       
   939     }
       
   940 
       
   941     void testAddLandmarkToCategory1() throws Exception
       
   942     {
       
   943         setCurrentTest("testAddLandmarkToCategory1()");
       
   944 
       
   945         ls = getDefaultLandmarkStore();
       
   946 
       
   947         // Create a category
       
   948         String category = "ViperTest Sightseeing";
       
   949         ls.addCategory(category);
       
   950 
       
   951         // Create a new Landmark in the category
       
   952         String name = "Eiffel Tower";
       
   953         Landmark lm = new Landmark(name, null, null, null);
       
   954         ls.addLandmark(lm, category);
       
   955 
       
   956         // Get Landmarks by category
       
   957         Enumeration e = ls.getLandmarks(category, null);
       
   958         int count = findLandmarks(e, new String[] { name });
       
   959         assertTrue(count == 1, "Wrong number of Landmarks found");
       
   960 
       
   961         ls.deleteLandmark(lm);
       
   962         ls.deleteCategory(category);
       
   963     }
       
   964 
       
   965     void testAddLandmarkToCategory2() throws Exception
       
   966     {
       
   967         setCurrentTest("testAddLandmarkToCategory2()");
       
   968 
       
   969         ls = getDefaultLandmarkStore();
       
   970 
       
   971         // Add a new Landmark to default category
       
   972         String name = "Liseberg";
       
   973         Landmark lm = new Landmark(name, null, null, null);
       
   974         ls.addLandmark(lm, null);
       
   975 
       
   976         // Create a category
       
   977         String category = "Amuzement Parks";
       
   978         ls.addCategory(category);
       
   979 
       
   980         // Get Landmarks by category
       
   981         Enumeration e = ls.getLandmarks(category, null);
       
   982         assertTrue(e == null, "Category should have been empty");
       
   983 
       
   984         // Add the Landmark to the category
       
   985         ls.addLandmark(lm, category);
       
   986 
       
   987         // Get Landmarks by category
       
   988         Enumeration e2 = ls.getLandmarks(category, null);
       
   989         int count = findLandmarks(e2, new String[] { name });
       
   990         assertTrue(count == 1, "Wrong number of Landmarks found");
       
   991 
       
   992         ls.deleteLandmark(lm);
       
   993         ls.deleteCategory(category);
       
   994     }
       
   995 
       
   996     void testAddLandmarkToCategory3() throws Exception
       
   997     {
       
   998         setCurrentTest("testAddLandmarkToCategory3()");
       
   999 
       
  1000         ls = getDefaultLandmarkStore();
       
  1001 
       
  1002         // Add category1
       
  1003         String category1 = "Food";
       
  1004         ls.addCategory(category1);
       
  1005         findCategory(ls.getCategories(), category1);
       
  1006 
       
  1007         // Add category2
       
  1008         String category2 = "Restuarants";
       
  1009         ls.addCategory(category2);
       
  1010         findCategory(ls.getCategories(), category2);
       
  1011 
       
  1012         // Create a new Landmark in category1
       
  1013         String name = "Da Pizza place";
       
  1014         Landmark lm = new Landmark(name, null, null, null);
       
  1015         ls.addLandmark(lm, category1);
       
  1016 
       
  1017         // Add the Landmark to category2
       
  1018         ls.addLandmark(lm, category2);
       
  1019 
       
  1020         // Get Landmarks by category1
       
  1021         Enumeration e1 = ls.getLandmarks(category1, null);
       
  1022         int count1 = findLandmarks(e1, new String[] { name });
       
  1023         assertTrue(count1 == 1, "Wrong number of Landmarks found");
       
  1024 
       
  1025         // Get Landmarks by category2
       
  1026         Enumeration e2 = ls.getLandmarks(category2, null);
       
  1027         int count2 = findLandmarks(e2, new String[] { name });
       
  1028         assertTrue(count2 == 1, "Wrong number of Landmarks found");
       
  1029 
       
  1030         ls.deleteLandmark(lm);
       
  1031         ls.deleteCategory(category1);
       
  1032         ls.deleteCategory(category2);
       
  1033     }
       
  1034 
       
  1035     void testAddLandmarkEmptyStrings() throws Exception
       
  1036     {
       
  1037         setCurrentTest("testAddLandmarkEmptyStrings()");
       
  1038 
       
  1039         ls = getDefaultLandmarkStore();
       
  1040 
       
  1041         try
       
  1042         {
       
  1043             Landmark lm = new Landmark("name", null, null, null);
       
  1044             ls.addLandmark(lm, ""); // Try to use empty string for category
       
  1045             assertTrue(false, "IllegalArgumentException not thrown");
       
  1046         }
       
  1047         catch (IllegalArgumentException iae)
       
  1048         {
       
  1049             // Exception thrown correctly
       
  1050         }
       
  1051 
       
  1052         // Create AddressInfo with all fields set to empty strings
       
  1053         AddressInfo addr = new AddressInfo();
       
  1054         for (int i = 0; i < 17; ++i)
       
  1055         {
       
  1056             addr.setField(i + 1, "");
       
  1057         }
       
  1058 
       
  1059         String name = "";
       
  1060         String description = " ";
       
  1061         Landmark lm = new Landmark(name, description, null, addr);
       
  1062         ls.addLandmark(lm, iCategory); // Should be ok
       
  1063 
       
  1064         // Get Landmarks
       
  1065         Enumeration e = ls.getLandmarks();
       
  1066         Landmark fetchedLm = findLandmark(e, name);
       
  1067         checkLandmark(fetchedLm, name, description, null, addr);
       
  1068 
       
  1069         ls.deleteLandmark(lm);
       
  1070     }
       
  1071 
       
  1072     void testAddLandmarkToCategoryDeleteCategory() throws Exception
       
  1073     {
       
  1074         setCurrentTest("testAddLandmarkToCategoryDeleteCategory()");
       
  1075 
       
  1076         ls = getDefaultLandmarkStore();
       
  1077 
       
  1078         // Create a category
       
  1079         String category = "Museums";
       
  1080         ls.addCategory(category);
       
  1081 
       
  1082         // Create a new Landmark in the category
       
  1083         String name = "Guggenheim";
       
  1084         Landmark lm = new Landmark(name, null, null, null);
       
  1085         ls.addLandmark(lm, category);
       
  1086 
       
  1087         // Delete the category
       
  1088         ls.deleteCategory(category);
       
  1089 
       
  1090         // Get Landmarks by category
       
  1091         Enumeration e = ls.getLandmarks(null, category);
       
  1092         assertTrue(e == null, "Expected getLandmarks to return null");
       
  1093 
       
  1094         // Make sure Landmark still exists
       
  1095         Landmark fetchedLm = findLandmark(ls.getLandmarks(), name);
       
  1096         checkLandmark(fetchedLm, name, null, null, null);
       
  1097 
       
  1098         ls.deleteLandmark(lm);
       
  1099     }
       
  1100 
       
  1101     void testAddLandmarkSameNameDifferentCategory() throws Exception
       
  1102     {
       
  1103         setCurrentTest("testAddLandmarkSameNameDifferentCategory()");
       
  1104 
       
  1105         ls = getDefaultLandmarkStore();
       
  1106 
       
  1107         // Create categories
       
  1108         String category1 = "category1";
       
  1109         String category2 = "category2";
       
  1110         ls.addCategory(category1);
       
  1111         ls.addCategory(category2);
       
  1112 
       
  1113         String name = "Same name";
       
  1114 
       
  1115         // Create a new Landmark in category1
       
  1116         String desc1 = "This landmark has a description";
       
  1117         Landmark lm1 = new Landmark(name, desc1, null, null);
       
  1118         ls.addLandmark(lm1, category1);
       
  1119 
       
  1120         // Create a new Landmark in category2
       
  1121         Landmark lm2 = new Landmark(name, null, null, null);
       
  1122         ls.addLandmark(lm2, category2);
       
  1123 
       
  1124         // Get Landmarks by category1
       
  1125         Enumeration e = ls.getLandmarks(category1, null);
       
  1126         Landmark fetchedLm = findLandmark(e, name);
       
  1127         checkLandmark(fetchedLm, name, desc1, null, null);
       
  1128 
       
  1129         // Get Landmarks by category2
       
  1130         e = ls.getLandmarks(category2, null);
       
  1131         fetchedLm = findLandmark(e, name);
       
  1132         checkLandmark(fetchedLm, name, null, null, null);
       
  1133 
       
  1134         // Get Landmarks by name
       
  1135         e = ls.getLandmarks(null, name);
       
  1136         int count = findLandmarks(e, new String[] { name, name });
       
  1137         assertTrue(count == 2, "Wrong number of landmarks found");
       
  1138 
       
  1139         ls.deleteLandmark(lm1);
       
  1140         ls.deleteLandmark(lm2);
       
  1141         ls.deleteCategory(category1);
       
  1142         ls.deleteCategory(category2);
       
  1143     }
       
  1144 
       
  1145     void testRemoveLandmarkFromCategoryBadArguments() throws Exception
       
  1146     {
       
  1147         setCurrentTest("testRemoveLandmarkFromCategoryBadArguments");
       
  1148 
       
  1149         ls = getDefaultLandmarkStore();
       
  1150 
       
  1151         String category1 = "Cat1";
       
  1152         String category2 = "Cat2";
       
  1153         ls.addCategory(category1);
       
  1154         ls.addCategory(category2);
       
  1155 
       
  1156         // Add a landmark to category1
       
  1157         Landmark lm = new Landmark("name", null, null, null);
       
  1158         ls.addLandmark(lm, category1);
       
  1159 
       
  1160         try
       
  1161         {
       
  1162             // Remove landmark from category with null Landmark parameter
       
  1163             ls.removeLandmarkFromCategory(null, category1);
       
  1164             assertTrue(false, "NullPointerException not thrown");
       
  1165         }
       
  1166         catch (NullPointerException npe)
       
  1167         {
       
  1168             // Exception was thrown correctly
       
  1169         }
       
  1170 
       
  1171         try
       
  1172         {
       
  1173             // Remove landmark from category with null category parameter
       
  1174             ls.removeLandmarkFromCategory(lm, null);
       
  1175             assertTrue(false, "NullPointerException not thrown");
       
  1176         }
       
  1177         catch (NullPointerException npe)
       
  1178         {
       
  1179             // Exception was thrown correctly
       
  1180         }
       
  1181 
       
  1182         // Silent failure if Landmark does not belong to category
       
  1183         ls.removeLandmarkFromCategory(lm, category2);
       
  1184 
       
  1185         // Silent failure if category does not exist
       
  1186         ls.removeLandmarkFromCategory(lm, "NonexistingCategory");
       
  1187 
       
  1188         // Silent failure if category is empty string
       
  1189         ls.removeLandmarkFromCategory(lm, "");
       
  1190 
       
  1191         // Silent failure if category name is too long
       
  1192         String tooLongCategory = new String(new char[MAX_CATEGORY_LENGTH + 1]);
       
  1193         ls.removeLandmarkFromCategory(lm, tooLongCategory);
       
  1194 
       
  1195         // Silent failure if Landmark does not belong to LandmarkStore
       
  1196         Landmark newLm = new Landmark("Not added", null, null, null);
       
  1197         ls.removeLandmarkFromCategory(newLm, category1);
       
  1198 
       
  1199         ls.deleteLandmark(lm);
       
  1200         ls.deleteCategory(category1);
       
  1201         ls.deleteCategory(category2);
       
  1202     }
       
  1203 
       
  1204     void testRemoveLandmarkFromCategory() throws Exception
       
  1205     {
       
  1206         setCurrentTest("testRemoveLandmarkFromCategory");
       
  1207 
       
  1208         String category = "Category";
       
  1209         String name = "lm";
       
  1210         Landmark lm = new Landmark(name, null, null, null);
       
  1211 
       
  1212         ls = getDefaultLandmarkStore();
       
  1213         ls.addCategory(category);
       
  1214         ls.addLandmark(lm, category);
       
  1215 
       
  1216         Enumeration e = ls.getLandmarks(category, name);
       
  1217         Landmark fetchedLm = findLandmark(e, name);
       
  1218         checkLandmark(fetchedLm, name, null, null, null);
       
  1219 
       
  1220         ls.removeLandmarkFromCategory(lm, category);
       
  1221 
       
  1222         Enumeration e2 = ls.getLandmarks(category, name);
       
  1223         assertTrue(e2 == null, "Landmark not removed from category");
       
  1224 
       
  1225         // Delete the category and Landmark from the store
       
  1226         ls.deleteCategory(category);
       
  1227         ls.deleteLandmark(lm);
       
  1228     }
       
  1229 
       
  1230     void testRemoveLandmarkFromCategory2() throws Exception
       
  1231     {
       
  1232         setCurrentTest("testRemoveLandmarkFromCategory2()");
       
  1233 
       
  1234         ls = getDefaultLandmarkStore();
       
  1235 
       
  1236         // Add categories
       
  1237         String category1 = "category1";
       
  1238         String category2 = "category2";
       
  1239         ls.addCategory(category1);
       
  1240         ls.addCategory(category2);
       
  1241 
       
  1242         String name = "lm";
       
  1243         Landmark lm = new Landmark(name, null, null, null);
       
  1244 
       
  1245         // Add landmark to category1 and category2
       
  1246         ls.addLandmark(lm, category1);
       
  1247         ls.addLandmark(lm, category2);
       
  1248 
       
  1249         // Remove landmark from category1
       
  1250         ls.removeLandmarkFromCategory(lm, category1);
       
  1251 
       
  1252         Enumeration e = ls.getLandmarks(category1, name);
       
  1253         assertTrue(e == null, "Landmark not removed from category");
       
  1254 
       
  1255         // Make sure landmark still belongs to category2
       
  1256         Enumeration e2 = ls.getLandmarks(category2, name);
       
  1257         Landmark fetchedLm = findLandmark(e2, name);
       
  1258         checkLandmark(fetchedLm, name, null, null, null);
       
  1259 
       
  1260         // Remove landmark from category2
       
  1261         ls.removeLandmarkFromCategory(lm, category2);
       
  1262 
       
  1263         Enumeration e3 = ls.getLandmarks(category2, name);
       
  1264         assertTrue(e3 == null, "Landmark not removed from category");
       
  1265 
       
  1266         // Delete the categories and Landmark from the store
       
  1267         ls.deleteCategory(category1);
       
  1268         ls.deleteCategory(category2);
       
  1269         ls.deleteLandmark(lm);
       
  1270     }
       
  1271 
       
  1272     void testGetLandmarksByNameBadArguments() throws Exception
       
  1273     {
       
  1274         setCurrentTest("testGetLandmarksByNameBadArguments()");
       
  1275 
       
  1276         String maxName = new String(new char[MAX_NAME_LENGTH]);
       
  1277 
       
  1278         ls = getDefaultLandmarkStore();
       
  1279         Landmark lm = new Landmark(maxName, null, null, null);
       
  1280         ls.addLandmark(lm, null);
       
  1281 
       
  1282         // Sanity check, find landmark by name
       
  1283         Enumeration e = ls.getLandmarks(null, maxName);
       
  1284         int count = findLandmarks(e, new String[] { maxName });
       
  1285         assertTrue(count == 1, "Wrong number of landmarks found");
       
  1286 
       
  1287         // Search with too long name
       
  1288         e = ls.getLandmarks(null, maxName + "x");
       
  1289         assertTrue(e == null, "Expected getLandmarks to return null (1)");
       
  1290 
       
  1291         // Search with empty string for name
       
  1292         e = ls.getLandmarks(null, "");
       
  1293         assertTrue(e == null, "Expected getLandmarks to return null (2)");
       
  1294 
       
  1295         ls.deleteLandmark(lm);
       
  1296     }
       
  1297 
       
  1298     void testGetLandmarksByName() throws Exception
       
  1299     {
       
  1300         setCurrentTest("testGetLandmarksByName()");
       
  1301 
       
  1302         ls = getDefaultLandmarkStore();
       
  1303 
       
  1304         addLandmarksAndCategoriesForSearch();
       
  1305 
       
  1306         Enumeration e;
       
  1307         int count;
       
  1308 
       
  1309         // Search for landmark that does not exist
       
  1310         e = ls.getLandmarks(null, searchName0);
       
  1311         assertTrue(e == null, "Expected getLandmarks to return null");
       
  1312 
       
  1313         // Search for landmarks 1
       
  1314         e = ls.getLandmarks(null, searchName1);
       
  1315         count = findLandmarks(e, new String[] { searchName1 });
       
  1316         assertTrue(count == 1, "Wrong number of landmarks found");
       
  1317 
       
  1318         // Search for landmarks 2
       
  1319         e = ls.getLandmarks(null, searchName2);
       
  1320         count = findLandmarks(e, new String[] { searchName2, searchName2 });
       
  1321         assertTrue(count == 2, "Wrong number of landmarks found");
       
  1322 
       
  1323         // Search for landmarks 3
       
  1324         e = ls.getLandmarks(null, searchName3);
       
  1325         count = findLandmarks(e, new String[] { searchName3, searchName3,
       
  1326                                                 searchName3
       
  1327                                               });
       
  1328         assertTrue(count == 3, "Wrong number of landmarks found");
       
  1329 
       
  1330         deleteAllLandmarksAndCategories();
       
  1331     }
       
  1332 
       
  1333     void testGetLandmarksByNameUncategorized() throws Exception
       
  1334     {
       
  1335         setCurrentTest("testGetLandmarksByNameUncategorized()");
       
  1336 
       
  1337         ls = getDefaultLandmarkStore();
       
  1338 
       
  1339         addLandmarksAndCategoriesForSearch();
       
  1340 
       
  1341         Enumeration e;
       
  1342         int count;
       
  1343 
       
  1344         // Find all landmarks that do not belong to a category
       
  1345         e = ls.getLandmarks("", null);
       
  1346         count = findLandmarks(e, new String[] { searchName1, searchName2 });
       
  1347         assertTrue(count == 2, "Wrong number of landmarks found");
       
  1348 
       
  1349         // Search for landmarks 1
       
  1350         e = ls.getLandmarks("", searchName1);
       
  1351         count = findLandmarks(e, new String[] { searchName1 });
       
  1352         assertTrue(count == 1, "Wrong number of landmarks found");
       
  1353 
       
  1354         // Search for landmarks 3
       
  1355         e = ls.getLandmarks("", searchName3);
       
  1356         assertTrue(e == null, "Expected getLandmarks to return null");
       
  1357 
       
  1358         deleteAllLandmarksAndCategories();
       
  1359     }
       
  1360 
       
  1361     void testGetLandmarksByNameWildcards() throws Exception
       
  1362     {
       
  1363         setCurrentTest("testGetLandmarksByNameWildcards()");
       
  1364 
       
  1365         ls = getDefaultLandmarkStore();
       
  1366 
       
  1367         String[] names = { "Hepa", "Huupa", "Lupa", "upo", "upa", "Lu?a", "*" };
       
  1368         int length = names.length;
       
  1369         Landmark[] lms = new Landmark[length];
       
  1370 
       
  1371         for (int i = 0; i < length; ++i)
       
  1372         {
       
  1373             lms[i] = new Landmark(names[i], null, null, null);
       
  1374             ls.addLandmark(lms[i], null);
       
  1375         }
       
  1376 
       
  1377         searchAndMatch("H*", new String[] { "Hepa", "Huupa" });
       
  1378         searchAndMatch("*o", new String[] { "upo" });
       
  1379         searchAndMatch("*upa", new String[] { "Huupa", "Lupa", "upa" });
       
  1380         searchAndMatch("L*a", new String[] { "Lupa", "Lu?a" });
       
  1381         searchAndMatch("*e*", new String[] { "Hepa" });
       
  1382 
       
  1383         searchAndMatch("?", new String[] { "*" });
       
  1384         searchAndMatch("up?", new String[] { "upa", "upo" });
       
  1385         searchAndMatch("?upa", new String[] { "Lupa" });
       
  1386         searchAndMatch("H?pa", new String[] { "Hepa" });
       
  1387         searchAndMatch("?u??", new String[] { "Lupa", "Lu?a" });
       
  1388         searchAndMatch("???", new String[] { "upo", "upa" });
       
  1389         searchAndMatch("?????", new String[] { "Huupa" });
       
  1390 
       
  1391         // Some complicated searches
       
  1392         searchAndMatch("*??pa", new String[] { "Hepa", "Huupa", "Lupa" });
       
  1393         searchAndMatch("*u?a", new String[] { "Huupa", "Lupa", "upa", "Lu?a" });
       
  1394         searchAndMatch("*up?", new String[] { "Huupa", "Lupa", "upo", "upa" });
       
  1395         searchAndMatch("?*up?", new String[] { "Huupa", "Lupa" });
       
  1396         searchAndMatch("*?*o*", new String[] { "upo" });
       
  1397 
       
  1398         // Find all landmarks
       
  1399         searchAndMatch("*", names);
       
  1400         searchAndMatch("?*", names);
       
  1401         searchAndMatch("*?", names);
       
  1402         searchAndMatch("*?*", names);
       
  1403 
       
  1404         for (int i = 0; i < length; ++i)
       
  1405         {
       
  1406             ls.deleteLandmark(lms[i]);
       
  1407         }
       
  1408     }
       
  1409 
       
  1410     void testGetLandmarksByCategoryBadArguments() throws Exception
       
  1411     {
       
  1412         setCurrentTest("testGetLandmarksByCategoryBadArguments()");
       
  1413 
       
  1414         ls = getDefaultLandmarkStore();
       
  1415 
       
  1416         String maxCategoryName = new String(new char[MAX_CATEGORY_LENGTH]);
       
  1417         ls.addCategory(maxCategoryName);
       
  1418 
       
  1419         String name = "Wherever";
       
  1420         Landmark lm = new Landmark(name, null, null, null);
       
  1421         ls.addLandmark(lm, maxCategoryName);
       
  1422 
       
  1423         // Sanity check, find landmark by category
       
  1424         Enumeration e = ls.getLandmarks(maxCategoryName, null);
       
  1425         int count = findLandmarks(e, new String[] { name });
       
  1426         assertTrue(count == 1, "Wrong number of landmarks found");
       
  1427 
       
  1428         // Search with too long category name
       
  1429         e = ls.getLandmarks(maxCategoryName + "x", null);
       
  1430         assertTrue(e == null, "Expected getLandmarks to return null (1)");
       
  1431 
       
  1432         // Search with empty string for category name
       
  1433         e = ls.getLandmarks("", null);
       
  1434         assertTrue(e == null, "Expected getLandmarks to return null (2)");
       
  1435 
       
  1436         ls.deleteLandmark(lm);
       
  1437         ls.deleteCategory(maxCategoryName);
       
  1438     }
       
  1439 
       
  1440     void testGetLandmarksByCategory() throws Exception
       
  1441     {
       
  1442         setCurrentTest("testGetLandmarksByCategory()");
       
  1443 
       
  1444         ls = getDefaultLandmarkStore();
       
  1445 
       
  1446         addLandmarksAndCategoriesForSearch();
       
  1447 
       
  1448         Enumeration e;
       
  1449         int count;
       
  1450 
       
  1451         // Search for category that does not exist
       
  1452         e = ls.getLandmarks(searchCategory0, null);
       
  1453         assertTrue(e == null, "Expected getLandmarks to return null");
       
  1454 
       
  1455         // Search for landmarks 1
       
  1456         e = ls.getLandmarks(searchCategory1, null);
       
  1457         count = findLandmarks(e, new String[] { searchName3 });
       
  1458         assertTrue(count == 1, "Wrong number of landmarks found");
       
  1459 
       
  1460         // Search for landmarks 2
       
  1461         e = ls.getLandmarks(searchCategory2, null);
       
  1462         count = findLandmarks(e, new String[] { searchName2, searchName3,
       
  1463                                                 searchName3
       
  1464                                               });
       
  1465         assertTrue(count == 3, "Wrong number of landmarks found");
       
  1466 
       
  1467         // Delete the categories and Landmarks from the store
       
  1468         deleteAllLandmarksAndCategories();
       
  1469     }
       
  1470 
       
  1471     void testGetLandmarksByNameAndCategory() throws Exception
       
  1472     {
       
  1473         setCurrentTest("testGetLandmarksByNameAndCategory()");
       
  1474 
       
  1475         ls = getDefaultLandmarkStore();
       
  1476 
       
  1477         addLandmarksAndCategoriesForSearch();
       
  1478 
       
  1479         Enumeration e;
       
  1480         int count;
       
  1481 
       
  1482         // Search with a category that does not exist
       
  1483         e = ls.getLandmarks(searchCategory0, searchName1);
       
  1484         assertTrue(e == null, "Expected getLandmarks to return null");
       
  1485 
       
  1486         // Search with a name that does not exist
       
  1487         e = ls.getLandmarks(searchCategory2, searchName0);
       
  1488         assertTrue(e == null, "Expected getLandmarks to return null");
       
  1489 
       
  1490         // Search with a name that does not exist in the category
       
  1491         e = ls.getLandmarks(searchCategory1, searchName1);
       
  1492         assertTrue(e == null, "Expected getLandmarks to return null");
       
  1493 
       
  1494         // Search for landmarks 1
       
  1495         e = ls.getLandmarks(searchCategory1, searchName3);
       
  1496         count = findLandmarks(e, new String[] { searchName3 });
       
  1497         assertTrue(count == 1, "Wrong number of landmarks found");
       
  1498 
       
  1499         // Search for landmarks 2
       
  1500         e = ls.getLandmarks(searchCategory2, searchName3);
       
  1501         count = findLandmarks(e, new String[] { searchName3, searchName3 });
       
  1502         assertTrue(count == 2, "Wrong number of landmarks found");
       
  1503 
       
  1504         // Search for landmarks 3
       
  1505         e = ls.getLandmarks(searchCategory2, searchName2);
       
  1506         Landmark fetchedLm = findLandmark(e, searchName2);
       
  1507         checkLandmark(fetchedLm, searchName2, searchCategory2, null, null);
       
  1508 
       
  1509         // Delete the categories and Landmarks from the store
       
  1510         deleteAllLandmarksAndCategories();
       
  1511     }
       
  1512 
       
  1513     void testGetLandmarksByAreaBadArguments() throws Exception
       
  1514     {
       
  1515         setCurrentTest("testGetLandmarksByAreaBadArguments()");
       
  1516         ls = getDefaultLandmarkStore();
       
  1517 
       
  1518         double minLat = -10.0d;
       
  1519         double maxLat = 10.0d;
       
  1520         double minLon = -110.0d;
       
  1521         double maxLon = 110.0d;
       
  1522 
       
  1523         // Bad minLat values
       
  1524         assertBadGetLandmarksByArea(-90.1d, maxLat, minLon, maxLon);
       
  1525         assertBadGetLandmarksByArea(90.1d, maxLat, minLon, maxLon);
       
  1526 
       
  1527         // Bad maxLat values
       
  1528         assertBadGetLandmarksByArea(minLat, -90.1d, minLon, maxLon);
       
  1529         assertBadGetLandmarksByArea(minLat, 90.1d, minLon, maxLon);
       
  1530 
       
  1531         // minLat > maxLat
       
  1532         assertBadGetLandmarksByArea(40.0d, 30.0d, minLon, maxLon);
       
  1533 
       
  1534         // Bad minLon values
       
  1535         assertBadGetLandmarksByArea(minLat, maxLat, -180.1d, maxLon);
       
  1536         assertBadGetLandmarksByArea(minLat, maxLat, 180.0d, maxLon);
       
  1537 
       
  1538         // Bad maxLon values
       
  1539         assertBadGetLandmarksByArea(minLat, maxLat, minLon, -180.1d);
       
  1540         assertBadGetLandmarksByArea(minLat, maxLat, minLon, 180.0d);
       
  1541 
       
  1542         try
       
  1543         {
       
  1544             Enumeration e = ls.getLandmarks(null, minLat, minLon, maxLat,
       
  1545                                             maxLon);
       
  1546             assertTrue(false,
       
  1547                        "IllegalArgumentException not thrown for long category name");
       
  1548         }
       
  1549         catch (IllegalArgumentException iae)
       
  1550         {
       
  1551             // Exception was thrown correctly
       
  1552             assertTrue(iae.getMessage() == null,
       
  1553                        "Message not allowed for exception: " + iae);
       
  1554         }
       
  1555 
       
  1556     }
       
  1557 
       
  1558     void testGetLandmarksByArea() throws Exception
       
  1559     {
       
  1560         setCurrentTest("testGetLandmarksByArea()");
       
  1561 
       
  1562         ls = getDefaultLandmarkStore();
       
  1563 
       
  1564         addLandmarksAndCategoriesForSearch();
       
  1565 
       
  1566         // -180 0 180
       
  1567         // |---------|- 90 1 = searchName1
       
  1568         // | | | 2 = searchName2
       
  1569         // | 1 | | 3 = searchName3, searchCategory1
       
  1570         // |----+----|- 0 4 = searchName3, searchCategory2
       
  1571         // |2 |4 5| 5 = searchName3, searchCategory2
       
  1572         // | 3 | |
       
  1573         // |---------|- -90
       
  1574 
       
  1575         Enumeration e;
       
  1576         int count;
       
  1577 
       
  1578         // Search an area where there are no landmarks
       
  1579         e = ls.getLandmarks(null, 0.0d, 90.0d, 0.0d, 179.9d);
       
  1580         assertTrue(e == null, "Expected getLandmarks to return null");
       
  1581 
       
  1582         // Search for landmarks 1
       
  1583         e = ls.getLandmarks(null, 0.0d, 90.0d, -180.0d, 0.0d);
       
  1584         count = findLandmarks(e, new String[] { searchName1 });
       
  1585         assertTrue(count == 1, "Wrong number of landmarks found");
       
  1586 
       
  1587         // Search for landmarks 2
       
  1588         e = ls.getLandmarks(null, -90.0d, 0.0d, -180.0d, 0.0d);
       
  1589         count = findLandmarks(e, new String[] { searchName2, searchName3 });
       
  1590         assertTrue(count == 2, "Wrong number of landmarks found");
       
  1591 
       
  1592         // Search for landmarks 3
       
  1593         e = ls.getLandmarks(null, -90.0d, 0.0d, 0.0d, 179.9d);
       
  1594         count = findLandmarks(e, new String[] { searchName3, searchName3 });
       
  1595         assertTrue(count == 2, "Wrong number of landmarks found");
       
  1596 
       
  1597         // Search for landmarks - find lm2, smallest possible area
       
  1598         e = ls.getLandmarks(null, -5.0d, -5.0d, -180.0d, -180.0d);
       
  1599         count = findLandmarks(e, new String[] { searchName2 });
       
  1600         assertTrue(count == 1, "Wrong number of landmarks found");
       
  1601 
       
  1602         // Search for landmarks - search lon [-180, 0] and [100, 180)
       
  1603         e = ls.getLandmarks(null, -90.0d, 90.0d, 100.0d, 0.0d);
       
  1604         count = findLandmarks(e, new String[] { searchName1, searchName2,
       
  1605                                                 searchName3, searchName3
       
  1606                                               });
       
  1607         assertTrue(count == 4, "Wrong number of landmarks found");
       
  1608 
       
  1609         deleteAllLandmarksAndCategories();
       
  1610     }
       
  1611 
       
  1612     void testGetLandmarksByArea2() throws Exception
       
  1613     {
       
  1614         setCurrentTest("testGetLandmarksByArea2()");
       
  1615 
       
  1616         ls = getDefaultLandmarkStore();
       
  1617         Landmark lm1 = new Landmark("Landmark1", "description",
       
  1618                                     new QualifiedCoordinates(10.0, 20.0, Float.NaN, Float.NaN,
       
  1619                                                              Float.NaN), null);
       
  1620         ls.addLandmark(lm1, null);
       
  1621         ls.addLandmark(new Landmark("Landmark2", null,
       
  1622                                     new QualifiedCoordinates(40.0, 30.0, 0, 0, 0), null), null);
       
  1623         ls.addLandmark(new Landmark("Landmark3", null,
       
  1624                                     new QualifiedCoordinates(30.0, -10.0, 0, 0, 0), null), null);
       
  1625         ls.addLandmark(new Landmark("Landmark4", null,
       
  1626                                     new QualifiedCoordinates(30.0, 165.0, 0, 0, 0), null), null);
       
  1627         ls.addLandmark(new Landmark("Landmark5", null,
       
  1628                                     new QualifiedCoordinates(30.0, -165.0, 0, 0, 0), null), null);
       
  1629 
       
  1630         Enumeration e = ls.getLandmarks(null, -30.0, 50.0, 5.0, 25.0);
       
  1631         Landmark gotLandmark = findLandmark(e, "Landmark1");
       
  1632         checkLandmark(gotLandmark, lm1.getName(), lm1.getDescription(), lm1
       
  1633                       .getQualifiedCoordinates(), lm1.getAddressInfo());
       
  1634         assertTrue(!e.hasMoreElements(), "Wrong number of landmarks found");
       
  1635 
       
  1636         deleteAllLandmarksAndCategories();
       
  1637     }
       
  1638 
       
  1639     void testGetLandmarksByAreaAndCategory() throws Exception
       
  1640     {
       
  1641         setCurrentTest("testGetLandmarksByAreaAndCategory()");
       
  1642 
       
  1643         ls = getDefaultLandmarkStore();
       
  1644 
       
  1645         addLandmarksAndCategoriesForSearch();
       
  1646 
       
  1647         // -180 0 180
       
  1648         // |---------|- 90 1 = searchName1
       
  1649         // | | | 2 = searchName2
       
  1650         // | 1 | | 3 = searchName3, searchCategory1
       
  1651         // |----+----|- 0 4 = searchName3, searchCategory2
       
  1652         // |2 |4 5| 5 = searchName3, searchCategory2
       
  1653         // | 3 | |
       
  1654         // |---------|- -90
       
  1655 
       
  1656         Enumeration e;
       
  1657         int count;
       
  1658 
       
  1659         // Search a category that does not contain any landmarks
       
  1660         e = ls.getLandmarks(searchCategory0, -90.0d, 90.0d, -180.0d, 179.9d);
       
  1661         assertTrue(e == null, "Expected getLandmarks to return null");
       
  1662 
       
  1663         // Search an area that does not contain any landmarks in the category
       
  1664         e = ls.getLandmarks(searchCategory2, -90.0d, 90.0d, -180.0d, 0.0d);
       
  1665         assertTrue(e == null, "Expected getLandmarks to return null");
       
  1666 
       
  1667         // Search for landmarks 1
       
  1668         e = ls.getLandmarks(searchCategory1, -90.0d, 90.0d, -180.0d, 179.9d);
       
  1669         count = findLandmarks(e, new String[] { searchName3 });
       
  1670         assertTrue(count == 1, "Wrong number of landmarks found");
       
  1671 
       
  1672         // Search for landmarks 2
       
  1673         e = ls.getLandmarks(searchCategory2, -90.0d, 0.0d, -180.0d, 179.9d);
       
  1674         count = findLandmarks(e, new String[] { searchName3, searchName3 });
       
  1675         assertTrue(count == 2, "Wrong number of landmarks found");
       
  1676 
       
  1677         // Search for landmarks 3
       
  1678         e = ls.getLandmarks(searchCategory2, -90.0d, 0.0d, 100.0d, 179.9d);
       
  1679         count = findLandmarks(e, new String[] { searchName3 });
       
  1680         assertTrue(count == 1, "Wrong number of landmarks found");
       
  1681 
       
  1682         // Search for landmarks 4 - smallest possible search area
       
  1683         e = ls.getLandmarks(searchCategory1, -90.0d, -90.0d, -21.0d, -21.0d);
       
  1684         count = findLandmarks(e, new String[] { searchName3 });
       
  1685         assertTrue(count == 1, "Wrong number of landmarks found");
       
  1686 
       
  1687         deleteAllLandmarksAndCategories();
       
  1688     }
       
  1689 
       
  1690     void testEnumerationDeleteLandmark1() throws Exception
       
  1691     {
       
  1692         setCurrentTest("testEnumerationDeleteLandmark1()");
       
  1693 
       
  1694         ls = getDefaultLandmarkStore();
       
  1695 
       
  1696         Landmark lm1 = new Landmark("lm1", null, null, null);
       
  1697         Landmark lm2 = new Landmark("lm2", null, null, null);
       
  1698         Landmark lm3 = new Landmark("lm3", null, null, null);
       
  1699 
       
  1700         ls.addLandmark(lm1, null);
       
  1701         ls.addLandmark(lm2, null);
       
  1702         ls.addLandmark(lm3, null);
       
  1703 
       
  1704         Enumeration e = ls.getLandmarks();
       
  1705         assertTrue(e.hasMoreElements(), "Enumeration is empty");
       
  1706         ls.deleteLandmark(lm1);
       
  1707         ls.deleteLandmark(lm2);
       
  1708 
       
  1709         // Expect lm1 because hasMoreElements should cache a Landmark
       
  1710         int count = findLandmarks(e, new String[] { "lm1", "lm3" });
       
  1711         assertTrue(count == 2, "Wrong number of landmarks found");
       
  1712 
       
  1713         ls.deleteLandmark(lm3);
       
  1714     }
       
  1715 
       
  1716     void testEnumerationDeleteLandmark2() throws Exception
       
  1717     {
       
  1718         setCurrentTest("testEnumerationDeleteLandmark2()");
       
  1719 
       
  1720         ls = getDefaultLandmarkStore();
       
  1721 
       
  1722         Landmark lm1 = new Landmark("lm1", null, null, null);
       
  1723         Landmark lm2 = new Landmark("lm2", null, null, null);
       
  1724         Landmark lm3 = new Landmark("lm3", null, null, null);
       
  1725 
       
  1726         ls.addLandmark(lm1, null);
       
  1727         ls.addLandmark(lm2, null);
       
  1728         ls.addLandmark(lm3, null);
       
  1729 
       
  1730         Enumeration e = ls.getLandmarks();
       
  1731         ls.deleteLandmark(lm1);
       
  1732         ls.deleteLandmark(lm2);
       
  1733         ls.deleteLandmark(lm3);
       
  1734 
       
  1735         assertTrue(!e.hasMoreElements(), "Wrong number of landmarks found");
       
  1736     }
       
  1737 
       
  1738     // ------------------------ Helper methods -----------------------
       
  1739 
       
  1740     private void searchAndMatch(String aPattern, String[] aNames)
       
  1741     throws Exception
       
  1742     {
       
  1743 
       
  1744         Enumeration e = ls.getLandmarks(null, aPattern);
       
  1745         int count = findLandmarks(e, aNames);
       
  1746         assertTrue(count == aNames.length, "Wrong number of landmarks found");
       
  1747     }
       
  1748 
       
  1749     // Add landmarks for testing getLandmarks
       
  1750     private void addLandmarksAndCategoriesForSearch() throws Exception
       
  1751     {
       
  1752         // -180 0 180
       
  1753         // |---------|- 90 1 = searchName1
       
  1754         // | | | 2 = searchName2
       
  1755         // | 1 | | 3 = searchName3, searchCategory1
       
  1756         // |----+----|- 0 4 = searchName3, searchCategory2
       
  1757         // |2 |4 5| 5 = searchName3, searchCategory2
       
  1758         // | 3 | |
       
  1759         // |---------|- -90
       
  1760 
       
  1761         float alt = 0.0f;
       
  1762         float hacc = 100.0f;
       
  1763         float vacc = 100.0f;
       
  1764 
       
  1765         QualifiedCoordinates qc1 = new QualifiedCoordinates(27.0d, -17.0d, alt,
       
  1766                 hacc, vacc);
       
  1767         QualifiedCoordinates qc2 = new QualifiedCoordinates(-5.0d, -180.0d,
       
  1768                 alt, hacc, vacc);
       
  1769         QualifiedCoordinates qc3 = new QualifiedCoordinates(-90.0d, -21.0d,
       
  1770                 alt, hacc, vacc);
       
  1771         QualifiedCoordinates qc4 = new QualifiedCoordinates(-5.0d, 5.0d, alt,
       
  1772                 hacc, vacc);
       
  1773         QualifiedCoordinates qc5 = new QualifiedCoordinates(-5.0d, 179.0d, alt,
       
  1774                 hacc, vacc);
       
  1775 
       
  1776         // | Category | Landmarks
       
  1777         // |-----------------|--------------------------------------
       
  1778         // | null | searchName1, searchName2
       
  1779         // | searchCategory1 | searchName3
       
  1780         // | searchCategory2 | searchName2, searchName3, searchName3
       
  1781 
       
  1782         // Add categories
       
  1783         ls.addCategory(searchCategory1);
       
  1784         ls.addCategory(searchCategory2);
       
  1785 
       
  1786         // Add landmarks to default category
       
  1787         Landmark lm1 = new Landmark(searchName1, null, qc1, null);
       
  1788         Landmark lm2 = new Landmark(searchName2, null, qc2, null);
       
  1789         ls.addLandmark(lm1, null);
       
  1790         ls.addLandmark(lm2, null);
       
  1791 
       
  1792         // Add landmarks to searchCategory1 (description = category)
       
  1793         Landmark lm3 = new Landmark(searchName3, searchCategory1, qc3, null);
       
  1794         ls.addLandmark(lm3, searchCategory1);
       
  1795 
       
  1796         // Add landmark to searchCategory2 (description = category)
       
  1797         Landmark lm4 = new Landmark(searchName3, searchCategory2, qc4, null);
       
  1798         Landmark lm5 = new Landmark(searchName3, searchCategory2, qc5, null);
       
  1799         Landmark lm6 = new Landmark(searchName2, searchCategory2, null, null);
       
  1800         ls.addLandmark(lm4, searchCategory2);
       
  1801         ls.addLandmark(lm5, searchCategory2);
       
  1802         ls.addLandmark(lm6, searchCategory2);
       
  1803     }
       
  1804 
       
  1805     private LandmarkStore getDefaultLandmarkStore()
       
  1806     {
       
  1807         LandmarkStore store = LandmarkStore.getInstance(null);
       
  1808         assertTrue(store != null, "LandmarkStore is null");
       
  1809         return store;
       
  1810     }
       
  1811 
       
  1812     private void assertEmptyLandmarkStore() throws Exception
       
  1813     {
       
  1814         setCurrentTest("assertEmptyLandmarkStore");
       
  1815         ls = getDefaultLandmarkStore();
       
  1816         Enumeration le = ls.getLandmarks();
       
  1817         assertTrue(le == null, "Landmarks exist in LandmarkStore");
       
  1818 
       
  1819         Enumeration ce = ls.getCategories();
       
  1820         assertTrue(!ce.hasMoreElements(), "Categories exist in LandmarkStore");
       
  1821     }
       
  1822 
       
  1823     private void saveRetrieveDelete(Landmark aLm) throws Exception
       
  1824     {
       
  1825         String name = aLm.getName();
       
  1826         String description = aLm.getDescription();
       
  1827         QualifiedCoordinates coords = aLm.getQualifiedCoordinates();
       
  1828         AddressInfo address = aLm.getAddressInfo();
       
  1829 
       
  1830         // Get the LandmarkStore
       
  1831         ls = LandmarkStore.getInstance(null);
       
  1832         assertTrue(ls != null, "LandmarkStore is null");
       
  1833 
       
  1834         // Save the landmark
       
  1835         ls.addLandmark(aLm, iCategory);
       
  1836 
       
  1837         // Get all landmarks and search for the one we added
       
  1838         Enumeration e = ls.getLandmarks();
       
  1839         Landmark fetchedLm = findLandmark(e, name);
       
  1840 
       
  1841         ls.deleteLandmark(aLm);
       
  1842         assertTrue(e != null, "No landmarks found in database");
       
  1843         checkLandmark(fetchedLm, name, description, coords, address);
       
  1844 
       
  1845         ls.deleteLandmark(aLm); // Try to delete again, should be silently
       
  1846         // ignored
       
  1847     }
       
  1848 
       
  1849     private Landmark findLandmark(Enumeration e, String aName)
       
  1850     {
       
  1851         Landmark foundLm = null;
       
  1852 
       
  1853         if (e != null)
       
  1854         {
       
  1855             // Search for the saved one
       
  1856             while (e.hasMoreElements())
       
  1857             {
       
  1858                 Landmark aLandmark = (Landmark) e.nextElement();
       
  1859                 assertTrue(aLandmark != null, "Enumeration contains a null");
       
  1860 
       
  1861                 if (aName.equals(aLandmark.getName()))
       
  1862                 {
       
  1863                     assertTrue(foundLm == null,
       
  1864                                "More that one landmark called " + aName);
       
  1865                     foundLm = aLandmark;
       
  1866                     break;
       
  1867                 }
       
  1868             }
       
  1869         }
       
  1870 
       
  1871         return foundLm;
       
  1872     }
       
  1873 
       
  1874     // Returns number of Landmarks in the enumeration
       
  1875     // Also checks that Landmarks with correct names are found
       
  1876     private int findLandmarks(Enumeration e, String[] aNames)
       
  1877     {
       
  1878         int length = aNames != null ? aNames.length : 0;
       
  1879 
       
  1880         boolean[] found = new boolean[length];
       
  1881         int numLandmarks = 0;
       
  1882 
       
  1883         if (e != null)
       
  1884         {
       
  1885             // Search for the saved one
       
  1886             while (e.hasMoreElements())
       
  1887             {
       
  1888                 Landmark aLandmark = (Landmark) e.nextElement();
       
  1889                 assertTrue(aLandmark != null, "Enumeration contains a null");
       
  1890                 ++numLandmarks;
       
  1891 
       
  1892                 for (int i = 0; i < length; ++i)
       
  1893                 {
       
  1894                     if (aNames[i].equals(aLandmark.getName()))
       
  1895                     {
       
  1896                         found[i] = true;
       
  1897                     }
       
  1898                 }
       
  1899             }
       
  1900         }
       
  1901 
       
  1902         for (int i = 0; i < length; ++i)
       
  1903         {
       
  1904             assertTrue(found[i], "Landmark '" + aNames[i] + "' not found");
       
  1905         }
       
  1906 
       
  1907         return numLandmarks;
       
  1908     }
       
  1909 
       
  1910     private void findCategory(Enumeration e, String aCategory)
       
  1911     {
       
  1912         assertTrue(e != null, "Categories enumeration is null");
       
  1913         assertTrue(e.hasMoreElements(), "Categories enumeration is empty");
       
  1914 
       
  1915         // Check that both category is present
       
  1916         boolean found = false;
       
  1917         while (e.hasMoreElements())
       
  1918         {
       
  1919             String s = (String) e.nextElement();
       
  1920             if (aCategory.equals(s))
       
  1921             {
       
  1922                 found = true;
       
  1923             }
       
  1924         }
       
  1925 
       
  1926         assertTrue(found, "Category " + aCategory + " not found");
       
  1927     }
       
  1928 
       
  1929     private void assertEqual(String errorMsg, String dis, String dat)
       
  1930     {
       
  1931         if (dis == null)
       
  1932         {
       
  1933             assertTrue(dat == null, errorMsg + ": " + dat + " != null");
       
  1934         }
       
  1935         else
       
  1936         {
       
  1937             assertTrue(dis.equals(dat), errorMsg + ": " + dis + " != " + dat);
       
  1938         }
       
  1939     }
       
  1940 
       
  1941     private void assertBadGetLandmarksByArea(double aMinLat, double aMaxLat,
       
  1942             double aMinLon, double aMaxLon) throws Exception
       
  1943     {
       
  1944         try
       
  1945         {
       
  1946             Enumeration e = ls.getLandmarks(null, aMinLat, aMinLon, aMaxLat,
       
  1947                                             aMaxLon);
       
  1948             String args = "getLandmarks(null, " + aMinLat + ", " + aMaxLat
       
  1949                           + ", " + aMinLon + ", " + aMaxLon + ")";
       
  1950 
       
  1951             assertTrue(false, "IllegalArgumentException not thrown for " + args);
       
  1952         }
       
  1953         catch (IllegalArgumentException iae)
       
  1954         {
       
  1955             // Exception was thrown correctly
       
  1956             assertTrue(iae.getMessage() == null,
       
  1957                        "Message not allowed for exception: " + iae);
       
  1958         }
       
  1959     }
       
  1960 
       
  1961     private void checkLandmark(Landmark aLm, String aName, String aDescription,
       
  1962                                QualifiedCoordinates aCoords, AddressInfo aAddress)
       
  1963     {
       
  1964 
       
  1965         assertTrue(aLm != null, "Landmark is null");
       
  1966 
       
  1967         // echo("name = " + aLm.getName() + ", desc = " + aLm.getDescription());
       
  1968 
       
  1969         // Check the values
       
  1970         String incorrect = "Wrong value retrieved from LandmarkStore: ";
       
  1971         assertEqual(incorrect + "name", aName, aLm.getName());
       
  1972         assertEqual(incorrect + "description", aDescription, aLm
       
  1973                     .getDescription());
       
  1974 
       
  1975         checkCoordinates(aCoords, aLm.getQualifiedCoordinates());
       
  1976         checkAddressInfo(aAddress, aLm.getAddressInfo());
       
  1977     }
       
  1978 
       
  1979     private void checkCoordinates(QualifiedCoordinates aCorrectCoords,
       
  1980                                   QualifiedCoordinates aThatCoords)
       
  1981     {
       
  1982         if (aCorrectCoords == null)
       
  1983         {
       
  1984             assertTrue(aThatCoords == null, "Coordinates should have been null");
       
  1985             return;
       
  1986         }
       
  1987 
       
  1988         assertTrue(aThatCoords != null,
       
  1989                    "Coordinate null when retrieved from LandmarkStore");
       
  1990 
       
  1991         // echo("lat = " + aThatCoords.getLatitude() +
       
  1992         // ", lon = " + aThatCoords.getLongitude() +
       
  1993         // ", alt = " + aThatCoords.getAltitude() +
       
  1994         // ", hacc = " + aThatCoords.getHorizontalAccuracy() +
       
  1995         // ", vacc = " + aThatCoords.getVerticalAccuracy());
       
  1996 
       
  1997         assertTrue(aThatCoords.getLatitude() == aCorrectCoords.getLatitude(),
       
  1998                    "Latitude incorrect when retrieved from LandmarkStore");
       
  1999         assertTrue(aThatCoords.getLongitude() == aCorrectCoords.getLongitude(),
       
  2000                    "Longitude incorrect when retrieved from LandmarkStore");
       
  2001         assertTrue(isSameFloat(aThatCoords.getAltitude(), aCorrectCoords
       
  2002                                .getAltitude()),
       
  2003                    "Altitude incorrect when retrieved from LandmarkStore");
       
  2004         assertTrue(isSameFloat(aThatCoords.getHorizontalAccuracy(),
       
  2005                                aCorrectCoords.getHorizontalAccuracy()),
       
  2006                    "HorizontalAccuracy incorrect when retrieved from LandmarkStore");
       
  2007         assertTrue(isSameFloat(aThatCoords.getVerticalAccuracy(),
       
  2008                                aCorrectCoords.getVerticalAccuracy()),
       
  2009                    "VerticalAccuracy incorrect when retrieved from LandmarkStore");
       
  2010     }
       
  2011 
       
  2012     private void checkAddressInfo(AddressInfo aCorrectAddress,
       
  2013                                   AddressInfo aThatAddress)
       
  2014     {
       
  2015         if (aCorrectAddress == null)
       
  2016         {
       
  2017             assertTrue(aThatAddress == null,
       
  2018                        "AddressInfo should have been null");
       
  2019             return;
       
  2020         }
       
  2021 
       
  2022         assertTrue(aThatAddress != null,
       
  2023                    "AddressInfo null when retrieved from LandmarkStore");
       
  2024 
       
  2025         // Check that address strings are the same as the input ones
       
  2026         for (int i = 1; i <= 17; ++i)
       
  2027         {
       
  2028             String original = aCorrectAddress.getField(i);
       
  2029             String stored = aThatAddress.getField(i);
       
  2030             if (original != null)
       
  2031             {
       
  2032                 assertTrue(original.equals(stored), "AddressInfo incorrect ("
       
  2033                            + i + "): " + original + " != " + stored);
       
  2034             }
       
  2035             else
       
  2036             {
       
  2037                 assertTrue(stored == null, "AddressInfo incorrect: (" + i
       
  2038                            + "): " + stored + " != null");
       
  2039             }
       
  2040         }
       
  2041     }
       
  2042 
       
  2043     private boolean isSameFloat(float aF1, float aF2)
       
  2044     {
       
  2045         if (new Float(aF1).equals(new Float(aF2)))
       
  2046         {
       
  2047             return true;
       
  2048         }
       
  2049         echo("Floats are different: " + aF1 + " != " + aF2);
       
  2050         echo("IntBits1 = " + Float.floatToIntBits(aF1));
       
  2051         echo("IntBits2 = " + Float.floatToIntBits(aF2));
       
  2052         return false;
       
  2053     }
       
  2054 
       
  2055 }