uk.co.unclealex.executable.generator.CodeGeneratorImplTest.java Source code

Java tutorial

Introduction

Here is the source code for uk.co.unclealex.executable.generator.CodeGeneratorImplTest.java

Source

/**
 * Copyright 2012 Alex Jones
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.    
 *
 * @author unclealex72
 *
 */

package uk.co.unclealex.executable.generator;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;

import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import uk.co.unclealex.executable.generator.model.ExecutableAnnotationInformation;
import uk.co.unclealex.executable.generator.scan.AllClassNamesCollector;
import uk.co.unclealex.executable.generator.scan.ExecutableAnnotationInformationFinder;
import uk.co.unclealex.executable.generator.scan.exception.ExecutableScanException;
import uk.co.unclealex.executable.generator.writer.GeneratedClassWriter;
import uk.co.unclealex.executable.generator.writer.WrittenClass;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

/**
 * @author alex
 * 
 */
public class CodeGeneratorImplTest {

    private Path tempDir;

    @Before
    public void setUp() throws IOException {
        tempDir = Files.createTempDirectory("codegen-");
    }

    @Test
    public void testCodeGeneration() throws IOException, ExecutableScanException {
        CodeGenerator codeGenerator = new CodeGeneratorImpl(new MockAllClassNamesCollector(),
                new MockExecutableAnnotationInformationFinder(), new MockGeneratedClassWriter());
        codeGenerator.generate(getClass().getClassLoader(), tempDir.resolve("nothing"), tempDir);
        final SortedSet<String> actualPathNames = Sets.newTreeSet();
        FileVisitor<Path> visitor = new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (Files.isDirectory(file)) {
                    Assert.fail("Found directory " + file + " when no directories were expected.");
                }
                actualPathNames.add(file.getFileName().toString());
                return FileVisitResult.CONTINUE;
            }
        };
        Files.walkFileTree(tempDir, visitor);
        Assert.assertArrayEquals("The wrong class files were written.",
                new String[] { "uk.co.unclealex.executable.generator.TestOne.txt",
                        "uk.co.unclealex.executable.generator.TestTwo.txt" },
                Iterables.toArray(actualPathNames, String.class));
        checkContents("uk.co.unclealex.executable.generator.TestOne");
        checkContents("uk.co.unclealex.executable.generator.TestTwo");
    }

    protected void checkContents(String className) throws IOException {
        Path path = tempDir.resolve(className + ".txt");
        String actualContents = new String(Files.readAllBytes(path));
        Assert.assertEquals("Path " + path.getFileName() + " had the wrong contents.",
                "ExecutableAnnotationInformation{className=" + className
                        + ", methodName=run, commandLineClassName=commandLineClassName, scriptName=scriptName, guiceModuleClassNames=[]}\n",
                actualContents);
    }

    @After
    public void tearDown() throws IOException {
        if (tempDir != null) {
            FileUtils.deleteDirectory(tempDir.toFile());
        }
    }

    /**
     * The Class MockAllClassNamesCollector.
     */
    class MockAllClassNamesCollector implements AllClassNamesCollector {

        @Override
        public List<String> listAllClassNames(Path baseDirectory) throws IOException {
            return Arrays.asList("uk.co.unclealex.executable.generator.TestOne",
                    "uk.co.unclealex.executable.generator.TestTwo");
        }
    }

    class MockExecutableAnnotationInformationFinder
            implements ExecutableAnnotationInformationFinder, Function<Class<?>, ExecutableAnnotationInformation> {

        @Override
        public List<ExecutableAnnotationInformation> findExecutableAnnotationInformation(
                List<? extends Class<?>> classes) throws ExecutableScanException {
            return Lists.newArrayList(Iterables.transform(classes, this));
        }

        @Override
        public ExecutableAnnotationInformation apply(Class<?> clazz) {
            return new ExecutableAnnotationInformation(clazz.getName(), "run", "commandLineClassName", "scriptName",
                    new ArrayList<String>());
        }
    }

    class MockGeneratedClassWriter implements GeneratedClassWriter {

        @Override
        public WrittenClass writeClass(ExecutableAnnotationInformation executableAnnotationInformation,
                ClassLoader classLoader, Path baseDirectory) throws IOException {
            String className = executableAnnotationInformation.getClassName();
            Path path = baseDirectory.resolve(className + ".txt");
            Files.write(path, Lists.newArrayList(executableAnnotationInformation.toString()),
                    Charset.defaultCharset());
            return new WrittenClass(className, path);
        }

        @Override
        public WrittenClass writeEntryPointClass(Path baseDirectory,
                Map<String, String> commandRunnerClassNamesByScriptName) throws IOException {
            // TODO Auto-generated method stub
            return null;
        }
    }
}

class TestOne {
    // Empty class for testing.
}

class TestTwo {
    // Empty class for testing.
}