| 1 | /* | |
| 2 | * Copyright 2019 Hector Espert. | |
| 3 | * | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | * you may not use this file except in compliance with the License. | |
| 6 | * You may obtain a copy of the License at | |
| 7 | * | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | * | |
| 10 | * Unless required by applicable law or agreed to in writing, software | |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | * See the License for the specific language governing permissions and | |
| 14 | * limitations under the License. | |
| 15 | */ | |
| 16 | package es.blackleg.jlibnotify.core; | |
| 17 | ||
| 18 | import com.sun.jna.Pointer; | |
| 19 | import es.blackleg.jlibnotify.ServerInfo; | |
| 20 | import es.blackleg.jlibnotify.jna.GBoolean; | |
| 21 | import java.util.Collection; | |
| 22 | import es.blackleg.jlibnotify.JLibnotify; | |
| 23 | import es.blackleg.jlibnotify.exception.JLibnotifyInitException; | |
| 24 | import es.blackleg.jlibnotify.jna.NativeLibnotify; | |
| 25 | import es.blackleg.jlibnotify.JLibnotifyNotification; | |
| 26 | ||
| 27 | /** | |
| 28 | * Reference implementation of {@link JLibnotify} on top of the JNA binding. | |
| 29 | * | |
| 30 | * <p>Delegates every call to {@link NativeLibnotify}, converting the {@link GBoolean} results of | |
| 31 | * the native functions into Java types or into exceptions. Reading the server capabilities is | |
| 32 | * delegated to a {@link ServerCapabilitiesReader}, the only part that walks native memory.</p> | |
| 33 | * | |
| 34 | * <p>Instances are built by {@link DefaultJLibnotifyLoader#load()} once the native library is | |
| 35 | * loaded; application code should depend on the {@link JLibnotify} interface instead.</p> | |
| 36 | * | |
| 37 | * @author Hector Espert | |
| 38 | */ | |
| 39 | public class DefaultJLibnotify implements JLibnotify { | |
| 40 | ||
| 41 | private final NativeLibnotify nativeLibnotify; | |
| 42 | ||
| 43 | private final ServerCapabilitiesReader serverCapabilitiesReader; | |
| 44 | ||
| 45 | /** | |
| 46 | * Creates a binding over an already loaded native library. | |
| 47 | * | |
| 48 | * @param libnotify the loaded native library | |
| 49 | * @param serverCapabilitiesReader reader used to walk the capabilities returned by the | |
| 50 | * notification server | |
| 51 | */ | |
| 52 | public DefaultJLibnotify(NativeLibnotify libnotify, ServerCapabilitiesReader serverCapabilitiesReader) { | |
| 53 | this.nativeLibnotify = libnotify; | |
| 54 | this.serverCapabilitiesReader = serverCapabilitiesReader; | |
| 55 | } | |
| 56 | ||
| 57 | @Override | |
| 58 | public void init(String appName) throws JLibnotifyInitException { | |
| 59 |
1
1. init : negated conditional → KILLED |
if (this.nativeLibnotify.notify_init(appName) == GBoolean.FALSE) { |
| 60 | throw new JLibnotifyInitException(String.format("Unable to init %s app", appName)); | |
| 61 | } | |
| 62 | } | |
| 63 | ||
| 64 | @Override | |
| 65 | public boolean isInitted() { | |
| 66 |
2
1. isInitted : negated conditional → KILLED 2. isInitted : replaced boolean return with true for es/blackleg/jlibnotify/core/DefaultJLibnotify::isInitted → KILLED |
return nativeLibnotify.notify_is_initted() == GBoolean.TRUE; |
| 67 | } | |
| 68 | ||
| 69 | @Override | |
| 70 | public void unInit() { | |
| 71 |
1
1. unInit : removed call to es/blackleg/jlibnotify/jna/NativeLibnotify::notify_uninit → KILLED |
nativeLibnotify.notify_uninit(); |
| 72 | } | |
| 73 | ||
| 74 | @Override | |
| 75 | public String getAppName() { | |
| 76 |
1
1. getAppName : replaced return value with "" for es/blackleg/jlibnotify/core/DefaultJLibnotify::getAppName → NO_COVERAGE |
return nativeLibnotify.notify_get_app_name(); |
| 77 | } | |
| 78 | ||
| 79 | @Override | |
| 80 | public void setAppName(String appName) { | |
| 81 |
1
1. setAppName : removed call to es/blackleg/jlibnotify/jna/NativeLibnotify::notify_set_app_name → NO_COVERAGE |
nativeLibnotify.notify_set_app_name(appName); |
| 82 | } | |
| 83 | ||
| 84 | @Override | |
| 85 | public ServerInfo getServerInfo() { | |
| 86 | String[] name = new String[1]; | |
| 87 | String[] vendor = new String[1]; | |
| 88 | String[] version = new String[1]; | |
| 89 | String[] specVersion = new String[1]; | |
| 90 | ||
| 91 |
1
1. getServerInfo : negated conditional → NO_COVERAGE |
if (nativeLibnotify.notify_get_server_info(name, vendor, version, specVersion) == GBoolean.FALSE) { |
| 92 | throw new RuntimeException("Error when get server info"); | |
| 93 | } | |
| 94 | ||
| 95 |
1
1. getServerInfo : replaced return value with null for es/blackleg/jlibnotify/core/DefaultJLibnotify::getServerInfo → NO_COVERAGE |
return new BasicServerInfo(name[0], vendor[0], version[0], specVersion[0]); |
| 96 | } | |
| 97 | ||
| 98 | @Override | |
| 99 | public Collection<String> getServerCapabilities() { | |
| 100 |
1
1. getServerCapabilities : replaced return value with Collections.emptyList for es/blackleg/jlibnotify/core/DefaultJLibnotify::getServerCapabilities → NO_COVERAGE |
return serverCapabilitiesReader.getServerCapabilitiesFromPointer(nativeLibnotify.notify_get_server_caps()); |
| 101 | } | |
| 102 | ||
| 103 | @Override | |
| 104 | public JLibnotifyNotification createNotification(String summary, String body, String icon) { | |
| 105 | Pointer pointer = nativeLibnotify.notify_notification_new(summary, body, icon); | |
| 106 |
1
1. createNotification : replaced return value with null for es/blackleg/jlibnotify/core/DefaultJLibnotify::createNotification → KILLED |
return new DefaultJLibnotifyNotification(pointer, nativeLibnotify); |
| 107 | } | |
| 108 | ||
| 109 | } | |
Mutations | ||
| 59 |
1.1 |
|
| 66 |
1.1 2.2 |
|
| 71 |
1.1 |
|
| 76 |
1.1 |
|
| 81 |
1.1 |
|
| 91 |
1.1 |
|
| 95 |
1.1 |
|
| 100 |
1.1 |
|
| 106 |
1.1 |