FileUserService implements UserService { @Override public String create(User user) throws UserException { Properties prop = new Properties(); OutputStream output = null; File userFile = pathToUserFile(user.getUserName()).toFile(); if (userFile.exists()) { throw new UserException("Username (" + user.getUserName() + ") already exists."); } try { output = new FileOutputStream(userFile); Set<String> userKeys = user.getUserInformationKeys(); for (String key : userKeys) { prop.setProperty(key, user.getUserInformation(key)); } prop.store(output, null); } catch (IOException e) { throw new UserException("IOException when creating the User file for " + user.getUserName() + ". " + e.getMessage()); } this.users.put(user.getUserName(), user); return user.getUserName(); } FileUserService(String userDirectoryPath, String fileBaseName); @Override String create(User user); @Override String retrieve(Properties properties); @Override void update(String userName, User user); @Override void delete(String userName); @Override User getUser(String userName); Path pathToUserFile(String userName);  }
@Test(expected = UserException.class) public void testCreateUserFailure() throws UserException { String tempFilePath = getTempFilePath(); User new_user = mockUser(); UserService user_service = new FileUserService(tempFilePath, fileBaseName); String userName = user_service.create(new_user); user_service.create(new_user); assertEquals(userName, "mock_UserName"); }
@Test(expected = UserException.class) public void testBadDirectory() throws UserException { UserService user_service = new FileUserService("bad/path/", fileBaseName); User user = mockUser(); user_service.create(user); }