JaxbFactory { static Jaxb create(CRestConfig crestConfig, Class<?> source) throws JAXBException { String prefix = source.getName(); Jaxb jaxb = crestConfig.get(prefix + JAXB); if(jaxb != null) { return jaxb; } String modelPackage = crestConfig.get(prefix + MODEL_PACKAGE); if(modelPackage != null) { return create(crestConfig, source, modelPackage); } Class<?> modelFactory = crestConfig.get(prefix + MODEL_FACTORY_CLASS); if(modelFactory != null) { return create(crestConfig, source, modelFactory); } return new TypeCachingJaxb(crestConfig, source); } private  JaxbFactory();   }
@Test public void shouldUseGivenCRestConfigJaxb() throws Exception { Jaxb predefined = mock(Jaxb.class); when(mockCRestConfig.get(getClass().getName() + JaxbFactory.JAXB)).thenReturn(predefined); Jaxb actual = JaxbFactory.create(mockCRestConfig, getClass()); assertSame(predefined,actual); }
@Test public void shouldCreateATypeCachingJaxb() throws Exception { TypeCachingJaxb mockTypeCachingJaxb = mock(TypeCachingJaxb.class); whenNew(TypeCachingJaxb.class).withArguments(any(CRestConfig.class), any(Class.class)).thenReturn(mockTypeCachingJaxb); Jaxb actual = JaxbFactory.create(mockCRestConfig, getClass()); assertTrue(actual instanceof TypeCachingJaxb); verifyNew(TypeCachingJaxb.class).withArguments(mockCRestConfig, getClass()); }
@Test public void shouldDelegateToCreateWithGivenPackageContext() throws Exception { String packageContext = "packageContext"; when(mockCRestConfig.get(getClass().getName() + JaxbFactory.MODEL_PACKAGE)).thenReturn(packageContext); mockStatic(JAXBContext.class); when(JAXBContext.newInstance(packageContext)).thenReturn(mockJaxbContext); Jaxb actual = JaxbFactory.create(mockCRestConfig, getClass()); assertTrue(actual instanceof SimpleJaxb); }
@Test public void shouldDelegateToCreateWithGivenModelFactory() throws Exception { Class<?> modelFactory = JaxbFactoryTest.class; when(mockCRestConfig.get(getClass().getName() + JaxbFactory.MODEL_FACTORY_CLASS)).thenReturn(modelFactory); mockStatic(JAXBContext.class); when(JAXBContext.newInstance(modelFactory)).thenReturn(mockJaxbContext); Jaxb actual = JaxbFactory.create(mockCRestConfig, getClass()); assertTrue(actual instanceof SimpleJaxb); }
@Test public void shouldCreateASimpleJaxbWithJaxContextCreateWithGivenClassToBeBound() throws Exception { Class<?>[] classesToBeBound = new Class<?>[0]; SimpleJaxb mockSimpleJaxb = mock(SimpleJaxb.class); mockStatic(JAXBContext.class); when(JAXBContext.newInstance(classesToBeBound)).thenReturn(mockJaxbContext); whenNew(SimpleJaxb.class).withArguments(any(JAXBContext.class)).thenReturn(mockSimpleJaxb); Jaxb actual = JaxbFactory.create(mockCRestConfig, getClass(), classesToBeBound); assertTrue(actual instanceof SimpleJaxb); verifyNew(SimpleJaxb.class).withArguments(mockJaxbContext); }
@Test public void shouldCreateASimpleJaxbWithJaxContextCreateWithGivenPackageContext() throws Exception { String packageContext = "packageContext"; SimpleJaxb mockSimpleJaxb = mock(SimpleJaxb.class); mockStatic(JAXBContext.class); when(JAXBContext.newInstance(packageContext)).thenReturn(mockJaxbContext); whenNew(SimpleJaxb.class).withArguments(any(JAXBContext.class)).thenReturn(mockSimpleJaxb); Jaxb actual = JaxbFactory.create(mockCRestConfig, getClass(), packageContext); assertTrue(actual instanceof SimpleJaxb); verifyNew(SimpleJaxb.class).withArguments(mockJaxbContext); }
@Test public void shouldCreateAPooledJaxbWithJaxContextCreateWithGivenClassToBeBound() throws Exception { Class<?>[] classesToBeBound = new Class<?>[0]; PooledJaxb mockPooledJaxb = mock(PooledJaxb.class); when(mockCRestConfig.getConcurrencyLevel()).thenReturn(2); when(mockCRestConfig.get(getClass().getName() + JaxbFactory.POOL_RETRIEVAL_MAX_WAIT, 30000l)).thenReturn(123l); mockStatic(JAXBContext.class); when(JAXBContext.newInstance(classesToBeBound)).thenReturn(mockJaxbContext); whenNew(PooledJaxb.class).withArguments(any(JAXBContext.class), anyInt(), anyLong()).thenReturn(mockPooledJaxb); Jaxb actual = JaxbFactory.create(mockCRestConfig, getClass(), classesToBeBound); assertTrue(actual instanceof PooledJaxb); verifyNew(PooledJaxb.class).withArguments(mockJaxbContext, 2, 123l); }
@Test public void shouldCreateAPooledJaxbWithJaxContextCreateWithGivenPackageContext() throws Exception { String packageContext = "packageContext"; PooledJaxb mockPooledJaxb = mock(PooledJaxb.class); when(mockCRestConfig.getConcurrencyLevel()).thenReturn(2); when(mockCRestConfig.get(getClass().getName() + JaxbFactory.POOL_RETRIEVAL_MAX_WAIT, 30000l)).thenReturn(123l); mockStatic(JAXBContext.class); when(JAXBContext.newInstance(packageContext)).thenReturn(mockJaxbContext); whenNew(PooledJaxb.class).withArguments(any(JAXBContext.class), anyInt(), anyLong()).thenReturn(mockPooledJaxb); Jaxb actual = JaxbFactory.create(mockCRestConfig, getClass(), packageContext); assertTrue(actual instanceof PooledJaxb); verifyNew(PooledJaxb.class).withArguments(mockJaxbContext, 2, 123l); }