001    package org.nakedobjects.applib.fixtures;
002    
003    import java.util.ArrayList;
004    import java.util.Arrays;
005    import java.util.Collections;
006    import java.util.List;
007    
008    
009    /**
010     * Indicates that the demo or test should be run as the specified user, with
011     * the specified roles.
012     * 
013     * <p>
014     * Note: this fixture does not in itself do anything (its {@link #install()} is a
015     * no-op).  However, if present in the fixture list then is &quot;noticed&quot; by the framework,
016     * and is used to automatically logon when the framework is booted (providing running in
017     * prototype or exploration, not in production).
018     * 
019     * <p>
020     * To change the user during the installation of fixtures, either use {@link SwitchUserFixture}.
021     * 
022     * @see SwitchUserFixture
023     */
024    public class LogonFixture implements InstallableFixture {
025    
026    
027            @SuppressWarnings("unchecked")
028            private static List<String> asList(final String... roles) {
029                    return roles != null ? Arrays.asList(roles) : Collections.EMPTY_LIST;
030            }
031    
032        private final String username;
033        private final List<String> roles = new ArrayList<String>();
034        
035        public LogonFixture(final String username, final String... roles) {
036            this(username, asList(roles));
037        }
038    
039        public LogonFixture(final String username, final List<String> roles) {
040            this.username = username;
041            this.roles.addAll(roles);
042        }
043    
044        public String getUsername() {
045            return username;
046        }
047    
048        public List<String> getRoles() {
049            return Collections.unmodifiableList(roles);
050        }
051    
052        public final void install() {
053            // does nothing; see comments above.
054        }
055    
056            public FixtureType getType() {
057                    return FixtureType.OTHER;
058            }
059    
060            @Override
061        public String toString() {
062            return "LogonFixture [user: " + getUsername() + ", roles: " + getRoles() + "]";
063        }
064    
065    }