BlobStoreUtilImpl implements BlobStoreUtil { @Override public boolean validateFilePath(final String filePath, final int maxLength) { requireNonNull(filePath); checkArgument(maxLength > 0, "maxLength should be greater than zero."); Path path = Paths.get(separatorsToSystem(filePath)).toAbsolutePath(); List<String> fileNames = new ArrayList<>(); Path pathParent = path.getParent(); while (pathParent != null) { if (path.getFileName() != null) { fileNames.add(path.getFileName().toString()); } path = pathParent; pathParent = path.getParent(); } return !fileNames.stream().anyMatch(fileOrDirectory -> fileOrDirectory.length() > maxLength); } @Inject  BlobStoreUtilImpl(final RepositoryManager repositoryManager); @Override int usageCount(final String blobStoreId); @Override boolean validateFilePath(final String filePath, final int maxLength);  }
@Test public void testRequirePath() throws Exception { exception.expect(NullPointerException.class); underTest.validateFilePath(null, MAX_NAME_LENGTH); }
@Test public void testRequirePositiveNonZeroLength() throws Exception { exception.expectMessage("maxLength should be greater than zero."); exception.expect(IllegalArgumentException.class); underTest.validateFilePath("test", 0); }
@Test public void testAcceptShortPath() throws Exception { String shortPath = File.separator + "test" + File.separator + "test"; String longMemberFolder = Strings.padEnd("", MAX_NAME_LENGTH, 'x'); assertTrue(underTest.validateFilePath(shortPath, MAX_NAME_LENGTH)); assertTrue(underTest.validateFilePath(shortPath + File.separator + "longer", 255)); assertTrue(underTest.validateFilePath(shortPath + File.separator + longMemberFolder, 255)); }
@Test public void testRejectLongPath() throws Exception { String shortPath = File.separator + "test" + File.separator + "testtestte"; assertFalse(underTest.validateFilePath(shortPath, 9)); String longMemberFolder = Strings.padEnd("", MAX_NAME_LENGTH + 1, 'x'); shortPath = File.separator + longMemberFolder + shortPath; assertFalse(underTest.validateFilePath(shortPath, MAX_NAME_LENGTH)); }