Util { public static String getNextAvailableName(Context context, String basename) { String name = basename; do { name = getNextName(name); } while (doesSurveyExist(context, name)); return name; }  static void ensureDirectoriesInPathExist(String path); static void ensureDataDirectoriesExist(Context context); static File[] getSurveyDirectories(Context context); static File[] getCalibrationFiles(Context context); static File[] getImportFiles(Context context); static String getNextDefaultSurveyName(Context context); static String getNextDefaultSurveyName(Context context, String defaultName); static String getNextAvailableName(Context context, String basename); static String getNextName(String basename); static boolean doesSurveyExist(Context context, String name); static boolean isSurveyNameUnique(Context context, String name); static String getDirectoryPathForSurvey(Context context, String name); static String getPathForSurveyFile(Context context, String name, String extension); static String getExportDirectoryPath(Context context, String exportFormat, String name); static void deleteSurvey(Context context, String name); static boolean doesFileExist(String path); static String getImportSourceDirectoryPath(Context context, Survey survey); static boolean wasSurveyImported(Context context, Survey survey); static File getFileSurveyWasImportedFrom(Context context, Survey survey); static boolean doWeHavePermissionToWriteToExternalStorage(Context context); static boolean isExternalStorageMounted(); static boolean isExternalStorageWriteable(Context context); static boolean isExternalStorageReadable(); static File getDocumentRoot(Context context); static File getInternalDirectory(Context context); static File getExternalDirectory(); static File getSurveyDirectory(Context context); static File getImportDir(Context context); static File getExportDirectory(Context context); static File getLogDirectory(Context context); static File getCalibrationDirectory(Context context); static String getAutosaveName(String filename); static Map<String, JSONArray> toMap(JSONObject object); static List<JSONObject> toList(JSONArray array); static List<String> toListOfStrings(JSONArray array); static String getPath(File directory, String filename);  }
@Test public void testGetNextAvailableNameInitial() throws Exception { String name = "foo"; PowerMockito.spy(Util.class); PowerMockito.doReturn(false).when(Util.class, "doesSurveyExist", null, name + "-2"); String next = Util.getNextAvailableName(null, name); Assert.assertEquals("foo-2", next); }
@Test public void testGetNextAvailableNameSecond() throws Exception { String name = "foo"; PowerMockito.spy(Util.class); PowerMockito.doReturn(true).when(Util.class, "doesSurveyExist", null, name + "-2"); PowerMockito.doReturn(false).when(Util.class, "doesSurveyExist", null, name + "-3"); String next = Util.getNextAvailableName(null, name); Assert.assertEquals("foo-3", next); }
@Test public void testGetNextAvailableNameWithExistingNumber() throws Exception { String name = "foo19"; PowerMockito.spy(Util.class); PowerMockito.doReturn(false).when(Util.class, "doesSurveyExist", null, "foo20"); String next = Util.getNextAvailableName(null, name); Assert.assertEquals("foo20", next); }