You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IdentityManagerTest.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2006-2015 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.tests.JimFsRule;
  26. import com.dmdirc.util.ClientInfo;
  27. import java.io.IOException;
  28. import java.nio.file.Files;
  29. import java.nio.file.Path;
  30. import java.util.Collection;
  31. import org.junit.Before;
  32. import org.junit.Rule;
  33. import org.junit.Test;
  34. import org.junit.runner.RunWith;
  35. import org.mockito.Mock;
  36. import org.mockito.runners.MockitoJUnitRunner;
  37. import static org.junit.Assert.assertEquals;
  38. import static org.junit.Assert.assertNotNull;
  39. @RunWith(MockitoJUnitRunner.class)
  40. public class IdentityManagerTest {
  41. @Rule public final JimFsRule jimFsRule = new JimFsRule();
  42. @Mock private DMDircMBassador eventBus;
  43. @Mock private ClientInfo clientInfo;
  44. private Path baseDirectory;
  45. private Path identitiesDirectory;
  46. @Before
  47. public void setUp() throws Exception {
  48. baseDirectory = jimFsRule.getPath("config");
  49. identitiesDirectory = baseDirectory.resolve("identities");
  50. }
  51. @Test
  52. public void testLoadsVersionIdentity() throws InvalidIdentityFileException {
  53. final IdentityManager identityManager = new IdentityManager(
  54. baseDirectory, identitiesDirectory, eventBus, clientInfo);
  55. identityManager.initialise();
  56. final ConfigProvider versionSettings = identityManager.getVersionSettings();
  57. assertNotNull(versionSettings);
  58. assertEquals(ConfigTarget.TYPE.GLOBALDEFAULT, versionSettings.getTarget().getType());
  59. assertEquals("DMDirc version information", versionSettings.getName());
  60. }
  61. @Test
  62. public void testUsesSystemUsernameForProfileNickname() throws InvalidIdentityFileException {
  63. final IdentityManager identityManager = new IdentityManager(
  64. baseDirectory, identitiesDirectory, eventBus, clientInfo);
  65. identityManager.initialise();
  66. System.setProperty("user.name", "Awesome User");
  67. final ConfigProvider config = identityManager.createProfileConfig("test 123");
  68. assertEquals("Awesome_User", config.getOption("profile", "nicknames"));
  69. }
  70. @Test
  71. public void testDoesNotCreateProfileIfOneExists()
  72. throws IOException, InvalidIdentityFileException {
  73. Files.createDirectories(identitiesDirectory);
  74. Files.copy(getClass().getResourceAsStream("profile-new"),
  75. identitiesDirectory.resolve("profile"));
  76. final IdentityManager identityManager = new IdentityManager(
  77. baseDirectory, identitiesDirectory, eventBus, clientInfo);
  78. identityManager.initialise();
  79. final Collection<ConfigProvider> profiles = identityManager.getProvidersByType("profile");
  80. assertEquals(1, profiles.size());
  81. assertEquals("New Profile", profiles.stream().findAny().get().getName());
  82. }
  83. }