javaextensions/location/tsrc/vipertest/src/MultipleLandmarkStoresTest.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 javax.microedition.location.*;
       
    18 
       
    19 import java.util.*;
       
    20 import java.io.IOException;
       
    21 
       
    22 public class MultipleLandmarkStoresTest extends ViperUnitTest
       
    23 {
       
    24     private static final String STORE_NAME = "store1";
       
    25 
       
    26     public MultipleLandmarkStoresTest()
       
    27     {
       
    28         super("MultipleLandmarkStoresTest");
       
    29     }
       
    30 
       
    31     // These tests assume that you can create/delete LandmarkStores
       
    32     protected void runTest() throws java.lang.Throwable
       
    33     {
       
    34         testGetInstanceDefaultOnly();
       
    35         testListLandmarkStoresOnlyDefault();
       
    36         testCreateLandmarkStoreBadArguments();
       
    37         testDeleteLandmarkStoreBadArguments();
       
    38         testCreateListDeleteLandmarkStore();
       
    39         testCreateLandmarkStoreAlreadyExists();
       
    40         testCreateGetInstanceSameObject();
       
    41         testCaseInsensitivity();
       
    42         testCreateDeleteUse();
       
    43         testAddLandmarkAndCategoryToStore();
       
    44     }
       
    45 
       
    46     // Test getInstance when only the default store exists
       
    47     void testGetInstanceDefaultOnly() throws Exception
       
    48     {
       
    49         setCurrentTest("testGetInstanceDefaultOnly()");
       
    50         removeExistingStores();
       
    51 
       
    52         LandmarkStore ls = null;
       
    53 
       
    54         // Try to get nonexisting store
       
    55         ls = LandmarkStore.getInstance("Apa");
       
    56         assertTrue(ls == null, "Got an instance for nonexisting store1");
       
    57 
       
    58         ls = LandmarkStore.getInstance("c:Heppa");
       
    59         assertTrue(ls == null, "Got an instance for nonexisting store");
       
    60 
       
    61         ls = LandmarkStore.getInstance("Huppa (d:)");
       
    62         assertTrue(ls == null, "Got an instance for nonexisting store");
       
    63 
       
    64         // Get default store
       
    65         ls = LandmarkStore.getInstance(null);
       
    66         assertTrue(ls != null, "Instance was null for default store");
       
    67     }
       
    68 
       
    69     void testListLandmarkStoresOnlyDefault() throws Exception
       
    70     {
       
    71         setCurrentTest("testListLandmarkStoresOnlyDefault()");
       
    72 
       
    73         // List all landmark stores, should return null
       
    74         String[] stores = LandmarkStore.listLandmarkStores();
       
    75         assertTrue(stores == null, "Landmarkstore found, should have been null");
       
    76     }
       
    77 
       
    78     void testCreateLandmarkStoreBadArguments() throws Exception
       
    79     {
       
    80         setCurrentTest("testCreateLandmarkStoreBadArguments()");
       
    81 
       
    82         try
       
    83         {
       
    84             // Create with null
       
    85             LandmarkStore.createLandmarkStore(null);
       
    86             assertTrue(false,
       
    87                        "No Exception thrown for createLandmarkStore(null)");
       
    88         }
       
    89         catch (NullPointerException npe)
       
    90         {
       
    91             // Exception was thrown correctly
       
    92             assertNoMessage(npe);
       
    93         }
       
    94 
       
    95         assertBadCreateStore("file://Helloworld");
       
    96         assertBadCreateStore("c:Heppa");
       
    97         assertBadCreateStore("Heppa (d:)");
       
    98         assertBadCreateStore("x:Heppa");
       
    99         assertBadCreateStore("/Bad \\Name %&#");
       
   100         assertBadCreateStore("..\\Heppa");
       
   101         assertBadCreateStore("../Heppa");
       
   102         assertBadCreateStore("");
       
   103     }
       
   104 
       
   105     void testDeleteLandmarkStoreBadArguments() throws Exception
       
   106     {
       
   107         setCurrentTest("testDeleteLandmarkStoreBadArguments()");
       
   108 
       
   109         try
       
   110         {
       
   111             // Delete with null
       
   112             LandmarkStore.deleteLandmarkStore(null);
       
   113             assertTrue(false, "No exception thrown for DeleteLandmarkStore()");
       
   114         }
       
   115         catch (NullPointerException npe)
       
   116         {
       
   117             // Exception was thrown correctly
       
   118             assertNoMessage(npe);
       
   119         }
       
   120 
       
   121         LandmarkStore.deleteLandmarkStore("Nonexisting"); // Silently fail
       
   122         LandmarkStore.deleteLandmarkStore("x:Nonexisting"); // Silently fail
       
   123         LandmarkStore.deleteLandmarkStore("/Bad \\Name %&#"); // Silently fail
       
   124     }
       
   125 
       
   126     void testCreateListDeleteLandmarkStore() throws Exception
       
   127     {
       
   128         setCurrentTest("testCreateListDeleteLandmarkStore()");
       
   129         String name = "store";
       
   130 
       
   131         // Create the store
       
   132         LandmarkStore.createLandmarkStore(name);
       
   133 
       
   134         // Check that store is returned in list
       
   135         assertTrue(storeExists(name), "Created store not found in list");
       
   136 
       
   137         // Delete the store
       
   138         LandmarkStore.deleteLandmarkStore(name);
       
   139         assertTrue(!storeExists(name), "Deleted store found in list");
       
   140     }
       
   141 
       
   142     void testCreateLandmarkStoreAlreadyExists() throws Exception
       
   143     {
       
   144         setCurrentTest("testCreateLandmarkStoreAlreadyExists()");
       
   145         String name = "Create me twice";
       
   146 
       
   147         // Create the store
       
   148         LandmarkStore.createLandmarkStore(name);
       
   149         assertTrue(storeExists(name), "Created store not found in list");
       
   150 
       
   151         // Create again with the same name
       
   152         try
       
   153         {
       
   154             LandmarkStore.createLandmarkStore(name);
       
   155             assertTrue(false, "No exception thrown for createLandmarkStore!");
       
   156         }
       
   157         catch (IllegalArgumentException iae)
       
   158         {
       
   159             // Exception thrown correctly
       
   160         }
       
   161 
       
   162         // Delete the store
       
   163         LandmarkStore.deleteLandmarkStore(name);
       
   164         assertTrue(!storeExists(name), "Deleted store found in list");
       
   165     }
       
   166 
       
   167     void testCreateGetInstanceSameObject() throws Exception
       
   168     {
       
   169         setCurrentTest("testCreateGetInstanceSameObject()");
       
   170 
       
   171         // Create the store
       
   172         LandmarkStore.createLandmarkStore(STORE_NAME);
       
   173         assertTrue(storeExists(STORE_NAME), "Created store not found in list");
       
   174 
       
   175         LandmarkStore ls1 = LandmarkStore.getInstance(STORE_NAME);
       
   176         LandmarkStore ls2 = LandmarkStore.getInstance(STORE_NAME);
       
   177 
       
   178         assertTrue(ls1.equals(ls2), "Not the same instance!");
       
   179 
       
   180         // Delete the store
       
   181         LandmarkStore.deleteLandmarkStore(STORE_NAME);
       
   182         assertTrue(!storeExists(STORE_NAME), "Deleted store found in list");
       
   183     }
       
   184 
       
   185     void testCaseInsensitivity() throws Exception
       
   186     {
       
   187         setCurrentTest("testCaseInsensitivity()");
       
   188         String lowerName1 = "store";
       
   189         String upperName1 = "STORE";
       
   190         String mixedName1 = "sToRe";
       
   191 
       
   192         String lowerName2 = "smores";
       
   193         String upperName2 = "SMORES";
       
   194         String mixedName2 = "SmoRES";
       
   195 
       
   196         String lowerName3 = "da store";
       
   197         String upperName3 = "DA STORE";
       
   198         String mixedName3 = "dA sToRe";
       
   199 
       
   200         // Create
       
   201         LandmarkStore.createLandmarkStore(lowerName1);
       
   202         LandmarkStore.createLandmarkStore(mixedName2);
       
   203         LandmarkStore.createLandmarkStore(upperName3);
       
   204 
       
   205         // Open
       
   206         LandmarkStore ls1 = LandmarkStore.getInstance(upperName1);
       
   207         assertTrue(ls1 != null, "Instance was null for store: " + upperName1);
       
   208 
       
   209         LandmarkStore ls2 = LandmarkStore.getInstance(lowerName2);
       
   210         assertTrue(ls2 != null, "Instance was null for store: " + lowerName2);
       
   211 
       
   212         LandmarkStore ls3 = LandmarkStore.getInstance(mixedName3);
       
   213         assertTrue(ls3 != null, "Instance was null for store: " + mixedName3);
       
   214 
       
   215         // Delete
       
   216         LandmarkStore.deleteLandmarkStore(mixedName1);
       
   217         LandmarkStore.deleteLandmarkStore(upperName2);
       
   218         LandmarkStore.deleteLandmarkStore(lowerName3);
       
   219     }
       
   220 
       
   221     void testCreateDeleteUse() throws Exception
       
   222     {
       
   223         setCurrentTest("testCreateDeleteUse()");
       
   224 
       
   225         // Create LandmarkStore
       
   226         LandmarkStore.createLandmarkStore(STORE_NAME);
       
   227         LandmarkStore ls = LandmarkStore.getInstance(STORE_NAME);
       
   228 
       
   229         // Add a Landmark
       
   230         Landmark lm = new Landmark("Hepp", null, null, null);
       
   231         ls.addLandmark(lm, null);
       
   232 
       
   233         // Get landmarks enumeration
       
   234         Enumeration eLms = ls.getLandmarks();
       
   235         assertTrue(eLms != null, "getLandmarks returned null");
       
   236 
       
   237         // Get categories enumeration
       
   238         Enumeration eCats = ls.getCategories();
       
   239         assertTrue(eCats != null, "getCategories returned null");
       
   240 
       
   241         // Delete the store
       
   242         LandmarkStore.deleteLandmarkStore(STORE_NAME);
       
   243 
       
   244         // All operations should fail now
       
   245         try
       
   246         {
       
   247             assertTrue(!eLms.hasMoreElements(), "eLms should not have elements");
       
   248             Object o = eLms.nextElement();
       
   249             assertTrue(false, "No Exception thrown for eLms");
       
   250         }
       
   251         catch (NoSuchElementException nsee)
       
   252         {
       
   253             // Exception thrown correctly
       
   254         }
       
   255         catch (Exception e)
       
   256         {
       
   257             assertTrue(false, "Wrong Exception thrown for eLms: " + e);
       
   258         }
       
   259 
       
   260         try
       
   261         {
       
   262             assertTrue(!eCats.hasMoreElements(),
       
   263                        "eCats should not have elements");
       
   264             Object o = eCats.nextElement();
       
   265             assertTrue(false, "No Exception thrown for eCats");
       
   266         }
       
   267         catch (NoSuchElementException nsee)
       
   268         {
       
   269             // Exception thrown correctly
       
   270         }
       
   271         catch (Exception e)
       
   272         {
       
   273             assertTrue(false, "Wrong Exception thrown for eCats: " + e);
       
   274         }
       
   275 
       
   276         for (int i = 0; i <= 8; ++i)
       
   277         {
       
   278             try
       
   279             {
       
   280                 failWithIOException(ls, lm, i);
       
   281                 assertTrue(false, "No IOException thrown: " + i);
       
   282             }
       
   283             catch (IOException ioe)
       
   284             {
       
   285                 // Exception thrown correctly
       
   286             }
       
   287             catch (Exception e)
       
   288             {
       
   289                 assertTrue(false, "Wrong Exception thrown: " + i + ", " + e);
       
   290             }
       
   291         }
       
   292 
       
   293         assertTrue(ls.getCategories() == null,
       
   294                    "Expected getCategories to return null");
       
   295     }
       
   296 
       
   297     void testAddLandmarkAndCategoryToStore() throws Exception
       
   298     {
       
   299         setCurrentTest("addLandmarkAndCategoryToStore()");
       
   300 
       
   301         removeExistingStores();
       
   302 
       
   303         LandmarkStore.createLandmarkStore(STORE_NAME);
       
   304         LandmarkStore ls = LandmarkStore.getInstance(STORE_NAME);
       
   305 
       
   306         // add category to store
       
   307         String category = "test category";
       
   308         ls.addCategory(category);
       
   309 
       
   310         // add Landmark in category to store
       
   311         Landmark landmark = new Landmark("test landmark", "test description",
       
   312                                          new QualifiedCoordinates(80d, 45d, 250f, 20f, 30f), null);
       
   313 
       
   314         addLandmarkToStore(ls, landmark, category);
       
   315 
       
   316         // delete landmark and category
       
   317         ls.deleteLandmark(landmark);
       
   318         ls.deleteCategory(category);
       
   319     }
       
   320 
       
   321     // ------------------------ Helper methods -----------------------
       
   322 
       
   323     private void assertBadCreateStore(String aStoreName) throws Exception
       
   324     {
       
   325         try
       
   326         {
       
   327             LandmarkStore.createLandmarkStore(aStoreName);
       
   328             assertTrue(false, "No exception thrown for createLandmarkStore()");
       
   329         }
       
   330         catch (IllegalArgumentException e)
       
   331         {
       
   332             // Exception was thrown correctly
       
   333         }
       
   334         catch (Exception e)
       
   335         {
       
   336             assertTrue(false,
       
   337                        "Wrong exception thrown for createLandmarkStore(\""
       
   338                        + aStoreName + "\"): " + e);
       
   339         }
       
   340     }
       
   341 
       
   342     private boolean storeExists(String aStoreName) throws Exception
       
   343     {
       
   344         String[] stores = LandmarkStore.listLandmarkStores();
       
   345         if (stores == null)
       
   346         {
       
   347             return false;
       
   348         }
       
   349 
       
   350         String listName = null;
       
   351         for (int i = 0; i < stores.length; ++i)
       
   352         {
       
   353             assertTrue(stores[i] != null, "Null found in listLandmarkStores");
       
   354             if (aStoreName.equals(stores[i]))
       
   355             {
       
   356                 listName = stores[i];
       
   357                 break;
       
   358             }
       
   359         }
       
   360 
       
   361         return aStoreName.equals(listName);
       
   362     }
       
   363 
       
   364     private void failWithIOException(LandmarkStore aLs, Landmark aLm, int aIndex)
       
   365     throws Exception
       
   366     {
       
   367         Enumeration e = null;
       
   368 
       
   369         switch (aIndex)
       
   370         {
       
   371         case 0:
       
   372             aLs.addLandmark(aLm, null);
       
   373             break;
       
   374         case 1:
       
   375             e = aLs.getLandmarks(null, null);
       
   376             break;
       
   377         case 2:
       
   378             e = aLs.getLandmarks();
       
   379             break;
       
   380         case 3:
       
   381             e = aLs.getLandmarks(null, -90, 90, -180, 179);
       
   382             break;
       
   383         case 4:
       
   384             aLs.removeLandmarkFromCategory(aLm, "categoryName");
       
   385             break;
       
   386         case 5:
       
   387             aLs.updateLandmark(aLm);
       
   388             break;
       
   389         case 6:
       
   390             aLs.deleteLandmark(aLm);
       
   391             break;
       
   392         case 7:
       
   393             aLs.addCategory("categoryName");
       
   394             break;
       
   395         case 8:
       
   396             aLs.deleteCategory("categoryName");
       
   397             break;
       
   398         default:
       
   399             assertTrue(false, "No test for index: " + aIndex);
       
   400         }
       
   401     }
       
   402 
       
   403 }