ModelItemNamer { public static String createName(String baseName, Iterable nameHolders) { int maxExistingIndex = 0; for (Object nameHolder : nameHolders) { if (nameHolder == null) { continue; } String name = getName(nameHolder); if (name.contains(baseName)) { try { int beginIndex = name.indexOf(baseName) + baseName.length(); int indexInProjectName = Integer.parseInt(name.substring(beginIndex).trim()); if (indexInProjectName > maxExistingIndex) { maxExistingIndex = indexInProjectName; } } catch (Exception e) { } } } return baseName + " " + (++maxExistingIndex); } private  ModelItemNamer(); static String createName(String baseName, Iterable nameHolders); static boolean isUnique(String baseName, Iterable nameHolders); static String getUniqueName(String def, ModelItem parent);  }
@Test public void createsFirstRestProjectWithNameRESTProject1() throws Exception { mockWorkspaceProjects(); String expectedFirstProjectName = DEFAULT_PROJECT_NAME + " 1"; assertThat(ModelItemNamer.createName(DEFAULT_PROJECT_NAME, workspace.getProjectList()), Is.is(expectedFirstProjectName)); }
@Test public void createsProjectWithNameRESTProject2IfProjectWithNameRESTProject1AlreadyExists() throws Exception { mockWorkspaceProjects(DEFAULT_PROJECT_NAME + " 1"); String expectedFirstProjectName = DEFAULT_PROJECT_NAME + " 2"; assertThat(ModelItemNamer.createName(DEFAULT_PROJECT_NAME, workspace.getProjectList()), Is.is(expectedFirstProjectName)); }
@Test public void createsProjectWithNameRESTProject3IfProjectsWithNameRESTProject1AndRESTProject2AlreadyExist() throws Exception { mockWorkspaceProjects(DEFAULT_PROJECT_NAME + " 1", DEFAULT_PROJECT_NAME + " 2"); String expectedFirstProjectName = DEFAULT_PROJECT_NAME + " 3"; assertThat(ModelItemNamer.createName(DEFAULT_PROJECT_NAME, workspace.getProjectList()), Is.is(expectedFirstProjectName)); }
@Test public void createsProjectWithNameRESTProject4IfProjectsExistWithNameRESTProject1AndRESTProject3() throws Exception { mockWorkspaceProjects(DEFAULT_PROJECT_NAME + "1", DEFAULT_PROJECT_NAME + "3"); String expectedFirstProjectName = DEFAULT_PROJECT_NAME + " 4"; assertThat(ModelItemNamer.createName(DEFAULT_PROJECT_NAME, workspace.getProjectList()), Is.is(expectedFirstProjectName)); }
@Test public void doesNotThrowAnExceptionIfProjectExistsWithNameRESTProject3x() throws Exception { mockWorkspaceProjects(DEFAULT_PROJECT_NAME + " 3x"); String expectedFirstProjectName = DEFAULT_PROJECT_NAME + " 1"; assertThat(ModelItemNamer.createName(DEFAULT_PROJECT_NAME, workspace.getProjectList()), Is.is(expectedFirstProjectName)); }