PentahoURLRewriter implements URLRewriter { public String rewrite( final ContentEntity contentEntry, final ContentEntity dataEntity ) throws URLRewriteException { try { final ArrayList<String> entityNames = new ArrayList<String>(); entityNames.add( useContentIdAsName ? dataEntity.getContentId().toString() : dataEntity.getName() ); ContentLocation location = dataEntity.getParent(); while ( location != null ) { entityNames.add( location.getName() ); location = location.getParent(); } final ArrayList<String> contentNames = new ArrayList<String>(); location = dataEntity.getRepository().getRoot(); while ( location != null ) { contentNames.add( location.getName() ); location = location.getParent(); } while ( ( contentNames.isEmpty() == false ) && ( entityNames.isEmpty() == false ) ) { final String lastEntity = entityNames.get( entityNames.size() - 1 ); final String lastContent = contentNames.get( contentNames.size() - 1 ); if ( lastContent.equals( lastEntity ) == false ) { break; } entityNames.remove( entityNames.size() - 1 ); contentNames.remove( contentNames.size() - 1 ); } final StringBuffer b = new StringBuffer(); for ( int i = entityNames.size() - 1; i >= 0; i-- ) { final String name = entityNames.get( i ); b.append( name ); if ( i != 0 ) { b.append( "/" ); } } if ( pattern == null ) { return b.toString(); } return MessageFormat.format( pattern, b.toString() ); } catch ( ContentIOException cioe ) { throw new URLRewriteException(); } } PentahoURLRewriter( final String pattern, final boolean useContentIdAsName ); String rewrite( final ContentEntity contentEntry, final ContentEntity dataEntity );  }
@Test public void testRewrite() throws Exception { ContentEntity contentEntity = mock( ContentEntity.class ); ContentEntity dataEntity = mock( ContentEntity.class ); Repository repository = mock( Repository.class ); doReturn( repository ).when( dataEntity ).getRepository(); String result = rewriter.rewrite( contentEntity, dataEntity ); assertEquals( "null", result ); ContentLocation location = mock( ContentLocation.class ); doReturn( "parent" ).when( location ).getName(); doReturn( location ).when( dataEntity ).getParent(); doReturn( repository ).when( dataEntity ).getRepository(); result = rewriter.rewrite( contentEntity, dataEntity ); assertEquals( "parent/null", result ); ContentLocation locationContent = mock( ContentLocation.class ); doReturn( "parent" ).when( locationContent ).getName(); doReturn( locationContent ).when( repository ).getRoot(); doReturn( repository ).when( dataEntity ).getRepository(); result = rewriter.rewrite( contentEntity, dataEntity ); assertEquals( "null", result ); ContentLocation locationParent = mock( ContentLocation.class ); doReturn( "parent" ).when( locationParent ).getName(); locationContent = mock( ContentLocation.class ); doReturn( "content" ).when( locationContent ).getName(); doReturn( locationContent ).when( repository ).getRoot(); result = rewriter.rewrite( contentEntity, dataEntity ); assertEquals( "parent/null", result ); rewriter = new PentahoURLRewriter( "{0}", false ); result = rewriter.rewrite( contentEntity, dataEntity ); assertEquals( "parent/null", result ); }