LocaleImportHandler extends RepositoryFileImportFileHandler implements IPlatformImportHandler { @VisibleForTesting RepositoryFile getLocaleParent( RepositoryFileImportBundle locale, Properties localePropertiesFromIndex ) { if ( unifiedRepository == null ) { return null; } RepositoryFile localeParent = null; String localeFileName = locale.getName(); if ( locale.getFile() != null ) { localeFileName = locale.getFile().getName(); } RepositoryFile localeFolder = unifiedRepository.getFile( locale.getPath() ); if ( isLocaleFolder( localeFileName, localePropertiesFromIndex ) ) { localeParent = localeFolder; } else if ( localeFolder != null ) { List<RepositoryFile> localeFolderChildren = unifiedRepository.getChildren( localeFolder.getId() ); RepositoryFile foundLocaleFolderChild = getLocaleFolderChild( localeFileName, localeFolderChildren ); if ( foundLocaleFolderChild == null ) { for ( RepositoryFile localeChild : localeFolderChildren ) { String localeChildName = extractFileName( localeChild.getName() ); String localeChildExtension = extractExtension( localeChild.getName() ); boolean localeFileNameAlsoContainsFileExtension = checkIfLocaleFileNameAlsoContainsAFileExtension( localeFileName ); if ( localeFileNameAlsoContainsFileExtension ) { String localeFileExtension = FilenameUtils.getExtension( FilenameUtils.getBaseName( localeFileName ) ); String localeFileNameWithoutExtensions = FilenameUtils.getBaseName( FilenameUtils.getBaseName( localeFileName ) ); if ( localeFileExtension.contains( "_" ) ) { localeFileExtension = localeFileExtension.substring( 0, localeFileExtension.indexOf( "_" ) ); } if ( localeFileNameWithoutExtensions.equals( localeChildName ) && localeFileExtension.equalsIgnoreCase( localeChildExtension ) && artifacts.contains( localeChildExtension ) ) { localeParent = localeChild; break; } } else if ( ( extractFileName( localeFileName ).equals( localeChildName ) || ( extractFileNameWithCountryLocalization( localeFileName ).equals( localeChildName ) ) || ( extractFileNameWithLanguageLocalization( localeFileName ).equals( localeChildName ) ) ) && artifacts.contains( localeChildExtension ) ) { localeParent = localeChild; break; } } } else { localeParent = foundLocaleFolderChild; } } return localeParent; } LocaleImportHandler( List<IMimeType> mimeTypes, List<String> artifacts ); void importFile( IPlatformImportBundle bundle );  }
@Test public void testImportDefaultPropertiesFiles() throws Exception { IUnifiedRepository mockUnifiedRepository = mock( IUnifiedRepository.class ); RepositoryFileImportBundle mockLocale = mock( RepositoryFileImportBundle.class ); Properties mockProperties = mock( Properties.class ); setInternalState( localeImportHandler, "unifiedRepository", mockUnifiedRepository ); when( mockLocale.getName() ).thenReturn( "Some File Name" ); when( mockLocale.getFile() ).thenReturn( mock( RepositoryFile.class ) ); when( mockLocale.getFile().getName() ).thenReturn( "someFile.properties" ); String propertiesContent = "description=Some Description\n" + "title=Some Title"; RepositoryFileImportBundle importBundle = createBundle( propertiesContent, "someFile.xaction" ); when( mockUnifiedRepository.getFile( anyString() ) ).thenReturn( importBundle.getFile() ); List<RepositoryFile> localeFolderChildren = new ArrayList<>( ); localeFolderChildren.add( importBundle.getFile() ); when( mockUnifiedRepository.getChildren( anyInt() ) ).thenReturn( localeFolderChildren ); RepositoryFile localeParent = localeImportHandler.getLocaleParent( mockLocale, mockProperties ); verify( mockUnifiedRepository, times( 1 ) ).getFile( anyString() ); verify( mockUnifiedRepository, times( 1 ) ).getChildren( anyInt() ); assertNotNull( localeParent ); }
@Test public void testImportLocalizedPropertiesFiles_fr() throws Exception { LocaleHelper.setLocale( new Locale( "fr" ) ); IUnifiedRepository mockUnifiedRepository = mock( IUnifiedRepository.class ); RepositoryFileImportBundle mockLocale = mock( RepositoryFileImportBundle.class ); Properties mockProperties = mock( Properties.class ); setInternalState( localeImportHandler, "unifiedRepository", mockUnifiedRepository ); when( mockLocale.getName() ).thenReturn( "Some File Name" ); when( mockLocale.getFile() ).thenReturn( mock( RepositoryFile.class ) ); when( mockLocale.getFile().getName() ).thenReturn( "someFile_fr.properties" ); String propertiesContent = "description=Some Description\n" + "title=Some Title"; RepositoryFileImportBundle importBundle = createBundle( propertiesContent, "someFile.xaction" ); when( mockUnifiedRepository.getFile( anyString() ) ).thenReturn( importBundle.getFile() ); List<RepositoryFile> localeFolderChildren = new ArrayList<>( ); localeFolderChildren.add( importBundle.getFile() ); when( mockUnifiedRepository.getChildren( anyInt() ) ).thenReturn( localeFolderChildren ); RepositoryFile localeParent = localeImportHandler.getLocaleParent( mockLocale, mockProperties ); verify( mockUnifiedRepository, times( 1 ) ).getFile( anyString() ); verify( mockUnifiedRepository, times( 1 ) ).getChildren( anyInt() ); assertNotNull( localeParent ); }
@Test public void testImportLocalizedPropertiesFiles_en_us() throws Exception { LocaleHelper.setLocale( new Locale( "en_US" ) ); IUnifiedRepository mockUnifiedRepository = mock( IUnifiedRepository.class ); RepositoryFileImportBundle mockLocale = mock( RepositoryFileImportBundle.class ); Properties mockProperties = mock( Properties.class ); setInternalState( localeImportHandler, "unifiedRepository", mockUnifiedRepository ); when( mockLocale.getName() ).thenReturn( "Some File Name" ); when( mockLocale.getFile() ).thenReturn( mock( RepositoryFile.class ) ); when( mockLocale.getFile().getName() ).thenReturn( "someFile_en_US.properties" ); String propertiesContent = "description=Some Description\n" + "title=Some Title"; RepositoryFileImportBundle importBundle = createBundle( propertiesContent, "someFile.xaction" ); when( mockUnifiedRepository.getFile( anyString() ) ).thenReturn( importBundle.getFile() ); List<RepositoryFile> localeFolderChildren = new ArrayList<>( ); localeFolderChildren.add( importBundle.getFile() ); when( mockUnifiedRepository.getChildren( anyInt() ) ).thenReturn( localeFolderChildren ); RepositoryFile localeParent = localeImportHandler.getLocaleParent( mockLocale, mockProperties ); verify( mockUnifiedRepository, times( 1 ) ).getFile( anyString() ); verify( mockUnifiedRepository, times( 1 ) ).getChildren( anyInt() ); assertNotNull( localeParent ); }
@Test public void testImportLocalizedPropertiesFiles_en_gb() throws Exception { LocaleHelper.setLocale( new Locale( "en_GB" ) ); IUnifiedRepository mockUnifiedRepository = mock( IUnifiedRepository.class ); RepositoryFileImportBundle mockLocale = mock( RepositoryFileImportBundle.class ); Properties mockProperties = mock( Properties.class ); setInternalState( localeImportHandler, "unifiedRepository", mockUnifiedRepository ); when( mockLocale.getName() ).thenReturn( "Some File Name" ); when( mockLocale.getFile() ).thenReturn( mock( RepositoryFile.class ) ); when( mockLocale.getFile().getName() ).thenReturn( "someFile_en_GB.properties" ); String propertiesContent = "description=Some Description\n" + "title=Some Title"; RepositoryFileImportBundle importBundle = createBundle( propertiesContent, "someFile.xaction" ); when( mockUnifiedRepository.getFile( anyString() ) ).thenReturn( importBundle.getFile() ); List<RepositoryFile> localeFolderChildren = new ArrayList<>( ); localeFolderChildren.add( importBundle.getFile() ); when( mockUnifiedRepository.getChildren( anyInt() ) ).thenReturn( localeFolderChildren ); RepositoryFile localeParent = localeImportHandler.getLocaleParent( mockLocale, mockProperties ); verify( mockUnifiedRepository, times( 1 ) ).getFile( anyString() ); verify( mockUnifiedRepository, times( 1 ) ).getChildren( anyInt() ); assertNotNull( localeParent ); }
@Test public void testImportDefaultWithFileExtensionPropertiesFiles() throws Exception { IUnifiedRepository mockUnifiedRepository = mock( IUnifiedRepository.class ); RepositoryFileImportBundle mockLocale = mock( RepositoryFileImportBundle.class ); Properties mockProperties = mock( Properties.class ); setInternalState( localeImportHandler, "unifiedRepository", mockUnifiedRepository ); when( mockLocale.getName() ).thenReturn( "Some File Name" ); when( mockLocale.getFile() ).thenReturn( mock( RepositoryFile.class ) ); when( mockLocale.getFile().getName() ).thenReturn( "someFile.xaction.properties" ); String propertiesContent = "description=Some Description\n" + "title=Some Title"; RepositoryFileImportBundle importBundle = createBundle( propertiesContent, "someFile.xaction" ); when( mockUnifiedRepository.getFile( anyString() ) ).thenReturn( importBundle.getFile() ); List<RepositoryFile> localeFolderChildren = new ArrayList<>( ); localeFolderChildren.add( importBundle.getFile() ); when( mockUnifiedRepository.getChildren( anyInt() ) ).thenReturn( localeFolderChildren ); RepositoryFile localeParent = localeImportHandler.getLocaleParent( mockLocale, mockProperties ); verify( mockUnifiedRepository, times( 1 ) ).getFile( anyString() ); verify( mockUnifiedRepository, times( 1 ) ).getChildren( anyInt() ); assertNotNull( localeParent ); }
@Test public void testImportLocalizedWithFileExtensionPropertiesFiles_fr() throws Exception { IUnifiedRepository mockUnifiedRepository = mock( IUnifiedRepository.class ); RepositoryFileImportBundle mockLocale = mock( RepositoryFileImportBundle.class ); Properties mockProperties = mock( Properties.class ); setInternalState( localeImportHandler, "unifiedRepository", mockUnifiedRepository ); when( mockLocale.getName() ).thenReturn( "Some File Name" ); when( mockLocale.getFile() ).thenReturn( mock( RepositoryFile.class ) ); when( mockLocale.getFile().getName() ).thenReturn( "some_File.xaction_fr.locale" ); String propertiesContent = "description=Some Description\n" + "title=Some Title"; RepositoryFileImportBundle importBundle = createBundle( propertiesContent, "some_File.xaction" ); when( mockUnifiedRepository.getFile( anyString() ) ).thenReturn( importBundle.getFile() ); List<RepositoryFile> localeFolderChildren = new ArrayList<>( ); localeFolderChildren.add( importBundle.getFile() ); when( mockUnifiedRepository.getChildren( anyInt() ) ).thenReturn( localeFolderChildren ); RepositoryFile localeParent = localeImportHandler.getLocaleParent( mockLocale, mockProperties ); verify( mockUnifiedRepository, times( 1 ) ).getFile( anyString() ); verify( mockUnifiedRepository, times( 1 ) ).getChildren( anyInt() ); assertNotNull( localeParent ); }
@Test public void testImportLocalizedWithFileExtensionPropertiesFiles_en_us() throws Exception { IUnifiedRepository mockUnifiedRepository = mock( IUnifiedRepository.class ); RepositoryFileImportBundle mockLocale = mock( RepositoryFileImportBundle.class ); Properties mockProperties = mock( Properties.class ); setInternalState( localeImportHandler, "unifiedRepository", mockUnifiedRepository ); when( mockLocale.getName() ).thenReturn( "Some File Name" ); when( mockLocale.getFile() ).thenReturn( mock( RepositoryFile.class ) ); when( mockLocale.getFile().getName() ).thenReturn( "someFile.xaction_en_US.locale" ); String propertiesContent = "description=Some Description\n" + "title=Some Title"; RepositoryFileImportBundle importBundle = createBundle( propertiesContent, "someFile.xaction" ); when( mockUnifiedRepository.getFile( anyString() ) ).thenReturn( importBundle.getFile() ); List<RepositoryFile> localeFolderChildren = new ArrayList<>( ); localeFolderChildren.add( importBundle.getFile() ); when( mockUnifiedRepository.getChildren( anyInt() ) ).thenReturn( localeFolderChildren ); RepositoryFile localeParent = localeImportHandler.getLocaleParent( mockLocale, mockProperties ); verify( mockUnifiedRepository, times( 1 ) ).getFile( anyString() ); verify( mockUnifiedRepository, times( 1 ) ).getChildren( anyInt() ); assertNotNull( localeParent ); }
@Test public void testImportLocalizedWithFileExtensionPropertiesFiles_en_gb() throws Exception { LocaleHelper.setLocale( new Locale( "en_GB" ) ); IUnifiedRepository mockUnifiedRepository = mock( IUnifiedRepository.class ); RepositoryFileImportBundle mockLocale = mock( RepositoryFileImportBundle.class ); Properties mockProperties = mock( Properties.class ); setInternalState( localeImportHandler, "unifiedRepository", mockUnifiedRepository ); when( mockLocale.getName() ).thenReturn( "Some File Name" ); when( mockLocale.getFile() ).thenReturn( mock( RepositoryFile.class ) ); when( mockLocale.getFile().getName() ).thenReturn( "someFile.xaction_en_GB.properties" ); String propertiesContent = "description=Some Description\n" + "title=Some Title"; RepositoryFileImportBundle importBundle = createBundle( propertiesContent, "someFile.xaction" ); when( mockUnifiedRepository.getFile( anyString() ) ).thenReturn( importBundle.getFile() ); List<RepositoryFile> localeFolderChildren = new ArrayList<>( ); localeFolderChildren.add( importBundle.getFile() ); when( mockUnifiedRepository.getChildren( anyInt() ) ).thenReturn( localeFolderChildren ); RepositoryFile localeParent = localeImportHandler.getLocaleParent( mockLocale, mockProperties ); verify( mockUnifiedRepository, times( 1 ) ).getFile( anyString() ); verify( mockUnifiedRepository, times( 1 ) ).getChildren( anyInt() ); assertNotNull( localeParent ); }
@Test public void testImportLocaleFolderChild() { IUnifiedRepository mockUnifiedRepository = mock( IUnifiedRepository.class ); RepositoryFileImportBundle mockLocale = mock( RepositoryFileImportBundle.class ); Properties mockProperties = mock( Properties.class ); setInternalState( localeImportHandler, "unifiedRepository", mockUnifiedRepository ); String someFile1 = "someFile.xaction"; String someFile2 = "someFile_rf.xaction"; String someFile3 = "someFile_rf.prpt"; String propertyFile = "someFile_rf.xaction.properties"; String propertiesContent = "description=Some Description\n" + "title=Some Title"; RepositoryFileImportBundle importBundle1 = createBundle( propertiesContent, someFile1 ); when( mockUnifiedRepository.getFile( FILE_BUNDLE_PATH + someFile1 ) ).thenReturn( importBundle1.getFile() ); RepositoryFileImportBundle importBundle2 = createBundle( propertiesContent, someFile2 ); when( mockUnifiedRepository.getFile( FILE_BUNDLE_PATH + someFile2 ) ).thenReturn( importBundle2.getFile() ); RepositoryFileImportBundle importBundle3 = createBundle( propertiesContent, someFile3 ); when( mockUnifiedRepository.getFile( FILE_BUNDLE_PATH + someFile3 ) ).thenReturn( importBundle3.getFile() ); RepositoryFileImportBundle importProperties = createBundle( propertiesContent, propertyFile ); when( mockUnifiedRepository.getFile( FILE_BUNDLE_PATH + propertyFile ) ).thenReturn( importProperties.getFile() ); List<RepositoryFile> localeFolderChildren = new ArrayList<>( ); localeFolderChildren.add( importBundle1.getFile() ); localeFolderChildren.add( importBundle2.getFile() ); localeFolderChildren.add( importBundle3.getFile() ); when( mockUnifiedRepository.getChildren( anyInt() ) ).thenReturn( localeFolderChildren ); RepositoryFile localeParent1 = localeImportHandler.getLocaleParent( importBundle1, mockProperties ); RepositoryFile localeParent2 = localeImportHandler.getLocaleParent( importBundle2, mockProperties ); RepositoryFile localeParent3 = localeImportHandler.getLocaleParent( importBundle3, mockProperties ); RepositoryFile localeParent4 = localeImportHandler.getLocaleParent( importProperties, mockProperties ); verify( mockUnifiedRepository, times( 4 ) ).getFile( anyString() ); verify( mockUnifiedRepository, times( 4 ) ).getChildren( anyInt() ); assertNotNull( localeParent1 ); assertNotNull( localeParent2 ); assertNotNull( localeParent3 ); assertNotNull( localeParent4 ); assertEquals( localeParent1.getName(), someFile1 ); assertEquals( localeParent2.getName(), someFile2 ); assertEquals( localeParent3.getName(), someFile3 ); assertEquals( localeParent4.getName(), someFile2 ); }