001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license 003 * agreements. See the NOTICE file distributed with this work for additional information regarding 004 * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the 005 * "License"); you may not use this file except in compliance with the License. You may obtain a 006 * copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software distributed under the License 011 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 012 * or implied. See the License for the specific language governing permissions and limitations under 013 * the License. 014 */ 015package org.apache.maven.execution.scope.internal; 016 017import java.util.concurrent.atomic.AtomicInteger; 018 019import org.apache.maven.execution.MojoExecutionEvent; 020import org.apache.maven.execution.scope.WeakMojoExecutionListener; 021import org.apache.maven.plugin.MojoExecutionException; 022 023import junit.framework.TestCase; 024 025import com.google.inject.Key; 026import com.google.inject.Provider; 027 028public class MojoExecutionScopeTest 029 extends TestCase 030{ 031 public void testNestedEnter() 032 throws Exception 033 { 034 MojoExecutionScope scope = new MojoExecutionScope(); 035 036 scope.enter(); 037 038 Object o1 = new Object(); 039 scope.seed( Object.class, o1 ); 040 assertSame( o1, scope.scope( Key.get( Object.class ), null ).get() ); 041 042 scope.enter(); 043 Object o2 = new Object(); 044 scope.seed( Object.class, o2 ); 045 assertSame( o2, scope.scope( Key.get( Object.class ), null ).get() ); 046 047 scope.exit(); 048 assertSame( o1, scope.scope( Key.get( Object.class ), null ).get() ); 049 050 scope.exit(); 051 052 try 053 { 054 scope.exit(); 055 fail(); 056 } 057 catch ( IllegalStateException expected ) 058 { 059 } 060 } 061 062 public void testMultiKeyInstance() 063 throws Exception 064 { 065 MojoExecutionScope scope = new MojoExecutionScope(); 066 scope.enter(); 067 068 final AtomicInteger beforeExecution = new AtomicInteger(); 069 final AtomicInteger afterExecutionSuccess = new AtomicInteger(); 070 final AtomicInteger afterExecutionFailure = new AtomicInteger(); 071 final WeakMojoExecutionListener instance = new WeakMojoExecutionListener() 072 { 073 @Override 074 public void beforeMojoExecution( MojoExecutionEvent event ) 075 throws MojoExecutionException 076 { 077 beforeExecution.incrementAndGet(); 078 } 079 080 @Override 081 public void afterMojoExecutionSuccess( MojoExecutionEvent event ) 082 throws MojoExecutionException 083 { 084 afterExecutionSuccess.incrementAndGet(); 085 } 086 087 @Override 088 public void afterExecutionFailure( MojoExecutionEvent event ) 089 { 090 afterExecutionFailure.incrementAndGet(); 091 } 092 }; 093 assertSame( instance, scope.scope( Key.get( Object.class ), new Provider<Object>() 094 { 095 @Override 096 public Object get() 097 { 098 return instance; 099 } 100 } ).get() ); 101 assertSame( instance, 102 scope.scope( Key.get( WeakMojoExecutionListener.class ), new Provider<WeakMojoExecutionListener>() 103 { 104 @Override 105 public WeakMojoExecutionListener get() 106 { 107 return instance; 108 } 109 } ).get() ); 110 111 final MojoExecutionEvent event = new MojoExecutionEvent( null, null, null, null ); 112 scope.beforeMojoExecution( event ); 113 scope.afterMojoExecutionSuccess( event ); 114 scope.afterExecutionFailure( event ); 115 116 assertEquals( 1, beforeExecution.get() ); 117 assertEquals( 1, afterExecutionSuccess.get() ); 118 assertEquals( 1, afterExecutionFailure.get() ); 119 120 scope.exit(); 121 } 122}