WebXml { public void setContextParamValue( String name, String value ) { String xPath = MessageFormat.format( CONTEXT_PARAM_NAME_TEMPLATE_XPATH, name ); Element contextParamNameElement = (Element) document.selectSingleNode( xPath ); if ( value == null ) { if ( contextParamNameElement != null ) { contextParamNameElement.getParent().detach(); } } else { if ( contextParamNameElement == null ) { contextParamNameElement = document.getRootElement().addElement( CONTEXT_PARAM_ELEMENT ); Element paramNameElement = contextParamNameElement.addElement( PARAM_NAME_ELEMENT ); paramNameElement.setText( name ); } Element paramValueElement = DocumentHelper.makeElement( contextParamNameElement.getParent(), PARAM_VALUE_ELEMENT ); paramValueElement.setText( value ); } } WebXml( File pentahoXmlFile );  WebXml( String xml );  WebXml( Document doc );  WebXml(); String getContextConfigFileName(); @Deprecated String getBaseUrl(); String getFullyQualifiedServerUrl(); String getSolutionPath(); String getLocaleLanguage(); String getLocaleCountry(); String getEncoding(); String getHomePage(); void setContextConfigFileName( String fileName ); void setBaseUrl( String baseUrl ); void setFullyQualifiedServerUrl( String fullyQualifiedServerUrl ); void setSolutionPath( String solutionPath ); void setLocaleLanguage( String language ); void setLocaleCountry( String country ); void setEncoding( String encoding ); void setHomePage( String homePage ); Document getDocument(); String getContextParamValue( String name ); void setContextParamValue( String name, String value ); boolean setServletMapping( String name, String value ); String getServletMapping( String name );  }
@Test public void testSetContextParamValue_nullValue() throws Exception { webXml = new WebXml( document ); Element node = mock( Element.class ); Element parent = mock( Element.class ); ; when( node.getParent() ).thenReturn( parent ); when( document.selectSingleNode( anyString() ) ).thenReturn( node ); webXml.setContextParamValue( "key", null ); verify( parent ).detach(); }
@Test public void testSetContextParamValue() throws Exception { webXml = new WebXml( document ); Element root = mock( Element.class ); when( document.getRootElement() ).thenReturn( root ); Element node = mock( Element.class ); Element paramNode = mock( Element.class ); when( root.addElement( anyString() ) ).thenReturn( node ); when( node.getParent() ).thenReturn( root ); when( document.selectSingleNode( anyString() ) ).thenReturn( null ); when( node.addElement( anyString() ) ).thenReturn( paramNode ); webXml.setContextParamValue( "key", value ); verify( root ).addElement( WebXml.CONTEXT_PARAM_ELEMENT ); verify( node ).addElement( WebXml.PARAM_NAME_ELEMENT ); verify( paramNode ).setText( "key" ); }