Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce LoggingSupport #585

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions plugins/org.locationtech.udig.core.tests/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ Bundle-Version: 2.3.0.qualifier
Bundle-Vendor: udig.refractions.net
Eclipse-BuddyPolicy: ext
Require-Bundle: org.eclipse.core.runtime,
org.junit,
org.locationtech.udig.libs
org.junit;bundle-version="4.12.0",
org.locationtech.udig.libs,
org.mockito;bundle-version="2.23.0",
org.objenesis;bundle-version="2.6.0",
net.bytebuddy.byte-buddy;bundle-version="1.9.0",
net.bytebuddy.byte-buddy-agent;bundle-version="1.9.0"
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Fragment-Host: org.locationtech.udig.core
Expand Down
3 changes: 2 additions & 1 deletion plugins/org.locationtech.udig.core.tests/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ bin.includes = META-INF/,\
.,\
bsd3-v10.html,\
epl-v10.html,\
about.html
about.html,\
src/mockito-extensions/
src.includes = LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mock-maker-inline
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* uDig - User Friendly Desktop Internet GIS client
* https://locationtech.org/projects/technology.udig
* (C) 2021, Eclipse Foundation
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
* License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html).
*/
package org.locationtech.udig.core.logging;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Plugin;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.osgi.framework.Bundle;

@RunWith(MockitoJUnitRunner.class)
public class LoggingSupportTest {

private static final String BUNDLE_NAME = "test.bundle";

@Mock
Plugin plugin;

@Mock
ILog log;

@Mock
Bundle bundle;

@Captor
ArgumentCaptor<IStatus> statusCapture;

@Before
public void setUp() {
when(plugin.getLog()).thenReturn(log);
when(plugin.getBundle()).thenReturn(bundle);
when(bundle.getSymbolicName()).thenReturn(BUNDLE_NAME);
}

@Test
public void logErrorSeverityWhileExceptionIsGiven() {
LoggingSupport.log(plugin, "whatever", new Exception("expect error test"));

verify(log).log(statusCapture.capture());
assertEquals(IStatus.ERROR, statusCapture.getValue().getSeverity());
}

@Test
public void logWarningSeverityWhileThrowableIsGiven() {
String errorMessage = "ErrorMessage 1";
LoggingSupport.log(plugin, errorMessage, new Throwable("expect error test"));

verify(log).log(statusCapture.capture());
assertStatus(IStatus.WARNING, errorMessage, BUNDLE_NAME, statusCapture.getValue());
}

@Test
public void logInfoSeverityWithNullThrowableIsGiven() {
String errorMessage = "ErrorMessage 1";
LoggingSupport.log(plugin, errorMessage, null);

verify(log).log(statusCapture.capture());
assertStatus(IStatus.INFO, errorMessage, BUNDLE_NAME, statusCapture.getValue());
}

@Test
public void logWithNullMessage() {
LoggingSupport.log(plugin, null, new Exception("expect error test"));

verify(log).log(statusCapture.capture());
assertStatus(IStatus.ERROR, "", "test.bundle", statusCapture.getValue());
}

@Test
public void doNotLogAnythingNullMessageAndNullThrowable() {
LoggingSupport.log(plugin, null, null);
verifyNoMoreInteractions(log);
}

@Test
public void doNotLogAnythingEmptyMessageAndNullThrowable() {
LoggingSupport.log(plugin, "", null);
verifyNoMoreInteractions(log);
}

private void assertStatus(int expectedSeverity, String expectedMessage,
String expectedBundleName, IStatus givenStatus) {
assertEquals(expectedSeverity, givenStatus.getSeverity());
assertEquals(expectedMessage, givenStatus.getMessage());
assertEquals(expectedBundleName, givenStatus.getPlugin());
}

}
1 change: 1 addition & 0 deletions plugins/org.locationtech.udig.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Export-Package: org.locationtech.udig.core,
org.locationtech.udig.core.filter,
org.locationtech.udig.core.internal,
org.locationtech.udig.core.jts,
org.locationtech.udig.core.logging,
org.locationtech.udig.core.opengis
Require-Bundle: org.eclipse.core.runtime,
org.eclipse.ui,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* uDig - User Friendly Desktop Internet GIS client
/**
* uDig - User Friendly Desktop Internet GIS client
* http://udig.refractions.net
* (C) 2012, Refractions Research Inc.
*
Expand All @@ -20,10 +21,9 @@
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
import org.locationtech.udig.core.logging.LoggingSupport;
import org.osgi.framework.BundleContext;

/**
Expand All @@ -34,23 +34,20 @@
*/
public class CorePlugin extends Plugin {

/** Plugin <code>ID</code> field */
public static final String ID = "org.locationtech.udig.core"; //$NON-NLS-1$
private static CorePlugin plugin;



/**
* A url stream handler that delegates to the default one but if it doesn't work then it returns null as the stream.
* A url stream handler that delegates to the default one but if it doesn't work then it returns
* null as the stream.
*/
public static final URLStreamHandler RELAXED_HANDLER=new URLStreamHandler(){
public static final URLStreamHandler RELAXED_HANDLER = new URLStreamHandler() {

@Override
protected URLConnection openConnection( URL u ) throws IOException {
try{
URL url=new URL(u.toString());
protected URLConnection openConnection(URL u) throws IOException {
try {
URL url = new URL(u.toString());
return url.openConnection();
}catch (MalformedURLException e){
} catch (MalformedURLException e) {
return null;
}
}
Expand All @@ -67,40 +64,40 @@ public CorePlugin() {
/**
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
public void start( BundleContext context ) throws Exception {
@Override
public void start(BundleContext context) throws Exception {
super.start(context);
}

/**
* Create a URL from the provided spec; willing to create
* a URL even if the spec does not have a registered handler.
* Can be used to create "jdbc" URLs for example.
* Create a URL from the provided spec; willing to create a URL even if the spec does not have a
* registered handler. Can be used to create "jdbc" URLs for example.
*
* @param spec
* @return URL if possible
* @throws RuntimeException of a MalformedURLException resulted
*/
public static URL createSafeURL( String spec ) {
public static URL createSafeURL(String spec) {
try {
return new URL(null, spec, RELAXED_HANDLER);
} catch (MalformedURLException e) {
throw (RuntimeException) new RuntimeException( e );
throw new RuntimeException(e);
}
}

/**
* Create a URI from the provided spec; willing to create
* a URI even if the spec does not have a registered handler.
* Can be used to create "jdbc" URLs for example.
* Create a URI from the provided spec; willing to create a URI even if the spec does not have a
* registered handler. Can be used to create "jdbc" URLs for example.
*
* @param spec
* @return URI if possible
* @throws RuntimeException of a URISyntaxException resulted
*/
public static URI createSafeURI( String spec ){
public static URI createSafeURI(String spec) {
try {
return new URI( spec );
return new URI(spec);
} catch (URISyntaxException e) {
throw (RuntimeException) new RuntimeException( e );
throw new RuntimeException(e);
}
}

Expand All @@ -116,7 +113,7 @@ public static CorePlugin getDefault() {
/**
* Takes a string, and splits it on '\n' and calls stringsToURLs(String[])
*/
public static List<URL> stringsToURLs( String string ) {
public static List<URL> stringsToURLs(String string) {
String[] strings = string.split("\n"); //$NON-NLS-1$

return stringsToURLs(strings);
Expand All @@ -131,16 +128,16 @@ public static List<URL> stringsToURLs( String string ) {
* @param strings an array of strings, each to be converted to a URL
* @return a List of URLs, in the same order as the array
*/
public static List<URL> stringsToURLs( String[] strings ) {
List<URL> urls = new ArrayList<URL>();
public static List<URL> stringsToURLs(String[] strings) {
List<URL> urls = new ArrayList<>();

for( String string : strings ) {
for (String string : strings) {
try {
urls.add(new URL(string));
} catch (MalformedURLException e) {
// not a URL, maybe it is a file
try {
urls.add( new File(string).toURI().toURL());
urls.add(new File(string).toURI().toURL());
} catch (MalformedURLException e1) {
// Not a URL, not a File. nothing to do now.
}
Expand All @@ -154,33 +151,31 @@ public static List<URL> stringsToURLs( String[] strings ) {
* <p>
* This should be used for user level messages.
* </p>
*
* @deprecated Use {@link LoggingSupport#log(Plugin, String, Throwable)} instead.
*/
public static void log( String message2, Throwable e ) {
String message=message2;
if (message == null)
message = ""; //$NON-NLS-1$
getDefault().getLog().log(new Status(IStatus.INFO, ID, 0, message, e));
public static void log(String message2, Throwable e) {
LoggingSupport.log(getDefault(), message2, e);
}

/**
* Messages that only engage if getDefault().isDebugging()
* <p>
* It is much prefered to do this:
* It is much preferred to do this:
*
* <pre><code>
* private static final String RENDERING = &quot;org.locationtech.udig.project/render/trace&quot;;
* if (ProjectUIPlugin.getDefault().isDebugging() &amp;&amp; &quot;true&quot;.equalsIgnoreCase(RENDERING)) {
* System.out.println(&quot;your message here&quot;);
* }
* <pre>
* <code> private static final String RENDERING =
* &quot;org.locationtech.udig.project/render/trace&quot;; if
* (ProjectUIPlugin.getDefault().isDebugging() &amp;&amp;
* &quot;true&quot;.equalsIgnoreCase(RENDERING)) { System.out.println(&quot;your message
* here&quot;); }
*
* @deprecated Use {@link LoggingSupport#trace(Plugin, String, Throwable)} instead.
*/
public static void trace( String message, Throwable e ) {
if (getDefault().isDebugging()) {
if (message != null)
System.out.println(message);
if (e != null)
e.printStackTrace();
}
public static void trace(String message, Throwable e) {
LoggingSupport.trace(getDefault(), message, e);
}

/**
* Performs the Platform.getDebugOption true check on the provided trace
* <p>
Expand All @@ -192,7 +187,7 @@ public static void trace( String message, Throwable e ) {
*
* @param trace currently only RENDER is defined
*/
public static boolean isDebugging( final String trace ) {
public static boolean isDebugging(final String trace) {
return getDefault().isDebugging()
&& "true".equalsIgnoreCase(Platform.getDebugOption(trace)); //$NON-NLS-1$
}
Expand Down
Loading