### Question:
JcrRepository implements Repository { public synchronized Session login() throws RepositoryException { return login(null, null); } @SuppressWarnings( "deprecation" )  JcrRepository( ExecutionContext executionContext,
                   RepositoryConnectionFactory connectionFactory,
                   String repositorySourceName,
                   Observable repositoryObservable,
                   RepositorySourceCapabilities repositorySourceCapabilities,
                   Map<String, String> descriptors,
                   Map<Option, String> options,
                   String initialContentForNewWorkspaces ); Map<Option, String> getOptions(); String getDescriptor( String key ); JcrValue getDescriptorValue( String key ); JcrValue[] getDescriptorValues( String key ); boolean isSingleValueDescriptor( String key ); boolean isStandardDescriptor( String key ); String[] getDescriptorKeys(); synchronized Session login(); synchronized Session login( Credentials credentials ); synchronized Session login( String workspaceName ); synchronized Session login( Credentials credentials,
                                       String workspaceName ); Metrics getMetrics();  }### Answer:
@Test( expected = javax.jcr.LoginException.class ) public void shouldNotAllowLoginWithNoCredentialsWhenAnonymousAuthenticationIsNotEnabled() throws Exception { Map<Option, String> options = new HashMap<Option, String>(); options.put(Option.ANONYMOUS_USER_ROLES, ""); repository = new JcrRepository(context, connectionFactory, sourceName, new MockObservable(), null, descriptors, options, null); repository.login(); }
@Test public void shouldAllowLoginWithNoCredentialsWhenAnonymousAuthenticationIsEnabled() throws Exception { repository.login(); }
@SuppressWarnings( "cast" ) @Test public void shouldAllowLoginWithNoCredentialsInPrivilegedBlock() throws Exception { LoginContext login = new LoginContext("modeshape-jcr", new UserPasswordCallbackHandler("superuser", "superuser".toCharArray())); login.login(); Subject subject = login.getSubject(); Session session = (Session)Subject.doAsPrivileged(subject, new PrivilegedExceptionAction<Session>() { @SuppressWarnings( "synthetic-access" ) public Session run() throws Exception { return repository.login(); } }, AccessController.getContext()); assertThat(session, is(notNullValue())); assertThat(session.getUserID(), is("superuser")); login.logout(); }
@Test public void shouldAllowLoginWithNoCredentialsIfAnonAccessEnabled() throws Exception { Map<JcrRepository.Option, String> options = new HashMap<JcrRepository.Option, String>(); options.put(JcrRepository.Option.ANONYMOUS_USER_ROLES, ModeShapeRoles.READONLY); JcrRepository repository = new JcrRepository(context, connectionFactory, sourceName, new MockObservable(), null, descriptors, options, null); session = (JcrSession)repository.login(); assertThat(session, is(notNullValue())); assertThat(session.getUserID(), is(JcrRepository.ANONYMOUS_USER_NAME)); }
@Test public void shouldAllowLoginWithProperCredentials() throws Exception { repository.login(credentials); repository.login(new JcrSecurityContextCredentials(new MockSecurityContext(null, Collections.singleton(ModeShapeRoles.ADMIN)))); }
@Test public void shouldAllowLoginWithNoWorkspaceName() throws Exception { Session session = repository.login(credentials, null); assertThat(session, notNullValue()); session.logout(); session = repository.login(new JcrSecurityContextCredentials( new MockSecurityContext( null, Collections.singleton(ModeShapeRoles.ADMIN))), (String)null); assertThat(session, notNullValue()); session.logout(); }
@Test( expected = IllegalArgumentException.class ) public void shouldNotAllowLoginIfCredentialsDoNotProvideJaasMethod() throws Exception { repository.login(Mockito.mock(Credentials.class)); }
@Test( expected = IllegalArgumentException.class ) public void shouldNotAllowLoginIfCredentialsReturnNoAccessControlContext() throws Exception { repository.login(new Credentials() { private static final long serialVersionUID = 1L; @SuppressWarnings( "unused" ) public AccessControlContext getAccessControlContext() { return null; } }); }
@Test( expected = IllegalArgumentException.class ) public void shouldNotAllowLoginIfCredentialsReturnNoLoginContext() throws Exception { repository.login(new Credentials() { private static final long serialVersionUID = 1L; @SuppressWarnings( "unused" ) public LoginContext getLoginContext() { return null; } }); }