/** * Copyright (c) 2000-present Liferay, Inc. All rights reserved. * * This library 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 2.1 of the License, or (at your option) * any later version. * * This library 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. */ package com.liferay.dynamic.data.mapping.io.internal; import com.liferay.dynamic.data.mapping.model.DDMFormLayoutColumn; import com.liferay.dynamic.data.mapping.model.DDMFormLayoutRow; import com.liferay.portal.json.JSONFactoryImpl; import com.liferay.portal.kernel.json.JSONFactoryUtil; import com.liferay.portal.kernel.language.Language; import com.liferay.portal.kernel.language.LanguageUtil; import com.liferay.portal.kernel.util.LocaleUtil; import com.liferay.portal.kernel.util.PortalClassLoaderUtil; import com.liferay.portal.kernel.util.ResourceBundleUtil; import com.liferay.portal.kernel.util.SetUtil; import com.liferay.portal.kernel.util.StringUtil; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Locale; import java.util.ResourceBundle; import java.util.Set; import org.junit.Before; import org.junit.runner.RunWith; import org.mockito.Matchers; import org.mockito.Mock; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; /** * @author Marcellus Tavares */ @PrepareForTest( {LocaleUtil.class, PortalClassLoaderUtil.class, ResourceBundleUtil.class} ) @RunWith(PowerMockRunner.class) public abstract class BaseDDMTestCase extends PowerMockito { @Before public void setUp() throws Exception { setUpJSONFactoryUtil(); setUpLanguageUtil(); setUpLocaleUtil(); setUpPortalClassLoaderUtil(); setUpResourceBundleUtil(); } protected DDMFormLayoutColumn createDDMFormLayoutColumn( int size, String... fieldNames) { DDMFormLayoutColumn ddmFormLayoutColumn = new DDMFormLayoutColumn( size, fieldNames); return ddmFormLayoutColumn; } protected List<DDMFormLayoutColumn> createDDMFormLayoutColumns( String... fieldNames) { List<DDMFormLayoutColumn> ddmFormLayoutColumns = new ArrayList<>(); int size = 12 / fieldNames.length; for (String fieldName : fieldNames) { ddmFormLayoutColumns.add(new DDMFormLayoutColumn(size, fieldName)); } return ddmFormLayoutColumns; } protected DDMFormLayoutRow createDDMFormLayoutRow( DDMFormLayoutColumn... ddmFormLayoutColumns) { return createDDMFormLayoutRow(Arrays.asList(ddmFormLayoutColumns)); } protected DDMFormLayoutRow createDDMFormLayoutRow( List<DDMFormLayoutColumn> ddmFormLayoutColumns) { DDMFormLayoutRow ddmFormLayoutRow = new DDMFormLayoutRow(); ddmFormLayoutRow.setDDMFormLayoutColumns(ddmFormLayoutColumns); return ddmFormLayoutRow; } protected String read(String fileName) throws IOException { Class<?> clazz = getClass(); InputStream inputStream = clazz.getResourceAsStream( "dependencies/" + fileName); return StringUtil.read(inputStream); } protected void setUpJSONFactoryUtil() { JSONFactoryUtil jsonFactoryUtil = new JSONFactoryUtil(); jsonFactoryUtil.setJSONFactory(new JSONFactoryImpl()); } protected void setUpLanguageUtil() { Set<Locale> availableLocales = SetUtil.fromArray( new Locale[] {LocaleUtil.BRAZIL, LocaleUtil.US}); whenLanguageGetAvailableLocalesThen(availableLocales); whenLanguageIsAvailableLocale("en_US"); whenLanguageIsAvailableLocale("pt_BR"); LanguageUtil languageUtil = new LanguageUtil(); languageUtil.setLanguage(language); } protected void setUpLocaleUtil() { mockStatic(LocaleUtil.class); when( LocaleUtil.fromLanguageId("en_US") ).thenReturn( LocaleUtil.US ); when( LocaleUtil.fromLanguageId("pt_BR") ).thenReturn( LocaleUtil.BRAZIL ); when( LocaleUtil.getDefault() ).thenReturn( LocaleUtil.US ); when( LocaleUtil.toLanguageId(LocaleUtil.US) ).thenReturn( "en_US" ); when( LocaleUtil.toLanguageId(LocaleUtil.BRAZIL) ).thenReturn( "pt_BR" ); when( LocaleUtil.toLanguageIds((Locale[])Matchers.any()) ).then( new Answer<String[]>() { @Override public String[] answer(InvocationOnMock invocationOnMock) throws Throwable { Object[] args = invocationOnMock.getArguments(); Locale[] locales = (Locale[])args[0]; String[] languageIds = new String[locales.length]; for (int i = 0; i < locales.length; i++) { languageIds[i] = LocaleUtil.toLanguageId(locales[i]); } return languageIds; } } ); } protected void setUpPortalClassLoaderUtil() { mockStatic(PortalClassLoaderUtil.class); when( PortalClassLoaderUtil.getClassLoader() ).thenReturn( _classLoader ); } protected void setUpResourceBundleUtil() { mockStatic(ResourceBundleUtil.class); when( ResourceBundleUtil.getBundle( "content.Language", LocaleUtil.BRAZIL, _classLoader) ).thenReturn( _resourceBundle ); when( ResourceBundleUtil.getBundle( "content.Language", LocaleUtil.US, _classLoader) ).thenReturn( _resourceBundle ); } protected void whenLanguageGetAvailableLocalesThen( Set<Locale> availableLocales) { when( language.getAvailableLocales() ).thenReturn( availableLocales ); } protected void whenLanguageIsAvailableLocale(String languageId) { when( language.isAvailableLocale(Matchers.eq(languageId)) ).thenReturn( true ); } @Mock protected Language language; @Mock private ClassLoader _classLoader; @Mock private ResourceBundle _resourceBundle; }