Merged HEAD-QA to HEAD (4.2) (including moving test classes into separate folders)

51903 to 54309 


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@54310 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Samuel Langlois
2013-08-20 17:17:31 +00:00
parent 5a8f6ee635
commit e60d57ea42
70 changed files with 7094 additions and 1988 deletions

View File

@@ -0,0 +1,116 @@
/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.web.app;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import junit.framework.TestCase;
/**
* Unit test for resource bundle wrapper
*
* @author Roy Wetherall
*/
public class ResourceBundleWrapperTest extends TestCase
{
private static final String BUNDLE_NAME = "org.alfresco.web.app.resourceBundleWrapperTest";
private static final String KEY_1 = "test_key_one";
private static final String KEY_2 = "test_key_two";
private static final String MSG_1 = "Test Key One";
private static final String MSG_2 = "Test Key Two";
/**
* Test adding the bundles
*/
public void test1AddingBundles()
{
// Check that the string's are not added to the bundle
ResourceBundle before = ResourceBundleWrapper.getResourceBundle("alfresco.messages.webclient", Locale.US);
Enumeration<String> keys = before.getKeys();
assertFalse(containsValue(keys, KEY_1));
assertFalse(containsValue(keys, KEY_2));
try
{
before.getString(KEY_1);
fail("Not expecting the key to be there");
}
catch (Throwable exception){};
try
{
before.getString(KEY_2);
fail("Not expecting the key to be there");
}
catch (Throwable exception){};
// Add an additional resource bundle
ResourceBundleWrapper.addResourceBundle(BUNDLE_NAME);
// Check that the string's are now added to the bundle
ResourceBundle after = ResourceBundleWrapper.getResourceBundle("alfresco.messages.webclient", Locale.US);
Enumeration<String> keys2 = after.getKeys();
assertTrue(containsValue(keys2, KEY_1));
assertEquals(after.getString(KEY_1), MSG_1);
assertEquals(after.getString(KEY_2), MSG_2);
}
/**
* Test the bootstrap bean
*/
public void test2Bootstrap()
{
// Use the bootstrap bean to add the bundles
List<String> bundles = new ArrayList<String>(1);
bundles.add(BUNDLE_NAME);
ResourceBundleBootstrap bootstrap = new ResourceBundleBootstrap();
bootstrap.setResourceBundles(bundles);
// Check that the string's are now added to the bundle
ResourceBundle after = ResourceBundleWrapper.getResourceBundle("alfresco.messages.webclient", Locale.US);
Enumeration<String> keys2 = after.getKeys();
assertTrue(containsValue(keys2, KEY_1));
assertTrue(containsValue(keys2, KEY_2));
assertEquals(after.getString(KEY_1), MSG_1);
assertEquals(after.getString(KEY_2), MSG_2);
}
/**
* Check whether the list contains the values
*
* @param values list of values to check
* @param value value to look for
* @return boolean true if value contained, false otherwise
*/
private boolean containsValue(Enumeration<String> values, String value)
{
boolean result = false;
while (values.hasMoreElements() == true)
{
if (values.nextElement().equals(value) == true)
{
result = true;
break;
}
}
return result;
}
}

View File

@@ -0,0 +1,81 @@
/*
* Copyright (C) 2005-2013 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.web.app.servlet;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;
import javax.servlet.FilterChain;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.alfresco.repo.tenant.TenantContextHolder;
import org.alfresco.web.config.ClientConfigElement;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.context.ApplicationEvent;
import org.springframework.extensions.config.ConfigImpl;
import org.springframework.extensions.config.ConfigService;
/**
* Test for the AuthenticationFilter class.
*
* @author alex.mukha
*/
@RunWith(MockitoJUnitRunner.class)
public class AuthenticationFilterTest
{
private static String loginPage = "loginpage";
private String tenantDomain = "tenantDomain-" + System.currentTimeMillis();
private @Mock ServletContext context;
private @Mock HttpServletResponse res;
private @Mock FilterChain chain;
private @Mock ApplicationEvent event;
/**
* Test for the fix for ALF-18611
* @throws Exception
*/
@Test
public void testALF18611() throws Exception
{
ClientConfigElement clientConfigElementMock = mock(ClientConfigElement.class);
when(clientConfigElementMock.getLoginPage()).thenReturn(loginPage);
ConfigImpl configImplMock = mock(ConfigImpl.class);
when(configImplMock.getConfigElement(ClientConfigElement.CONFIG_ELEMENT_ID)).thenReturn(clientConfigElementMock);
ConfigService configServiceMock = mock(ConfigService.class);
when(configServiceMock.getGlobalConfig()).thenReturn(configImplMock);
TenantContextHolder.setTenantDomain(tenantDomain);
assertTrue("Tenant domain should be equal", TenantContextHolder.getTenantDomain().equals(tenantDomain.toLowerCase()));
AuthenticationFilter authenticationFilter = new AuthenticationFilter();
authenticationFilter.setConfigService(configServiceMock);
authenticationFilter.onBootstrap(event);
HttpServletRequest reqMock = mock(HttpServletRequest.class);
when(reqMock.getRequestURI()).thenReturn(loginPage);
authenticationFilter.doFilter(context, reqMock, res, chain);
assertTrue("Tenant domain should be empty", TenantContextHolder.getTenantDomain() == null);
}
}

View File

@@ -0,0 +1,124 @@
/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.web.app.servlet;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import javax.servlet.http.HttpServletRequest;
import org.alfresco.repo.management.subsystems.AbstractChainedSubsystemTest;
import org.alfresco.repo.management.subsystems.ChildApplicationContextFactory;
import org.alfresco.repo.management.subsystems.DefaultChildApplicationContextManager;
import org.alfresco.repo.webdav.auth.RemoteUserMapper;
import org.alfresco.util.ApplicationContextHelper;
import org.springframework.context.ApplicationContext;
/**
* @author dward
*
*/
public class DefaultRemoteUserMapperTest extends AbstractChainedSubsystemTest
{
ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
DefaultChildApplicationContextManager childApplicationContextManager;
ChildApplicationContextFactory childApplicationContextFactory;
/* (non-Javadoc)
* @see junit.framework.TestCase#setUp()
*/
@Override
protected void setUp() throws Exception
{
childApplicationContextManager = (DefaultChildApplicationContextManager) ctx.getBean("Authentication");
childApplicationContextManager.stop();
childApplicationContextManager.setProperty("chain", "external1:external");
childApplicationContextFactory = getChildApplicationContextFactory(childApplicationContextManager, "external1");
}
/* (non-Javadoc)
* @see junit.framework.TestCase#tearDown()
*/
@Override
protected void tearDown() throws Exception
{
childApplicationContextManager.destroy();
childApplicationContextManager = null;
childApplicationContextFactory = null;
}
public void testUnproxiedHeader() throws Exception
{
// Clear the proxy user name
childApplicationContextFactory.stop();
childApplicationContextFactory.setProperty("external.authentication.proxyUserName", "");
// Mock a request with a username in the header
HttpServletRequest mockRequest = mock(HttpServletRequest.class);
when(mockRequest.getHeader("X-Alfresco-Remote-User")).thenReturn("AdMiN");
assertEquals("admin", ((RemoteUserMapper) childApplicationContextFactory.getApplicationContext().getBean(
"remoteUserMapper")).getRemoteUser(mockRequest));
// Mock an unauthenticated request
when(mockRequest.getHeader("X-Alfresco-Remote-User")).thenReturn(null);
assertNull(((RemoteUserMapper) childApplicationContextFactory.getApplicationContext().getBean(
"remoteUserMapper")).getRemoteUser(mockRequest));
// Mock a remote user request
when(mockRequest.getRemoteUser()).thenReturn("ADMIN");
assertEquals("admin", ((RemoteUserMapper) childApplicationContextFactory.getApplicationContext().getBean(
"remoteUserMapper")).getRemoteUser(mockRequest));
}
public void testProxiedHeader() throws Exception
{
// Set the proxy user name
childApplicationContextFactory.stop();
childApplicationContextFactory.setProperty("external.authentication.proxyUserName", "bob");
// Mock a request with both a user and a header
HttpServletRequest mockRequest = mock(HttpServletRequest.class);
when(mockRequest.getRemoteUser()).thenReturn("bob");
when(mockRequest.getHeader("X-Alfresco-Remote-User")).thenReturn("AdMiN");
assertEquals("admin", ((RemoteUserMapper) childApplicationContextFactory.getApplicationContext().getBean(
"remoteUserMapper")).getRemoteUser(mockRequest));
// Now try header pattern matching
childApplicationContextFactory.stop();
childApplicationContextFactory.setProperty("external.authentication.userIdPattern", "abc-(.*)-999");
when(mockRequest.getHeader("X-Alfresco-Remote-User")).thenReturn("abc-AdMiN-999");
assertEquals("admin", ((RemoteUserMapper) childApplicationContextFactory.getApplicationContext().getBean(
"remoteUserMapper")).getRemoteUser(mockRequest));
// Try a request with an invalid match
when(mockRequest.getHeader("X-Alfresco-Remote-User")).thenReturn("abc-AdMiN-998");
assertNull(((RemoteUserMapper) childApplicationContextFactory.getApplicationContext().getBean(
"remoteUserMapper")).getRemoteUser(mockRequest));
// Try a request without the remote user
when(mockRequest.getRemoteUser()).thenReturn(null);
assertNull(((RemoteUserMapper) childApplicationContextFactory.getApplicationContext().getBean(
"remoteUserMapper")).getRemoteUser(mockRequest));
}
}