/** * Return the name provider registered for a given container resource. * * @param containerName * the name of the container. * @return the provider for the specified container resource. */ public final NameProvider nameProvider(Name<?> containerName) { NameProvider result = this.nameProviders.get(containerName); if(result==null) { throw new ApplicationRuntimeException("Unknown container '"+containerName+"'"); } return result; }
@Test public void testAttachmentNameLifecycle$unknownAttachment() { Name<String> owner = name("test"); NameProvider sut = NameProvider.create(owner); assertThat(sut.pendingAttachmentNames("attachment"),hasSize(0)); try { sut.nextAttachmentName("attachment"); fail("Should not provide attachment names if not available"); } catch (ApplicationRuntimeException e) { assertThat(e.getMessage(),equalTo("No more attachment <<attachment>> names available for resource 'test'")); } }
@Test public void testDeletion$fail() throws Exception { PersonHandler sut = new PersonHandler(); final Name<String> resourceName = name("resource"); final DataSet data=empty(resourceName); sut.add(resourceName, data); new Expectations() {{ snapshot.name();result=resourceName; session.delete(snapshot); session.saveChanges();result=new WriteSessionException("FAILURE"); }}; try { sut.delete(snapshot, session); fail("Should not delete if the session fails"); } catch (ApplicationRuntimeException e) { assertThat(e.getCause(),instanceOf(WriteSessionException.class)); assertThat(e.getCause().getMessage(),equalTo("FAILURE")); assertThat(sut.get(snapshot),sameInstance(data)); } }
@SuppressWarnings("unchecked") @Test public void testResourceNameLifecycle() { Name<String> owner = name("test"); NameProvider sut = NameProvider.create(owner); sut.addResourceName(name("m1")); sut.addResourceName(name("m2")); assertThat(sut.pendingResourceNames(),contains(name("m1"),name("m2"))); assertThat(sut.nextResourceName(),equalTo(name("m1"))); assertThat(sut.pendingResourceNames(),contains(name("m2"))); assertThat(sut.nextResourceName(),equalTo(name("m2"))); assertThat(sut.pendingResourceNames(),hasSize(0)); try { sut.nextResourceName(); fail("Should not provide resource names if not available"); } catch (ApplicationRuntimeException e) { assertThat(e.getMessage(),equalTo("No more resource names available for resource 'test'")); } }
@Test public void testUpdate$sessionFailure() throws Exception { PersonHandler sut = new PersonHandler(); final Name<String> resourceName = name("resource"); final DataSet data=empty(resourceName); sut.add(resourceName, data); new Expectations() {{ snapshot.name();result=resourceName; session.modify(snapshot); session.saveChanges();result=new WriteSessionException("FAILURE"); }}; try { sut.update(snapshot,empty(resourceName),session); fail("Should not modify if the session fails"); } catch (ApplicationRuntimeException e) { assertThat(e.getCause(),instanceOf(WriteSessionException.class)); assertThat(e.getCause().getMessage(),equalTo("FAILURE")); assertThat(sut.get(snapshot),sameInstance(data)); } }
protected final ApplicationRuntimeException unexpectedFailure(String message, Object... args) { String result = String.format(message,args); this.logger.error(result); return new ApplicationRuntimeException(result); }
@SuppressWarnings("unchecked") @Test public void testMemberNameLifecycle() { Name<String> owner = name("test"); NameProvider sut = NameProvider.create(owner); sut.addMemberName(name("m1")); sut.addMemberName(name("m2")); assertThat(sut.pendingMemberNames(),contains(name("m1"),name("m2"))); assertThat(sut.nextMemberName(),equalTo(name("m1"))); assertThat(sut.pendingMemberNames(),contains(name("m2"))); assertThat(sut.nextMemberName(),equalTo(name("m2"))); assertThat(sut.pendingMemberNames(),hasSize(0)); try { sut.nextMemberName(); fail("Should not provide member names if not available"); } catch (ApplicationRuntimeException e) { assertThat(e.getMessage(),equalTo("No more member names available for resource 'test'")); } }
@Test public void testCreate$exceptionPath() throws Exception { PersonContainerHandler containerHandler = new PersonContainerHandler(); Deencapsulation.setField(containerHandler,"id",new AtomicInteger()); PersonHandler resourceHandler=new PersonHandler(); final Name<String> containerName = name("container"); containerHandler.setHandler(resourceHandler); final DataSet data = empty(containerName); final Name<Integer> memberName = NamingScheme.getDefault().name(1); new Expectations() {{ memberSnapshot.name();result=memberName; session.saveChanges();result=new WriteSessionException("FAILURE"); }}; try { containerHandler.create(snapshot, data, session); fail("Should not create if the session fails"); } catch (ApplicationRuntimeException e) { assertThat(e.getCause(),instanceOf(WriteSessionException.class)); assertThat(e.getCause().getMessage(),equalTo("FAILURE")); try { resourceHandler.get(memberSnapshot); fail("Resource should not be available in the resource handler"); } catch (UnknownResourceException e1) { } } }
protected final ApplicationRuntimeException unexpectedFailure(final String message, final Object... args) { final String result = String.format(message,args); this.logger.error(result); return new ApplicationRuntimeException(result); }
@Test public void tesNameProviderRoundtrip$exceptionPath(@Mocked final NameProvider provider) throws Exception { Container container = new Container(); Name<String> name = name("test"); container.addNameProvider(name, provider); Name<String> unknown = name("unknown"); try { container.nameProvider(unknown); fail("Should not return provider for unknown resource"); } catch(ApplicationRuntimeException e) { assertThat(e.getMessage(),equalTo("Unknown container 'unknown'")); } }
private Name<String> nextName() { if(this.pendingNames.isEmpty()) { throw new ApplicationRuntimeException(String.format("No more %s names available for resource '%s'",tag,NameProvider.this.owner)); } return this.pendingNames.pop(); }
@SuppressWarnings("unchecked") @Test public void testAttachmentNameLifecycle() { Name<String> owner = name("test"); NameProvider sut = NameProvider.create(owner); sut.addAttachmentName("attachment",name("m1")); sut.addAttachmentName("attachment",name("m2")); assertThat(sut.pendingAttachmentNames("attachment"),contains(name("m1"),name("m2"))); assertThat(sut.nextAttachmentName("attachment"),equalTo(name("m1"))); assertThat(sut.pendingAttachmentNames("attachment"),contains(name("m2"))); assertThat(sut.nextAttachmentName("attachment"),equalTo(name("m2"))); assertThat(sut.pendingAttachmentNames("attachment"),hasSize(0)); try { sut.nextAttachmentName("attachment"); fail("Should not provide attachment names if not available"); } catch (ApplicationRuntimeException e) { assertThat(e.getMessage(),equalTo("No more attachment <<attachment>> names available for resource 'test'")); } }
protected final ApplicationRuntimeException unexpectedFailure(final Throwable failure, final String message, final Object... args) { final String result = String.format(message,args); this.logger.error(result.concat(". Full stacktrace follows"),failure); final String errorMessage=result; return new ApplicationRuntimeException(errorMessage,failure); }
protected final ApplicationRuntimeException unexpectedFailure(Throwable failure, String message, Object... args) { String result = String.format(message,args); this.logger.error(result.concat(". Full stacktrace follows"),failure); String errorMessage=result; return new ApplicationRuntimeException(errorMessage,failure); }
@Override public DataSet get(ResourceSnapshot resource) throws ApplicationRuntimeException { throw new ApplicationRuntimeException("Failure"); }
@Override public void update(ResourceSnapshot resource, DataSet content, WriteSession session) throws ApplicationRuntimeException { throw new ApplicationRuntimeException("Failure"); }
@Override public ResourceSnapshot create(ContainerSnapshot container, DataSet representation, WriteSession session) throws ApplicationRuntimeException { throw new ApplicationRuntimeException("Failure"); }
@Override public void delete(ResourceSnapshot resource, WriteSession session) throws ApplicationRuntimeException { throw new ApplicationRuntimeException("Failure"); }
@Override public DataSet query(ResourceSnapshot resource, Query query, ReadSession session) throws ApplicationRuntimeException { throw new ApplicationRuntimeException("Failure"); }