FastaQueryService extends BioQueryService { protected static int parseExtension(final String extension) throws BadRequestException { if (StringUtils.isBlank(extension)) { return 0; } final String ext = extension.toLowerCase().trim(); if (!ext.matches("^((\\d+)|(\\d+(\\.\\d+)?(k|m)))(bp?)?$")) { throw new BadRequestException("Illegal extension format: " + ext); } final String justTheNumber = ext.replaceAll("[kmbp]", ""); final int scale = (ext.contains("k") ? 1000 : ext.contains("m") ? 1000000 : 1); final float number; try { number = Float.parseFloat(justTheNumber) * scale; } catch (NumberFormatException e) { throw new BadRequestException("Illegal number: " + justTheNumber, e); } if (number < 0) { throw new BadRequestException("Negative extensions are not allowed."); } if (number != Math.ceil(number)) { throw new BadRequestException("The extension must be a whole number of base pairs. " + "I got: " + number + "bp"); } return Math.round(number); } FastaQueryService(InterMineAPI im);   }
@Test public void goodExtensions() { int tested = 0; for (String ext: extensionsToValues.keySet()) { assertEquals("Can parse this extension: " + ext, extensionsToValues.get(ext), Integer.valueOf(FastaQueryService.parseExtension(ext))); tested++; } assertEquals(tested, extensionsToValues.size()); }
@Test public void badExtensions() { for (String ext: badExtensions) { try { FastaQueryService.parseExtension(ext); fail("Did not throw an exception at " + ext); } catch (BadRequestException e) { } } }