PageRenderTime 58ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/sisu-2.2.3/sisu-inject/containers/guice-plexus/guice-plexus-locators/src/test/java/org/sonatype/guice/plexus/locators/PlexusBeanLocatorTest.java

#
Java | 541 lines | 459 code | 75 blank | 7 comment | 0 complexity | 4a1535cfd9f9c5464e55a81d38f1fbb8 MD5 | raw file
Possible License(s): Apache-2.0, EPL-1.0
  1. /*******************************************************************************
  2. * Copyright (c) 2010-2011 Sonatype, Inc.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *******************************************************************************/
  8. package org.sonatype.guice.plexus.locators;
  9. import java.util.Iterator;
  10. import java.util.Map.Entry;
  11. import junit.framework.TestCase;
  12. import org.codehaus.plexus.classworlds.ClassWorld;
  13. import org.codehaus.plexus.classworlds.ClassWorldException;
  14. import org.codehaus.plexus.classworlds.realm.DuplicateRealmException;
  15. import org.codehaus.plexus.classworlds.realm.NoSuchRealmException;
  16. import org.sonatype.guice.bean.locators.DefaultBeanLocator;
  17. import org.sonatype.guice.bean.locators.HiddenBinding;
  18. import org.sonatype.guice.bean.locators.MutableBeanLocator;
  19. import com.google.inject.AbstractModule;
  20. import com.google.inject.Binder;
  21. import com.google.inject.Guice;
  22. import com.google.inject.ImplementedBy;
  23. import com.google.inject.Injector;
  24. import com.google.inject.Module;
  25. import com.google.inject.Provider;
  26. import com.google.inject.ProvisionException;
  27. import com.google.inject.TypeLiteral;
  28. import com.google.inject.name.Names;
  29. public class PlexusBeanLocatorTest
  30. extends TestCase
  31. {
  32. static final HiddenBinding TEST_HIDDEN_SOURCE = new HiddenBinding()
  33. {
  34. };
  35. @ImplementedBy( BeanImpl.class )
  36. interface Bean
  37. {
  38. }
  39. static class BeanImpl
  40. implements Bean
  41. {
  42. }
  43. Injector parent;
  44. Injector child1;
  45. Injector child2;
  46. Injector child3;
  47. @Override
  48. public void setUp()
  49. throws Exception
  50. {
  51. parent = Guice.createInjector( new AbstractModule()
  52. {
  53. @Override
  54. protected void configure()
  55. {
  56. bind( Bean.class ).annotatedWith( Names.named( "A" ) ).to( BeanImpl.class );
  57. bind( Bean.class ).annotatedWith( Names.named( "-" ) ).to( BeanImpl.class );
  58. bind( Bean.class ).annotatedWith( Names.named( "Z" ) ).to( BeanImpl.class );
  59. }
  60. } );
  61. child1 = parent.createChildInjector( new AbstractModule()
  62. {
  63. @Override
  64. protected void configure()
  65. {
  66. bind( Bean.class ).annotatedWith( Names.named( "M1" ) ).to( BeanImpl.class );
  67. final Binder hiddenBinder = binder().withSource( TEST_HIDDEN_SOURCE );
  68. hiddenBinder.bind( Bean.class ).annotatedWith( Names.named( "!" ) ).to( BeanImpl.class );
  69. bind( Bean.class ).annotatedWith( Names.named( "N1" ) ).to( BeanImpl.class );
  70. }
  71. } );
  72. child2 = parent.createChildInjector( new AbstractModule()
  73. {
  74. @Override
  75. protected void configure()
  76. {
  77. }
  78. } );
  79. child3 = parent.createChildInjector( new AbstractModule()
  80. {
  81. @Override
  82. protected void configure()
  83. {
  84. bind( Bean.class ).annotatedWith( Names.named( "M3" ) ).to( BeanImpl.class );
  85. bind( Bean.class ).annotatedWith( Names.named( "N3" ) ).to( BeanImpl.class );
  86. }
  87. } );
  88. }
  89. public void testInjectorOrdering()
  90. {
  91. final MutableBeanLocator locator = new DefaultBeanLocator();
  92. final Iterable<? extends Entry<String, Bean>> roles =
  93. new DefaultPlexusBeanLocator( locator ).locate( TypeLiteral.get( Bean.class ) );
  94. locator.add( parent, 0 );
  95. locator.add( child1, 1 );
  96. locator.add( child2, 2 );
  97. locator.add( child3, 3 );
  98. locator.remove( child1 );
  99. locator.add( child1, 4 );
  100. Iterator<? extends Entry<String, Bean>> i;
  101. i = roles.iterator();
  102. assertEquals( "M1", i.next().getKey() );
  103. assertEquals( "N1", i.next().getKey() );
  104. assertEquals( "M3", i.next().getKey() );
  105. assertEquals( "N3", i.next().getKey() );
  106. assertEquals( "A", i.next().getKey() );
  107. assertEquals( "-", i.next().getKey() );
  108. assertEquals( "Z", i.next().getKey() );
  109. assertFalse( i.hasNext() );
  110. locator.remove( child2 );
  111. locator.remove( child2 );
  112. i = roles.iterator();
  113. assertEquals( "M1", i.next().getKey() );
  114. assertEquals( "N1", i.next().getKey() );
  115. assertEquals( "M3", i.next().getKey() );
  116. assertEquals( "N3", i.next().getKey() );
  117. assertEquals( "A", i.next().getKey() );
  118. assertEquals( "-", i.next().getKey() );
  119. assertEquals( "Z", i.next().getKey() );
  120. assertFalse( i.hasNext() );
  121. locator.remove( child3 );
  122. locator.add( child3, 5 );
  123. locator.add( child3, 5 );
  124. i = roles.iterator();
  125. assertEquals( "M3", i.next().getKey() );
  126. assertEquals( "N3", i.next().getKey() );
  127. assertEquals( "M1", i.next().getKey() );
  128. assertEquals( "N1", i.next().getKey() );
  129. assertEquals( "A", i.next().getKey() );
  130. assertEquals( "-", i.next().getKey() );
  131. assertEquals( "Z", i.next().getKey() );
  132. assertFalse( i.hasNext() );
  133. locator.remove( parent );
  134. i = roles.iterator();
  135. assertEquals( "M3", i.next().getKey() );
  136. assertEquals( "N3", i.next().getKey() );
  137. assertEquals( "M1", i.next().getKey() );
  138. assertEquals( "N1", i.next().getKey() );
  139. assertFalse( i.hasNext() );
  140. locator.remove( child1 );
  141. locator.remove( child3 );
  142. i = roles.iterator();
  143. assertFalse( i.hasNext() );
  144. }
  145. public void testExistingInjectors()
  146. {
  147. final MutableBeanLocator locator = new DefaultBeanLocator();
  148. locator.add( parent, 0 );
  149. locator.add( child1, 1 );
  150. final Iterable<? extends Entry<String, Bean>> roles =
  151. new DefaultPlexusBeanLocator( locator ).locate( TypeLiteral.get( Bean.class ) );
  152. locator.add( child2, 2 );
  153. locator.add( child3, 3 );
  154. Iterator<? extends Entry<String, Bean>> i;
  155. i = roles.iterator();
  156. assertEquals( "M3", i.next().getKey() );
  157. assertEquals( "N3", i.next().getKey() );
  158. assertEquals( "M1", i.next().getKey() );
  159. assertEquals( "N1", i.next().getKey() );
  160. assertEquals( "A", i.next().getKey() );
  161. assertEquals( "-", i.next().getKey() );
  162. assertEquals( "Z", i.next().getKey() );
  163. assertFalse( i.hasNext() );
  164. }
  165. public void testRoleHintLookup()
  166. {
  167. final MutableBeanLocator locator = new DefaultBeanLocator();
  168. final Iterable<? extends Entry<String, Bean>> roles =
  169. new DefaultPlexusBeanLocator( locator ).locate( TypeLiteral.get( Bean.class ), "A", "M1", "N3", "-", "!",
  170. "-", "M3", "N1", "Z" );
  171. Iterator<? extends Entry<String, Bean>> i;
  172. i = roles.iterator();
  173. assertEquals( "A", i.next().getKey() );
  174. assertEquals( "M1", i.next().getKey() );
  175. assertEquals( "N3", i.next().getKey() );
  176. assertEquals( "-", i.next().getKey() );
  177. final Entry<String, Bean> pling = i.next();
  178. assertEquals( "!", pling.getKey() );
  179. assertEquals( "-", i.next().getKey() );
  180. assertEquals( "M3", i.next().getKey() );
  181. assertEquals( "N1", i.next().getKey() );
  182. assertEquals( "Z", i.next().getKey() );
  183. assertFalse( i.hasNext() );
  184. assertEquals( "!=<missing>", pling.toString() );
  185. try
  186. {
  187. pling.getValue();
  188. fail( "Expected ProvisionException" );
  189. }
  190. catch ( final ProvisionException e )
  191. {
  192. }
  193. try
  194. {
  195. pling.setValue( null );
  196. fail( "Expected UnsupportedOperationException" );
  197. }
  198. catch ( final UnsupportedOperationException e )
  199. {
  200. }
  201. locator.add( parent, 0 );
  202. locator.add( child1, 1 );
  203. locator.add( child2, 2 );
  204. locator.add( child3, 3 );
  205. locator.add( parent.createChildInjector( new AbstractModule()
  206. {
  207. @Override
  208. protected void configure()
  209. {
  210. bind( Bean.class ).annotatedWith( Names.named( "M1" ) ).toProvider( new Provider<Bean>()
  211. {
  212. public Bean get()
  213. {
  214. return null;
  215. }
  216. } );
  217. bind( Bean.class ).annotatedWith( Names.named( "M3" ) ).toProvider( new Provider<Bean>()
  218. {
  219. public Bean get()
  220. {
  221. return null;
  222. }
  223. } );
  224. }
  225. } ), 4 );
  226. Entry<String, Bean> m1, dash1, dash2, m3;
  227. i = roles.iterator();
  228. assertEquals( "A", i.next().getKey() );
  229. m1 = i.next();
  230. assertEquals( "M1", m1.getKey() );
  231. assertEquals( "N3", i.next().getKey() );
  232. dash1 = i.next();
  233. assertEquals( "-", dash1.getKey() );
  234. assertEquals( "!", i.next().getKey() );
  235. dash2 = i.next();
  236. assertEquals( "-", dash2.getKey() );
  237. m3 = i.next();
  238. assertEquals( "M3", m3.getKey() );
  239. assertEquals( "N1", i.next().getKey() );
  240. assertEquals( "Z", i.next().getKey() );
  241. assertFalse( i.hasNext() );
  242. assertNull( m1.getValue() );
  243. assertSame( dash1.getValue(), dash2.getValue() );
  244. assertNull( m3.getValue() );
  245. locator.remove( child1 );
  246. locator.add( child1, 5 );
  247. i = roles.iterator();
  248. assertEquals( "A", i.next().getKey() );
  249. m1 = i.next();
  250. assertEquals( "M1", m1.getKey() );
  251. assertEquals( "N3", i.next().getKey() );
  252. dash1 = i.next();
  253. assertEquals( "-", dash1.getKey() );
  254. assertEquals( "!", i.next().getKey() );
  255. dash2 = i.next();
  256. assertEquals( "-", dash2.getKey() );
  257. m3 = i.next();
  258. assertEquals( "M3", m3.getKey() );
  259. assertEquals( "N1", i.next().getKey() );
  260. assertEquals( "Z", i.next().getKey() );
  261. assertFalse( i.hasNext() );
  262. assertEquals( "M1=" + m1.getValue(), m1.toString() );
  263. assertEquals( BeanImpl.class, m1.getValue().getClass() );
  264. assertSame( m1.getValue(), m1.getValue() );
  265. assertSame( dash1.getValue(), dash2.getValue() );
  266. assertNull( m3.getValue() );
  267. }
  268. public void testInjectorVisibility()
  269. throws NoSuchRealmException
  270. {
  271. final MutableBeanLocator locator = new DefaultBeanLocator();
  272. final ClassWorld world = new ClassWorld();
  273. locator.add( Guice.createInjector( new Module()
  274. {
  275. public void configure( final Binder binder )
  276. {
  277. try
  278. {
  279. binder.withSource( world.newRealm( "A" ) ).bind( Bean.class ).annotatedWith( Names.named( "A" ) ).to( BeanImpl.class );
  280. }
  281. catch ( final DuplicateRealmException e )
  282. {
  283. throw new RuntimeException( e );
  284. }
  285. }
  286. } ), 1 );
  287. locator.add( Guice.createInjector( new Module()
  288. {
  289. public void configure( final Binder binder )
  290. {
  291. try
  292. {
  293. binder.withSource( world.newRealm( "B" ) ).bind( Bean.class ).annotatedWith( Names.named( "B" ) ).to( BeanImpl.class );
  294. }
  295. catch ( final DuplicateRealmException e )
  296. {
  297. throw new RuntimeException( e );
  298. }
  299. }
  300. } ), 2 );
  301. locator.add( Guice.createInjector( new Module()
  302. {
  303. public void configure( final Binder binder )
  304. {
  305. try
  306. {
  307. binder.withSource( world.newRealm( "C" ) ).bind( Bean.class ).annotatedWith( Names.named( "C" ) ).to( BeanImpl.class );
  308. }
  309. catch ( final DuplicateRealmException e )
  310. {
  311. throw new RuntimeException( e );
  312. }
  313. }
  314. } ), 3 );
  315. locator.add( Guice.createInjector( new Module()
  316. {
  317. public void configure( final Binder binder )
  318. {
  319. try
  320. {
  321. binder.withSource( world.getRealm( "B" ).createChildRealm( "B1" ) ).bind( Bean.class ).annotatedWith( Names.named( "B1" ) ).to( BeanImpl.class );
  322. }
  323. catch ( final ClassWorldException e )
  324. {
  325. throw new RuntimeException( e );
  326. }
  327. }
  328. } ), 4 );
  329. locator.add( Guice.createInjector( new Module()
  330. {
  331. public void configure( final Binder binder )
  332. {
  333. try
  334. {
  335. binder.withSource( world.getRealm( "B" ).createChildRealm( "B2" ) ).bind( Bean.class ).annotatedWith( Names.named( "B2" ) ).to( BeanImpl.class );
  336. }
  337. catch ( final ClassWorldException e )
  338. {
  339. throw new RuntimeException( e );
  340. }
  341. }
  342. } ), 5 );
  343. locator.add( Guice.createInjector( new Module()
  344. {
  345. public void configure( final Binder binder )
  346. {
  347. try
  348. {
  349. binder.withSource( world.getRealm( "B" ).createChildRealm( "B3" ) ).bind( Bean.class ).annotatedWith( Names.named( "B3" ) ).to( BeanImpl.class );
  350. }
  351. catch ( final ClassWorldException e )
  352. {
  353. throw new RuntimeException( e );
  354. }
  355. }
  356. } ), 6 );
  357. locator.add( Guice.createInjector( new Module()
  358. {
  359. public void configure( final Binder binder )
  360. {
  361. try
  362. {
  363. binder.withSource( world.getRealm( "B2" ).createChildRealm( "B2B" ) ).bind( Bean.class ).annotatedWith( Names.named( "B2B" ) ).to( BeanImpl.class );
  364. }
  365. catch ( final ClassWorldException e )
  366. {
  367. throw new RuntimeException( e );
  368. }
  369. }
  370. } ), 7 );
  371. locator.add( Guice.createInjector( new Module()
  372. {
  373. public void configure( final Binder binder )
  374. {
  375. try
  376. {
  377. binder.withSource( world.newRealm( "?" ) ).bind( Bean.class ).annotatedWith( Names.named( "?" ) ).to( BeanImpl.class );
  378. }
  379. catch ( final DuplicateRealmException e )
  380. {
  381. throw new RuntimeException( e );
  382. }
  383. }
  384. } ), 8 );
  385. world.getRealm( "B" ).importFrom( "A", "A" );
  386. world.getRealm( "B" ).importFrom( "C", "C" );
  387. world.getRealm( "B2" ).importFrom( "B1", "B1" );
  388. world.getRealm( "B2" ).importFrom( "B3", "B3" );
  389. locator.add( Guice.createInjector( new AbstractModule()
  390. {
  391. @Override
  392. protected void configure()
  393. {
  394. bind( Bean.class ).annotatedWith( Names.named( "!" ) ).to( BeanImpl.class );
  395. }
  396. } ), 9 );
  397. final Iterable<? extends Entry<String, Bean>> beans =
  398. new DefaultPlexusBeanLocator( locator ).locate( TypeLiteral.get( Bean.class ) );
  399. Iterator<? extends Entry<String, Bean>> i;
  400. Thread.currentThread().setContextClassLoader( world.getClassRealm( "A" ) );
  401. i = beans.iterator();
  402. assertTrue( i.hasNext() );
  403. assertEquals( "!", i.next().getKey() );
  404. assertEquals( "A", i.next().getKey() );
  405. assertFalse( i.hasNext() );
  406. Thread.currentThread().setContextClassLoader( world.getClassRealm( "B" ) );
  407. i = beans.iterator();
  408. assertTrue( i.hasNext() );
  409. assertEquals( "!", i.next().getKey() );
  410. assertEquals( "C", i.next().getKey() );
  411. assertEquals( "B", i.next().getKey() );
  412. assertEquals( "A", i.next().getKey() );
  413. assertFalse( i.hasNext() );
  414. Thread.currentThread().setContextClassLoader( world.getClassRealm( "B2" ) );
  415. i = beans.iterator();
  416. assertTrue( i.hasNext() );
  417. assertEquals( "!", i.next().getKey() );
  418. assertEquals( "B3", i.next().getKey() );
  419. assertEquals( "B2", i.next().getKey() );
  420. assertEquals( "B1", i.next().getKey() );
  421. assertEquals( "C", i.next().getKey() );
  422. assertEquals( "B", i.next().getKey() );
  423. assertEquals( "A", i.next().getKey() );
  424. assertFalse( i.hasNext() );
  425. Thread.currentThread().setContextClassLoader( world.getClassRealm( "B2B" ) );
  426. i = beans.iterator();
  427. assertTrue( i.hasNext() );
  428. assertEquals( "!", i.next().getKey() );
  429. assertEquals( "B2B", i.next().getKey() );
  430. assertEquals( "B3", i.next().getKey() );
  431. assertEquals( "B2", i.next().getKey() );
  432. assertEquals( "B1", i.next().getKey() );
  433. assertEquals( "C", i.next().getKey() );
  434. assertEquals( "B", i.next().getKey() );
  435. assertEquals( "A", i.next().getKey() );
  436. assertFalse( i.hasNext() );
  437. Thread.currentThread().setContextClassLoader( world.getClassRealm( "B3" ) );
  438. i = beans.iterator();
  439. assertTrue( i.hasNext() );
  440. assertEquals( "!", i.next().getKey() );
  441. assertEquals( "B3", i.next().getKey() );
  442. assertEquals( "C", i.next().getKey() );
  443. assertEquals( "B", i.next().getKey() );
  444. assertEquals( "A", i.next().getKey() );
  445. assertFalse( i.hasNext() );
  446. Thread.currentThread().setContextClassLoader( world.getClassRealm( "C" ) );
  447. i = beans.iterator();
  448. assertTrue( i.hasNext() );
  449. assertEquals( "!", i.next().getKey() );
  450. assertEquals( "C", i.next().getKey() );
  451. assertFalse( i.hasNext() );
  452. Thread.currentThread().setContextClassLoader( null );
  453. i = beans.iterator();
  454. assertTrue( i.hasNext() );
  455. assertEquals( "!", i.next().getKey() );
  456. assertEquals( "?", i.next().getKey() );
  457. assertEquals( "B2B", i.next().getKey() );
  458. assertEquals( "B3", i.next().getKey() );
  459. assertEquals( "B2", i.next().getKey() );
  460. assertEquals( "B1", i.next().getKey() );
  461. assertEquals( "C", i.next().getKey() );
  462. assertEquals( "B", i.next().getKey() );
  463. assertEquals( "A", i.next().getKey() );
  464. assertFalse( i.hasNext() );
  465. }
  466. }