Junit因空指针异常而失败

时间:2019-05-28 17:39:24

标签: java templates junit

我无法获得用于模板引擎的junit。我有4段代码,再加上junit。我已经运行了调试程序,并且可以看到它应该工作,但是没有。

主要代码如下:

public class webSiteGenerator {

private static Template T = new Template();

public static void main(String[] args) {
    int flag = 0;
    while (flag != 1) {
        Scanner input = new Scanner(System.in);
        System.out.println(
                "Please enter the link to you your source folder (i.e. /uk.ac.uos.i2p.assignment/sourcepath: ");
        File sourceFolder = new File(input.nextLine());

        File dest = new File("testdesination");
        try {
            Files.copy(sourceFolder.toPath(), dest.toPath());
        } catch (IOException e) {

        }
    }
}
public void loadTemplateFolder(File templates) throws FileNotFoundException,IOException {
    for (File letterTemplate : templates.listFiles()) {
        String templateName = letterTemplate.getName();
        String fileString = new String(Files.readAllBytes(Paths.get(letterTemplate.toString())));
        T.loadTemplate(templateName, fileString);
    }

}
public void process(File source, File dest) throws IOException {
    String expandedTemplate = null;
    for (File item : source.listFiles()) {
        File destination = new File(dest, item.getName());
        if (item.isDirectory()) {
        destination.mkdir();
            process(item, destination);
        } else if (item.isFile()) {
            Properties p = new Properties();

            if (item.exists() && item.canRead()) {
                InputStream fileOutOfFolder = null;
                try {
                    fileOutOfFolder = new FileInputStream(item);
                    p.load(fileOutOfFolder);
                    if (p.containsKey("$template")) {
                        String templateName = p.getProperty("$template");
                        @SuppressWarnings({ "rawtypes", "unchecked" })
                        Map<String, Object> contextMap = new HashMap(p);
                        expandedTemplate = T.expandTemplate(templateName, contextMap);
                        FileWriter returnedTemplateString = new FileWriter(destination);
                        returnedTemplateString.write(expandedTemplate);
                        returnedTemplateString.close();
                    }

                } finally {
                    if (null != fileOutOfFolder)
                        fileOutOfFolder.close();
                }


            }

        }

    }
}

}

TemplateProcessor代码:     公共接口TemplateProcessor {

void loadTemplate(String name, String template);

void loadTemplates(Map<String, String> templates);

String expandTemplate(String templateName, Map<String, Object> context);

}

模板代码:

public class Template implements TemplateProcessor {
Map<String, String> templateStore;

public Template() {
    this.templateStore = new HashMap<>();
}

@Override
public void loadTemplate(String name, String template) {
    templateStore.put(name, template);
}

@Override
public void loadTemplates(Map<String, String> templates) {
    templateStore.putAll(templates);
}

@Override
public String expandTemplate(String templateName, Map<String, Object> context) {
    String templateValue = null;
    List<String> templateStoreKey = new ArrayList<>(templateStore.keySet());
    for (String templateNameKey : templateStoreKey) {
        if (templateNameKey.equalsIgnoreCase(templateName)) {
            templateValue = templateStore.get(templateNameKey);
            while (templateValue.contains("$")) {
                Reader templateText = new StringReader(templateValue);
                placeholderFinder find = new placeholderFinder(templateText);
                try {
                    String returnedPlaceholder = find.nextPlaceholderFinder();
                    String editedPlaceholder = returnedPlaceholder.substring(2, (returnedPlaceholder.length() - 1));
                    if (context.containsKey(editedPlaceholder)) {
                        String returnedContextValue = context.get(editedPlaceholder).toString();
                        templateValue = templateValue.replace(returnedPlaceholder, returnedContextValue);
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        }
    }

    return templateValue;

}

}

还有placeHolder状态机代码:

public class placeholderFinder {
private Reader reader;

public placeholderFinder(Reader reader) {
    this.reader = reader;
}

public static final int NORMALTEXT = 0;
public static final int PLACEHOLDERTEXT = 1;

private int state = NORMALTEXT;
private StringBuilder placeholder = new StringBuilder();

public String nextPlaceholderFinder() throws IOException {
    for (int p = reader.read(); p != -1; p = reader.read()) {
        if (p == '$') {
            if (state == NORMALTEXT) {
                state = PLACEHOLDERTEXT;
                placeholder.append((char) p);
            }
        } else if (state == PLACEHOLDERTEXT) {
            placeholder.append((char) p);
        }
        if (p == '}') {
            if (state == PLACEHOLDERTEXT) {
                state = NORMALTEXT;
                return placeholder.toString();
            }
        }
    }
    return null;

}

} 最后是junit测试:

我检查了所有文件(模板文件和上下文文件)的设置是否正确,并且我看不到为什么失败,尤其是在调试显示其正常工作的情况下。

Junit代码

class Part2Test {
@Test
void test1() {
    File destination = new File ("testdestination");
    File source = new File ("source");
    File templates = new File ("templates");
    webSiteGenerator w = new webSiteGenerator();
    try {w.loadTemplateFolder(templates);
        w.process(source, destination);;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    File gilby = new File("testdestination/Gilbert Clarke/offer.txt");
    assertTrue(gilby.exists());
}

}

堆栈跟踪(我认为): java.lang.NullPointerException     在java.io.Writer.write(未知源)     在uk.ac.uos.i2p.assignment.part2.webSiteGenerator.process(webSiteGenerator.java:68)     在uk.ac.uos.i2p.assignment.part2.webSiteGenerator.process(webSiteGenerator.java:53)     在uk.ac.uos.i2p.assignment.part2.Part2Test.test1(Part2Test.java:17)     在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处     在sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)     在sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)     在java.lang.reflect.Method.invoke(未知来源)     在org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:515)     在org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115)     在org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda $ invokeTestMethod $ 6(TestMethodTestDescriptor.java:171)     在org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)处     在org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:167)     在org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:114)     在org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:59)     在org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda $ executeRecursively $ 5(NodeTestTask.java:105)     在org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)处     在org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:95)上     在org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:71)     在java.util.ArrayList.forEach(未知来源)     在org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)     在org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda $ executeRecursively $ 5(NodeTestTask.java:110)     在org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)处     在org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:95)上     在org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:71)     在java.util.ArrayList.forEach(未知来源)     在org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)     在org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda $ executeRecursively $ 5(NodeTestTask.java:110)     在org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)处     在org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:95)上     在org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:71)     在org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)     在org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)     在org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)     在org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220)     在org.junit.platform.launcher.core.DefaultLauncher.lambda $ execute $ 6(DefaultLauncher.java:188)     在org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202)     在org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181)     在org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)     在org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:89)     在org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)     在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)     在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)     在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)     在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

0 个答案:

没有答案