Código Java com testes - loop infinito?

Eu tento obter o relacionamento entre as pessoas. No entanto, quando eu executo o teste de unidade, o teste é executado para sempre, ele não obtém o resultado e meu uso da CPU foi alto. Alguém poderia ver o que há de errado com o meu código?

As relações de string são entradas de múltiplas linhas de string no formato de"A , B C , D" OndeA é o pai deB eC é o pai deD.

Este é o construtor padrão para o código e a entrada no formato de string. Não precisamos verificar se o formato está correto:

public SeeRelations(String relations){
    this.relations = relations;
}

Esta é a função auxiliar para obter cada linha da string da entrada formatada:

//helper function to get each line of the string
private ArrayList<String> lineRelations(){
    int i;
    ArrayList<String> lineRelations = new ArrayList<String>();
    String[] lines = relations.split("\n");
    for(i = 0; i < lines.length; i++){
        lineRelations.add(lines[i]);
    }
    return lineRelations;
}

Esta é a função para colocar todas as relações da string formatada de entrada em arraylists:

//helper function to put each of the relationship in arraylists
private ArrayList<ArrayList<String>> allRelations(){
    int i;
    ArrayList<ArrayList<String>> allRelations = new ArrayList<ArrayList<String>>();
    ArrayList<String> lineRelations = lineRelations();
    for(i = 0; i < lineRelations.size(); i++){
        ArrayList<String> eachLine = new ArrayList<String>(Arrays.asList(lineRelations.get(i).split("\\s*,\\s*")));
        allRelations.add(eachLine);
    }
    return allRelations;
}

Este é o método para verificar se o nome da entrada existe:

//helper function to see if the name exist for seeRelations()
private boolean hasThisName(String name){
    ArrayList<ArrayList<String>> allRelations = allRelations();
    int i;
    int j;
    for(i = 0; i < allRelations.size(); i++){
        for(j = 0; j < allRelations.get(i).size(); j++){
            if(name.equals(allRelations.get(i).get(j))){
                return true;
            }
        }
    }
    return false;
}

Esta é a função para obter o número de geração entre duas pessoas:

//helper function to get Generation number of seeRelations()
private int getGenerationNum(String person, String ancestor){
    ArrayList<ArrayList<String>> allRelations = allRelations();
    String name;
    int i;
    int j;
    int generationNum = 0;
    for(i = 0, j = 0, name = ancestor; i < allRelations.size(); i++){
        if(name.equals(allRelations.get(i).get(0)) && !person.equals(allRelations.get(i).get(1))){
            generationNum++;
            ancestor = allRelations.get(i).get(1);
            i = 0;
            j = 1;
        }
        else if(ancestor.equals(allRelations.get(i).get(0)) && person.equals(allRelations.get(i).get(1))){
            generationNum++;
            j = 1;
            break;
        }
    }
    if(j == 0){
        return 0;
    }
    else{
        return generationNum;
    }
}

Este é o método para obter vários"great" para o resultado final:

private String great(int num){
    int i;
    String great = "";
    for(i = 0; i < num; i++){
        great += "great";
    }
    return great;
}

Este é o meu método final para verificar a relação entre duas pessoas:

public String SeeRelations(String person, String ancestor){
    int generationNum = getGenerationNum(person, ancestor);
    String great = great(generationNum  - 2);
    if(!(hasThisName(person) && hasThisName(ancestor))){
        return null;
    }
    else{
        if(generationNum == 0){
            return null;
        }
        else if(generationNum == 1){
            return ancestor + " is the parent of " + person;
        }
        else if(generationNum == 2){
            return ancestor + " is the grandparent of " + person;
        }
        else{
            return ancestor + " is the" + " " +  great +"grandparent of " + person;
          }
    }
}

Estes são os meus casos de teste, E isso corre para sempre e não conseguiu obter um resultado

public class FamilyTreeTest {

    @Test
    public void testSeeRelations() {
        FamilyTree relation2 = new FamilyTree("John Doe ,   Mary Smith" + "\n" + "Martin Weasel ,  John Doe");
        assertEquals("Martin Weasel is the grandparent of Mary Smith", familyTree2.SeeRelations("Mary Smith", "Martin Weasel"));
    }

questionAnswers(1)

yourAnswerToTheQuestion