/*
 * #%L
 * Alfresco Repository
 * %%
 * Copyright (C) 2005 - 2016 Alfresco Software Limited
 * %%
 * This file is part of the Alfresco software. 
 * If the software was purchased under a paid Alfresco license, the terms of 
 * the paid license agreement will prevail.  Otherwise, the software is 
 * provided under the following open source license terms:
 * 
 * Alfresco is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Alfresco is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with Alfresco. If not, see .
 * #L%
 */
package org.alfresco.tools;
import static org.junit.Assert.assertEquals;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Iterator;
import org.alfresco.tools.RenameUser.User;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
 * Unit test for RenameUser. {@link org.alfresco.repo.security.person.PersonTest} contains integration tests.
 * 
 * @author Alan Davis
 */
public class RenameUserTest
{
    private String[] args;
    private File file;
    private RenameUser renameUser = new RenameUser();
    @Before
    public void setUp() throws Exception
    {
        args = new String[6];
        args[0] = "-user";
        args[1] = "admin";
        args[2] = "-pwd";
        args[3] = "admin";
        args[4] = "oldUsername";
        args[5] = "newUsername";
    }
    @After
    public void tearDown()// throws Exception
    {
        if (file != null && file.exists())
        {
            file.delete();
        }
    }
    
    private void createFile(String content) throws Exception
    {
        file = File.createTempFile("RenameUserTest", ".txt");
        args[4] = "-file";
        args[5] = file.getPath();
        BufferedWriter out = new BufferedWriter(new FileWriter(file));
        out.write(content);
        out.close();
    }
    private void processArgsAndValidate()
    {
        renameUser.processArgs(args);
        renameUser.context.validate();
    }
    
    // Check that the expected (supplied) usernames are in the context 
    private void assertUsers(String... usernames)
    {
        int length = usernames.length/2;
        assertEquals("Must have an even number of usernames passed to assertUsers", usernames.length, length*2);
        
        assertEquals(length, renameUser.context.userCount());
        Iterator iterator = renameUser.context.iterator();
        for (int i=0; i