PicoContainer
  1. PicoContainer
  2. PICO-357

Checked exceptions thrown from provider methods silently ignored

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.8
    • Fix Version/s: None
    • Component/s: PicoContainer (Java)
    • Labels:
      None
    • Testcase included:
      yes
    • Number of attachments :
      0

      Description

      When using a provider method to create a component, the behavior for checked exceptions is not consistent with direct container instantiation or the behavior exposed when using runtime exceptions.

      The tests below expose the issue. All three methods of creating an object should expose the same behavior. However, when throwing a checked exception from the constructor, the exception is swallowed and null is returned. Using a direct ConstructorInjector does transform a checked to a PicoException-derived exception; a runtime exception is passed on without any mangling.

      ProviderTest.java
      	public static class SimpleFoo {
      		public SimpleFoo() throws Exception {
      			throw new Exception("deliberate");
      		}
      	}
      
      	public static class SimpleProvider implements Provider {
      		public SimpleFoo provide() throws Exception {
      			return new SimpleFoo();
      		}
      	}
      
      	@Test(expected = PicoException.class)
      	// this method should throw an exception, but silently returns null (the issue)
      	public void testShouldThrowWhenProvideMethodThrowsForCheckedException() {
      		DefaultPicoContainer cont = new DefaultPicoContainer();
      		cont.addAdapter(new ProviderAdapter(new SimpleProvider()));
      		assertNotNull(cont.getComponent(SimpleFoo.class));
      	}
      
      	@Test(expected = PicoException.class)
      	public void testShouldThrowWhenConstructorThrowsForCheckedException() {
      		DefaultPicoContainer cont = new DefaultPicoContainer();
      		cont.addComponent(SimpleFoo.class);
      		assertNotNull(cont.getComponent(SimpleFoo.class));
      	}
      
      	public static class SimpleFooRuntime {
      		public SimpleFooRuntime() {
      			throw new RuntimeException("deliberate");
      		}
      	}
      
      	public static class SimpleProviderRuntime implements Provider {
      		public SimpleFooRuntime provide() {
      			return new SimpleFooRuntime();
      		}
      	}
      
      	@Test(expected = RuntimeException.class)
      	public void testShouldThrowWhenProvideMethodThrowsForRuntimeException() {
      		DefaultPicoContainer cont = new DefaultPicoContainer();
      		cont.addAdapter(new ProviderAdapter(new SimpleProviderRuntime()));
      		assertNotNull(cont.getComponent(SimpleFooRuntime.class));
      	}
      

        People

        • Assignee:
          Paul Hammant
          Reporter:
          Mark J. Sinke
        • Votes:
          0 Vote for this issue
          Watchers:
          0 Start watching this issue

          Dates

          • Created:
            Updated:
            Resolved: