TherionImporter extends Importer { public static List<String> getContentsOfBeginEndBlock(List<String> lines, String tag) throws Exception { boolean foundStartBlock = false; boolean foundEndBlock = false; String endTag = "end" + tag; List<String> contents = new ArrayList<>(); for (String line : lines) { line = line.trim(); if (line.equals(tag)) { if (foundStartBlock) { throw new Exception("Opening block tag " + tag + " encountered twice"); } else { foundStartBlock = true; continue; } } else if (line.equals(endTag)) { if (foundStartBlock) { foundEndBlock = true; break; } else { throw new Exception("End block tag " + endTag + " encountered before block start"); } } else if (!foundStartBlock) { continue; } contents.add(line); } if (!foundStartBlock) { throw new Exception("Failed to find opening block tag " + tag); } else if (!foundEndBlock) { throw new Exception("Failed to find end block tag " + endTag); } return contents; }  Survey toSurvey(File directory); static String getSurveyName(File file); boolean canHandleFile(File directory); static void updateCentreline(List<String> lines, Survey survey); static List<String> getContentsOfBeginEndBlock(List<String> lines, String tag);  }
@Test public void testBasicBlockExtraction() throws Exception { List<String> LINES = Arrays.asList("block", "content", "endblock"); List<String> output = TherionImporter.getContentsOfBeginEndBlock(LINES, "block"); Assert.assertEquals(output.size(), 1); Assert.assertEquals(output.get(0), "content"); }
@Test(expected=Exception.class) public void testBlockExtractionFailsIfNoClosingTag() throws Exception { List<String> LINES = Arrays.asList("block", "content", "blah"); TherionImporter.getContentsOfBeginEndBlock(LINES, "block"); }
@Test(expected=Exception.class) public void testBlockExtractionFailsIfTwoOpeningTags() throws Exception { List<String> LINES = Arrays.asList("block", "blah", "block", "blah", "endblock"); TherionImporter.getContentsOfBeginEndBlock(LINES, "block"); }