PageRenderTime 920ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/private-components/integrated-ui/src/test/java/org/limewire/ui/AnnotationsCheckTest.java

https://github.com/wiregit/wirecode
Java | 166 lines | 119 code | 23 blank | 24 comment | 11 complexity | d6a15db7f9986299da341162c2d20ba1 MD5 | raw file
  1. package org.limewire.ui;
  2. import java.io.File;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.concurrent.ConcurrentHashMap;
  7. import java.util.concurrent.atomic.AtomicReference;
  8. import javax.swing.SwingUtilities;
  9. import junit.framework.Test;
  10. import org.limewire.gnutella.tests.LimeTestUtils;
  11. import org.limewire.inject.AbstractModule;
  12. import org.limewire.inject.GuiceUtils;
  13. import org.limewire.inject.LimeWireInjectModule;
  14. import org.limewire.inject.Modules;
  15. import org.limewire.inspection.InspectionException;
  16. import org.limewire.inspection.InspectionRequirement;
  17. import org.limewire.inspection.InspectionTool;
  18. import org.limewire.inspection.Inspector;
  19. import org.limewire.ui.swing.AllLimeWireModules__DO_NOT_USE;
  20. import org.limewire.ui.swing.LimeWireModule;
  21. import org.limewire.ui.swing.LimeWireSwingUiModule;
  22. import org.limewire.ui.swing.mainframe.AppFrame;
  23. import org.limewire.util.BaseTestCase;
  24. import org.limewire.util.OSUtils;
  25. import com.google.inject.Binding;
  26. import com.google.inject.Guice;
  27. import com.google.inject.Injector;
  28. import com.google.inject.Key;
  29. import com.google.inject.Scopes;
  30. public class AnnotationsCheckTest extends BaseTestCase {
  31. public AnnotationsCheckTest(String name) {
  32. super(name);
  33. }
  34. public static Test suite() {
  35. return buildTestSuite(AnnotationsCheckTest.class);
  36. }
  37. private Map<String, String> createInspectionMappings(final List<File> buildFolders, final AtomicReference<Injector> injectorRef) throws Exception {
  38. final Map<String, String> results = new ConcurrentHashMap<String, String>();
  39. // This is explicitly using the DoNotUse module because that's what the build uses.
  40. // (do it in the Swing thread to account for UI injectables that need the Swing thread)
  41. SwingUtilities.invokeAndWait(new Runnable() {
  42. @Override
  43. public void run() {
  44. Injector injector = Guice.createInjector(new AllLimeWireModules__DO_NOT_USE());
  45. GuiceUtils.loadEagerSingletons(injector);
  46. injectorRef.set(injector);
  47. for(File path : buildFolders) {
  48. results.putAll(InspectionTool.generateMappings(path, injectorRef.get(), new String[0]));
  49. }
  50. }
  51. });
  52. return results;
  53. }
  54. private void runInspectorTests(Inspector inspector, Map<String, String> results) {
  55. for(String key : results.keySet()) {
  56. try {
  57. inspector.inspect(results.get(key), true);
  58. } catch(InspectionException ie) {
  59. boolean validFailure = false;
  60. if(OSUtils.isLinux()) {
  61. validFailure = ie.getRequirements().size() > 0 && !ie.getRequirements().contains(InspectionRequirement.OS_LINUX);
  62. } else if(OSUtils.isMacOSX()) {
  63. validFailure = ie.getRequirements().size() > 0 && !ie.getRequirements().contains(InspectionRequirement.OS_OSX);
  64. } else if(OSUtils.isWindows()) {
  65. validFailure = ie.getRequirements().size() > 0 && !ie.getRequirements().contains(InspectionRequirement.OS_WINDOWS);
  66. }
  67. if(!validFailure) {
  68. fail("failed on key: " + key + ", value: " + results.get(key), ie);
  69. // System.out.println("failed on key: " + key + ", value: " + results.get(key) + " -- " + ie);
  70. }
  71. }
  72. }
  73. }
  74. public void testAnnotations() throws Exception {
  75. List<File> buildFolders = LimeTestUtils.getBuildFolders(getClass());
  76. AtomicReference<Injector> injectorRef = new AtomicReference<Injector>();
  77. Map<String, String> results = createInspectionMappings(buildFolders, injectorRef);
  78. // make sure we got some good # of inspections
  79. assertFalse(results.isEmpty());
  80. assertGreaterThan(100, results.size());
  81. // and run through them all and make sure they work!
  82. Inspector inspector = injectorRef.get().getInstance(Inspector.class);
  83. Map<Key<?>, Binding<?>> preBindings = injectorRef.get().getAllBindings();
  84. runInspectorTests(inspector, results);
  85. Map<Key<?>, Binding<?>> postBindings = injectorRef.get().getAllBindings();
  86. // We look at the difference in bindings before & after and fail if any
  87. // bindings were added. The idea here is that only things that were singletons
  88. // should have been bound, so no new bindings should have been added.
  89. // If a new binding was added, something either wasn't a singleton,
  90. // wasn't bound, or was created twice!
  91. Map<Key<?>, Binding<?>> diff = new HashMap<Key<?>, Binding<?>>(postBindings);
  92. diff.keySet().removeAll(preBindings.keySet());
  93. assertTrue("added bindings: " + diff, diff.isEmpty());
  94. }
  95. // This tests exists because the way LW starts, it uses two injectors --
  96. // one for core, and another for UI. We can't use child injectors because
  97. // of quirks with just-in-time bindings.
  98. // However, by using two injectors, it messes up some detection of singletons...
  99. // This test makes sure that LW's setup can properly let us get everything
  100. // we want.
  101. public void testAnnotationsTheWayLimeWireWillDoIt() throws Exception {
  102. List<File> buildFolders = LimeTestUtils.getBuildFolders(getClass());
  103. final AtomicReference<Injector> injectorRef = new AtomicReference<Injector>();
  104. Map<String, String> results = createInspectionMappings(buildFolders, injectorRef);
  105. // make sure we got some good # of inspections
  106. assertFalse(results.isEmpty());
  107. assertGreaterThan(100, results.size());
  108. // Now create the real injector that we'll do inspection tests on,
  109. // and make sure that we reset the injector on the Inspector.
  110. injectorRef.set(null);
  111. final Injector coreInjector = Guice.createInjector(new LimeWireModule());
  112. SwingUtilities.invokeAndWait(new Runnable() {
  113. @Override
  114. public void run() {
  115. Injector injector = Guice.createInjector(
  116. Modules.providersFrom(coreInjector),
  117. new LimeWireInjectModule(),
  118. new LimeWireSwingUiModule(),
  119. new AbstractModule() {
  120. @Override
  121. protected void configure() {
  122. bind(AppFrame.class).in(Scopes.SINGLETON);
  123. }
  124. });
  125. GuiceUtils.loadEagerSingletons(injector);
  126. injectorRef.set(injector);
  127. }
  128. });
  129. Inspector inspector = injectorRef.get().getInstance(Inspector.class);
  130. inspector.setInjector(injectorRef.get());
  131. Map<Key<?>, Binding<?>> preBindings = injectorRef.get().getAllBindings();
  132. runInspectorTests(inspector, results);
  133. Map<Key<?>, Binding<?>> postBindings = injectorRef.get().getAllBindings();
  134. // We look at the difference in bindings before & after and fail if any
  135. // bindings were added. The idea here is that only things that were singletons
  136. // should have been bound, so no new bindings should have been added.
  137. // If a new binding was added, something either wasn't a singleton,
  138. // wasn't bound, or was created twice!
  139. Map<Key<?>, Binding<?>> diff = new HashMap<Key<?>, Binding<?>>(postBindings);
  140. diff.keySet().removeAll(preBindings.keySet());
  141. assertTrue("added bindings: " + diff, diff.isEmpty());
  142. }
  143. }