您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

IdentityManagerTest.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.config;
  23. import com.dmdirc.DMDircMBassador;
  24. import com.dmdirc.interfaces.config.ConfigProvider;
  25. import com.dmdirc.util.ClientInfo;
  26. import com.google.common.jimfs.Configuration;
  27. import com.google.common.jimfs.Jimfs;
  28. import java.io.IOException;
  29. import java.nio.file.FileSystem;
  30. import java.nio.file.Files;
  31. import java.nio.file.Path;
  32. import java.util.List;
  33. import org.junit.Before;
  34. import org.junit.Test;
  35. import org.junit.runner.RunWith;
  36. import org.mockito.Mock;
  37. import org.mockito.runners.MockitoJUnitRunner;
  38. import static org.junit.Assert.assertEquals;
  39. import static org.junit.Assert.assertNotNull;
  40. @RunWith(MockitoJUnitRunner.class)
  41. public class IdentityManagerTest {
  42. @Mock private DMDircMBassador eventBus;
  43. @Mock private ClientInfo clientInfo;
  44. private Path baseDirectory;
  45. private Path identitiesDirectory;
  46. @Before
  47. @SuppressWarnings("resource")
  48. public void setUp() throws Exception {
  49. final FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
  50. baseDirectory = fs.getPath("config");
  51. identitiesDirectory = baseDirectory.resolve("identities");
  52. }
  53. @Test
  54. public void testLoadsVersionIdentity() throws InvalidIdentityFileException {
  55. final IdentityManager identityManager = new IdentityManager(
  56. baseDirectory, identitiesDirectory, eventBus, clientInfo);
  57. identityManager.initialise();
  58. final ConfigProvider versionSettings = identityManager.getVersionSettings();
  59. assertNotNull(versionSettings);
  60. assertEquals(ConfigTarget.TYPE.GLOBALDEFAULT, versionSettings.getTarget().getType());
  61. assertEquals("DMDirc version information", versionSettings.getName());
  62. }
  63. @Test
  64. public void testUsesSystemUsernameForProfileNickname() throws InvalidIdentityFileException {
  65. final IdentityManager identityManager = new IdentityManager(
  66. baseDirectory, identitiesDirectory, eventBus, clientInfo);
  67. identityManager.initialise();
  68. System.setProperty("user.name", "Awesome User");
  69. final ConfigProvider config = identityManager.createProfileConfig("test 123");
  70. assertEquals("Awesome_User", config.getOption("profile", "nicknames"));
  71. }
  72. @Test
  73. public void testDoesNotCreateProfileIfOneExists()
  74. throws IOException, InvalidIdentityFileException {
  75. Files.createDirectories(identitiesDirectory);
  76. Files.copy(getClass().getResourceAsStream("profile-new"),
  77. identitiesDirectory.resolve("profile"));
  78. final IdentityManager identityManager = new IdentityManager(
  79. baseDirectory, identitiesDirectory, eventBus, clientInfo);
  80. identityManager.initialise();
  81. final List<ConfigProvider> profiles = identityManager.getProvidersByType("profile");
  82. assertEquals(1, profiles.size());
  83. assertEquals("New Profile", profiles.get(0).getName());
  84. }
  85. }