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.

NaiveConsolidatorTest.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2006-2013 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.updater.checking;
  23. import com.dmdirc.updater.UpdateComponent;
  24. import java.util.ArrayList;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. import org.junit.Before;
  29. import org.junit.Test;
  30. import static org.junit.Assert.*;
  31. import static org.mockito.Mockito.*;
  32. public class NaiveConsolidatorTest {
  33. private NaiveConsolidator consolidator;
  34. private List<UpdateComponent> components;
  35. private List<UpdateCheckResult> positiveResults;
  36. private List<UpdateCheckResult> negativeResults;
  37. @Before
  38. public void setUp() {
  39. consolidator = new NaiveConsolidator();
  40. components = new ArrayList<UpdateComponent>();
  41. positiveResults = new ArrayList<UpdateCheckResult>();
  42. negativeResults = new ArrayList<UpdateCheckResult>();
  43. for (int i = 0; i < 4; i++) {
  44. final UpdateComponent component = mock(UpdateComponent.class);
  45. final UpdateCheckResult negative = mock(UpdateCheckResult.class);
  46. final UpdateCheckResult positive = mock(UpdateCheckResult.class);
  47. when(negative.getComponent()).thenReturn(component);
  48. when(negative.isUpdateAvailable()).thenReturn(false);
  49. when(positive.getComponent()).thenReturn(component);
  50. when(positive.isUpdateAvailable()).thenReturn(true);
  51. components.add(component);
  52. negativeResults.add(negative);
  53. positiveResults.add(negative);
  54. }
  55. }
  56. @Test
  57. public void testIncludesAllComponentsFromAllMaps() {
  58. final Map<UpdateComponent, UpdateCheckResult> map1
  59. = new HashMap<UpdateComponent, UpdateCheckResult>();
  60. final Map<UpdateComponent, UpdateCheckResult> map2
  61. = new HashMap<UpdateComponent, UpdateCheckResult>();
  62. final List<Map<UpdateComponent, UpdateCheckResult>> maps
  63. = new ArrayList<Map<UpdateComponent, UpdateCheckResult>>();
  64. map1.put(components.get(0), negativeResults.get(0));
  65. map1.put(components.get(1), negativeResults.get(1));
  66. map2.put(components.get(2), negativeResults.get(2));
  67. maps.add(map1);
  68. maps.add(map2);
  69. final Map<UpdateComponent, UpdateCheckResult> res = consolidator.consolidate(maps);
  70. assertEquals(3, res.size());
  71. assertTrue(res.containsKey(components.get(0)));
  72. assertTrue(res.containsKey(components.get(1)));
  73. assertTrue(res.containsKey(components.get(2)));
  74. }
  75. @Test
  76. public void testIncludesPositiveResultsForKnownNegativeComponents() {
  77. final Map<UpdateComponent, UpdateCheckResult> map1
  78. = new HashMap<UpdateComponent, UpdateCheckResult>();
  79. final Map<UpdateComponent, UpdateCheckResult> map2
  80. = new HashMap<UpdateComponent, UpdateCheckResult>();
  81. final List<Map<UpdateComponent, UpdateCheckResult>> maps
  82. = new ArrayList<Map<UpdateComponent, UpdateCheckResult>>();
  83. map1.put(components.get(0), negativeResults.get(0));
  84. map1.put(components.get(1), negativeResults.get(1));
  85. map2.put(components.get(1), positiveResults.get(1));
  86. map2.put(components.get(2), negativeResults.get(2));
  87. maps.add(map1);
  88. maps.add(map2);
  89. final Map<UpdateComponent, UpdateCheckResult> res = consolidator.consolidate(maps);
  90. assertTrue(res.containsKey(components.get(1)));
  91. assertSame(positiveResults.get(1), res.get(components.get(1)));
  92. }
  93. @Test
  94. public void testIgnoresNegativeResultsForKnownPositiveComponents() {
  95. final Map<UpdateComponent, UpdateCheckResult> map1
  96. = new HashMap<UpdateComponent, UpdateCheckResult>();
  97. final Map<UpdateComponent, UpdateCheckResult> map2
  98. = new HashMap<UpdateComponent, UpdateCheckResult>();
  99. final List<Map<UpdateComponent, UpdateCheckResult>> maps
  100. = new ArrayList<Map<UpdateComponent, UpdateCheckResult>>();
  101. map1.put(components.get(0), negativeResults.get(0));
  102. map1.put(components.get(1), positiveResults.get(1));
  103. map2.put(components.get(1), negativeResults.get(1));
  104. map2.put(components.get(2), negativeResults.get(2));
  105. maps.add(map1);
  106. maps.add(map2);
  107. final Map<UpdateComponent, UpdateCheckResult> res = consolidator.consolidate(maps);
  108. assertTrue(res.containsKey(components.get(1)));
  109. assertSame(positiveResults.get(1), res.get(components.get(1)));
  110. }
  111. }