package org.intermine.api.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import junit.framework.TestCase;

public class NameUtilTest extends TestCase {

    String[] names = {"newList_copy1", "name2"};

    public void testIsValidName() {
        assertFalse(NameUtil.isValidName(""));
        assertFalse(NameUtil.isValidName(" "));
        assertTrue(NameUtil.isValidName("valid"));
        assertTrue(NameUtil.isValidName("1234567890"));
        assertTrue(NameUtil.isValidName("_"));
        assertTrue(NameUtil.isValidName("ABCDEFGHIJKLMNOQRSTUVWXYZ"));
        assertTrue(NameUtil.isValidName("Valid list name with spaces"));
        assertTrue(NameUtil.isValidName("dot.dot.dot"));
        assertTrue(NameUtil.isValidName("dash-dash-dash"));
        assertTrue(NameUtil.isValidName("colon:colon:colon"));

        String badChars  = "¬!£$%^&*()+}{@~?<,/;'#][";

        for (int i = 0; i < badChars.length(); i++) {
            char c = badChars.charAt(i);
            String badName = c + "invalid " + c;
            assertFalse(NameUtil.isValidName(badName));
        }
    }

    public void testGenerateNewName() {

        String listName = "newList";
        String newListName = NameUtil.generateNewName(new HashSet<String>(Arrays.asList(names)), listName);
        assertEquals("newList_copy2", newListName);

        newListName = NameUtil.generateNewName(new HashSet(), listName);
        assertEquals("newList_copy1", newListName);
    }

    public void testValidateName() {
        List listNames = new ArrayList();
        listNames.add("name");
        String name = "name";
        assertEquals("name_1", NameUtil.validateName(listNames, name));
        listNames.add("name_1");
        assertEquals("name_2", NameUtil.validateName(listNames, name));

        String badListName = "badListName&";
        String correctedBadListName = "badListName";

        assertEquals(correctedBadListName, NameUtil.validateName(listNames, badListName));

        listNames.add(correctedBadListName);
        assertEquals(correctedBadListName + "_1", NameUtil.validateName(listNames, badListName));

        assertEquals("ab", NameUtil.validateName(listNames, "a`b"));
    }

    public void testFindNewQueryName() {
        Set<String> listNames = new HashSet();
        listNames.add("query_1");
        assertEquals("query_2", NameUtil.findNewQueryName(listNames));

        listNames.add("query_2");
        assertEquals("query_3", NameUtil.findNewQueryName(listNames, "query_1"));
        assertEquals("newQuery", NameUtil.findNewQueryName(listNames, "newQuery"));
    }
}

@@ -886,33 +886,32 @@
 n:colon%22));%0A
-%0A
         
 String badCh
@@ -902,56 +902,187 @@
     
-String badChars  = %22%C2%AC!%C2%A3$%25%5E&*()+%7D%7B@~?%3C,/;'#%5D%5B%22
+assertFalse(NameUtil.isValidName(%22Hello World!%22));%0A        char%5B%5D  badChars  = %7B'%C2%AC','!','%C2%A3','$','%25','%5E','&','*','(',')','+','%7D','%7B','@','~','?','%3C',',','/',';','%5C'','#','%5D','%5B'%7D
 ;%0A
-%0A
     
@@ -1120,18 +1120,16 @@
 s.length
-()
 ; i++) %7B
@@ -1162,18 +1162,11 @@
 hars
-.charAt(i)
+%5Bi%5D
 ;%0A  

test update