### Question:
AttachmentManager { static String generateFilenameForKey(SQLDatabase db, String keyString) throws NameGenerationException { String filename = null; long result = -1; int tries = 0; while (result == -1 && tries < 200) { byte[] randomBytes = new byte[20]; filenameRandom.nextBytes(randomBytes); String candidate = keyToString(randomBytes); ContentValues contentValues = new ContentValues(); contentValues.put("key", keyString); contentValues.put("filename", candidate); result = db.insert(ATTACHMENTS_KEY_FILENAME, contentValues); if (result != -1) { filename = candidate; } tries++; } if (filename != null) { return filename; } else { throw new NameGenerationException(String.format( "Couldn't generate unique filename for attachment with key %s", keyString)); } }  static void addAttachmentsToRevision(SQLDatabase db, String attachmentsDir,
                                                InternalDocumentRevision rev,
                                                Map<String, PreparedAttachment> attachments); static void addAttachment(SQLDatabase db, String attachmentsDir,
                                     InternalDocumentRevision rev, PreparedAttachment a, String name); static PreparedAttachment prepareAttachment(String attachmentsDir,
                                                          AttachmentStreamFactory attachmentStreamFactory,
                                                          Attachment attachment); static Map<String, SavedAttachment> findExistingAttachments(
            Map<String, ? extends Attachment> attachments); static Map<String, Attachment> findNewAttachments(Map<String, ? extends Attachment> attachments); static Map<String, PreparedAttachment> prepareAttachments(String attachmentsDir,
                                                                 AttachmentStreamFactory attachmentStreamFactory,
                                                                 Map<String, Attachment> attachments); static Map<String, ? extends Attachment> attachmentsForRevision(SQLDatabase db, String attachmentsDir,
                                                                              AttachmentStreamFactory attachmentStreamFactory,
                                                                              long sequence); static void copyAttachmentsToRevision(SQLDatabase db, Map<String, SavedAttachment> attachments,
                                                 InternalDocumentRevision rev); static void copyAttachment(SQLDatabase db, long parentSequence, long newSequence,
                                         String filename); static void purgeAttachments(SQLDatabase db, String attachmentsDir); static File fileFromKey(SQLDatabase db, byte[] key, String attachmentsDir,
                            boolean allowCreateName); static final String SQL_ATTACHMENTS_SELECT; static final String SQL_ATTACHMENTS_SELECT_ALL; }### Answer:
@Test public void testNameGeneration() throws AttachmentManager.NameGenerationException { SQLDatabase db = mock(SQLDatabase.class); when(db.insert(anyString(), any(ContentValues.class))).thenReturn(1l); Assert.assertNotNull(AttachmentManager.generateFilenameForKey(db, "blah")); verify(db, times(1)).insert(anyString(), any(ContentValues.class)); }
@Test public void testNameGenerationFirstTimeFails() throws AttachmentManager.NameGenerationException { SQLDatabase db = mock(SQLDatabase.class); when(db.insert(anyString(), any(ContentValues.class))).thenReturn(-1l).thenReturn(1l); Assert.assertNotNull(AttachmentManager.generateFilenameForKey(db, "blah")); verify(db, times(2)).insert(anyString(), any(ContentValues.class)); }
@Test public void testNameGenerationFailure() throws AttachmentManager.NameGenerationException { SQLDatabase db = mock(SQLDatabase.class); when(db.insert(anyString(), any(ContentValues.class))).thenReturn(-1l); try { AttachmentManager.generateFilenameForKey(db, "blah"); Assert.fail("Exception not thrown when name not generated"); } catch (AttachmentManager.NameGenerationException ex) { verify(db, times(200)).insert(anyString(), any(ContentValues.class)); } }