com.dougchimento.at.AtManagerSimpleTest.java Source code

Java tutorial

Introduction

Here is the source code for com.dougchimento.at.AtManagerSimpleTest.java

Source

package com.dougchimento.at;

/*
 * Copyright (c) 2012. Douglas Chimento
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.apache.commons.vfs2.AllFileSelector;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.VFS;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;

/**
 * @author Douglas Chimento
 */
public class AtManagerSimpleTest {

    static AtConfig atConfig;
    static FileObject fakeAt;
    static FileObject atListOut;
    static FileObject atListRemovedOut;
    static FileObject atListAddedOut;
    static File tmpDir;
    static AtManager atManager;

    @BeforeClass
    public void setUp() throws Exception {
        FileSystemManager manager = VFS.getManager();
        atManager = At.getManager(new AtTestConfig("atTest.properties"));

        FileObject fakeAtResource = manager
                .resolveFile(new File(ClassLoader.getSystemResource("fakeAt.sh").getFile()).toURI().toString());
        FileObject atListOutResource = manager.resolveFile(
                new File(ClassLoader.getSystemResource("atListOutput.txt").getFile()).toURI().toString());
        FileObject atListRemovedOutResource = manager.resolveFile(
                new File(ClassLoader.getSystemResource("atListRemovedOutput.txt").getFile()).toURI().toString());
        FileObject atListAddedOutResource = manager.resolveFile(
                new File(ClassLoader.getSystemResource("atListAddedOutput.txt").getFile()).toURI().toString());

        fakeAt = manager.resolveFile("file://tmp/.at/fakeAt.sh");
        atListOut = manager.resolveFile("file://tmp/.at/atListOutput.txt");
        atListRemovedOut = manager.resolveFile("file://tmp/.at/atListRemovedOutput.txt");
        atListAddedOut = manager.resolveFile("file://tmp/.at/atListAddedOutput.txt");

        //Copy from jar resources to /tmp so we can fake at commands
        //TODO Clean up
        //TODO Permission on this file may not allow deleting
        tmpDir = new File("/tmp/.at");
        if (tmpDir.exists()) {
            tmpDir.delete();
        }
        tmpDir = new File("/tmp/.at/");
        tmpDir.mkdir();

        atListOut.copyFrom(atListOutResource, new AllFileSelector());
        atListRemovedOut.copyFrom(atListRemovedOutResource, new AllFileSelector());
        atListAddedOut.copyFrom(atListAddedOutResource, new AllFileSelector());
        fakeAt.copyFrom(fakeAtResource, new AllFileSelector());

        atConfig = new AtTestConfig("atTest.properties");
        atManager = At.getManager(atConfig);
    }

    @AfterClass
    public void tearDown() {
        try {
            //Clean up temp files
            atListOut.delete();
            atListRemovedOut.delete();
            atListAddedOut.delete();
            fakeAt.delete();
            atListOut.close();
            atListRemovedOut.close();
            atListAddedOut.close();
            fakeAt.close();
            tmpDir.delete();
        } catch (Exception e) {

        }
    }

    @Test
    public void testGetJobById() throws Exception {
        AtJob job = atManager.getJobById(1);
        assertNotNull(job);
        assertEquals(job.getId(), 1);
        assertEquals(job.getQueue(), "q");
        assertEquals(job.getScript(), "echo this is a test\n");

        DateFormat dateFormatter = new SimpleDateFormat("yyyyMMddHHmm");
        assertEquals(job.getJobDate(), dateFormatter.parse("202001281600"));
    }

    @Test
    public void testGetAtJobs() throws Exception {
        Collection<AtJob> jobs = atManager.getAtJobs();
        assertNotNull(jobs);
        assertEquals(jobs.size(), 3);
        int id = 1;
        for (AtJob job : jobs) {
            assertEquals(job.getId(), id++);
        }
    }

    @Test(dependsOnMethods = { "testGetJobById", "testGetAtJobs" })
    public void testRemoveJob() throws Exception {
        atManager.removeJob(1);
        Collection<AtJob> jobs = atManager.getAtJobs();
        assertNotNull(jobs);
        assertEquals(jobs.size(), 2);
        int id = 2;
        for (AtJob job : jobs) {
            assertEquals(job.getId(), id++);
        }
    }

    @Test(dependsOnMethods = { "testGetJobById", "testGetAtJobs", "testRemoveJob" })
    public void testScheduleJob() throws Exception {
        Date date = new SimpleDateFormat("yyyyMMDDHHmm").parse("202001281600");
        JobCode code = new JobCode("echo The greatest barrier to success is the fear of failure");
        AtScheduler scheduleJob = new AtScheduler(date, code);
        atManager.submitJob(scheduleJob);
        AtJob job = atManager.getJobById(4);
        assertNotNull(job);
        assertEquals(job.getId(), 4);
    }

    @Test
    public void testMultiManager() throws Exception {
        //TODO Write MultiManager test
    }
}