001package org.apache.maven.plugin.internal;
002/*
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *  http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 */
020
021import junit.framework.TestCase;
022import org.apache.maven.execution.DefaultMavenExecutionRequest;
023import org.apache.maven.execution.MavenExecutionRequest;
024import org.apache.maven.execution.MavenSession;
025
026import java.util.concurrent.CountDownLatch;
027
028/**
029 * @author Kristian Rosenvold
030 */
031public class DefaultLegacySupportTest extends TestCase {
032    final CountDownLatch latch = new CountDownLatch(1);
033    final DefaultLegacySupport defaultLegacySupport = new DefaultLegacySupport();
034
035    public void testSetSession() throws Exception {
036
037        MavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest();
038        MavenSession m1 = new MavenSession(null, null, mavenExecutionRequest, null);
039        defaultLegacySupport.setSession(m1);
040
041        MyRunnable myRunnable = new MyRunnable();
042        Thread thread = new Thread(myRunnable);
043        thread.start();
044
045        MavenSession m2 = new MavenSession(null, null, mavenExecutionRequest, null);
046        defaultLegacySupport.setSession(m2);
047        latch.countDown();
048        thread.join();
049        assertNull( myRunnable.getSession());
050    }
051
052
053    class MyRunnable implements Runnable {
054
055        private volatile MavenSession session;
056
057        public void run() {
058            try
059            {
060                latch.await();
061            }
062            catch (InterruptedException ingore)
063            {
064                // Test may fail if we get interrupted
065            }
066            session = defaultLegacySupport.getSession();
067        }
068
069        public MavenSession getSession() {
070            return session;
071        }
072    }
073
074}