001package org.apache.maven.wagon.events;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import junit.framework.TestCase;
023import org.apache.maven.wagon.ConnectionException;
024import org.apache.maven.wagon.Wagon;
025import org.apache.maven.wagon.authentication.AuthenticationException;
026import org.apache.maven.wagon.repository.Repository;
027import org.easymock.EasyMock;
028
029/**
030 * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
031 *
032 */
033public class SessionEventTest
034    extends TestCase
035{
036    /*
037         * Class to test for void SESSIONEvent(Wagon, Repository, String, int,
038         * int)
039         */
040    public void testSessionEventProperties()
041        throws ConnectionException, AuthenticationException
042    {
043
044        final Wagon wagon = EasyMock.createMock( Wagon.class );
045        final Repository repo = new Repository();
046
047        wagon.connect( repo );
048
049        final long timestamp = System.currentTimeMillis();
050        final Exception exception = new AuthenticationException( "dummy" );
051
052        SessionEvent event = new SessionEvent( wagon, SessionEvent.SESSION_CLOSED );
053
054        assertEquals( wagon, event.getWagon() );
055
056        assertEquals( SessionEvent.SESSION_CLOSED, event.getEventType() );
057
058        event = new SessionEvent( wagon, exception );
059
060        assertEquals( wagon, event.getWagon() );
061        assertEquals( SessionEvent.SESSION_ERROR_OCCURRED, event.getEventType() );
062        assertEquals( exception, event.getException() );
063
064        event.setException( null );
065        assertEquals( null, event.getException() );
066
067        event.setException( exception );
068        assertEquals( exception, event.getException() );
069
070        event.setTimestamp( timestamp );
071        assertEquals( timestamp, event.getTimestamp() );
072
073        event.setEventType( SessionEvent.SESSION_CLOSED );
074        assertEquals( SessionEvent.SESSION_CLOSED, event.getEventType() );
075
076        event.setEventType( SessionEvent.SESSION_DISCONNECTED );
077        assertEquals( SessionEvent.SESSION_DISCONNECTED, event.getEventType() );
078
079        event.setEventType( SessionEvent.SESSION_DISCONNECTING );
080        assertEquals( SessionEvent.SESSION_DISCONNECTING, event.getEventType() );
081
082        event.setEventType( SessionEvent.SESSION_ERROR_OCCURRED );
083        assertEquals( SessionEvent.SESSION_ERROR_OCCURRED, event.getEventType() );
084
085        event.setEventType( SessionEvent.SESSION_LOGGED_IN );
086        assertEquals( SessionEvent.SESSION_LOGGED_IN, event.getEventType() );
087
088        event.setEventType( SessionEvent.SESSION_LOGGED_OFF );
089        assertEquals( SessionEvent.SESSION_LOGGED_OFF, event.getEventType() );
090
091        event.setEventType( SessionEvent.SESSION_OPENED );
092        assertEquals( SessionEvent.SESSION_OPENED, event.getEventType() );
093
094        event.setEventType( SessionEvent.SESSION_OPENING );
095        assertEquals( SessionEvent.SESSION_OPENING, event.getEventType() );
096
097        event.setEventType( SessionEvent.SESSION_CONNECTION_REFUSED );
098        assertEquals( SessionEvent.SESSION_CONNECTION_REFUSED, event.getEventType() );
099
100        try
101        {
102            event.setEventType( -1 );
103            fail( "Exception expected" );
104        }
105        catch ( IllegalArgumentException e )
106        {
107            assertTrue( true );
108        }
109
110    }
111
112    public void testConstantValueConflict()
113    {
114        final int[] values = {SessionEvent.SESSION_CLOSED, SessionEvent.SESSION_DISCONNECTED,
115                              SessionEvent.SESSION_DISCONNECTING, SessionEvent.SESSION_ERROR_OCCURRED,
116                              SessionEvent.SESSION_LOGGED_IN, SessionEvent.SESSION_LOGGED_OFF,
117                              SessionEvent.SESSION_OPENED, SessionEvent.SESSION_OPENING,
118                              SessionEvent.SESSION_CONNECTION_REFUSED};
119
120        for ( int i = 0; i < values.length; i++ )
121        {
122            for ( int j = i + 1; j < values.length; j++ )
123            {
124
125                final String msg = "Value confict at [i,j]=[" + i + "," + j + "]";
126                assertTrue( msg, values[i] != values[j] );
127            }
128        }
129
130    }
131
132}