@Override public void addToken( NamedToken token ) throws NonUniqueTokenException { tokenRegistry.put( token ); }
private void tryToAddDuplicate( TokenRegistry tokenCache ) { try { tokenCache.put( new NamedToken( INBOUND1_TYPE, 3 ) ); } catch ( NonUniqueTokenException ignored ) { } }
/** * Create and put new token in cache. * * @param name token name * @return newly created token id * @throws KernelException */ @Override protected synchronized int createToken( String name ) throws KernelException { Integer id = tokenRegistry.getId( name ); if ( id != null ) { return id; } id = tokenCreator.createToken( name ); try { tokenRegistry.put( new NamedToken( name, id ) ); } catch ( NonUniqueTokenException e ) { throw new IllegalStateException( "Newly created token should be unique.", e ); } return id; }
@Test public void addTokenWithDuplicatedNotAllowed() { TokenRegistry tokenCache = createTokenCache(); tokenCache.put( new NamedToken( INBOUND1_TYPE, 1 ) ); tokenCache.put( new NamedToken( INBOUND2_TYPE, 2 ) ); expectedException.expect( NonUniqueTokenException.class ); expectedException.expectMessage( "The testType \"inbound1\" is not unique" ); tokenCache.put( new NamedToken( INBOUND1_TYPE, 3 ) ); }
@Test public void keepOriginalTokenWhenAddDuplicate() { TokenRegistry tokenCache = createTokenCache(); tokenCache.put( new NamedToken( INBOUND1_TYPE, 1 ) ); tokenCache.put( new NamedToken( INBOUND2_TYPE, 2 ) ); tryToAddDuplicate( tokenCache ); assertEquals( 1, tokenCache.getId( INBOUND1_TYPE ).intValue() ); assertEquals( 2, tokenCache.getId( INBOUND2_TYPE ).intValue() ); assertNull( tokenCache.getToken( 3 ) ); }
@Override public void addToken( NamedToken token ) throws NonUniqueTokenException { tokenRegistry.put( token ); }
/** * Create and put new token in cache. * * @param name token name * @return newly created token id * @throws KernelException */ @Override protected synchronized int createToken( String name ) throws KernelException { Integer id = tokenRegistry.getId( name ); if ( id != null ) { return id; } id = tokenCreator.createToken( name ); try { tokenRegistry.put( new NamedToken( name, id ) ); } catch ( NonUniqueTokenException e ) { throw new IllegalStateException( "Newly created token should be unique.", e ); } return id; }