View Javadoc
1   /*
2    * Copyright (c) 2002-2025, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.portal.service.user.menu;
35  
36  import java.util.List;
37  
38  import org.springframework.mock.web.MockHttpServletRequest;
39  
40  import fr.paris.lutece.portal.business.user.menu.AdminUserMenuItem;
41  import fr.paris.lutece.test.LuteceTestCase;
42  
43  public class AdminUserMenuItemProviderRegistrarTest extends LuteceTestCase
44  {
45      private TestAdminUserMenuService _adminUserMenuService;
46      private AdminUserMenuItemProviderRegistrar _instance;
47  
48      @Override
49      protected void setUp( ) throws Exception
50      {
51          super.setUp( );
52          _adminUserMenuService = new TestAdminUserMenuService( );
53          _instance = new AdminUserMenuItemProviderRegistrar( _adminUserMenuService );
54          _instance.setBeanName( "junit" );
55      }
56  
57      public void testSetClassName( ) throws InstantiationException, IllegalAccessException, IllegalStateException, ClassNotFoundException
58      {
59          _instance.setClassName( TestAdminUserMenuItemProvider.class.getName( ) );
60          _instance.registerAdminUserMenuItemProvider( );
61          List<AdminUserMenuItem> items = _adminUserMenuService.getItems( new MockHttpServletRequest( ) );
62  
63          assertNotNull( items );
64          assertEquals( 1, items.size( ) );
65          assertEquals( TestAdminUserMenuItemProvider.ITEM, items.get( 0 ) );
66      }
67  
68      public void testSetClassNameProviderAlreadySetDirectly( )
69              throws InstantiationException, IllegalAccessException, IllegalStateException, ClassNotFoundException
70      {
71          _instance.setProvider( new TestAdminUserMenuItemProvider( ) );
72          try
73          {
74              _instance.setClassName( TestAdminUserMenuItemProvider.class.getName( ) );
75              fail( "Should have thrown" );
76          }
77          catch( IllegalStateException e )
78          {
79  
80          }
81      }
82  
83      public void testSetClassNameProviderAlreadySetByClassName( )
84              throws InstantiationException, IllegalAccessException, IllegalStateException, ClassNotFoundException
85      {
86          _instance.setClassName( TestAdminUserMenuItemProvider.class.getName( ) );
87          try
88          {
89              _instance.setClassName( TestAdminUserMenuItemProvider.class.getName( ) );
90              fail( "Should have thrown" );
91          }
92          catch( IllegalStateException e )
93          {
94  
95          }
96      }
97  
98      public void testSetProvider( ) throws InstantiationException, IllegalAccessException, IllegalStateException, ClassNotFoundException
99      {
100         _instance.setProvider( new TestAdminUserMenuItemProvider( ) );
101         _instance.registerAdminUserMenuItemProvider( );
102         List<AdminUserMenuItem> items = _adminUserMenuService.getItems( new MockHttpServletRequest( ) );
103 
104         assertNotNull( items );
105         assertEquals( 1, items.size( ) );
106         assertEquals( TestAdminUserMenuItemProvider.ITEM, items.get( 0 ) );
107     }
108 
109     public void testSetProviderProviderAlreadySetDirectly( )
110             throws InstantiationException, IllegalAccessException, IllegalStateException, ClassNotFoundException
111     {
112         _instance.setProvider( new TestAdminUserMenuItemProvider( ) );
113         try
114         {
115             _instance.setProvider( new TestAdminUserMenuItemProvider( ) );
116             fail( "Should have thrown" );
117         }
118         catch( IllegalStateException e )
119         {
120 
121         }
122     }
123 
124     public void testSetProviderProviderAlreadySetByClassName( )
125             throws InstantiationException, IllegalAccessException, IllegalStateException, ClassNotFoundException
126     {
127         _instance.setClassName( TestAdminUserMenuItemProvider.class.getName( ) );
128         try
129         {
130             _instance.setProvider( new TestAdminUserMenuItemProvider( ) );
131             fail( "Should have thrown" );
132         }
133         catch( IllegalStateException e )
134         {
135 
136         }
137     }
138 
139     public void testRegisterAdminUserMenuItemProviderNullProvider( )
140     {
141         try
142         {
143             _instance.registerAdminUserMenuItemProvider( );
144             fail( "Should have thrown" );
145         }
146         catch( IllegalStateException e )
147         {
148 
149         }
150     }
151 }