Skip to content
Browse files

NXCM-3951 product name cleanup

add some tests and fix product name use in logs and web.xml
  • Loading branch information...
1 parent 15816e7 commit 6fc03a4324ec45a3e39aedda3248ab0d01fc94b8 @peterlynch peterlynch committed
View
86 nexus/nexus-app/src/main/java/org/sonatype/nexus/DefaultNexus.java
@@ -20,6 +20,7 @@
import java.util.List;
import java.util.Map;
+import com.google.common.annotations.VisibleForTesting;
import org.apache.commons.httpclient.CustomMultiThreadedHttpConnectionManager;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
@@ -29,6 +30,7 @@
import org.codehaus.plexus.personality.plexus.lifecycle.phase.StartingException;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.StoppingException;
import org.codehaus.plexus.util.StringUtils;
+import org.slf4j.Logger;
import org.sonatype.configuration.ConfigurationException;
import org.sonatype.nexus.configuration.ConfigurationChangeEvent;
import org.sonatype.nexus.configuration.application.NexusConfiguration;
@@ -72,7 +74,7 @@
/**
* The default Nexus implementation.
- *
+ *
* @author Jason van Zyl
* @author cstamas
*/
@@ -81,6 +83,7 @@
extends AbstractLoggingComponent
implements Nexus, Initializable, Startable
{
+
@Requirement
private ApplicationEventMulticaster applicationEventMulticaster;
@@ -360,21 +363,7 @@ public NexusStreamResponse getConfigurationAsStreamByKey( String key )
public void initialize()
throws InitializationException
{
- StringBuffer sysInfoLog = new StringBuffer();
-
- sysInfoLog.append( "\n" );
- sysInfoLog.append( "-------------------------------------------------\n" );
- sysInfoLog.append( "\n" );
- sysInfoLog.append( "Initializing Nexus (" );
- if( !StringUtils.isEmpty( applicationStatusSource.getSystemStatus().getEditionLong() ) )
- {
- sysInfoLog.append( applicationStatusSource.getSystemStatus().getEditionLong() ).append( "), ");
- }
- sysInfoLog.append( "Version " ).append( applicationStatusSource.getSystemStatus().getVersion() ).append( "\n" );
- sysInfoLog.append( "\n" );
- sysInfoLog.append( "-------------------------------------------------" );
-
- getLogger().info( sysInfoLog.toString() );
+ logInitialize();
artifactPackagingMapper.setPropertiesFile( new File( nexusConfiguration.getConfigurationDirectory(),
MAPPING_PROPERTIES_FILE ) );
@@ -408,6 +397,29 @@ public void initialize()
applicationEventMulticaster.notifyEventListeners( new NexusInitializedEvent( this ) );
}
+ /**
+ * Logs a message indicating initialization of this component
+ */
+ @VisibleForTesting
+ protected void logInitialize(){
+ final StringBuilder sysInfoLog = new StringBuilder();
+ sysInfoLog.append( "\n-------------------------------------------------\n\n" );
+ sysInfoLog.append( "Initializing " ).append(getNexusNameForLogs());
+ sysInfoLog.append( "\n\n-------------------------------------------------" );
+ getLogger().info( sysInfoLog.toString() );
+ }
+
+ /**
+ * @return the full name of this Nexus to use in logs including App name, version and long edition if defined
+ */
+ @VisibleForTesting
+ protected final String getNexusNameForLogs(){
+ final StringBuilder msg = new StringBuilder();
+ msg.append(getSystemStatus().getAppName());
+ msg.append( " " ).append( getSystemStatus().getVersion() );
+ return msg.toString();
+ }
+
public void start()
throws StartingException
{
@@ -487,14 +499,24 @@ protected void startService()
applicationStatusSource.getSystemStatus().setStartedAt( new Date() );
- getLogger().info( "Nexus Work Directory : "
- + nexusConfiguration.getWorkingDirectory().getAbsolutePath().toString() );
-
- getLogger().info( "Started Nexus (version " + getSystemStatus().getVersion()
- + (StringUtils.isEmpty( getSystemStatus().getEditionLong() )
- ? ""
- : " " + getSystemStatus().getEditionLong() )
- + ")" );
+ if(getLogger().isInfoEnabled())
+ {
+ final File workDir = nexusConfiguration.getWorkingDirectory();
+ String workDirPath = null;
+ if(workDir != null)
+ {
+ try
+ {
+ workDirPath = workDir.getCanonicalPath();
+ }
+ catch (IOException ioe)
+ {
+ workDirPath = workDir.getAbsolutePath();
+ }
+ }
+ getLogger().info( "Nexus Work Directory : {}", workDirPath );
+ getLogger().info( "Started {}", getNexusNameForLogs() );
+ }
applicationEventMulticaster.notifyEventListeners( new NexusStartedEvent( this ) );
}
@@ -527,7 +549,7 @@ protected void stopService()
// Due to no dependency mechanism in NX for components, we need to fire off a hint about shutdown first
applicationEventMulticaster.notifyEventListeners( new NexusStoppingEvent( this ) );
-
+
nexusScheduler.shutdown();
applicationEventMulticaster.notifyEventListeners( new NexusStoppedEvent( this ) );
@@ -537,15 +559,11 @@ protected void stopService()
securitySystem.stop();
applicationStatusSource.getSystemStatus().setState( SystemState.STOPPED );
-
+
// Now a cleanup, to kill dangling thread of HttpClients
CustomMultiThreadedHttpConnectionManager.shutdownAll();
- getLogger().info( "Stopped Nexus (version " + getSystemStatus().getVersion()
- + (StringUtils.isEmpty( getSystemStatus().getEditionLong() )
- ? ""
- : " " + getSystemStatus().getEditionLong() )
- + ")" );
+ getLogger().info( "Stopped {}", getNexusNameForLogs());
}
private void synchronizeShadowsAtStartup()
@@ -580,4 +598,10 @@ public RepositoryTemplate getRepositoryTemplateById( String id )
{
return (RepositoryTemplate) templateManager.getTemplate( RepositoryTemplate.class, id );
}
+
+ @Override
+ @VisibleForTesting
+ protected Logger getLogger() {
+ return super.getLogger();
+ }
}
View
178 nexus/nexus-app/src/test/java/org/sonatype/nexus/DefaultNexusTest.java
@@ -12,31 +12,102 @@
*/
package org.sonatype.nexus;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.instanceOf;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.mockito.Matchers.endsWith;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Matchers.isNull;
+import static org.mockito.Matchers.startsWith;
+import static org.mockito.Mockito.RETURNS_MOCKS;
+import static org.mockito.Mockito.contains;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import java.io.File;
import java.util.Map;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Answers;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.Spy;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.slf4j.Logger;
+import org.sonatype.nexus.configuration.application.NexusConfiguration;
+import org.sonatype.nexus.events.EventInspectorHost;
+import org.sonatype.nexus.plugins.NexusPluginManager;
+import org.sonatype.nexus.proxy.maven.packaging.ArtifactPackagingMapper;
import org.sonatype.nexus.proxy.registry.ContentClass;
import org.sonatype.nexus.proxy.registry.RepositoryRegistry;
import org.sonatype.nexus.proxy.registry.RepositoryTypeRegistry;
import org.sonatype.nexus.proxy.repository.HostedRepository;
import org.sonatype.nexus.proxy.repository.ProxyRepository;
import org.sonatype.nexus.proxy.repository.Repository;
+import org.sonatype.nexus.scheduling.NexusScheduler;
import org.sonatype.nexus.templates.TemplateSet;
import org.sonatype.nexus.templates.repository.AbstractRepositoryTemplate;
import org.sonatype.nexus.templates.repository.RepositoryTemplate;
+import org.sonatype.plexus.appevents.ApplicationEventMulticaster;
+import org.sonatype.security.SecuritySystem;
+@RunWith( MockitoJUnitRunner.class )
public class DefaultNexusTest
extends AbstractNexusTestCase
{
- private DefaultNexus defaultNexus;
+
+ private DefaultNexus injectedDefaultNexus;
private RepositoryTypeRegistry repositoryTypeRegistry;
private RepositoryRegistry repositoryRegistry;
- public DefaultNexus getDefaultNexus()
+ @Mock
+ private ApplicationEventMulticaster applicationEventMulticaster;
+
+ @Mock
+ private ApplicationStatusSource applicationStatusSource;
+
+ @Mock
+ private ArtifactPackagingMapper artifactPackagingMapper;
+
+ @Mock
+ private NexusConfiguration nexusConfiguration;
+
+ @Mock
+ private NexusPluginManager nexusPluginManager;
+
+ @Mock
+ private EventInspectorHost eventInspectorHost;
+
+ @Mock
+ private SystemStatus systemStatus;
+
+ @Mock
+ private Logger mockLogger;
+
+ @Mock
+ private NexusScheduler nexusScheduler;
+
+ @Mock
+ private SecuritySystem securitySystem;
+
+ @Mock
+ private RepositoryRegistry mockedRepositoryRegistry;
+
+ @InjectMocks
+ @Spy
+ private DefaultNexus spyDefaultNexus;
+
+ public DefaultNexus getInjectedDefaultNexus()
{
- return defaultNexus;
+ return injectedDefaultNexus;
}
@Override
@@ -45,11 +116,64 @@ protected void setUp()
{
super.setUp();
- defaultNexus = (DefaultNexus) lookup( Nexus.class );
+ injectedDefaultNexus = (DefaultNexus) lookup( Nexus.class );
repositoryTypeRegistry = lookup( RepositoryTypeRegistry.class );
repositoryRegistry = lookup( RepositoryRegistry.class );
+
+ // sensible mock defaults
+ doReturn( "App Name" ).when( systemStatus ).getAppName();
+ doReturn( "123-SNAPSHOT" ).when( systemStatus ).getVersion();
+ doReturn( mockLogger ).when( spyDefaultNexus ).getLogger();
+ doReturn( systemStatus ).when( spyDefaultNexus ).getSystemStatus();
+ doReturn( systemStatus ).when( applicationStatusSource ).getSystemStatus();
+ }
+
+ @Test
+ public void initializeLogsApplicationInitialize()
+ throws Exception
+ {
+ spyDefaultNexus.initialize();
+ verify( spyDefaultNexus, times( 1 ) ).logInitialize();
+ }
+
+ @Test
+ public void logInitializeFormat()
+ throws Exception
+ {
+ spyDefaultNexus.logInitialize();
+ verify( mockLogger ).info( startsWith( "\n-------------------------------------------------\n\n" ) );
+ verify( mockLogger ).info( contains( "Initializing App Name 123-SNAPSHOT" ) );
+ verify( mockLogger ).info( endsWith( "\n\n-------------------------------------------------" ) );
+ }
+
+ @Test
+ public void getNexusNameForLogsWithLongEdition()
+ throws Exception
+ {
+ final String name = spyDefaultNexus.getNexusNameForLogs();
+ assertThat( name, equalTo( "App Name 123-SNAPSHOT" ) );
+ }
+
+ @Test
+ public void stopServiceLogsStop()
+ throws Exception
+ {
+ spyDefaultNexus.stopService();
+ verify( mockLogger ).info( eq( "Stopped {}" ), eq( "App Name 123-SNAPSHOT" ) );
+ }
+
+ @Test
+ public void startServiceLogsStartAndNexusWork()
+ throws Exception
+ {
+ doReturn( true ).when( mockLogger ).isInfoEnabled();
+ //doReturn( mock( File.class ) ).when( nexusConfiguration ).getWorkingDirectory();
+ spyDefaultNexus.startService();
+ verify( mockLogger ).info( eq( "Started {}" ), eq( "App Name 123-SNAPSHOT" ) );
+ verify( mockLogger ).info( eq( "Nexus Work Directory : {}" ), isNull() );
+
}
@Override
@@ -62,16 +186,17 @@ protected boolean loadConfigurationAtSetUp()
public void testRepositoryTemplates()
throws Exception
{
- TemplateSet repoTemplates = getDefaultNexus().getRepositoryTemplates();
+ TemplateSet repoTemplates = getInjectedDefaultNexus().getRepositoryTemplates();
- assertNotNull( "template list is null", repoTemplates );
- assertEquals( "there should be 12 templates", 12, repoTemplates.size() );
+ assertThat( repoTemplates, notNullValue() );
+ assertThat( repoTemplates.size(), is( 12 ) );
RepositoryTemplate template =
- (AbstractRepositoryTemplate) getDefaultNexus().getRepositoryTemplateById( "default_hosted_release" );
+ (AbstractRepositoryTemplate) getInjectedDefaultNexus().getRepositoryTemplateById(
+ "default_hosted_release" );
- assertNotNull( "template should exist", template );
- assertTrue( "it should be RepositoryTemplate", template instanceof AbstractRepositoryTemplate );
+ assertThat( template, notNullValue() );
+ assertThat( template, instanceOf( AbstractRepositoryTemplate.class ) );
// just adjust some params on template
{
@@ -85,20 +210,17 @@ public void testRepositoryTemplates()
Repository repository = template.create();
// this call will throw NoSuchRepositoryException if repo is not registered with registry
- assertTrue( "it should be registered with registry",
- repositoryRegistry.getRepository( "created-from-template" ) != null );
-
- assertTrue( "it should be plain hosted release", repository.getRepositoryKind()
- .isFacetAvailable( HostedRepository.class ) );
- assertFalse( "it should be plain hosted release", repository.getRepositoryKind()
- .isFacetAvailable( ProxyRepository.class ) );
-
- // assertNotNull( getDefaultNexus().createFromTemplate( RepositoryTemplate.DEFAULT_HOSTED_RELEASE ) );
- // assertNotNull( getDefaultNexus().createFromTemplate( RepositoryTemplate.DEFAULT_HOSTED_SNAPSHOT ) );
- // assertNotNull( getDefaultNexus().createFromTemplate( RepositoryTemplate.DEFAULT_PROXY_RELEASE ) );
- // assertNotNull( getDefaultNexus().createFromTemplate( RepositoryTemplate.DEFAULT_PROXY_SNAPSHOT ) );
+ assertThat( repositoryRegistry.getRepository( "created-from-template" ), notNullValue() );
+ assertThat( repository.getRepositoryKind(), notNullValue() );
+ assertThat( repository.getRepositoryKind().isFacetAvailable( HostedRepository.class ), is( true ) );
+ assertThat( repository.getRepositoryKind().isFacetAvailable( ProxyRepository.class ), is( false ) );
+
+ // assertNotNull( getInjectedDefaultNexus().createFromTemplate( RepositoryTemplate.DEFAULT_HOSTED_RELEASE ) );
+ // assertNotNull( getInjectedDefaultNexus().createFromTemplate( RepositoryTemplate.DEFAULT_HOSTED_SNAPSHOT ) );
+ // assertNotNull( getInjectedDefaultNexus().createFromTemplate( RepositoryTemplate.DEFAULT_PROXY_RELEASE ) );
+ // assertNotNull( getInjectedDefaultNexus().createFromTemplate( RepositoryTemplate.DEFAULT_PROXY_SNAPSHOT ) );
// FIXME tamas here you go
- // assertNotNull( getDefaultNexus().createFromTemplate( RepositoryTemplate.DEFAULT_VIRTUAL ) );
+ // assertNotNull( getInjectedDefaultNexus().createFromTemplate( RepositoryTemplate.DEFAULT_VIRTUAL ) );
}
@Test
@@ -106,20 +228,18 @@ public void testListRepositoryContentClasses()
throws Exception
{
Map<String, ContentClass> plexusContentClasses = getContainer().lookupMap( ContentClass.class );
-
Map<String, ContentClass> contentClasses = repositoryTypeRegistry.getContentClasses();
- assertEquals( plexusContentClasses.size(), contentClasses.size() );
-
- assertTrue( plexusContentClasses.values().containsAll( contentClasses.values() ) );
+ assertThat( plexusContentClasses.size(), equalTo( contentClasses.size() ) );
+ assertThat( plexusContentClasses.values().containsAll( contentClasses.values() ), is( true ) );
}
@Test
public void testBounceNexus()
throws Exception
{
- getDefaultNexus().stop();
+ getInjectedDefaultNexus().stop();
- getDefaultNexus().start();
+ getInjectedDefaultNexus().start();
}
}
View
4 nexus/nexus-webapp/src/main/webapp/WEB-INF/web.xml
@@ -17,8 +17,8 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <display-name>Nexus Application</display-name>
-
+ <display-name>Sonatype Nexus</display-name>
+
<!-- Booter -->
<!--
TODO: we have a chicken and egg problem: this produces WARNINGs in
View
2 sonatype-nexus-idea-formatter.xml
@@ -13,7 +13,7 @@
Eclipse Foundation. All other trademarks are the property of their respective owners.
-->
-<code_scheme name="Maven">
+<code_scheme name="Sonatype Nexus">
<option name="JAVA_INDENT_OPTIONS">
<value>
<option name="INDENT_SIZE" value="4" />

0 comments on commit 6fc03a4

Please sign in to comment.
Something went wrong with that request. Please try again.