ContextLoaderUtils { public static Context loadContext(String path) throws IOException, MalformedContextFileException, IllogicalContextException { File file = new File(path); LineIterator it = FileUtils.lineIterator(file, "UTF-8"); ArrayList<Box> boxes = new ArrayList<>(); try { Double x = loadLine(it, "^LX=[0-9]{1,13}(\\.[0-9]*)?$"); Double y = loadLine(it, "LY=[0-9]{1,13}(\\.[0-9]*)?"); int cost = loadLine(it, "m=[0-9]{1,13}(\\.[0-9]*)?").intValue(); while (it.hasNext()) { boxes.add(loadBox(it.nextLine())); } LineIterator.closeQuietly(it); double max = Math.max(x, y); if (boxes.parallelStream().anyMatch(b -> b.getSize().getX() > max) || boxes.parallelStream().anyMatch(b -> b.getSize().getY() > max)) { throw new IllogicalContextException("There is an image which is bigger than the pattern."); } return new Context(file.getName(), 20, 1, boxes, new Vector(x, y)); } catch (MalformedContextFileException mctx) { throw mctx; } finally { LineIterator.closeQuietly(it); } }  static Context loadContext(String path);  }
@Test(expected = IOException.class) public void testLoadContextInvalidFile() throws Exception { File file = FileUtils.getFile("src", "test", "java", "resources", "test.txt"); Context context = ContextLoaderUtils.loadContext(file.getPath()); }
@Test public void testLoadContextValidFile() throws Exception { File file = FileUtils.getFile("src", "test", "java", "resources", "ContextTest.txt"); Context context = ContextLoaderUtils.loadContext(file.getPath()); TestCase.assertEquals(179, context.getBoxes().get(0).getAmount()); TestCase.assertEquals(20, context.getBoxes().size()); TestCase.assertEquals(20, context.getPatternCost()); TestCase.assertEquals(1, context.getSheetCost()); TestCase.assertEquals(new Vector(1400, 700), context.getPatternSize()); }
@Test(expected = IllogicalContextException.class) public void testFail1() throws Exception { File file = FileUtils.getFile("src", "test", "java", "resources", "ContextTestFail1.txt"); Context context = ContextLoaderUtils.loadContext(file.getPath()); }
@Test(expected = IllogicalContextException.class) public void testFail2() throws Exception { File file = FileUtils.getFile("src", "test", "java", "resources", "ContextTestFail2.txt"); Context context = ContextLoaderUtils.loadContext(file.getPath()); }