### Question:
TwitterAdapterFactory implements AdapterFactory { @SuppressWarnings("unchecked") @Override public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) { TwitterClient client = null; if (adaptable instanceof Page) { client = createTwitterClientFromPage((Page) adaptable); } else if (adaptable instanceof com.day.cq.wcm.webservicesupport.Configuration) { client = createTwitterClientFromConfiguration((com.day.cq.wcm.webservicesupport.Configuration) adaptable); } if (client != null) { if (type == TwitterClient.class) { return (AdapterType) client; } else if (type == Twitter.class) { return (AdapterType) client.getTwitter(); } } return null; } TwitterAdapterFactory(String httpProxyHost, int httpProxyPort, boolean useSsl); @VisibleForTesting  TwitterAdapterFactory(); @SuppressWarnings("unchecked") @Override AdapterType getAdapter(Object adaptable, Class<AdapterType> type);  }### Answer:
@Test public void testAdaptFromValidConfiguration() throws Exception { com.day.cq.wcm.webservicesupport.Configuration config = setupConfiguration(validOauthValueMap); Twitter twitter = mock(Twitter.class); TwitterAdapterFactory adapterFactory = new TwitterAdapterFactory() { @Override Twitter getInstance() { return twitter; } }; TwitterClient client = adapterFactory.getAdapter(config, TwitterClient.class); assertNotNull(client); Twitter adapterTwitter = adapterFactory.getAdapter(config, Twitter.class); assertEquals(twitter, adapterTwitter); verify(twitter, times(2)).setOAuthConsumer(VALID_OAUTH_CLIENT_ID, VALID_OAUTH_SECRET); verify(twitter, times(2)).getOAuth2Token(); verifyNoMoreInteractions(twitter); }
@Test public void testAdaptFromInvalidConfiguration() throws Exception { com.day.cq.wcm.webservicesupport.Configuration config = setupConfiguration(ValueMap.EMPTY); TwitterAdapterFactory adapterFactory = new TwitterAdapterFactory(); TwitterClient client = adapterFactory.getAdapter(config, TwitterClient.class); assertNull(client); }
@Test public void testAdaptFromValidConfigurationUnableToCreateClient() throws Exception { com.day.cq.wcm.webservicesupport.Configuration config = setupConfiguration(validOauthValueMap); Twitter twitter = mock(Twitter.class); doThrow(new TwitterException("test")).when(twitter).getOAuth2Token(); TwitterAdapterFactory adapterFactory = new TwitterAdapterFactory() { @Override Twitter getInstance() { return twitter; } }; TwitterClient client = adapterFactory.getAdapter(config, TwitterClient.class); assertNull(client); verify(twitter, times(1)).setOAuthConsumer(VALID_OAUTH_CLIENT_ID, VALID_OAUTH_SECRET); verify(twitter, times(1)).getOAuth2Token(); verifyNoMoreInteractions(twitter); }
@Test public void testAdaptFromValidPage() throws Exception { Page page = setupPage(validOauthValueMap); Twitter twitter = mock(Twitter.class); TwitterAdapterFactory adapterFactory = new TwitterAdapterFactory() { @Override Twitter getInstance() { return twitter; } }; TwitterClient client = adapterFactory.getAdapter(page, TwitterClient.class); assertNotNull(client); Twitter adapterTwitter = adapterFactory.getAdapter(page, Twitter.class); assertEquals(twitter, adapterTwitter); verify(twitter, times(2)).setOAuthConsumer(VALID_OAUTH_CLIENT_ID, VALID_OAUTH_SECRET); verify(twitter, times(2)).getOAuth2Token(); verifyNoMoreInteractions(twitter); }