From 42252cf3eb7f8d66553cf117abaca162404b26f5 Mon Sep 17 00:00:00 2001 From: Luis Fernando Pardo Date: Thu, 4 Jul 2019 16:27:51 +0200 Subject: [PATCH 1/2] add parent pid field --- .../jprocesses/info/AbstractProcessesService.java | 1 + .../jprocesses/info/UnixProcessesService.java | 3 ++- .../jprocesses/info/WindowsProcessesService.java | 4 +++- .../org/jutils/jprocesses/model/ProcessInfo.java | 13 +++++++++++++ 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jutils/jprocesses/info/AbstractProcessesService.java b/src/main/java/org/jutils/jprocesses/info/AbstractProcessesService.java index e8e3017..3ed4fd3 100644 --- a/src/main/java/org/jutils/jprocesses/info/AbstractProcessesService.java +++ b/src/main/java/org/jutils/jprocesses/info/AbstractProcessesService.java @@ -83,6 +83,7 @@ private List buildInfoFromMap(List> mapList) { info.setUser(map.get("user")); info.setVirtualMemory(map.get("virtual_memory")); info.setPriority(map.get("priority")); + info.setPpid(map.get("ppid")); //Adds extra data info.setExtraData(map); diff --git a/src/main/java/org/jutils/jprocesses/info/UnixProcessesService.java b/src/main/java/org/jutils/jprocesses/info/UnixProcessesService.java index 74e9bf2..3834f91 100644 --- a/src/main/java/org/jutils/jprocesses/info/UnixProcessesService.java +++ b/src/main/java/org/jutils/jprocesses/info/UnixProcessesService.java @@ -35,7 +35,7 @@ class UnixProcessesService extends AbstractProcessesService { //Use BSD sytle to get data in order to be compatible with Mac Systems(thanks to jkuharev for this tip) - private static final String PS_COLUMNS = "pid,ruser,vsize,rss,%cpu,lstart,cputime,nice,ucomm"; + private static final String PS_COLUMNS = "pid,ppid,ruser,vsize,rss,%cpu,lstart,cputime,nice,ucomm"; private static final String PS_FULL_COMMAND = "pid,command"; private static final int PS_COLUMNS_SIZE = PS_COLUMNS.split(",").length; @@ -59,6 +59,7 @@ protected List> parseList(String rawData) { String[] elements = line.split("\\s+", PS_COLUMNS_SIZE + 5); index = 0; element.put("pid", elements[index++]); + element.put("ppid", elements[index++]); element.put("user", elements[index++]); element.put("virtual_memory", elements[index++]); element.put("physical_memory", elements[index++]); diff --git a/src/main/java/org/jutils/jprocesses/info/WindowsProcessesService.java b/src/main/java/org/jutils/jprocesses/info/WindowsProcessesService.java index 91dcc3d..e286a93 100644 --- a/src/main/java/org/jutils/jprocesses/info/WindowsProcessesService.java +++ b/src/main/java/org/jutils/jprocesses/info/WindowsProcessesService.java @@ -57,6 +57,7 @@ class WindowsProcessesService extends AbstractProcessesService { private static final String COMMANDLINE_PROPNAME = "CommandLine"; private static final String CREATIONDATE_PROPNAME = "CreationDate"; private static final String CAPTION_PROPNAME = "Caption"; + private static final String PARENTID_PROPNAME = "ParentProcessId"; private final WMI4Java wmi4Java; @@ -70,6 +71,7 @@ class WindowsProcessesService extends AbstractProcessesService { tmpMap.put(WORKINGSETSIZE_PROPNAME, "physical_memory"); tmpMap.put(COMMANDLINE_PROPNAME, "command"); tmpMap.put(CREATIONDATE_PROPNAME, "start_time"); + tmpMap.put(PARENTID_PROPNAME, "ppid"); keyMap = Collections.unmodifiableMap(tmpMap); } @@ -138,7 +140,7 @@ protected String getProcessesData(String name) { .properties(Arrays.asList(CAPTION_PROPNAME, PROCESSID_PROPNAME, NAME_PROPNAME, USERMODETIME_PROPNAME, COMMANDLINE_PROPNAME, WORKINGSETSIZE_PROPNAME, CREATIONDATE_PROPNAME, - VIRTUALSIZE_PROPNAME, PRIORITY_PROPNAME)) + VIRTUALSIZE_PROPNAME, PRIORITY_PROPNAME, PARENTID_PROPNAME)) .filters(Collections.singletonList("Name like '%" + name + "%'")) .getRawWMIObjectOutput(WMIClass.WIN32_PROCESS); } diff --git a/src/main/java/org/jutils/jprocesses/model/ProcessInfo.java b/src/main/java/org/jutils/jprocesses/model/ProcessInfo.java index c01c825..4365215 100644 --- a/src/main/java/org/jutils/jprocesses/model/ProcessInfo.java +++ b/src/main/java/org/jutils/jprocesses/model/ProcessInfo.java @@ -35,6 +35,7 @@ public class ProcessInfo { private String startTime; private String priority; private String command; + private String ppid; //Used to store system specific data private Map extraData = new HashMap(); @@ -43,6 +44,9 @@ public ProcessInfo() { } public ProcessInfo(String pid, String time, String name, String user, String virtualMemory, String physicalMemory, String cpuUsage, String startTime, String priority, String command) { + this(pid, time, name, user, virtualMemory, physicalMemory, cpuUsage, startTime, priority, command, null); + } + public ProcessInfo(String pid, String time, String name, String user, String virtualMemory, String physicalMemory, String cpuUsage, String startTime, String priority, String command, String ppid) { this.pid = pid; this.time = time; this.name = name; @@ -53,6 +57,7 @@ public ProcessInfo(String pid, String time, String name, String user, String vir this.startTime = startTime; this.priority = priority; this.command = command; + this.ppid = ppid; } public String getPid() { @@ -127,6 +132,14 @@ public void setCommand(String command) { this.command = command; } + public String getPpid() { + return ppid; + } + + public void setPpid(String ppid) { + this.ppid = ppid; + } + public String getPriority() { return priority; } From 563fcf75e8ca3338a91b2578a0909a467ab6dd1b Mon Sep 17 00:00:00 2001 From: lfern Date: Sun, 14 Feb 2021 13:48:44 +0100 Subject: [PATCH 2/2] . --- .gitignore | 8 +- .travis.yml | 6 +- LICENSE | 404 ++++---- README.md | 160 +-- pom.xml | 318 +++--- .../org/jutils/jprocesses/JProcesses.java | 448 ++++---- .../info/AbstractProcessesService.java | 192 ++-- .../jprocesses/info/ProcessesFactory.java | 82 +- .../jprocesses/info/ProcessesService.java | 76 +- .../jprocesses/info/UnixProcessesService.java | 400 +++---- .../jprocesses/info/VBScriptHelper.java | 320 +++--- .../info/WindowsProcessesService.java | 576 +++++----- .../jprocesses/model/JProcessesResponse.java | 88 +- .../jprocesses/model/WindowsPriority.java | 68 +- .../jutils/jprocesses/util/OSDetector.java | 100 +- .../jprocesses/util/ProcessesUtils.java | 444 ++++---- .../org/jutils/jprocesses/JProcessesTest.java | 296 +++--- .../info/WindowsProcessesServiceTest.java | 980 +++++++++--------- .../jprocesses/util/ProcessesUtilsTest.java | 46 +- 19 files changed, 2506 insertions(+), 2506 deletions(-) diff --git a/.gitignore b/.gitignore index 67dca12..63e0556 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ -/target/ -/nbactions.xml -/.classpath -/.project +/target/ +/nbactions.xml +/.classpath +/.project /.idea \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 9bcf999..684811b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,3 @@ -language: java -jdk: - - oraclejdk8 +language: java +jdk: + - oraclejdk8 diff --git a/LICENSE b/LICENSE index 8f71f43..113e843 100644 --- a/LICENSE +++ b/LICENSE @@ -1,202 +1,202 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "{}" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright {yyyy} {name of copyright owner} - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright {yyyy} {name of copyright owner} + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + diff --git a/README.md b/README.md index e711669..c205663 100644 --- a/README.md +++ b/README.md @@ -1,80 +1,80 @@ -![](https://img.shields.io/maven-central/v/org.jprocesses/jProcesses.svg) -![](https://img.shields.io/github/license/profesorfalken/jProcesses.svg) -![](https://travis-ci.org/profesorfalken/jProcesses.svg) - -# jProcesses -Get crossplatform processes details with Java - -## Installation ## - -To install jProcesses you can add the dependecy to your software project management tool: http://mvnrepository.com/artifact/org.jprocesses/jProcesses/1.6.4 - -For example, for Maven you have just to add to your pom.xml: - - - org.jprocesses - jProcesses - 1.6.4 - - - -Instead, you can direct download the JAR file and add it to your classpath. -http://central.maven.org/maven2/org/jprocesses/jProcesses/1.6.4/jProcesses-1.6.4.jar - -The only dependency you will need to add to the classpath is [WMI4Java](https://repo1.maven.org/maven2/com/profesorfalken/WMI4Java). You can download de JAR file [here](https://repo1.maven.org/maven2/com/profesorfalken/WMI4Java/1.6.1/WMI4Java-1.6.1.jar). - - -## Basic Usage ## - -#### Get processes details #### - -```java - List processesList = JProcesses.getProcessList(); - - for (final ProcessInfo processInfo : processesList) { - System.out.println("Process PID: " + processInfo.getPid()); - System.out.println("Process Name: " + processInfo.getName()); - System.out.println("Process Time: " + processInfo.getTime()); - System.out.println("User: " + processInfo.getUser()); - System.out.println("Virtual Memory: " + processInfo.getVirtualMemory()); - System.out.println("Physical Memory: " + processInfo.getPhysicalMemory()); - System.out.println("CPU usage: " + processInfo.getCpuUsage()); - System.out.println("Start Time: " + processInfo.getStartTime()); - System.out.println("Priority: " + processInfo.getPriority()); - System.out.println("Full command: " + processInfo.getCommand()); - System.out.println("------------------"); - } -``` - -#### Kill process by PID #### - -```java - boolean success = JProcesses.killProcess(3844).isSuccess(); -``` - -#### Change process Priority #### - -Unix/Mac: - -```java - boolean ok = JProcesses.changePriority(3844, 5).isSuccess(); -``` - -Windows: - -```java - boolean ok = JProcesses.changePriority(3844, WindowsPriority.HIGH).isSuccess(); -``` - -## More info ## - -Webpage: http://www.jprocesses.org - - -## Special thanks ## - -@jkuharev: for his help to make jProcess work on Mac - -@Gobliins: for fixing executeCommand hang with lots of process using ProcessBuilder - -@janhoy: for his contribution that fix long date parsing with locales different from english (Norwegian in his case) +![](https://img.shields.io/maven-central/v/org.jprocesses/jProcesses.svg) +![](https://img.shields.io/github/license/profesorfalken/jProcesses.svg) +![](https://travis-ci.org/profesorfalken/jProcesses.svg) + +# jProcesses +Get crossplatform processes details with Java + +## Installation ## + +To install jProcesses you can add the dependecy to your software project management tool: http://mvnrepository.com/artifact/org.jprocesses/jProcesses/1.6.4 + +For example, for Maven you have just to add to your pom.xml: + + + org.jprocesses + jProcesses + 1.6.4 + + + +Instead, you can direct download the JAR file and add it to your classpath. +http://central.maven.org/maven2/org/jprocesses/jProcesses/1.6.4/jProcesses-1.6.4.jar + +The only dependency you will need to add to the classpath is [WMI4Java](https://repo1.maven.org/maven2/com/profesorfalken/WMI4Java). You can download de JAR file [here](https://repo1.maven.org/maven2/com/profesorfalken/WMI4Java/1.6.1/WMI4Java-1.6.1.jar). + + +## Basic Usage ## + +#### Get processes details #### + +```java + List processesList = JProcesses.getProcessList(); + + for (final ProcessInfo processInfo : processesList) { + System.out.println("Process PID: " + processInfo.getPid()); + System.out.println("Process Name: " + processInfo.getName()); + System.out.println("Process Time: " + processInfo.getTime()); + System.out.println("User: " + processInfo.getUser()); + System.out.println("Virtual Memory: " + processInfo.getVirtualMemory()); + System.out.println("Physical Memory: " + processInfo.getPhysicalMemory()); + System.out.println("CPU usage: " + processInfo.getCpuUsage()); + System.out.println("Start Time: " + processInfo.getStartTime()); + System.out.println("Priority: " + processInfo.getPriority()); + System.out.println("Full command: " + processInfo.getCommand()); + System.out.println("------------------"); + } +``` + +#### Kill process by PID #### + +```java + boolean success = JProcesses.killProcess(3844).isSuccess(); +``` + +#### Change process Priority #### + +Unix/Mac: + +```java + boolean ok = JProcesses.changePriority(3844, 5).isSuccess(); +``` + +Windows: + +```java + boolean ok = JProcesses.changePriority(3844, WindowsPriority.HIGH).isSuccess(); +``` + +## More info ## + +Webpage: http://www.jprocesses.org + + +## Special thanks ## + +@jkuharev: for his help to make jProcess work on Mac + +@Gobliins: for fixing executeCommand hang with lots of process using ProcessBuilder + +@janhoy: for his contribution that fix long date parsing with locales different from english (Norwegian in his case) diff --git a/pom.xml b/pom.xml index f0ab678..36e86b2 100644 --- a/pom.xml +++ b/pom.xml @@ -1,159 +1,159 @@ - - - 4.0.0 - org.jprocesses - jProcesses - 1.6.5 - jar - - jProcesses - Library to manage system processes using Java - https://github.com/profesorfalken/jProcesses - - - - Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.txt - repo - - - - - - Javier Garcia Alonso - profesorfalken.github@gmail.com - ProfesorFalken - http://www.profesorfalken.com - - - - - https://github.com/profesorfalken/jProcesses.git - - - - - ossrh - https://oss.sonatype.org/content/repositories/snapshots - - - ossrh - https://oss.sonatype.org/service/local/staging/deploy/maven2/ - - - - - - release-profile - - - - org.sonatype.plugins - nexus-staging-maven-plugin - 1.6.3 - true - - ossrh - https://oss.sonatype.org/ - true - - - - org.apache.maven.plugins - maven-gpg-plugin - 1.5 - - - sign-artifacts - verify - - sign - - - - - - - - - coverture - - - - org.jacoco - jacoco-maven-plugin - 0.7.6.201602180812 - - ${sonar.jacoco.reportPath} - - - - default-prepare-agent - - prepare-agent - - - - default-report - prepare-package - - report - - - - - - - - - with-dependencies - - - - maven-assembly-plugin - - - jar-with-dependencies - - - - - make-assembly - package - - single - - - - - - - - - - - - junit - junit - 4.10 - test - - - com.profesorfalken - WMI4Java - 1.6.3 - - - org.mockito - mockito-all - 1.8.4 - test - - - - UTF-8 - 1.7 - 1.7 - 1.7 - - + + + 4.0.0 + org.jprocesses + jProcesses + 1.6.5 + jar + + jProcesses + Library to manage system processes using Java + https://github.com/profesorfalken/jProcesses + + + + Apache License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + + + + + + Javier Garcia Alonso + profesorfalken.github@gmail.com + ProfesorFalken + http://www.profesorfalken.com + + + + + https://github.com/profesorfalken/jProcesses.git + + + + + ossrh + https://oss.sonatype.org/content/repositories/snapshots + + + ossrh + https://oss.sonatype.org/service/local/staging/deploy/maven2/ + + + + + + release-profile + + + + org.sonatype.plugins + nexus-staging-maven-plugin + 1.6.3 + true + + ossrh + https://oss.sonatype.org/ + true + + + + org.apache.maven.plugins + maven-gpg-plugin + 1.5 + + + sign-artifacts + verify + + sign + + + + + + + + + coverture + + + + org.jacoco + jacoco-maven-plugin + 0.7.6.201602180812 + + ${sonar.jacoco.reportPath} + + + + default-prepare-agent + + prepare-agent + + + + default-report + prepare-package + + report + + + + + + + + + with-dependencies + + + + maven-assembly-plugin + + + jar-with-dependencies + + + + + make-assembly + package + + single + + + + + + + + + + + + junit + junit + 4.10 + test + + + com.profesorfalken + WMI4Java + 1.6.3 + + + org.mockito + mockito-all + 1.8.4 + test + + + + UTF-8 + 1.7 + 1.7 + 1.7 + + diff --git a/src/main/java/org/jutils/jprocesses/JProcesses.java b/src/main/java/org/jutils/jprocesses/JProcesses.java index 9eceeeb..4fefb33 100644 --- a/src/main/java/org/jutils/jprocesses/JProcesses.java +++ b/src/main/java/org/jutils/jprocesses/JProcesses.java @@ -1,224 +1,224 @@ -/* - * Copyright 2016 Javier Garcia Alonso. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jutils.jprocesses; - -import org.jutils.jprocesses.info.ProcessesFactory; -import org.jutils.jprocesses.info.ProcessesService; -import org.jutils.jprocesses.model.JProcessesResponse; -import org.jutils.jprocesses.model.ProcessInfo; -import org.jutils.jprocesses.model.WindowsPriority; - -import java.util.List; - -/** - * Class that gives access to Processes details.

- * - * JProcesses allows to interact with system processes. It is possible to - * list all alive processes, kill them or change the priority. - * - * @author Javier Garcia Alonso - */ -public class JProcesses { - - //This mode retrieves less information but faster - private boolean fastMode = false; - - private JProcesses() { - } - - /** - * Gets the JProcesses instance.
- * This method creates a new instance each time. - * - * @return JProcesses instance - */ - public static JProcesses get() { - return new JProcesses(); - } - - /** - * Enables the fast mode.
In this mode JProcesses does not try to retrieve - * time consuming data as the use percentage or the owner of the process in Windows implementation.
- * This flag has no effect in Linux implementation as it is fast enough. - * - * @return JProcesses instance - */ - public JProcesses fastMode() { - this.fastMode = true; - return this; - } - - /** - * Enables/disables the fast mode.
In this mode JProcesses does not try to retrieve - * time consuming data as the use percentage or the owner of the process in Windows implementation.
- * This flag has no effect in Linux implementation as it is fast enough. - * - * @param enabled boolean true or false - * @return JProcesses instance - */ - public JProcesses fastMode(boolean enabled) { - this.fastMode = enabled; - return this; - } - - /** - * Return the list of processes running in the system.
- * For each process some information is retrieved: - *

    - *
  • PID
  • - *
  • Name
  • - *
  • Used memory
  • - *
  • Date/time
  • - *
  • Priority
  • - *
- * [...]

- * - * For further details see {@link ProcessInfo} - * - * @return List of processes - */ - public List listProcesses() { - return getService().getList(fastMode); - } - - /** - * Return the list of processes that match with the provided name.
- * For each process some information is retrieved: - *

    - *
  • PID
  • - *
  • Name
  • - *
  • Used memory
  • - *
  • Date/time
  • - *
  • Priority
  • - *
- * [...]

- * - * For further details see {@link ProcessInfo} - * - * @param name The name of the searched process - * @return List of found processes - */ - public List listProcesses(String name) { - return getService().getList(name, fastMode); - } - - /** - * Convenience static method that returns the list of processes - * that match with the provided name.
- * For each process some information is retrieved: - *

    - *
  • PID
  • - *
  • Name
  • - *
  • Used memory
  • - *
  • Date/time
  • - *
  • Priority
  • - *
- * [...]

- * - * For further details see {@link ProcessInfo} - * - * @return List of found processes - */ - public static List getProcessList() { - return getService().getList(); - } - - /** - * Convenience static method that returns the list of processes - * that match with the provided name.
- * For each process some information is retrieved: - *

    - *
  • PID
  • - *
  • Name
  • - *
  • Used memory
  • - *
  • Date/time
  • - *
  • Priority
  • - *
- * [...]

- * - * For further details see {@link ProcessInfo} - * - * @param name The name of the searched process - * @return List of found processes - */ - public static List getProcessList(String name) { - return getService().getList(name); - } - - /** - * Static method that returns the information of a process
- * Some information is retrieved: - *

    - *
  • PID
  • - *
  • Name
  • - *
  • Used memory
  • - *
  • Date/time
  • - *
  • Priority
  • - *
- * [...]

- * - * For further details see {@link ProcessInfo} - * - * @param pid The PID of the searched process - * @return {@link JProcesses} object with the information of the process - */ - public static ProcessInfo getProcess(int pid) { - return getService().getProcess(pid); - } - - /** - * Static method that kills a process abruptly (forced mode)
- * - * For further details see {@link ProcessInfo} - * - * @param pid The PID of the process to kill - * @return {@link JProcessesResponse} response object that contains information about the result of the operation - */ - public static JProcessesResponse killProcess(int pid) { - return getService().killProcess(pid); - } - - /** - * Static method that kills a process, letting it the necessary time to finish
- * If the process does not finish with this method, try with the stronger killProcess()

- * - * For further details see {@link ProcessInfo} - * - * @param pid The PID of the process to kill - * @return {@link JProcessesResponse} response object that contains information about the result of the operation - */ - public static JProcessesResponse killProcessGracefully(int pid) { - return getService().killProcessGracefully(pid); - } - - /** - * Static method that changes the priority of a process
- * If the process does not finish with this method, try with the stronger killProcess()

- * - * For further details see {@link ProcessInfo} - * - * @param pid the PID of the process tochange priority - * @param newPriority integer with the new version. For windows you can use the helper constants at {@link WindowsPriority} - * @return {@link JProcessesResponse} response object that contains information about the result of the operation - */ - public static JProcessesResponse changePriority(int pid, int newPriority) { - return getService().changePriority(pid, newPriority); - } - - private static ProcessesService getService() { - return ProcessesFactory.getService(); - } -} +/* + * Copyright 2016 Javier Garcia Alonso. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jutils.jprocesses; + +import org.jutils.jprocesses.info.ProcessesFactory; +import org.jutils.jprocesses.info.ProcessesService; +import org.jutils.jprocesses.model.JProcessesResponse; +import org.jutils.jprocesses.model.ProcessInfo; +import org.jutils.jprocesses.model.WindowsPriority; + +import java.util.List; + +/** + * Class that gives access to Processes details.

+ * + * JProcesses allows to interact with system processes. It is possible to + * list all alive processes, kill them or change the priority. + * + * @author Javier Garcia Alonso + */ +public class JProcesses { + + //This mode retrieves less information but faster + private boolean fastMode = false; + + private JProcesses() { + } + + /** + * Gets the JProcesses instance.
+ * This method creates a new instance each time. + * + * @return JProcesses instance + */ + public static JProcesses get() { + return new JProcesses(); + } + + /** + * Enables the fast mode.
In this mode JProcesses does not try to retrieve + * time consuming data as the use percentage or the owner of the process in Windows implementation.
+ * This flag has no effect in Linux implementation as it is fast enough. + * + * @return JProcesses instance + */ + public JProcesses fastMode() { + this.fastMode = true; + return this; + } + + /** + * Enables/disables the fast mode.
In this mode JProcesses does not try to retrieve + * time consuming data as the use percentage or the owner of the process in Windows implementation.
+ * This flag has no effect in Linux implementation as it is fast enough. + * + * @param enabled boolean true or false + * @return JProcesses instance + */ + public JProcesses fastMode(boolean enabled) { + this.fastMode = enabled; + return this; + } + + /** + * Return the list of processes running in the system.
+ * For each process some information is retrieved: + *

    + *
  • PID
  • + *
  • Name
  • + *
  • Used memory
  • + *
  • Date/time
  • + *
  • Priority
  • + *
+ * [...]

+ * + * For further details see {@link ProcessInfo} + * + * @return List of processes + */ + public List listProcesses() { + return getService().getList(fastMode); + } + + /** + * Return the list of processes that match with the provided name.
+ * For each process some information is retrieved: + *

    + *
  • PID
  • + *
  • Name
  • + *
  • Used memory
  • + *
  • Date/time
  • + *
  • Priority
  • + *
+ * [...]

+ * + * For further details see {@link ProcessInfo} + * + * @param name The name of the searched process + * @return List of found processes + */ + public List listProcesses(String name) { + return getService().getList(name, fastMode); + } + + /** + * Convenience static method that returns the list of processes + * that match with the provided name.
+ * For each process some information is retrieved: + *

    + *
  • PID
  • + *
  • Name
  • + *
  • Used memory
  • + *
  • Date/time
  • + *
  • Priority
  • + *
+ * [...]

+ * + * For further details see {@link ProcessInfo} + * + * @return List of found processes + */ + public static List getProcessList() { + return getService().getList(); + } + + /** + * Convenience static method that returns the list of processes + * that match with the provided name.
+ * For each process some information is retrieved: + *

    + *
  • PID
  • + *
  • Name
  • + *
  • Used memory
  • + *
  • Date/time
  • + *
  • Priority
  • + *
+ * [...]

+ * + * For further details see {@link ProcessInfo} + * + * @param name The name of the searched process + * @return List of found processes + */ + public static List getProcessList(String name) { + return getService().getList(name); + } + + /** + * Static method that returns the information of a process
+ * Some information is retrieved: + *

    + *
  • PID
  • + *
  • Name
  • + *
  • Used memory
  • + *
  • Date/time
  • + *
  • Priority
  • + *
+ * [...]

+ * + * For further details see {@link ProcessInfo} + * + * @param pid The PID of the searched process + * @return {@link JProcesses} object with the information of the process + */ + public static ProcessInfo getProcess(int pid) { + return getService().getProcess(pid); + } + + /** + * Static method that kills a process abruptly (forced mode)
+ * + * For further details see {@link ProcessInfo} + * + * @param pid The PID of the process to kill + * @return {@link JProcessesResponse} response object that contains information about the result of the operation + */ + public static JProcessesResponse killProcess(int pid) { + return getService().killProcess(pid); + } + + /** + * Static method that kills a process, letting it the necessary time to finish
+ * If the process does not finish with this method, try with the stronger killProcess()

+ * + * For further details see {@link ProcessInfo} + * + * @param pid The PID of the process to kill + * @return {@link JProcessesResponse} response object that contains information about the result of the operation + */ + public static JProcessesResponse killProcessGracefully(int pid) { + return getService().killProcessGracefully(pid); + } + + /** + * Static method that changes the priority of a process
+ * If the process does not finish with this method, try with the stronger killProcess()

+ * + * For further details see {@link ProcessInfo} + * + * @param pid the PID of the process tochange priority + * @param newPriority integer with the new version. For windows you can use the helper constants at {@link WindowsPriority} + * @return {@link JProcessesResponse} response object that contains information about the result of the operation + */ + public static JProcessesResponse changePriority(int pid, int newPriority) { + return getService().changePriority(pid, newPriority); + } + + private static ProcessesService getService() { + return ProcessesFactory.getService(); + } +} diff --git a/src/main/java/org/jutils/jprocesses/info/AbstractProcessesService.java b/src/main/java/org/jutils/jprocesses/info/AbstractProcessesService.java index 3ed4fd3..4809956 100644 --- a/src/main/java/org/jutils/jprocesses/info/AbstractProcessesService.java +++ b/src/main/java/org/jutils/jprocesses/info/AbstractProcessesService.java @@ -1,96 +1,96 @@ -/* - * Copyright 2016 Javier Garcia Alonso. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jutils.jprocesses.info; - -import org.jutils.jprocesses.model.JProcessesResponse; -import org.jutils.jprocesses.model.ProcessInfo; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -/** - * Info related with processes - * - * @author Javier Garcia Alonso - */ -abstract class AbstractProcessesService implements ProcessesService { - - protected boolean fastMode = false; - - public List getList() { - return getList(null); - } - - public List getList(boolean fastMode) { - return getList(null, fastMode); - } - - public List getList(String name) { - return getList(name, false); - } - - public List getList(String name, boolean fastMode) { - this.fastMode = fastMode; - String rawData = getProcessesData(name); - - List> mapList = parseList(rawData); - - return buildInfoFromMap(mapList); - } - - public JProcessesResponse killProcess(int pid) { - return kill(pid); - } - - public JProcessesResponse killProcessGracefully(int pid) { - return killGracefully(pid); - } - - protected abstract List> parseList(String rawData); - - protected abstract String getProcessesData(String name); - - protected abstract JProcessesResponse kill(int pid); - - protected abstract JProcessesResponse killGracefully(int pid); - - private List buildInfoFromMap(List> mapList) { - List infoList = new ArrayList(); - - for (final Map map : mapList) { - ProcessInfo info = new ProcessInfo(); - info.setPid(map.get("pid")); - info.setName(map.get("proc_name")); - info.setTime(map.get("proc_time")); - info.setCommand((map.get("command") != null) ? map.get("command") : ""); - info.setCpuUsage(map.get("cpu_usage")); - info.setPhysicalMemory(map.get("physical_memory")); - info.setStartTime(map.get("start_time")); - info.setUser(map.get("user")); - info.setVirtualMemory(map.get("virtual_memory")); - info.setPriority(map.get("priority")); - info.setPpid(map.get("ppid")); - - //Adds extra data - info.setExtraData(map); - - infoList.add(info); - } - - return infoList; - } -} +/* + * Copyright 2016 Javier Garcia Alonso. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jutils.jprocesses.info; + +import org.jutils.jprocesses.model.JProcessesResponse; +import org.jutils.jprocesses.model.ProcessInfo; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * Info related with processes + * + * @author Javier Garcia Alonso + */ +abstract class AbstractProcessesService implements ProcessesService { + + protected boolean fastMode = false; + + public List getList() { + return getList(null); + } + + public List getList(boolean fastMode) { + return getList(null, fastMode); + } + + public List getList(String name) { + return getList(name, false); + } + + public List getList(String name, boolean fastMode) { + this.fastMode = fastMode; + String rawData = getProcessesData(name); + + List> mapList = parseList(rawData); + + return buildInfoFromMap(mapList); + } + + public JProcessesResponse killProcess(int pid) { + return kill(pid); + } + + public JProcessesResponse killProcessGracefully(int pid) { + return killGracefully(pid); + } + + protected abstract List> parseList(String rawData); + + protected abstract String getProcessesData(String name); + + protected abstract JProcessesResponse kill(int pid); + + protected abstract JProcessesResponse killGracefully(int pid); + + private List buildInfoFromMap(List> mapList) { + List infoList = new ArrayList(); + + for (final Map map : mapList) { + ProcessInfo info = new ProcessInfo(); + info.setPid(map.get("pid")); + info.setName(map.get("proc_name")); + info.setTime(map.get("proc_time")); + info.setCommand((map.get("command") != null) ? map.get("command") : ""); + info.setCpuUsage(map.get("cpu_usage")); + info.setPhysicalMemory(map.get("physical_memory")); + info.setStartTime(map.get("start_time")); + info.setUser(map.get("user")); + info.setVirtualMemory(map.get("virtual_memory")); + info.setPriority(map.get("priority")); + info.setPpid(map.get("ppid")); + + //Adds extra data + info.setExtraData(map); + + infoList.add(info); + } + + return infoList; + } +} diff --git a/src/main/java/org/jutils/jprocesses/info/ProcessesFactory.java b/src/main/java/org/jutils/jprocesses/info/ProcessesFactory.java index 601be3f..bc0d735 100644 --- a/src/main/java/org/jutils/jprocesses/info/ProcessesFactory.java +++ b/src/main/java/org/jutils/jprocesses/info/ProcessesFactory.java @@ -1,41 +1,41 @@ -/* - * Copyright 2016 Javier Garcia Alonso. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jutils.jprocesses.info; - -import org.jutils.jprocesses.util.OSDetector; - -/** - * Factory class to get the right information for the OS - * - * @author Javier Garcia Alonso - */ -public class ProcessesFactory { - - //Hide constructor - private ProcessesFactory() { - } - - //use getShape method to get object of type shape - public static ProcessesService getService(){ - if (OSDetector.isWindows()) { - return new WindowsProcessesService(); - } else if (OSDetector.isUnix()) { - return new UnixProcessesService(); - } - - throw new UnsupportedOperationException("Your Operating System is not supported by this library."); - } -} +/* + * Copyright 2016 Javier Garcia Alonso. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jutils.jprocesses.info; + +import org.jutils.jprocesses.util.OSDetector; + +/** + * Factory class to get the right information for the OS + * + * @author Javier Garcia Alonso + */ +public class ProcessesFactory { + + //Hide constructor + private ProcessesFactory() { + } + + //use getShape method to get object of type shape + public static ProcessesService getService(){ + if (OSDetector.isWindows()) { + return new WindowsProcessesService(); + } else if (OSDetector.isUnix()) { + return new UnixProcessesService(); + } + + throw new UnsupportedOperationException("Your Operating System is not supported by this library."); + } +} diff --git a/src/main/java/org/jutils/jprocesses/info/ProcessesService.java b/src/main/java/org/jutils/jprocesses/info/ProcessesService.java index e720d5a..f571f41 100644 --- a/src/main/java/org/jutils/jprocesses/info/ProcessesService.java +++ b/src/main/java/org/jutils/jprocesses/info/ProcessesService.java @@ -1,38 +1,38 @@ -/* - * Copyright 2016 Javier Garcia Alonso. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jutils.jprocesses.info; - -import org.jutils.jprocesses.model.JProcessesResponse; -import org.jutils.jprocesses.model.ProcessInfo; - -import java.util.List; - -/** - * Interface for service retrieving processes information - * - * @author Javier Garcia Alonso - */ -public interface ProcessesService { - List getList(); - List getList(boolean fastMode); - List getList(String name); - List getList(String name, boolean fastMode); - ProcessInfo getProcess(int pid); - ProcessInfo getProcess(int pid, boolean fastMode); - JProcessesResponse killProcess(int pid); - JProcessesResponse killProcessGracefully(int pid); - JProcessesResponse changePriority(int pid, int priority); -} +/* + * Copyright 2016 Javier Garcia Alonso. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jutils.jprocesses.info; + +import org.jutils.jprocesses.model.JProcessesResponse; +import org.jutils.jprocesses.model.ProcessInfo; + +import java.util.List; + +/** + * Interface for service retrieving processes information + * + * @author Javier Garcia Alonso + */ +public interface ProcessesService { + List getList(); + List getList(boolean fastMode); + List getList(String name); + List getList(String name, boolean fastMode); + ProcessInfo getProcess(int pid); + ProcessInfo getProcess(int pid, boolean fastMode); + JProcessesResponse killProcess(int pid); + JProcessesResponse killProcessGracefully(int pid); + JProcessesResponse changePriority(int pid, int priority); +} diff --git a/src/main/java/org/jutils/jprocesses/info/UnixProcessesService.java b/src/main/java/org/jutils/jprocesses/info/UnixProcessesService.java index 3834f91..4d1d353 100644 --- a/src/main/java/org/jutils/jprocesses/info/UnixProcessesService.java +++ b/src/main/java/org/jutils/jprocesses/info/UnixProcessesService.java @@ -1,200 +1,200 @@ -/* - * Copyright 2016 Javier Garcia Alonso. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jutils.jprocesses.info; - -import java.text.ParseException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import org.jutils.jprocesses.model.JProcessesResponse; -import org.jutils.jprocesses.model.ProcessInfo; -import org.jutils.jprocesses.util.OSDetector; -import org.jutils.jprocesses.util.ProcessesUtils; - -/** - * Service implementation for Unix/BSD systems - * - * @author Javier Garcia Alonso - */ -class UnixProcessesService extends AbstractProcessesService { - - //Use BSD sytle to get data in order to be compatible with Mac Systems(thanks to jkuharev for this tip) - private static final String PS_COLUMNS = "pid,ppid,ruser,vsize,rss,%cpu,lstart,cputime,nice,ucomm"; - private static final String PS_FULL_COMMAND = "pid,command"; - - private static final int PS_COLUMNS_SIZE = PS_COLUMNS.split(",").length; - private static final int PS_FULL_COMMAND_SIZE = PS_FULL_COMMAND.split(",").length; - - private String nameFilter = null; - - @Override - protected List> parseList(String rawData) { - List> processesDataList = new ArrayList>(); - String[] dataStringLines = rawData.split("\\r?\\n"); - - int index; - for (final String dataLine : dataStringLines) { - String line = dataLine.trim(); - if (line.startsWith("PID")) { - // skip header - } else { - // LinkedHashMap keeps the insertion order, thus easier to debug - Map element = new LinkedHashMap(); - String[] elements = line.split("\\s+", PS_COLUMNS_SIZE + 5); - index = 0; - element.put("pid", elements[index++]); - element.put("ppid", elements[index++]); - element.put("user", elements[index++]); - element.put("virtual_memory", elements[index++]); - element.put("physical_memory", elements[index++]); - element.put("cpu_usage", elements[index++]); - index++; // Skip weekday - String longDate = elements[index++] + " " - + elements[index++] + " " - + elements[index++]+ " " - + elements[index++]; - element.put("start_time", elements[index - 2]); - try { - element.put("start_datetime", - ProcessesUtils.parseUnixLongTimeToFullDate(longDate)); - } catch (ParseException e) { - element.put("start_datetime", "01/01/2000 00:00:00"); - System.err.println("Failed formatting date from ps: " + longDate + ", using \"01/01/2000 00:00:00\""); - } - element.put("proc_time", elements[index++]); - element.put("priority", elements[index++]); - element.put("proc_name", elements[index++]); - // first init full command by content of proc_name - element.put("command", elements[index - 1]); - - processesDataList.add(element); - } - } - loadFullCommandData(processesDataList); - - if (nameFilter != null) { - filterByName(processesDataList); - } - - return processesDataList; - } - - @Override - protected String getProcessesData(String name) { - if (name != null) { - if (OSDetector.isLinux()) { - return ProcessesUtils.executeCommand("ps", - "-o", PS_COLUMNS, "-C", name); - } else { - this.nameFilter = name; - } - } - return ProcessesUtils.executeCommand("ps", - "-e", "-o", PS_COLUMNS); - } - - @Override - protected JProcessesResponse kill(int pid) { - JProcessesResponse response = new JProcessesResponse(); - if (ProcessesUtils.executeCommandAndGetCode("kill", "-9", String.valueOf(pid)) == 0) { - response.setSuccess(true); - } - return response; - } - - @Override - protected JProcessesResponse killGracefully(int pid) { - JProcessesResponse response = new JProcessesResponse(); - if (ProcessesUtils.executeCommandAndGetCode("kill", "-15", String.valueOf(pid)) == 0) { - response.setSuccess(true); - } - return response; - } - - public JProcessesResponse changePriority(int pid, int priority) { - JProcessesResponse response = new JProcessesResponse(); - if (ProcessesUtils.executeCommandAndGetCode("renice", String.valueOf(priority), - "-p", String.valueOf(pid)) == 0) { - response.setSuccess(true); - } - return response; - } - - public ProcessInfo getProcess(int pid) { - return getProcess (pid, false); - } - - public ProcessInfo getProcess(int pid, boolean fastMode) { - this.fastMode = fastMode; - List> processList - = parseList(ProcessesUtils.executeCommand("ps", - "-o", PS_COLUMNS, "-p", String.valueOf(pid))); - - if (processList != null && !processList.isEmpty()) { - Map processData = processList.get(0); - ProcessInfo info = new ProcessInfo(); - info.setPid(processData.get("pid")); - info.setName(processData.get("proc_name")); - info.setTime(processData.get("proc_time")); - info.setCommand(processData.get("command")); - info.setCpuUsage(processData.get("cpu_usage")); - info.setPhysicalMemory(processData.get("physical_memory")); - info.setStartTime(processData.get("start_time")); - info.setUser(processData.get("user")); - info.setVirtualMemory(processData.get("virtual_memory")); - info.setPriority(processData.get("priority")); - - return info; - } - return null; - } - - private static void loadFullCommandData(List> processesDataList) { - Map commandsMap = new HashMap(); - String data = ProcessesUtils.executeCommand("ps", - "-e", "-o", PS_FULL_COMMAND); - String[] dataStringLines = data.split("\\r?\\n"); - - for (final String dataLine : dataStringLines) { - if (!(dataLine.trim().startsWith("PID"))) { - String[] elements = dataLine.trim().split("\\s+", PS_FULL_COMMAND_SIZE); - if (elements.length == PS_FULL_COMMAND_SIZE) { - commandsMap.put(elements[0], elements[1]); - } - } - } - - for (final Map process : processesDataList) { - if (commandsMap.containsKey(process.get("pid"))) { - process.put("command", commandsMap.get(process.get("pid"))); - } - } - } - - - private void filterByName(List> processesDataList) { - List> processesToRemove = new ArrayList>(); - for (final Map process : processesDataList) { - if (!nameFilter.equals(process.get("proc_name"))) { - processesToRemove.add(process); - } - } - processesDataList.removeAll(processesToRemove); - } -} +/* + * Copyright 2016 Javier Garcia Alonso. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jutils.jprocesses.info; + +import java.text.ParseException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.jutils.jprocesses.model.JProcessesResponse; +import org.jutils.jprocesses.model.ProcessInfo; +import org.jutils.jprocesses.util.OSDetector; +import org.jutils.jprocesses.util.ProcessesUtils; + +/** + * Service implementation for Unix/BSD systems + * + * @author Javier Garcia Alonso + */ +class UnixProcessesService extends AbstractProcessesService { + + //Use BSD sytle to get data in order to be compatible with Mac Systems(thanks to jkuharev for this tip) + private static final String PS_COLUMNS = "pid,ppid,ruser,vsize,rss,%cpu,lstart,cputime,nice,ucomm"; + private static final String PS_FULL_COMMAND = "pid,command"; + + private static final int PS_COLUMNS_SIZE = PS_COLUMNS.split(",").length; + private static final int PS_FULL_COMMAND_SIZE = PS_FULL_COMMAND.split(",").length; + + private String nameFilter = null; + + @Override + protected List> parseList(String rawData) { + List> processesDataList = new ArrayList>(); + String[] dataStringLines = rawData.split("\\r?\\n"); + + int index; + for (final String dataLine : dataStringLines) { + String line = dataLine.trim(); + if (line.startsWith("PID")) { + // skip header + } else { + // LinkedHashMap keeps the insertion order, thus easier to debug + Map element = new LinkedHashMap(); + String[] elements = line.split("\\s+", PS_COLUMNS_SIZE + 5); + index = 0; + element.put("pid", elements[index++]); + element.put("ppid", elements[index++]); + element.put("user", elements[index++]); + element.put("virtual_memory", elements[index++]); + element.put("physical_memory", elements[index++]); + element.put("cpu_usage", elements[index++]); + index++; // Skip weekday + String longDate = elements[index++] + " " + + elements[index++] + " " + + elements[index++]+ " " + + elements[index++]; + element.put("start_time", elements[index - 2]); + try { + element.put("start_datetime", + ProcessesUtils.parseUnixLongTimeToFullDate(longDate)); + } catch (ParseException e) { + element.put("start_datetime", "01/01/2000 00:00:00"); + System.err.println("Failed formatting date from ps: " + longDate + ", using \"01/01/2000 00:00:00\""); + } + element.put("proc_time", elements[index++]); + element.put("priority", elements[index++]); + element.put("proc_name", elements[index++]); + // first init full command by content of proc_name + element.put("command", elements[index - 1]); + + processesDataList.add(element); + } + } + loadFullCommandData(processesDataList); + + if (nameFilter != null) { + filterByName(processesDataList); + } + + return processesDataList; + } + + @Override + protected String getProcessesData(String name) { + if (name != null) { + if (OSDetector.isLinux()) { + return ProcessesUtils.executeCommand("ps", + "-o", PS_COLUMNS, "-C", name); + } else { + this.nameFilter = name; + } + } + return ProcessesUtils.executeCommand("ps", + "-e", "-o", PS_COLUMNS); + } + + @Override + protected JProcessesResponse kill(int pid) { + JProcessesResponse response = new JProcessesResponse(); + if (ProcessesUtils.executeCommandAndGetCode("kill", "-9", String.valueOf(pid)) == 0) { + response.setSuccess(true); + } + return response; + } + + @Override + protected JProcessesResponse killGracefully(int pid) { + JProcessesResponse response = new JProcessesResponse(); + if (ProcessesUtils.executeCommandAndGetCode("kill", "-15", String.valueOf(pid)) == 0) { + response.setSuccess(true); + } + return response; + } + + public JProcessesResponse changePriority(int pid, int priority) { + JProcessesResponse response = new JProcessesResponse(); + if (ProcessesUtils.executeCommandAndGetCode("renice", String.valueOf(priority), + "-p", String.valueOf(pid)) == 0) { + response.setSuccess(true); + } + return response; + } + + public ProcessInfo getProcess(int pid) { + return getProcess (pid, false); + } + + public ProcessInfo getProcess(int pid, boolean fastMode) { + this.fastMode = fastMode; + List> processList + = parseList(ProcessesUtils.executeCommand("ps", + "-o", PS_COLUMNS, "-p", String.valueOf(pid))); + + if (processList != null && !processList.isEmpty()) { + Map processData = processList.get(0); + ProcessInfo info = new ProcessInfo(); + info.setPid(processData.get("pid")); + info.setName(processData.get("proc_name")); + info.setTime(processData.get("proc_time")); + info.setCommand(processData.get("command")); + info.setCpuUsage(processData.get("cpu_usage")); + info.setPhysicalMemory(processData.get("physical_memory")); + info.setStartTime(processData.get("start_time")); + info.setUser(processData.get("user")); + info.setVirtualMemory(processData.get("virtual_memory")); + info.setPriority(processData.get("priority")); + + return info; + } + return null; + } + + private static void loadFullCommandData(List> processesDataList) { + Map commandsMap = new HashMap(); + String data = ProcessesUtils.executeCommand("ps", + "-e", "-o", PS_FULL_COMMAND); + String[] dataStringLines = data.split("\\r?\\n"); + + for (final String dataLine : dataStringLines) { + if (!(dataLine.trim().startsWith("PID"))) { + String[] elements = dataLine.trim().split("\\s+", PS_FULL_COMMAND_SIZE); + if (elements.length == PS_FULL_COMMAND_SIZE) { + commandsMap.put(elements[0], elements[1]); + } + } + } + + for (final Map process : processesDataList) { + if (commandsMap.containsKey(process.get("pid"))) { + process.put("command", commandsMap.get(process.get("pid"))); + } + } + } + + + private void filterByName(List> processesDataList) { + List> processesToRemove = new ArrayList>(); + for (final Map process : processesDataList) { + if (!nameFilter.equals(process.get("proc_name"))) { + processesToRemove.add(process); + } + } + processesDataList.removeAll(processesToRemove); + } +} diff --git a/src/main/java/org/jutils/jprocesses/info/VBScriptHelper.java b/src/main/java/org/jutils/jprocesses/info/VBScriptHelper.java index 00078f0..cf4c1ab 100644 --- a/src/main/java/org/jutils/jprocesses/info/VBScriptHelper.java +++ b/src/main/java/org/jutils/jprocesses/info/VBScriptHelper.java @@ -1,160 +1,160 @@ -/* - * Copyright 2016 Javier. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jutils.jprocesses.info; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStreamReader; -import java.util.Date; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Helper class of windows implementation to create and execute VBS scripts - * - * @author Javier Garcia Alonso - */ -class VBScriptHelper { - - private static final String CRLF = "\r\n"; - - //Hide constructor - private VBScriptHelper() { - } - - private static String executeScript(String scriptCode) { - String scriptResponse = ""; - File tmpFile = null; - FileWriter writer = null; - BufferedReader processOutput = null; - BufferedReader errorOutput = null; - - try { - tmpFile = File.createTempFile("wmi4java" + new Date().getTime(), ".vbs"); - writer = new FileWriter(tmpFile); - writer.write(scriptCode); - writer.flush(); - writer.close(); - - Process process = Runtime.getRuntime().exec( - new String[]{"cmd.exe", "/C", "cscript.exe", "/NoLogo", tmpFile.getAbsolutePath()}); - processOutput - = new BufferedReader(new InputStreamReader(process.getInputStream())); - String line; - while ((line = processOutput.readLine()) != null) { - if (!line.isEmpty()) { - scriptResponse += line + CRLF; - } - } - - if (scriptResponse.isEmpty()) { - errorOutput - = new BufferedReader(new InputStreamReader(process.getInputStream())); - String errorResponse = ""; - while ((line = errorOutput.readLine()) != null) { - if (!line.isEmpty()) { - errorResponse += line + CRLF; - } - } - if (!errorResponse.isEmpty()) { - Logger.getLogger(VBScriptHelper.class.getName()).log(Level.SEVERE, "WMI operation finished in error: "); - } - errorOutput.close(); - } - - } catch (Exception ex) { - Logger.getLogger(VBScriptHelper.class.getName()).log(Level.SEVERE, null, ex); - } finally { - try { - if (processOutput != null) { - processOutput.close(); - } - if (errorOutput != null) { - errorOutput.close(); - } - if (writer != null) { - writer.close(); - } - if (tmpFile != null) { - tmpFile.delete(); - } - } catch (IOException ioe) { - Logger.getLogger(VBScriptHelper.class.getName()).log(Level.SEVERE, null, ioe); - } - } - return scriptResponse.trim(); - } - - /** - * Retrieve the owner of the processes. - * - * @return list of strings composed by PID and owner, separated by : - */ - public static String getProcessesOwner() { - - try { - StringBuilder scriptCode = new StringBuilder(200); - - scriptCode.append("Set objWMIService=GetObject(\"winmgmts:{impersonationLevel=impersonate}!\\\\") - .append(".").append("/").append("root/cimv2").append("\")").append(CRLF); - - scriptCode.append("Set colProcessList = objWMIService.ExecQuery(\"Select * from Win32_Process\")").append(CRLF); - - scriptCode.append("For Each objProcess in colProcessList").append(CRLF); - scriptCode.append("colProperties = objProcess.GetOwner(strNameOfUser,strUserDomain)").append(CRLF); - scriptCode.append("Wscript.Echo objProcess.ProcessId & \":\" & strNameOfUser").append(CRLF); - scriptCode.append("Next").append(CRLF); - - return executeScript(scriptCode.toString()); - } catch (Exception ex) { - Logger.getLogger(VBScriptHelper.class.getName()).log(Level.SEVERE, null, ex); - } - - return null; - } - - /** - * Changes the priority of a process - * - * @param pid the PID - * @param newPriority the priority to set @see Windo - * @return the output of the script - */ - public static String changePriority(int pid, int newPriority) { - - try { - StringBuilder scriptCode = new StringBuilder(200); - - scriptCode.append("Set objWMIService=GetObject(\"winmgmts:{impersonationLevel=impersonate}!\\\\") - .append(".").append("/").append("root/cimv2").append("\")").append(CRLF); - - scriptCode.append("Set colProcesses = objWMIService.ExecQuery(\"Select * from Win32_Process Where ProcessId = ") - .append(pid).append("\")").append(CRLF); - - scriptCode.append("For Each objProcess in colProcesses").append(CRLF); - scriptCode.append("objProcess.SetPriority(").append(newPriority).append(")").append(CRLF); - scriptCode.append("Next").append(CRLF); - - return executeScript(scriptCode.toString()); - } catch (Exception ex) { - Logger.getLogger(VBScriptHelper.class.getName()).log(Level.SEVERE, null, ex); - } - - return null; - } -} +/* + * Copyright 2016 Javier. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jutils.jprocesses.info; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Date; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Helper class of windows implementation to create and execute VBS scripts + * + * @author Javier Garcia Alonso + */ +class VBScriptHelper { + + private static final String CRLF = "\r\n"; + + //Hide constructor + private VBScriptHelper() { + } + + private static String executeScript(String scriptCode) { + String scriptResponse = ""; + File tmpFile = null; + FileWriter writer = null; + BufferedReader processOutput = null; + BufferedReader errorOutput = null; + + try { + tmpFile = File.createTempFile("wmi4java" + new Date().getTime(), ".vbs"); + writer = new FileWriter(tmpFile); + writer.write(scriptCode); + writer.flush(); + writer.close(); + + Process process = Runtime.getRuntime().exec( + new String[]{"cmd.exe", "/C", "cscript.exe", "/NoLogo", tmpFile.getAbsolutePath()}); + processOutput + = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line; + while ((line = processOutput.readLine()) != null) { + if (!line.isEmpty()) { + scriptResponse += line + CRLF; + } + } + + if (scriptResponse.isEmpty()) { + errorOutput + = new BufferedReader(new InputStreamReader(process.getInputStream())); + String errorResponse = ""; + while ((line = errorOutput.readLine()) != null) { + if (!line.isEmpty()) { + errorResponse += line + CRLF; + } + } + if (!errorResponse.isEmpty()) { + Logger.getLogger(VBScriptHelper.class.getName()).log(Level.SEVERE, "WMI operation finished in error: "); + } + errorOutput.close(); + } + + } catch (Exception ex) { + Logger.getLogger(VBScriptHelper.class.getName()).log(Level.SEVERE, null, ex); + } finally { + try { + if (processOutput != null) { + processOutput.close(); + } + if (errorOutput != null) { + errorOutput.close(); + } + if (writer != null) { + writer.close(); + } + if (tmpFile != null) { + tmpFile.delete(); + } + } catch (IOException ioe) { + Logger.getLogger(VBScriptHelper.class.getName()).log(Level.SEVERE, null, ioe); + } + } + return scriptResponse.trim(); + } + + /** + * Retrieve the owner of the processes. + * + * @return list of strings composed by PID and owner, separated by : + */ + public static String getProcessesOwner() { + + try { + StringBuilder scriptCode = new StringBuilder(200); + + scriptCode.append("Set objWMIService=GetObject(\"winmgmts:{impersonationLevel=impersonate}!\\\\") + .append(".").append("/").append("root/cimv2").append("\")").append(CRLF); + + scriptCode.append("Set colProcessList = objWMIService.ExecQuery(\"Select * from Win32_Process\")").append(CRLF); + + scriptCode.append("For Each objProcess in colProcessList").append(CRLF); + scriptCode.append("colProperties = objProcess.GetOwner(strNameOfUser,strUserDomain)").append(CRLF); + scriptCode.append("Wscript.Echo objProcess.ProcessId & \":\" & strNameOfUser").append(CRLF); + scriptCode.append("Next").append(CRLF); + + return executeScript(scriptCode.toString()); + } catch (Exception ex) { + Logger.getLogger(VBScriptHelper.class.getName()).log(Level.SEVERE, null, ex); + } + + return null; + } + + /** + * Changes the priority of a process + * + * @param pid the PID + * @param newPriority the priority to set @see Windo + * @return the output of the script + */ + public static String changePriority(int pid, int newPriority) { + + try { + StringBuilder scriptCode = new StringBuilder(200); + + scriptCode.append("Set objWMIService=GetObject(\"winmgmts:{impersonationLevel=impersonate}!\\\\") + .append(".").append("/").append("root/cimv2").append("\")").append(CRLF); + + scriptCode.append("Set colProcesses = objWMIService.ExecQuery(\"Select * from Win32_Process Where ProcessId = ") + .append(pid).append("\")").append(CRLF); + + scriptCode.append("For Each objProcess in colProcesses").append(CRLF); + scriptCode.append("objProcess.SetPriority(").append(newPriority).append(")").append(CRLF); + scriptCode.append("Next").append(CRLF); + + return executeScript(scriptCode.toString()); + } catch (Exception ex) { + Logger.getLogger(VBScriptHelper.class.getName()).log(Level.SEVERE, null, ex); + } + + return null; + } +} diff --git a/src/main/java/org/jutils/jprocesses/info/WindowsProcessesService.java b/src/main/java/org/jutils/jprocesses/info/WindowsProcessesService.java index e286a93..8085c17 100644 --- a/src/main/java/org/jutils/jprocesses/info/WindowsProcessesService.java +++ b/src/main/java/org/jutils/jprocesses/info/WindowsProcessesService.java @@ -1,288 +1,288 @@ -/* - * Copyright 2016 Javier Garcia Alonso. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jutils.jprocesses.info; - -import com.profesorfalken.wmi4java.WMI4Java; -import com.profesorfalken.wmi4java.WMIClass; -import org.jutils.jprocesses.model.JProcessesResponse; -import org.jutils.jprocesses.model.ProcessInfo; -import org.jutils.jprocesses.util.ProcessesUtils; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * Service implementation for Windows - * - * @author Javier Garcia Alonso - */ -class WindowsProcessesService extends AbstractProcessesService { -//TODO: This Windows implementation works but it is not optimized by lack of time. -//For example, the filter by name or the search by pid is done retrieving -//all the processes searching in the returning list. -//Moreover, the information is dispersed and I had to get it from different sources (WMI classes, VBS scripts...) - - private final Map userData = new HashMap(); - private final Map cpuData = new HashMap(); - - private static final String LINE_BREAK_REGEX = "\\r?\\n"; - - private static final Map keyMap; - - private Map processMap; - - private static final String NAME_PROPNAME = "Name"; - private static final String PROCESSID_PROPNAME = "ProcessId"; - private static final String USERMODETIME_PROPNAME = "UserModeTime"; - private static final String PRIORITY_PROPNAME = "Priority"; - private static final String VIRTUALSIZE_PROPNAME = "VirtualSize"; - private static final String WORKINGSETSIZE_PROPNAME = "WorkingSetSize"; - private static final String COMMANDLINE_PROPNAME = "CommandLine"; - private static final String CREATIONDATE_PROPNAME = "CreationDate"; - private static final String CAPTION_PROPNAME = "Caption"; - private static final String PARENTID_PROPNAME = "ParentProcessId"; - - private final WMI4Java wmi4Java; - - static { - Map tmpMap = new HashMap(); - tmpMap.put(NAME_PROPNAME, "proc_name"); - tmpMap.put(PROCESSID_PROPNAME, "pid"); - tmpMap.put(USERMODETIME_PROPNAME, "proc_time"); - tmpMap.put(PRIORITY_PROPNAME, "priority"); - tmpMap.put(VIRTUALSIZE_PROPNAME, "virtual_memory"); - tmpMap.put(WORKINGSETSIZE_PROPNAME, "physical_memory"); - tmpMap.put(COMMANDLINE_PROPNAME, "command"); - tmpMap.put(CREATIONDATE_PROPNAME, "start_time"); - tmpMap.put(PARENTID_PROPNAME, "ppid"); - - keyMap = Collections.unmodifiableMap(tmpMap); - } - - public WindowsProcessesService() { - this(null); - } - - WindowsProcessesService(WMI4Java wmi4Java) { - this.wmi4Java = wmi4Java; - } - - public WMI4Java getWmi4Java() { - if (wmi4Java == null) { - return WMI4Java.get(); - } - return wmi4Java; - } - - @Override - protected List> parseList(String rawData) { - List> processesDataList = new ArrayList>(); - - String[] dataStringLines = rawData.split(LINE_BREAK_REGEX); - - for (final String dataLine : dataStringLines) { - if (dataLine.trim().length() > 0) { - processLine(dataLine, processesDataList); - } - } - - return processesDataList; - } - - private void processLine(String dataLine, List> processesDataList) { - if (dataLine.startsWith(CAPTION_PROPNAME)) { - processMap = new HashMap(); - processesDataList.add(processMap); - } - - if (processMap != null) { - String[] dataStringInfo = dataLine.split(":", 2); - processMap.put(normalizeKey(dataStringInfo[0].trim()), - normalizeValue(dataStringInfo[0].trim(), dataStringInfo[1].trim())); - - if (PROCESSID_PROPNAME.equals(dataStringInfo[0].trim())) { - processMap.put("user", userData.get(dataStringInfo[1].trim())); - processMap.put("cpu_usage", cpuData.get(dataStringInfo[1].trim())); - } - - if (CREATIONDATE_PROPNAME.equals(dataStringInfo[0].trim())) { - processMap.put("start_datetime", - ProcessesUtils.parseWindowsDateTimeToFullDate(dataStringInfo[1].trim())); - } - } - } - - @Override - protected String getProcessesData(String name) { - if (!fastMode) { - fillExtraProcessData(); - } - - if (name != null) { - return getWmi4Java().VBSEngine() - .properties(Arrays.asList(CAPTION_PROPNAME, PROCESSID_PROPNAME, NAME_PROPNAME, - USERMODETIME_PROPNAME, COMMANDLINE_PROPNAME, - WORKINGSETSIZE_PROPNAME, CREATIONDATE_PROPNAME, - VIRTUALSIZE_PROPNAME, PRIORITY_PROPNAME, PARENTID_PROPNAME)) - .filters(Collections.singletonList("Name like '%" + name + "%'")) - .getRawWMIObjectOutput(WMIClass.WIN32_PROCESS); - } - - return getWmi4Java().VBSEngine().getRawWMIObjectOutput(WMIClass.WIN32_PROCESS); - } - - @Override - protected JProcessesResponse kill(int pid) { - JProcessesResponse response = new JProcessesResponse(); - if (ProcessesUtils.executeCommandAndGetCode("taskkill", "/PID", String.valueOf(pid), "/F") == 0) { - response.setSuccess(true); - } - - return response; - } - - @Override - protected JProcessesResponse killGracefully(int pid) { - JProcessesResponse response = new JProcessesResponse(); - if (ProcessesUtils.executeCommandAndGetCode("taskkill", "/PID", String.valueOf(pid)) == 0) { - response.setSuccess(true); - } - - return response; - } - - private static String normalizeKey(String origKey) { - return keyMap.get(origKey); - } - - private static String normalizeValue(String origKey, String origValue) { - if (USERMODETIME_PROPNAME.equals(origKey)) { - //100 nano to second - https://msdn.microsoft.com/en-us/library/windows/desktop/aa394372(v=vs.85).aspx - long seconds = Long.parseLong(origValue) * 100 / 1000000 / 1000; - return nomalizeTime(seconds); - } - if (VIRTUALSIZE_PROPNAME.equals(origKey) || WORKINGSETSIZE_PROPNAME.equals(origKey)) { - if (!(origValue.isEmpty())) { - return String.valueOf(Long.parseLong(origValue) / 1024); - } - } - if (CREATIONDATE_PROPNAME.equals(origKey)) { - return ProcessesUtils.parseWindowsDateTimeToSimpleTime(origValue); - } - - return origValue; - } - - private static String nomalizeTime(long seconds) { - long hours = seconds / 3600; - long minutes = (seconds % 3600) / 60; - - return String.format("%02d:%02d:%02d", hours, minutes, seconds); - } - - private void fillExtraProcessData() { - String perfData = getWmi4Java().VBSEngine().getRawWMIObjectOutput(WMIClass.WIN32_PERFFORMATTEDDATA_PERFPROC_PROCESS); - - String[] dataStringLines = perfData.split(LINE_BREAK_REGEX); - String pid = null; - String cpuUsage = null; - for (final String dataLine : dataStringLines) { - - if (dataLine.trim().length() > 0) { - if (dataLine.startsWith(CAPTION_PROPNAME)) { - if (pid != null && cpuUsage != null) { - cpuData.put(pid, cpuUsage); - pid = null; - cpuUsage = null; - } - continue; - } - - if (pid == null) { - pid = checkAndGetDataInLine("IDProcess", dataLine); - } - if (cpuUsage == null) { - cpuUsage = checkAndGetDataInLine("PercentProcessorTime", dataLine); - } - } - } - - String processesData = VBScriptHelper.getProcessesOwner(); - - if (processesData != null) { - dataStringLines = processesData.split(LINE_BREAK_REGEX); - for (final String dataLine : dataStringLines) { - String[] dataStringInfo = dataLine.split(":", 2); - if (dataStringInfo.length == 2) { - userData.put(dataStringInfo[0].trim(), dataStringInfo[1].trim()); - } - } - } - } - - private static String checkAndGetDataInLine(String dataName, String dataLine) { - if (dataLine.startsWith(dataName)) { - String[] dataStringInfo = dataLine.split(":"); - if (dataStringInfo.length == 2) { - return dataStringInfo[1].trim(); - } - } - return null; - } - - public JProcessesResponse changePriority(int pid, int priority) { - JProcessesResponse response = new JProcessesResponse(); - String message = VBScriptHelper.changePriority(pid, priority); - if (message == null || message.length() == 0) { - response.setSuccess(true); - } else { - response.setMessage(message); - } - return response; - } - - public ProcessInfo getProcess(int pid) { - return getProcess(pid, false); - } - - public ProcessInfo getProcess(int pid, boolean fastMode) { - this.fastMode = fastMode; - List> allProcesses = parseList(getProcessesData(null)); - - for (final Map process : allProcesses) { - if (String.valueOf(pid).equals(process.get("pid"))) { - ProcessInfo info = new ProcessInfo(); - info.setPid(process.get("pid")); - info.setName(process.get("proc_name")); - info.setTime(process.get("proc_time")); - info.setCommand(process.get("command")); - info.setCpuUsage(process.get("cpu_usage")); - info.setPhysicalMemory(process.get("physical_memory")); - info.setStartTime(process.get("start_time")); - info.setUser(process.get("user")); - info.setVirtualMemory(process.get("virtual_memory")); - info.setPriority(process.get("priority")); - - return info; - } - } - return null; - } -} +/* + * Copyright 2016 Javier Garcia Alonso. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jutils.jprocesses.info; + +import com.profesorfalken.wmi4java.WMI4Java; +import com.profesorfalken.wmi4java.WMIClass; +import org.jutils.jprocesses.model.JProcessesResponse; +import org.jutils.jprocesses.model.ProcessInfo; +import org.jutils.jprocesses.util.ProcessesUtils; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Service implementation for Windows + * + * @author Javier Garcia Alonso + */ +class WindowsProcessesService extends AbstractProcessesService { +//TODO: This Windows implementation works but it is not optimized by lack of time. +//For example, the filter by name or the search by pid is done retrieving +//all the processes searching in the returning list. +//Moreover, the information is dispersed and I had to get it from different sources (WMI classes, VBS scripts...) + + private final Map userData = new HashMap(); + private final Map cpuData = new HashMap(); + + private static final String LINE_BREAK_REGEX = "\\r?\\n"; + + private static final Map keyMap; + + private Map processMap; + + private static final String NAME_PROPNAME = "Name"; + private static final String PROCESSID_PROPNAME = "ProcessId"; + private static final String USERMODETIME_PROPNAME = "UserModeTime"; + private static final String PRIORITY_PROPNAME = "Priority"; + private static final String VIRTUALSIZE_PROPNAME = "VirtualSize"; + private static final String WORKINGSETSIZE_PROPNAME = "WorkingSetSize"; + private static final String COMMANDLINE_PROPNAME = "CommandLine"; + private static final String CREATIONDATE_PROPNAME = "CreationDate"; + private static final String CAPTION_PROPNAME = "Caption"; + private static final String PARENTID_PROPNAME = "ParentProcessId"; + + private final WMI4Java wmi4Java; + + static { + Map tmpMap = new HashMap(); + tmpMap.put(NAME_PROPNAME, "proc_name"); + tmpMap.put(PROCESSID_PROPNAME, "pid"); + tmpMap.put(USERMODETIME_PROPNAME, "proc_time"); + tmpMap.put(PRIORITY_PROPNAME, "priority"); + tmpMap.put(VIRTUALSIZE_PROPNAME, "virtual_memory"); + tmpMap.put(WORKINGSETSIZE_PROPNAME, "physical_memory"); + tmpMap.put(COMMANDLINE_PROPNAME, "command"); + tmpMap.put(CREATIONDATE_PROPNAME, "start_time"); + tmpMap.put(PARENTID_PROPNAME, "ppid"); + + keyMap = Collections.unmodifiableMap(tmpMap); + } + + public WindowsProcessesService() { + this(null); + } + + WindowsProcessesService(WMI4Java wmi4Java) { + this.wmi4Java = wmi4Java; + } + + public WMI4Java getWmi4Java() { + if (wmi4Java == null) { + return WMI4Java.get(); + } + return wmi4Java; + } + + @Override + protected List> parseList(String rawData) { + List> processesDataList = new ArrayList>(); + + String[] dataStringLines = rawData.split(LINE_BREAK_REGEX); + + for (final String dataLine : dataStringLines) { + if (dataLine.trim().length() > 0) { + processLine(dataLine, processesDataList); + } + } + + return processesDataList; + } + + private void processLine(String dataLine, List> processesDataList) { + if (dataLine.startsWith(CAPTION_PROPNAME)) { + processMap = new HashMap(); + processesDataList.add(processMap); + } + + if (processMap != null) { + String[] dataStringInfo = dataLine.split(":", 2); + processMap.put(normalizeKey(dataStringInfo[0].trim()), + normalizeValue(dataStringInfo[0].trim(), dataStringInfo[1].trim())); + + if (PROCESSID_PROPNAME.equals(dataStringInfo[0].trim())) { + processMap.put("user", userData.get(dataStringInfo[1].trim())); + processMap.put("cpu_usage", cpuData.get(dataStringInfo[1].trim())); + } + + if (CREATIONDATE_PROPNAME.equals(dataStringInfo[0].trim())) { + processMap.put("start_datetime", + ProcessesUtils.parseWindowsDateTimeToFullDate(dataStringInfo[1].trim())); + } + } + } + + @Override + protected String getProcessesData(String name) { + if (!fastMode) { + fillExtraProcessData(); + } + + if (name != null) { + return getWmi4Java().VBSEngine() + .properties(Arrays.asList(CAPTION_PROPNAME, PROCESSID_PROPNAME, NAME_PROPNAME, + USERMODETIME_PROPNAME, COMMANDLINE_PROPNAME, + WORKINGSETSIZE_PROPNAME, CREATIONDATE_PROPNAME, + VIRTUALSIZE_PROPNAME, PRIORITY_PROPNAME, PARENTID_PROPNAME)) + .filters(Collections.singletonList("Name like '%" + name + "%'")) + .getRawWMIObjectOutput(WMIClass.WIN32_PROCESS); + } + + return getWmi4Java().VBSEngine().getRawWMIObjectOutput(WMIClass.WIN32_PROCESS); + } + + @Override + protected JProcessesResponse kill(int pid) { + JProcessesResponse response = new JProcessesResponse(); + if (ProcessesUtils.executeCommandAndGetCode("taskkill", "/PID", String.valueOf(pid), "/F") == 0) { + response.setSuccess(true); + } + + return response; + } + + @Override + protected JProcessesResponse killGracefully(int pid) { + JProcessesResponse response = new JProcessesResponse(); + if (ProcessesUtils.executeCommandAndGetCode("taskkill", "/PID", String.valueOf(pid)) == 0) { + response.setSuccess(true); + } + + return response; + } + + private static String normalizeKey(String origKey) { + return keyMap.get(origKey); + } + + private static String normalizeValue(String origKey, String origValue) { + if (USERMODETIME_PROPNAME.equals(origKey)) { + //100 nano to second - https://msdn.microsoft.com/en-us/library/windows/desktop/aa394372(v=vs.85).aspx + long seconds = Long.parseLong(origValue) * 100 / 1000000 / 1000; + return nomalizeTime(seconds); + } + if (VIRTUALSIZE_PROPNAME.equals(origKey) || WORKINGSETSIZE_PROPNAME.equals(origKey)) { + if (!(origValue.isEmpty())) { + return String.valueOf(Long.parseLong(origValue) / 1024); + } + } + if (CREATIONDATE_PROPNAME.equals(origKey)) { + return ProcessesUtils.parseWindowsDateTimeToSimpleTime(origValue); + } + + return origValue; + } + + private static String nomalizeTime(long seconds) { + long hours = seconds / 3600; + long minutes = (seconds % 3600) / 60; + + return String.format("%02d:%02d:%02d", hours, minutes, seconds); + } + + private void fillExtraProcessData() { + String perfData = getWmi4Java().VBSEngine().getRawWMIObjectOutput(WMIClass.WIN32_PERFFORMATTEDDATA_PERFPROC_PROCESS); + + String[] dataStringLines = perfData.split(LINE_BREAK_REGEX); + String pid = null; + String cpuUsage = null; + for (final String dataLine : dataStringLines) { + + if (dataLine.trim().length() > 0) { + if (dataLine.startsWith(CAPTION_PROPNAME)) { + if (pid != null && cpuUsage != null) { + cpuData.put(pid, cpuUsage); + pid = null; + cpuUsage = null; + } + continue; + } + + if (pid == null) { + pid = checkAndGetDataInLine("IDProcess", dataLine); + } + if (cpuUsage == null) { + cpuUsage = checkAndGetDataInLine("PercentProcessorTime", dataLine); + } + } + } + + String processesData = VBScriptHelper.getProcessesOwner(); + + if (processesData != null) { + dataStringLines = processesData.split(LINE_BREAK_REGEX); + for (final String dataLine : dataStringLines) { + String[] dataStringInfo = dataLine.split(":", 2); + if (dataStringInfo.length == 2) { + userData.put(dataStringInfo[0].trim(), dataStringInfo[1].trim()); + } + } + } + } + + private static String checkAndGetDataInLine(String dataName, String dataLine) { + if (dataLine.startsWith(dataName)) { + String[] dataStringInfo = dataLine.split(":"); + if (dataStringInfo.length == 2) { + return dataStringInfo[1].trim(); + } + } + return null; + } + + public JProcessesResponse changePriority(int pid, int priority) { + JProcessesResponse response = new JProcessesResponse(); + String message = VBScriptHelper.changePriority(pid, priority); + if (message == null || message.length() == 0) { + response.setSuccess(true); + } else { + response.setMessage(message); + } + return response; + } + + public ProcessInfo getProcess(int pid) { + return getProcess(pid, false); + } + + public ProcessInfo getProcess(int pid, boolean fastMode) { + this.fastMode = fastMode; + List> allProcesses = parseList(getProcessesData(null)); + + for (final Map process : allProcesses) { + if (String.valueOf(pid).equals(process.get("pid"))) { + ProcessInfo info = new ProcessInfo(); + info.setPid(process.get("pid")); + info.setName(process.get("proc_name")); + info.setTime(process.get("proc_time")); + info.setCommand(process.get("command")); + info.setCpuUsage(process.get("cpu_usage")); + info.setPhysicalMemory(process.get("physical_memory")); + info.setStartTime(process.get("start_time")); + info.setUser(process.get("user")); + info.setVirtualMemory(process.get("virtual_memory")); + info.setPriority(process.get("priority")); + + return info; + } + } + return null; + } +} diff --git a/src/main/java/org/jutils/jprocesses/model/JProcessesResponse.java b/src/main/java/org/jutils/jprocesses/model/JProcessesResponse.java index 00bdd96..2a838a4 100644 --- a/src/main/java/org/jutils/jprocesses/model/JProcessesResponse.java +++ b/src/main/java/org/jutils/jprocesses/model/JProcessesResponse.java @@ -1,44 +1,44 @@ -/* - * Copyright 2016 Javier Garcia Alonso - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jutils.jprocesses.model; - -/** - * Encapsulates the response of a JProcesses operation - * - * @author Javier Garcia Alonso - */ -public class JProcessesResponse { - private boolean success; - private String message; - - public boolean isSuccess() { - return success; - } - - public void setSuccess(boolean success) { - this.success = success; - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } - - -} +/* + * Copyright 2016 Javier Garcia Alonso + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jutils.jprocesses.model; + +/** + * Encapsulates the response of a JProcesses operation + * + * @author Javier Garcia Alonso + */ +public class JProcessesResponse { + private boolean success; + private String message; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + +} diff --git a/src/main/java/org/jutils/jprocesses/model/WindowsPriority.java b/src/main/java/org/jutils/jprocesses/model/WindowsPriority.java index e7ee221..e167a5a 100644 --- a/src/main/java/org/jutils/jprocesses/model/WindowsPriority.java +++ b/src/main/java/org/jutils/jprocesses/model/WindowsPriority.java @@ -1,34 +1,34 @@ -/* - * Copyright 2016 Javier Garcia Alonso. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jutils.jprocesses.model; - -/** - * Helper constants class to set different priority levels in Windows - * - * @author Javier Garcia Alonso - */ -public class WindowsPriority { - - public static final int IDLE = 64; - public static final int BELOW_NORMAL = 16384; - public static final int NORMAL = 32; - public static final int ABOVE_NORMAL = 32768; - public static final int HIGH = 128; - public static final int REAL_TIME = 258; - - //Hide constructor - private WindowsPriority () {} -} +/* + * Copyright 2016 Javier Garcia Alonso. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jutils.jprocesses.model; + +/** + * Helper constants class to set different priority levels in Windows + * + * @author Javier Garcia Alonso + */ +public class WindowsPriority { + + public static final int IDLE = 64; + public static final int BELOW_NORMAL = 16384; + public static final int NORMAL = 32; + public static final int ABOVE_NORMAL = 32768; + public static final int HIGH = 128; + public static final int REAL_TIME = 258; + + //Hide constructor + private WindowsPriority () {} +} diff --git a/src/main/java/org/jutils/jprocesses/util/OSDetector.java b/src/main/java/org/jutils/jprocesses/util/OSDetector.java index 1e9ad57..b75090d 100644 --- a/src/main/java/org/jutils/jprocesses/util/OSDetector.java +++ b/src/main/java/org/jutils/jprocesses/util/OSDetector.java @@ -1,50 +1,50 @@ -/* - * Copyright 2016 Javier Garcia Alonso. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jutils.jprocesses.util; - -/** - * Detects used OS - * - * @author Javier Garcia Alonso - */ -public class OSDetector { - - private static final String OS = System.getProperty("os.name").toLowerCase(); - - //Hide constructor - private OSDetector() { - } - - public static boolean isWindows() { - return OS.contains("win"); - } - - public static boolean isMac() { - return OS.contains("mac"); - } - - public static boolean isUnix() { - return OS.contains("nix") || OS.contains("nux") || OS.contains("aix") || OS.matches( "mac.*os.*x" ); - } - - public static boolean isLinux() { - return OS.contains("linux"); - } - - public static boolean isSolaris() { - return OS.contains("sunos"); - } -} +/* + * Copyright 2016 Javier Garcia Alonso. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jutils.jprocesses.util; + +/** + * Detects used OS + * + * @author Javier Garcia Alonso + */ +public class OSDetector { + + private static final String OS = System.getProperty("os.name").toLowerCase(); + + //Hide constructor + private OSDetector() { + } + + public static boolean isWindows() { + return OS.contains("win"); + } + + public static boolean isMac() { + return OS.contains("mac"); + } + + public static boolean isUnix() { + return OS.contains("nix") || OS.contains("nux") || OS.contains("aix") || OS.matches( "mac.*os.*x" ); + } + + public static boolean isLinux() { + return OS.contains("linux"); + } + + public static boolean isSolaris() { + return OS.contains("sunos"); + } +} diff --git a/src/main/java/org/jutils/jprocesses/util/ProcessesUtils.java b/src/main/java/org/jutils/jprocesses/util/ProcessesUtils.java index a0b4e51..2ca8832 100644 --- a/src/main/java/org/jutils/jprocesses/util/ProcessesUtils.java +++ b/src/main/java/org/jutils/jprocesses/util/ProcessesUtils.java @@ -1,222 +1,222 @@ -/* - * Copyright 2016 Javier Garcia Alonso. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jutils.jprocesses.util; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.text.DateFormat; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Locale; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Utility methods - * - * @author Javier Garcia Alonso - */ -@SuppressWarnings("Since15") -public class ProcessesUtils { - - private static final String CRLF = "\r\n"; - private static String customDateFormat; - private static Locale customLocale; - - - //Hide constructor - private ProcessesUtils() { - } - - public static String executeCommand(String... command) { - String commandOutput = null; - - try { - ProcessBuilder processBuilder = new ProcessBuilder(command); - processBuilder.redirectErrorStream(true); // redirect error stream to output stream - - commandOutput = readData(processBuilder.start()); - } catch (IOException ex) { - commandOutput = ""; - Logger.getLogger(ProcessesUtils.class.getName()).log(Level.SEVERE, "Error executing command", ex); - ex.printStackTrace(); - } - - return commandOutput; - } - - private static String readData(Process process) { - StringBuilder commandOutput = new StringBuilder(); - BufferedReader processOutput = null; - - try { - processOutput = new BufferedReader(new InputStreamReader(process.getInputStream())); - - String line; - while ((line = processOutput.readLine()) != null) { - if (!line.isEmpty()) { - commandOutput.append(line).append(CRLF); - } - } - } catch (IOException ex) { - Logger.getLogger(ProcessesUtils.class.getName()).log(Level.SEVERE, "Error reading data", ex); - } finally { - try { - if (processOutput != null) { - processOutput.close(); - } - } catch (IOException ioe) { - Logger.getLogger(ProcessesUtils.class.getName()).log(Level.SEVERE, "Error closing reader", ioe); - } - } - - return commandOutput.toString(); - } - - public static int executeCommandAndGetCode(String... command) { - Process process; - - try { - process = Runtime.getRuntime().exec(command); - process.waitFor(); - } catch (IOException ex) { - Logger.getLogger(ProcessesUtils.class.getName()).log(Level.SEVERE, null, ex); - return -1; - } catch (InterruptedException ex) { - Logger.getLogger(ProcessesUtils.class.getName()).log(Level.SEVERE, null, ex); - return -1; - } - - return process.exitValue(); - } - - /** - * Parse Window DateTime format to time format (hh:mm:ss). - * - * @param dateTime original datetime format - * @see - * - * https://msdn.microsoft.com/fr-fr/library/windows/desktop/aa387237(v=vs.85).aspx - * - * @return string with formatted time (hh:mm:ss) - */ - public static String parseWindowsDateTimeToSimpleTime(String dateTime) { - String returnedDate = dateTime; - if (dateTime != null && !dateTime.isEmpty()) { - String hour = dateTime.substring(8, 10); - String minutes = dateTime.substring(10, 12); - String seconds = dateTime.substring(12, 14); - - returnedDate = hour + ":" + minutes + ":" + seconds; - } - return returnedDate; - } - - /** - * Parse Window DateTime format to time format (MM/dd/yyyy hh:mm:ss) - * - * @param dateTime original datetime format - * @see - * - * https://msdn.microsoft.com/fr-fr/library/windows/desktop/aa387237(v=vs.85).aspx - * - * @return string with formatted time (mm/dd/yyyy HH:mm:ss) - */ - public static String parseWindowsDateTimeToFullDate(String dateTime) { - String returnedDate = dateTime; - if (dateTime != null && !dateTime.isEmpty()) { - String year = dateTime.substring(0, 4); - String month = dateTime.substring(4, 6); - String day = dateTime.substring(6, 8); - String hour = dateTime.substring(8, 10); - String minutes = dateTime.substring(10, 12); - String seconds = dateTime.substring(12, 14); - - returnedDate = month + "/" + day + "/" + year + " " + hour + ":" - + minutes + ":" + seconds; - } - return returnedDate; - } - - /** - * Parse Unix long date format(ex: Fri Jun 10 04:35:36 2016) to format - * MM/dd/yyyy HH:mm:ss - * - * @param longFormatDate original datetime format - * - * @return string with formatted date and time (mm/dd/yyyy HH:mm:ss) - */ - public static String parseUnixLongTimeToFullDate(String longFormatDate) throws ParseException { - DateFormat targetFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); - List formatsToTry = new ArrayList(); - formatsToTry.addAll(Arrays.asList("MMM dd HH:mm:ss yyyy", "dd MMM HH:mm:ss yyyy")); - List localesToTry = new ArrayList(); - localesToTry.addAll(Arrays.asList(Locale.getDefault(), - Locale.getDefault(Locale.Category.FORMAT), - Locale.ENGLISH) - ); - if (getCustomDateFormat() != null) { - formatsToTry.add(0, getCustomDateFormat()); - } - if (getCustomLocale() != null) { - localesToTry.add(0, getCustomLocale()); - } - - ParseException lastException = null; - for (Locale locale : localesToTry) { - for (String format : formatsToTry) { - DateFormat originalFormat = new SimpleDateFormat(format, locale); - try { - return targetFormat.format(originalFormat.parse(longFormatDate)); - } catch (ParseException ex) { - lastException = ex; - } - } - } - throw lastException; - } - - public static String getCustomDateFormat() { - return customDateFormat; - } - - /** - * Custom date format to be used when parsing date string in "ps" output
- * NOTE: We assume 5 space separated fields for date, where we pass the last 4 to the parser, e.g. - * for input text søn 23 okt 08:30:00 2016 we would send 23 okt 08:30:00 2016 - * to the parser, so a pattern dd MMM HH:mm:ss yyyy would work. - * @param dateFormat the custom date format string to use - */ - public static void setCustomDateFormat(String dateFormat) { - customDateFormat = dateFormat; - } - - public static Locale getCustomLocale() { - return customLocale; - } - - /** - * Sets a custom locale if the defaults won't parse your ps output - * @param customLocale the Locale object to use - */ - public static void setCustomLocale(Locale customLocale) { - ProcessesUtils.customLocale = customLocale; - } -} +/* + * Copyright 2016 Javier Garcia Alonso. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jutils.jprocesses.util; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Locale; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Utility methods + * + * @author Javier Garcia Alonso + */ +@SuppressWarnings("Since15") +public class ProcessesUtils { + + private static final String CRLF = "\r\n"; + private static String customDateFormat; + private static Locale customLocale; + + + //Hide constructor + private ProcessesUtils() { + } + + public static String executeCommand(String... command) { + String commandOutput = null; + + try { + ProcessBuilder processBuilder = new ProcessBuilder(command); + processBuilder.redirectErrorStream(true); // redirect error stream to output stream + + commandOutput = readData(processBuilder.start()); + } catch (IOException ex) { + commandOutput = ""; + Logger.getLogger(ProcessesUtils.class.getName()).log(Level.SEVERE, "Error executing command", ex); + ex.printStackTrace(); + } + + return commandOutput; + } + + private static String readData(Process process) { + StringBuilder commandOutput = new StringBuilder(); + BufferedReader processOutput = null; + + try { + processOutput = new BufferedReader(new InputStreamReader(process.getInputStream())); + + String line; + while ((line = processOutput.readLine()) != null) { + if (!line.isEmpty()) { + commandOutput.append(line).append(CRLF); + } + } + } catch (IOException ex) { + Logger.getLogger(ProcessesUtils.class.getName()).log(Level.SEVERE, "Error reading data", ex); + } finally { + try { + if (processOutput != null) { + processOutput.close(); + } + } catch (IOException ioe) { + Logger.getLogger(ProcessesUtils.class.getName()).log(Level.SEVERE, "Error closing reader", ioe); + } + } + + return commandOutput.toString(); + } + + public static int executeCommandAndGetCode(String... command) { + Process process; + + try { + process = Runtime.getRuntime().exec(command); + process.waitFor(); + } catch (IOException ex) { + Logger.getLogger(ProcessesUtils.class.getName()).log(Level.SEVERE, null, ex); + return -1; + } catch (InterruptedException ex) { + Logger.getLogger(ProcessesUtils.class.getName()).log(Level.SEVERE, null, ex); + return -1; + } + + return process.exitValue(); + } + + /** + * Parse Window DateTime format to time format (hh:mm:ss). + * + * @param dateTime original datetime format + * @see + * + * https://msdn.microsoft.com/fr-fr/library/windows/desktop/aa387237(v=vs.85).aspx + * + * @return string with formatted time (hh:mm:ss) + */ + public static String parseWindowsDateTimeToSimpleTime(String dateTime) { + String returnedDate = dateTime; + if (dateTime != null && !dateTime.isEmpty()) { + String hour = dateTime.substring(8, 10); + String minutes = dateTime.substring(10, 12); + String seconds = dateTime.substring(12, 14); + + returnedDate = hour + ":" + minutes + ":" + seconds; + } + return returnedDate; + } + + /** + * Parse Window DateTime format to time format (MM/dd/yyyy hh:mm:ss) + * + * @param dateTime original datetime format + * @see + * + * https://msdn.microsoft.com/fr-fr/library/windows/desktop/aa387237(v=vs.85).aspx + * + * @return string with formatted time (mm/dd/yyyy HH:mm:ss) + */ + public static String parseWindowsDateTimeToFullDate(String dateTime) { + String returnedDate = dateTime; + if (dateTime != null && !dateTime.isEmpty()) { + String year = dateTime.substring(0, 4); + String month = dateTime.substring(4, 6); + String day = dateTime.substring(6, 8); + String hour = dateTime.substring(8, 10); + String minutes = dateTime.substring(10, 12); + String seconds = dateTime.substring(12, 14); + + returnedDate = month + "/" + day + "/" + year + " " + hour + ":" + + minutes + ":" + seconds; + } + return returnedDate; + } + + /** + * Parse Unix long date format(ex: Fri Jun 10 04:35:36 2016) to format + * MM/dd/yyyy HH:mm:ss + * + * @param longFormatDate original datetime format + * + * @return string with formatted date and time (mm/dd/yyyy HH:mm:ss) + */ + public static String parseUnixLongTimeToFullDate(String longFormatDate) throws ParseException { + DateFormat targetFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); + List formatsToTry = new ArrayList(); + formatsToTry.addAll(Arrays.asList("MMM dd HH:mm:ss yyyy", "dd MMM HH:mm:ss yyyy")); + List localesToTry = new ArrayList(); + localesToTry.addAll(Arrays.asList(Locale.getDefault(), + Locale.getDefault(Locale.Category.FORMAT), + Locale.ENGLISH) + ); + if (getCustomDateFormat() != null) { + formatsToTry.add(0, getCustomDateFormat()); + } + if (getCustomLocale() != null) { + localesToTry.add(0, getCustomLocale()); + } + + ParseException lastException = null; + for (Locale locale : localesToTry) { + for (String format : formatsToTry) { + DateFormat originalFormat = new SimpleDateFormat(format, locale); + try { + return targetFormat.format(originalFormat.parse(longFormatDate)); + } catch (ParseException ex) { + lastException = ex; + } + } + } + throw lastException; + } + + public static String getCustomDateFormat() { + return customDateFormat; + } + + /** + * Custom date format to be used when parsing date string in "ps" output
+ * NOTE: We assume 5 space separated fields for date, where we pass the last 4 to the parser, e.g. + * for input text søn 23 okt 08:30:00 2016 we would send 23 okt 08:30:00 2016 + * to the parser, so a pattern dd MMM HH:mm:ss yyyy would work. + * @param dateFormat the custom date format string to use + */ + public static void setCustomDateFormat(String dateFormat) { + customDateFormat = dateFormat; + } + + public static Locale getCustomLocale() { + return customLocale; + } + + /** + * Sets a custom locale if the defaults won't parse your ps output + * @param customLocale the Locale object to use + */ + public static void setCustomLocale(Locale customLocale) { + ProcessesUtils.customLocale = customLocale; + } +} diff --git a/src/test/java/org/jutils/jprocesses/JProcessesTest.java b/src/test/java/org/jutils/jprocesses/JProcessesTest.java index 0cfd69c..b343e47 100644 --- a/src/test/java/org/jutils/jprocesses/JProcessesTest.java +++ b/src/test/java/org/jutils/jprocesses/JProcessesTest.java @@ -1,148 +1,148 @@ -package org.jutils.jprocesses; - -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.jutils.jprocesses.model.ProcessInfo; -import org.jutils.jprocesses.model.WindowsPriority; -import org.jutils.jprocesses.util.OSDetector; - -import java.util.ArrayList; -import java.util.List; - -import static org.junit.Assert.assertTrue; - -/** - * - * @author javier - */ -public class JProcessesTest { - - public JProcessesTest() { - } - - @BeforeClass - public static void setUpClass() { - } - - @AfterClass - public static void tearDownClass() { - } - - @Before - public void setUp() { - } - - @After - public void tearDown() { - } - - /** - * Test of getProcessList method, of class JProcesses. - */ - @Test - public void testGetProcessList() { - System.out.println("===============Testing getProcessList============"); - List processesList = JProcesses.get().fastMode().listProcesses(); - - assertTrue(processesList != null && processesList.size() > 0); - - for (final ProcessInfo processInfo : processesList) { - System.out.println("Process PID: " + processInfo.getPid()); - System.out.println("Process Name: " + processInfo.getName()); - System.out.println("Process Time: " + processInfo.getTime()); - System.out.println("User: " + processInfo.getUser()); - System.out.println("Virtual Memory: " + processInfo.getVirtualMemory()); - System.out.println("Physical Memory: " + processInfo.getPhysicalMemory()); - System.out.println("CPU usage: " + processInfo.getCpuUsage()); - System.out.println("Start Time: " + processInfo.getStartTime()); - System.out.println("Start DateTime: " - + processInfo.getExtraData().get("start_datetime")); - System.out.println("Priority: " + processInfo.getPriority()); - System.out.println("Full command: " + processInfo.getCommand()); - System.out.println("------------------"); - } - System.out.println("===============End test getProcessList============"); - } - - /** - * Test of getProcessList method by name, of class JProcesses. - */ - @Test - public void testGetProcessListByName() { - System.out.println("===============Testing getProcessList by name============"); - //JProcesses.fastMode = true; - String processToSearch = "java"; - if (OSDetector.isWindows()) { - processToSearch += ".exe"; - } - - List processesList = JProcesses.getProcessList(processToSearch); - - assertTrue(processesList != null && processesList.size() > 0); - - for (final ProcessInfo processInfo : processesList) { - System.out.println("Process PID: " + processInfo.getPid()); - System.out.println("Process Name: " + processInfo.getName()); - System.out.println("Process Time: " + processInfo.getTime()); - System.out.println("User: " + processInfo.getUser()); - System.out.println("Virtual Memory: " + processInfo.getVirtualMemory()); - System.out.println("Physical Memory: " + processInfo.getPhysicalMemory()); - System.out.println("CPU usage: " + processInfo.getCpuUsage()); - System.out.println("Start Time: " + processInfo.getStartTime()); - System.out.println("Priority: " + processInfo.getPriority()); - System.out.println("Command: " + processInfo.getCommand()); - System.out.println("------------------"); - } - - //Compare list with a manually retrieved list - List processesListFull = JProcesses.getProcessList(); - List processesListFound = new ArrayList(); - for (final ProcessInfo process : processesListFull) { - if (processToSearch.equals(process.getName())) { - processesListFound.add(process); - } - } - - assertTrue("Manually list differs from search founded " - + processesListFound.size() + " instead of " + processesList.size(), - processesList.size() == processesListFound.size() - ); - - System.out.println("===============End test getProcessList by name============"); - } - - /** - * Test of getProcessList method by name, of class JProcesses. - */ - //@Test - public void testKill() { - System.out.println("===============Testing killProcess============"); - boolean success = JProcesses.killProcess(3844).isSuccess(); - - System.out.println("===============End test killProcess============"); - } - - /** - * Test of getProcessList method by name, of class JProcesses. - */ - //@Test - public void testChangePriority() { - System.out.println("===============Testing changePriority============"); - boolean ok = JProcesses.changePriority(3260, WindowsPriority.HIGH).isSuccess(); - assertTrue(ok); - - ProcessInfo process = JProcesses.getProcess(3260); - assertTrue(String.valueOf(13).equals(process.getPriority())); - - ok = JProcesses.changePriority(3260, WindowsPriority.NORMAL).isSuccess(); - assertTrue(ok); - - process = JProcesses.getProcess(3260); - assertTrue(String.valueOf(8).equals(process.getPriority())); - - System.out.println("===============End test changePriority============"); - } -} +package org.jutils.jprocesses; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.jutils.jprocesses.model.ProcessInfo; +import org.jutils.jprocesses.model.WindowsPriority; +import org.jutils.jprocesses.util.OSDetector; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertTrue; + +/** + * + * @author javier + */ +public class JProcessesTest { + + public JProcessesTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Test of getProcessList method, of class JProcesses. + */ + @Test + public void testGetProcessList() { + System.out.println("===============Testing getProcessList============"); + List processesList = JProcesses.get().fastMode().listProcesses(); + + assertTrue(processesList != null && processesList.size() > 0); + + for (final ProcessInfo processInfo : processesList) { + System.out.println("Process PID: " + processInfo.getPid()); + System.out.println("Process Name: " + processInfo.getName()); + System.out.println("Process Time: " + processInfo.getTime()); + System.out.println("User: " + processInfo.getUser()); + System.out.println("Virtual Memory: " + processInfo.getVirtualMemory()); + System.out.println("Physical Memory: " + processInfo.getPhysicalMemory()); + System.out.println("CPU usage: " + processInfo.getCpuUsage()); + System.out.println("Start Time: " + processInfo.getStartTime()); + System.out.println("Start DateTime: " + + processInfo.getExtraData().get("start_datetime")); + System.out.println("Priority: " + processInfo.getPriority()); + System.out.println("Full command: " + processInfo.getCommand()); + System.out.println("------------------"); + } + System.out.println("===============End test getProcessList============"); + } + + /** + * Test of getProcessList method by name, of class JProcesses. + */ + @Test + public void testGetProcessListByName() { + System.out.println("===============Testing getProcessList by name============"); + //JProcesses.fastMode = true; + String processToSearch = "java"; + if (OSDetector.isWindows()) { + processToSearch += ".exe"; + } + + List processesList = JProcesses.getProcessList(processToSearch); + + assertTrue(processesList != null && processesList.size() > 0); + + for (final ProcessInfo processInfo : processesList) { + System.out.println("Process PID: " + processInfo.getPid()); + System.out.println("Process Name: " + processInfo.getName()); + System.out.println("Process Time: " + processInfo.getTime()); + System.out.println("User: " + processInfo.getUser()); + System.out.println("Virtual Memory: " + processInfo.getVirtualMemory()); + System.out.println("Physical Memory: " + processInfo.getPhysicalMemory()); + System.out.println("CPU usage: " + processInfo.getCpuUsage()); + System.out.println("Start Time: " + processInfo.getStartTime()); + System.out.println("Priority: " + processInfo.getPriority()); + System.out.println("Command: " + processInfo.getCommand()); + System.out.println("------------------"); + } + + //Compare list with a manually retrieved list + List processesListFull = JProcesses.getProcessList(); + List processesListFound = new ArrayList(); + for (final ProcessInfo process : processesListFull) { + if (processToSearch.equals(process.getName())) { + processesListFound.add(process); + } + } + + assertTrue("Manually list differs from search founded " + + processesListFound.size() + " instead of " + processesList.size(), + processesList.size() == processesListFound.size() + ); + + System.out.println("===============End test getProcessList by name============"); + } + + /** + * Test of getProcessList method by name, of class JProcesses. + */ + //@Test + public void testKill() { + System.out.println("===============Testing killProcess============"); + boolean success = JProcesses.killProcess(3844).isSuccess(); + + System.out.println("===============End test killProcess============"); + } + + /** + * Test of getProcessList method by name, of class JProcesses. + */ + //@Test + public void testChangePriority() { + System.out.println("===============Testing changePriority============"); + boolean ok = JProcesses.changePriority(3260, WindowsPriority.HIGH).isSuccess(); + assertTrue(ok); + + ProcessInfo process = JProcesses.getProcess(3260); + assertTrue(String.valueOf(13).equals(process.getPriority())); + + ok = JProcesses.changePriority(3260, WindowsPriority.NORMAL).isSuccess(); + assertTrue(ok); + + process = JProcesses.getProcess(3260); + assertTrue(String.valueOf(8).equals(process.getPriority())); + + System.out.println("===============End test changePriority============"); + } +} diff --git a/src/test/java/org/jutils/jprocesses/info/WindowsProcessesServiceTest.java b/src/test/java/org/jutils/jprocesses/info/WindowsProcessesServiceTest.java index 3d0cf4f..0f8ba6d 100644 --- a/src/test/java/org/jutils/jprocesses/info/WindowsProcessesServiceTest.java +++ b/src/test/java/org/jutils/jprocesses/info/WindowsProcessesServiceTest.java @@ -1,490 +1,490 @@ -package org.jutils.jprocesses.info; - -import com.profesorfalken.wmi4java.WMI4Java; -import com.profesorfalken.wmi4java.WMIClass; -import org.junit.Before; -import org.junit.Test; -import org.jutils.jprocesses.model.ProcessInfo; -import org.jutils.jprocesses.util.OSDetector; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -import java.util.List; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.anyList; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.when; - -public class WindowsProcessesServiceTest { - - @Mock - WMI4Java wmi4Java; - WindowsProcessesService srv; - ProcessInfo processInfo1; - ProcessInfo processInfo2; - ProcessInfo processInfo3; - ProcessInfo processInfo4; - ProcessInfo processInfo5; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - srv = new WindowsProcessesService(wmi4Java); - processInfo1 = new ProcessInfo("3644", "00:09:568", "idea64.exe", "Denis", "4188632", "1056660", "100", "13:58:15", "8", "\"C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\bin\\idea64.exe\""); - processInfo2 = new ProcessInfo("6936", "00:01:85", "DOOMx64.exe", "Denis", "2429212", "1023348", "6", "14:47:12", "8", "\"D:\\SteamLibrary\\steamapps\\common\\DOOM\\DOOMx64.exe\""); - processInfo3 = new ProcessInfo("2876", "00:03:194", "chrome.exe", "Denis", "554672", "141348", "0", "11:54:00", "8", "\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\" --no-startup-window /prefetch:5"); - processInfo4 = new ProcessInfo("6700", "00:00:01", "java.exe", "Denis", "2006344", "83756", "2", "13:58:45", "8", "\"C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\bin\\java\" -Djava.awt.headless=true -Didea.version==2016.1.2 -Xmx512m -Didea.maven.embedder.version=3.0.5 -Dfile.encoding=windows-1251 -classpath \"C:/Program Files (x86)/JetBrains/IntelliJ IDEA Community Edition 2016.1/lib/resources_en.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\log4j.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\slf4j-api-1.7.10.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\slf4j-log4j12-1.7.10.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\jna.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\resources_en.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\util.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\oromatcher.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\annotations.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\snappy-in-java-0.3.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\trove4j.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\jna-platform.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\jdom.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\picocontainer.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\lucene-core-2.4.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven-server-api.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3-server-common.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3-server-lib\\archetype-catalog-2.2.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3-server-lib\\archetype-common-2.2.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3-server-lib\\maven-dependency-tree-1.2.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3-server-lib\\nexus-indexer-3.0.4.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3-server-lib\\nexus-indexer-artifact-1.0.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven30-server-impl.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\aether-api-1.13.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\aether-connector-wagon-1.13.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\aether-impl-1.13.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\aether-spi-1.13.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\aether-util-1.13.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\commons-cli-1.2.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\commons-io-2.2.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\commons-lang-2.6.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-aether-provider-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-artifact-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-compat-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-core-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-embedder-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-model-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-model-builder-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-plugin-api-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-repository-metadata-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-settings-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-settings-builder-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\plexus-cipher-1.7.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\plexus-component-annotations-1.5.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\plexus-interpolation-1.14.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\plexus-sec-dispatcher-1.3.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\plexus-utils-2.0.6.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\sisu-guava-0.9.9.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\sisu-guice-3.1.0-no_aop.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\sisu-inject-bean-2.3.0.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\sisu-inject-plexus-2.3.0.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\wagon-file-2.8.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\wagon-http-2.8-shaded.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\wagon-http-shared-2.8.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\wagon-provider-api-2.8.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\boot\\plexus-classworlds-2.4.jar\" org.jetbrains.idea.maven.server.RemoteMavenServer"); - processInfo5 = new ProcessInfo("8160", "00:00:00", "java.exe", "Denis", "3596832", "461036", "3", "15:18:48", "8", "\"C:\\Program Files\\Java\\jdk1.8.0_60\\bin\\java\" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:53995,suspend=y,server=n -ea -Didea.junit.sm_runner -Dfile.encoding=UTF-8 -classpath \"C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\idea_rt.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\junit\\lib\\junit-rt.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\charsets.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\deploy.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\javaws.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\jce.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\jfr.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\jfxswt.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\jsse.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\management-agent.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\plugin.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\resources.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\rt.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\access-bridge-64.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\cldrdata.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\dnsns.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\jaccess.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\jfxrt.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\localedata.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\nashorn.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\sunec.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\sunjce_provider.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\sunmscapi.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\sunpkcs11.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\zipfs.jar;D:\\dev\\jProcesses\\target\\test-classes;D:\\dev\\jProcesses\\target\\classes;C:\\Users\\Denis\\.m2\\repository\\junit\\junit\\4.10\\junit-4.10.jar;C:\\Users\\Denis\\.m2\\repository\\org\\hamcrest\\hamcrest-core\\1.1\\hamcrest-core-1.1.jar;C:\\Users\\Denis\\.m2\\repository\\com\\profesorfalken\\WMI4Java\\1.1\\WMI4Java-1.1.jar;C:\\Users\\Denis\\.m2\\repository\\com\\profesorfalken\\jPowerShell\\1.3\\jPowerShell-1.3.jar;C:\\Users\\Denis\\.m2\\repository\\org\\mockito\\mockito-all\\1.8.4\\mockito-all-1.8.4.jar\" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 org.jutils.jprocesses.info.WindowsProcessesServiceTest,testGetListWithName"); - when(wmi4Java.getRawWMIObjectOutput(eq(WMIClass.WIN32_PROCESS))).thenReturn(WMI_PROCESS1 + WMI_PROCESS2 + WMI_PROCESS3 + WMI_PROCESS4 + WMI_PROCESS5); - when(wmi4Java.getRawWMIObjectOutput(eq(WMIClass.WIN32_PERFFORMATTEDDATA_PERFPROC_PROCESS))).thenReturn(WMI_PROCESS1_PERF + WMI_PROCESS2_PERF + WMI_PROCESS3_PERF + WMI_PROCESS4_PERF + WMI_PROCESS5_PERF); - when(wmi4Java.VBSEngine()).thenReturn(wmi4Java); - } - - /** - * Test of getProcessList method, of class JProcesses. - */ - @Test - public void testGetList() { - if (OSDetector.isWindows()) { - List list = srv.getList(); - assertEquals(5, list.size()); - assertTrue(list.contains(processInfo1)); - assertTrue(list.contains(processInfo2)); - assertTrue(list.contains(processInfo3)); - assertTrue(list.contains(processInfo4)); - assertTrue(list.contains(processInfo5)); - } - } - - @Test - public void testGetListWithName() { - if (OSDetector.isWindows()) { - when(wmi4Java.properties(anyList())).thenReturn(wmi4Java); - when(wmi4Java.filters(anyList())).thenReturn(wmi4Java); - when(wmi4Java.filters(anyList())).thenReturn(wmi4Java); - when(wmi4Java.getRawWMIObjectOutput(eq(WMIClass.WIN32_PROCESS))).thenReturn(WMI_PROCESS4 + WMI_PROCESS5); - when(wmi4Java.getRawWMIObjectOutput(eq(WMIClass.WIN32_PERFFORMATTEDDATA_PERFPROC_PROCESS))).thenReturn(WMI_PROCESS4_PERF + WMI_PROCESS5_PERF); - List list = srv.getList("java.exe"); - assertEquals(2, list.size()); - assertTrue(list.contains(processInfo4)); - assertTrue(list.contains(processInfo5)); - } - } - - private static final String WMI_PROCESS1 = "Caption: idea64.exe\n" - + "CommandLine: \"C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\bin\\idea64.exe\" \n" - + "CreationClassName: Win32_Process\n" - + "CreationDate: 20160514135815.444566+180\n" - + "CSCreationClassName: Win32_ComputerSystem\n" - + "CSName: DENIS-PC\n" - + "Description: idea64.exe\n" - + "ExecutablePath: C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\bin\\idea64.exe\n" - + "ExecutionState: \n" - + "Handle: 3644\n" - + "HandleCount: 910\n" - + "InstallDate: \n" - + "KernelModeTime: 524007359\n" - + "MaximumWorkingSetSize: 1380\n" - + "MinimumWorkingSetSize: 200\n" - + "Name: idea64.exe\n" - + "OSCreationClassName: Win32_OperatingSystem\n" - + "OSName: Microsoft Windows 7 Ultimate |C:\\Windows|\\Device\\Harddisk0\\Partition2\n" - + "OtherOperationCount: 1934035\n" - + "OtherTransferCount: 35525870\n" - + "PageFaults: 2541972\n" - + "PageFileUsage: 1117360\n" - + "ParentProcessId: 500\n" - + "PeakPageFileUsage: 1133232\n" - + "PeakVirtualSize: 4297838592\n" - + "PeakWorkingSetSize: 1071420\n" - + "Priority: 8\n" - + "PrivatePageCount: 1144176640\n" - + "ProcessId: 3644\n" - + "QuotaNonPagedPoolUsage: 75\n" - + "QuotaPagedPoolUsage: 304\n" - + "QuotaPeakNonPagedPoolUsage: 81\n" - + "QuotaPeakPagedPoolUsage: 326\n" - + "ReadOperationCount: 178223\n" - + "ReadTransferCount: 661366634\n" - + "SessionId: 1\n" - + "Status: \n" - + "TerminationDate: \n" - + "ThreadCount: 64\n" - + "UserModeTime: 5684832441\n" - + "VirtualSize: 4289159168\n" - + "WindowsVersion: 6.1.7601\n" - + "WorkingSetSize: 1082019840\n" - + "WriteOperationCount: 4141\n" - + "WriteTransferCount: 1499671310\n"; - - private static final String WMI_PROCESS2 = "Caption: DOOMx64.exe\n" - + "CommandLine: \"D:\\SteamLibrary\\steamapps\\common\\DOOM\\DOOMx64.exe\"\n" - + "CreationClassName: Win32_Process\n" - + "CreationDate: 20160514144712.680566+180\n" - + "CSCreationClassName: Win32_ComputerSystem\n" - + "CSName: DENIS-PC\n" - + "Description: DOOMx64.exe\n" - + "ExecutablePath: D:\\SteamLibrary\\steamapps\\common\\DOOM\\DOOMx64.exe\n" - + "ExecutionState:\n" - + "Handle: 6936\n" - + "HandleCount: 1110\n" - + "InstallDate:\n" - + "KernelModeTime: 709960551\n" - + "MaximumWorkingSetSize: 628680\n" - + "MinimumWorkingSetSize: 628568\n" - + "Name: DOOMx64.exe\n" - + "OSCreationClassName: Win32_OperatingSystem\n" - + "OSName: Microsoft Windows 7 Ultimate |C:\\Windows|\\Device\\Harddisk0\\Partition2\n" - + "OtherOperationCount: 10494\n" - + "OtherTransferCount: 109196\n" - + "PageFaults: 386552\n" - + "PageFileUsage: 1585468\n" - + "ParentProcessId: 2548\n" - + "PeakPageFileUsage: 1585476\n" - + "PeakVirtualSize: 2490068992\n" - + "PeakWorkingSetSize: 1023348\n" - + "Priority: 8\n" - + "PrivatePageCount: 1623519232\n" - + "ProcessId: 6936\n" - + "QuotaNonPagedPoolUsage: 128\n" - + "QuotaPagedPoolUsage: 3330\n" - + "QuotaPeakNonPagedPoolUsage: 128\n" - + "QuotaPeakPagedPoolUsage: 3340\n" - + "ReadOperationCount: 2376\n" - + "ReadTransferCount: 2433886942\n" - + "SessionId: 1\n" - + "Status:\n" - + "TerminationDate:\n" - + "ThreadCount: 33\n" - + "UserModeTime: 857069494\n" - + "VirtualSize: 2487513088\n" - + "WindowsVersion: 6.1.7601\n" - + "WorkingSetSize: 1047908352\n" - + "WriteOperationCount: 210\n" - + "WriteTransferCount: 11894\n"; - private static final String WMI_PROCESS3 = "Caption: chrome.exe\n" - + "CommandLine: \"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\" --no-startup-window /prefetch:5\n" - + "CreationClassName: Win32_Process\n" - + "CreationDate: 20160514115400.098497+180\n" - + "CSCreationClassName: Win32_ComputerSystem\n" - + "CSName: DENIS-PC\n" - + "Description: chrome.exe\n" - + "ExecutablePath: C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\n" - + "ExecutionState:\n" - + "Handle: 2876\n" - + "HandleCount: 1265\n" - + "InstallDate:\n" - + "KernelModeTime: 661912243\n" - + "MaximumWorkingSetSize: 1380\n" - + "MinimumWorkingSetSize: 200\n" - + "Name: chrome.exe\n" - + "OSCreationClassName: Win32_OperatingSystem\n" - + "OSName: Microsoft Windows 7 Ultimate |C:\\Windows|\\Device\\Harddisk0\\Partition2\n" - + "OtherOperationCount: 493715\n" - + "OtherTransferCount: 18990227\n" - + "PageFaults: 1548415\n" - + "PageFileUsage: 104732\n" - + "ParentProcessId: 500\n" - + "PeakPageFileUsage: 138056\n" - + "PeakVirtualSize: 764755968\n" - + "PeakWorkingSetSize: 171256\n" - + "Priority: 8\n" - + "PrivatePageCount: 107245568\n" - + "ProcessId: 2876\n" - + "QuotaNonPagedPoolUsage: 65\n" - + "QuotaPagedPoolUsage: 852\n" - + "QuotaPeakNonPagedPoolUsage: 232\n" - + "QuotaPeakPagedPoolUsage: 1247\n" - + "ReadOperationCount: 1549009\n" - + "ReadTransferCount: 1148378511\n" - + "SessionId: 1\n" - + "Status:\n" - + "TerminationDate:\n" - + "ThreadCount: 39\n" - + "UserModeTime: 1948764492\n" - + "VirtualSize: 567984128\n" - + "WindowsVersion: 6.1.7601\n" - + "WorkingSetSize: 144740352\n" - + "WriteOperationCount: 1793811\n" - + "WriteTransferCount: 1503510606\n"; - private static final String WMI_PROCESS4 = "Caption: java.exe\n" - + "CommandLine: \"C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\bin\\java\" -Djava.awt.headless=true -Didea.version==2016.1.2 -Xmx512m -Didea.maven.embedder.version=3.0.5 -Dfile.encoding=windows-1251 -classpath \"C:/Program Files (x86)/JetBrains/IntelliJ IDEA Community Edition 2016.1/lib/resources_en.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\log4j.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\slf4j-api-1.7.10.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\slf4j-log4j12-1.7.10.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\jna.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\resources_en.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\util.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\oromatcher.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\annotations.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\snappy-in-java-0.3.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\trove4j.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\jna-platform.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\jdom.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\picocontainer.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\lucene-core-2.4.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven-server-api.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3-server-common.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3-server-lib\\archetype-catalog-2.2.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3-server-lib\\archetype-common-2.2.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3-server-lib\\maven-dependency-tree-1.2.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3-server-lib\\nexus-indexer-3.0.4.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3-server-lib\\nexus-indexer-artifact-1.0.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven30-server-impl.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\aether-api-1.13.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\aether-connector-wagon-1.13.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\aether-impl-1.13.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\aether-spi-1.13.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\aether-util-1.13.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\commons-cli-1.2.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\commons-io-2.2.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\commons-lang-2.6.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-aether-provider-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-artifact-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-compat-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-core-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-embedder-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-model-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-model-builder-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-plugin-api-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-repository-metadata-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-settings-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-settings-builder-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\plexus-cipher-1.7.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\plexus-component-annotations-1.5.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\plexus-interpolation-1.14.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\plexus-sec-dispatcher-1.3.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\plexus-utils-2.0.6.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\sisu-guava-0.9.9.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\sisu-guice-3.1.0-no_aop.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\sisu-inject-bean-2.3.0.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\sisu-inject-plexus-2.3.0.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\wagon-file-2.8.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\wagon-http-2.8-shaded.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\wagon-http-shared-2.8.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\wagon-provider-api-2.8.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\boot\\plexus-classworlds-2.4.jar\" org.jetbrains.idea.maven.server.RemoteMavenServer\n" - + "CreationClassName: Win32_Process\n" - + "CreationDate: 20160514135845.836304+180\n" - + "CSCreationClassName: Win32_ComputerSystem\n" - + "CSName: DENIS-PC\n" - + "Description: java.exe\n" - + "ExecutablePath: C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\bin\\java.exe\n" - + "ExecutionState: \n" - + "Handle: 6700\n" - + "HandleCount: 324\n" - + "InstallDate: \n" - + "KernelModeTime: 1716011\n" - + "MaximumWorkingSetSize: 1380\n" - + "MinimumWorkingSetSize: 200\n" - + "Name: java.exe\n" - + "OSCreationClassName: Win32_OperatingSystem\n" - + "OSName: Microsoft Windows 7 Ultimate |C:\\Windows|\\Device\\Harddisk0\\Partition2\n" - + "OtherOperationCount: 12619\n" - + "OtherTransferCount: 274107\n" - + "PageFaults: 34915\n" - + "PageFileUsage: 240784\n" - + "ParentProcessId: 3644\n" - + "PeakPageFileUsage: 245768\n" - + "PeakVirtualSize: 2058690560\n" - + "PeakWorkingSetSize: 94992\n" - + "Priority: 8\n" - + "PrivatePageCount: 246562816\n" - + "ProcessId: 6700\n" - + "QuotaNonPagedPoolUsage: 22\n" - + "QuotaPagedPoolUsage: 169\n" - + "QuotaPeakNonPagedPoolUsage: 26\n" - + "QuotaPeakPagedPoolUsage: 171\n" - + "ReadOperationCount: 9873\n" - + "ReadTransferCount: 11487969\n" - + "SessionId: 1\n" - + "Status: \n" - + "TerminationDate: \n" - + "ThreadCount: 24\n" - + "UserModeTime: 18252117\n" - + "VirtualSize: 2054496256\n" - + "WindowsVersion: 6.1.7601\n" - + "WorkingSetSize: 85766144\n" - + "WriteOperationCount: 118\n" - + "WriteTransferCount: 3848\n"; - - private static final String WMI_PROCESS5 = "Caption: java.exe\n" - + "CommandLine: \"C:\\Program Files\\Java\\jdk1.8.0_60\\bin\\java\" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:53995,suspend=y,server=n -ea -Didea.junit.sm_runner -Dfile.encoding=UTF-8 -classpath \"C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\idea_rt.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\junit\\lib\\junit-rt.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\charsets.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\deploy.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\javaws.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\jce.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\jfr.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\jfxswt.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\jsse.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\management-agent.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\plugin.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\resources.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\rt.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\access-bridge-64.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\cldrdata.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\dnsns.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\jaccess.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\jfxrt.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\localedata.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\nashorn.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\sunec.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\sunjce_provider.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\sunmscapi.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\sunpkcs11.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\zipfs.jar;D:\\dev\\jProcesses\\target\\test-classes;D:\\dev\\jProcesses\\target\\classes;C:\\Users\\Denis\\.m2\\repository\\junit\\junit\\4.10\\junit-4.10.jar;C:\\Users\\Denis\\.m2\\repository\\org\\hamcrest\\hamcrest-core\\1.1\\hamcrest-core-1.1.jar;C:\\Users\\Denis\\.m2\\repository\\com\\profesorfalken\\WMI4Java\\1.1\\WMI4Java-1.1.jar;C:\\Users\\Denis\\.m2\\repository\\com\\profesorfalken\\jPowerShell\\1.3\\jPowerShell-1.3.jar;C:\\Users\\Denis\\.m2\\repository\\org\\mockito\\mockito-all\\1.8.4\\mockito-all-1.8.4.jar\" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 org.jutils.jprocesses.info.WindowsProcessesServiceTest,testGetListWithName\n" - + "CreationClassName: Win32_Process\n" - + "CreationDate: 20160514151848.264987+180\n" - + "CSCreationClassName: Win32_ComputerSystem\n" - + "CSName: DENIS-PC\n" - + "Description: java.exe\n" - + "ExecutablePath: C:\\Program Files\\Java\\jdk1.8.0_60\\bin\\java.exe\n" - + "ExecutionState: \n" - + "Handle: 8160\n" - + "HandleCount: 252\n" - + "InstallDate: \n" - + "KernelModeTime: 2808018\n" - + "MaximumWorkingSetSize: 1380\n" - + "MinimumWorkingSetSize: 200\n" - + "Name: java.exe\n" - + "OSCreationClassName: Win32_OperatingSystem\n" - + "OSName: Microsoft Windows 7 Ultimate |C:\\Windows|\\Device\\Harddisk0\\Partition2\n" - + "OtherOperationCount: 7038\n" - + "OtherTransferCount: 163028\n" - + "PageFaults: 118732\n" - + "PageFileUsage: 892380\n" - + "ParentProcessId: 3644\n" - + "PeakPageFileUsage: 894172\n" - + "PeakVirtualSize: 3683155968\n" - + "PeakWorkingSetSize: 462196\n" - + "Priority: 8\n" - + "PrivatePageCount: 913797120\n" - + "ProcessId: 8160\n" - + "QuotaNonPagedPoolUsage: 20\n" - + "QuotaPagedPoolUsage: 178\n" - + "QuotaPeakNonPagedPoolUsage: 119\n" - + "QuotaPeakPagedPoolUsage: 178\n" - + "ReadOperationCount: 4176\n" - + "ReadTransferCount: 6494968\n" - + "SessionId: 1\n" - + "Status: \n" - + "TerminationDate: \n" - + "ThreadCount: 20\n" - + "UserModeTime: 8268053\n" - + "VirtualSize: 3683155968\n" - + "WindowsVersion: 6.1.7601\n" - + "WorkingSetSize: 472100864\n" - + "WriteOperationCount: 14\n" - + "WriteTransferCount: 6635\n"; - - private static final String WMI_PROCESS1_PERF = "Caption: \n" - + "CreatingProcessID: 2548\n" - + "Description: \n" - + "ElapsedTime: 361\n" - + "Frequency_Object: \n" - + "Frequency_PerfTime: \n" - + "Frequency_Sys100NS: \n" - + "HandleCount: 1739\n" - + "IDProcess: 6936\n" - + "IODataBytesPersec: 3631675\n" - + "IODataOperationsPersec: 265\n" - + "IOOtherBytesPersec: 949\n" - + "IOOtherOperationsPersec: 296\n" - + "IOReadBytesPersec: 3630607\n" - + "IOReadOperationsPersec: 146\n" - + "IOWriteBytesPersec: 1068\n" - + "IOWriteOperationsPersec: 118\n" - + "Name: DOOMx64\n" - + "PageFaultsPersec: 0\n" - + "PageFileBytes: 2646224896\n" - + "PageFileBytesPeak: 2648330240\n" - + "PercentPrivilegedTime: 67\n" - + "PercentProcessorTime: 100\n" - + "PercentUserTime: 80\n" - + "PoolNonpagedBytes: 301176\n" - + "PoolPagedBytes: 2782520\n" - + "PriorityBase: 8\n" - + "PrivateBytes: 2646224896\n" - + "ThreadCount: 36\n" - + "Timestamp_Object: \n" - + "Timestamp_PerfTime: \n" - + "Timestamp_Sys100NS: \n" - + "VirtualBytes: 3179716608\n" - + "VirtualBytesPeak: 3231305728\n" - + "WorkingSet: 2166816768\n" - + "WorkingSetPeak: 2166833152\n" - + "WorkingSetPrivate: 1682055168\n"; - private static final String WMI_PROCESS2_PERF = "Caption: \n" - + "CreatingProcessID: 500\n" - + "Description: \n" - + "ElapsedTime: 3298\n" - + "Frequency_Object: \n" - + "Frequency_PerfTime: \n" - + "Frequency_Sys100NS: \n" - + "HandleCount: 917\n" - + "IDProcess: 3644\n" - + "IODataBytesPersec: 1867\n" - + "IODataOperationsPersec: 11\n" - + "IOOtherBytesPersec: 13672\n" - + "IOOtherOperationsPersec: 854\n" - + "IOReadBytesPersec: 1867\n" - + "IOReadOperationsPersec: 11\n" - + "IOWriteBytesPersec: 0\n" - + "IOWriteOperationsPersec: 0\n" - + "Name: idea64\n" - + "PageFaultsPersec: 3\n" - + "PageFileBytes: 1155874816\n" - + "PageFileBytesPeak: 1166286848\n" - + "PercentPrivilegedTime: 0\n" - + "PercentProcessorTime: 6\n" - + "PercentUserTime: 6\n" - + "PoolNonpagedBytes: 74096\n" - + "PoolPagedBytes: 310792\n" - + "PriorityBase: 8\n" - + "PrivateBytes: 1155874816\n" - + "ThreadCount: 65\n" - + "Timestamp_Object: \n" - + "Timestamp_PerfTime: \n" - + "Timestamp_Sys100NS: \n" - + "VirtualBytes: 4290207744\n" - + "VirtualBytesPeak: 4300398592\n" - + "WorkingSet: 1061761024\n" - + "WorkingSetPeak: 1097666560\n" - + "WorkingSetPrivate: 1050238976\n"; - private static final String WMI_PROCESS3_PERF = "Caption: \n" - + "CreatingProcessID: 500\n" - + "Description: \n" - + "ElapsedTime: 10753\n" - + "Frequency_Object: \n" - + "Frequency_PerfTime: \n" - + "Frequency_Sys100NS: \n" - + "HandleCount: 1259\n" - + "IDProcess: 2876\n" - + "IODataBytesPersec: 236304\n" - + "IODataOperationsPersec: 522\n" - + "IOOtherBytesPersec: 443\n" - + "IOOtherOperationsPersec: 106\n" - + "IOReadBytesPersec: 107811\n" - + "IOReadOperationsPersec: 174\n" - + "IOWriteBytesPersec: 128493\n" - + "IOWriteOperationsPersec: 348\n" - + "Name: chrome\n" - + "PageFaultsPersec: 0\n" - + "PageFileBytes: 107134976\n" - + "PageFileBytesPeak: 141369344\n" - + "PercentPrivilegedTime: 0\n" - + "PercentProcessorTime: 0\n" - + "PercentUserTime: 0\n" - + "PoolNonpagedBytes: 63448\n" - + "PoolPagedBytes: 870848\n" - + "PriorityBase: 8\n" - + "PrivateBytes: 107134976\n" - + "ThreadCount: 37\n" - + "Timestamp_Object: \n" - + "Timestamp_PerfTime: \n" - + "Timestamp_Sys100NS: \n" - + "VirtualBytes: 565362688\n" - + "VirtualBytesPeak: 764755968\n" - + "WorkingSet: 132898816\n" - + "WorkingSetPeak: 175366144\n" - + "WorkingSetPrivate: 86532096\n"; - private static final String WMI_PROCESS4_PERF = "Caption: \n" - + "CreatingProcessID: 3644\n" - + "Description: \n" - + "ElapsedTime: 4808\n" - + "Frequency_Object: \n" - + "Frequency_PerfTime: \n" - + "Frequency_Sys100NS: \n" - + "HandleCount: 325\n" - + "IDProcess: 6700\n" - + "IODataBytesPersec: 0\n" - + "IODataOperationsPersec: 0\n" - + "IOOtherBytesPersec: 0\n" - + "IOOtherOperationsPersec: 0\n" - + "IOReadBytesPersec: 0\n" - + "IOReadOperationsPersec: 0\n" - + "IOWriteBytesPersec: 0\n" - + "IOWriteOperationsPersec: 0\n" - + "Name: java\n" - + "PageFaultsPersec: 0\n" - + "PageFileBytes: 246562816\n" - + "PageFileBytesPeak: 251666432\n" - + "PercentPrivilegedTime: 0\n" - + "PercentProcessorTime: 2\n" - + "PercentUserTime: 0\n" - + "PoolNonpagedBytes: 23296\n" - + "PoolPagedBytes: 172744\n" - + "PriorityBase: 8\n" - + "PrivateBytes: 246562816\n" - + "ThreadCount: 24\n" - + "Timestamp_Object: \n" - + "Timestamp_PerfTime: \n" - + "Timestamp_Sys100NS: \n" - + "VirtualBytes: 2054496256\n" - + "VirtualBytesPeak: 2058690560\n" - + "WorkingSet: 85757952\n" - + "WorkingSetPeak: 97271808\n" - + "WorkingSetPrivate: 80453632\n"; - private static final String WMI_PROCESS5_PERF = "Caption: \n" - + "CreatingProcessID: 3644\n" - + "Description: \n" - + "ElapsedTime: 5\n" - + "Frequency_Object: \n" - + "Frequency_PerfTime: \n" - + "Frequency_Sys100NS: \n" - + "HandleCount: 245\n" - + "IDProcess: 8160\n" - + "IODataBytesPersec: 0\n" - + "IODataOperationsPersec: 0\n" - + "IOOtherBytesPersec: 0\n" - + "IOOtherOperationsPersec: 0\n" - + "IOReadBytesPersec: 0\n" - + "IOReadOperationsPersec: 0\n" - + "IOWriteBytesPersec: 0\n" - + "IOWriteOperationsPersec: 0\n" - + "Name: java#1\n" - + "PageFaultsPersec: 0\n" - + "PageFileBytes: 250654720\n" - + "PageFileBytesPeak: 255041536\n" - + "PercentPrivilegedTime: 0\n" - + "PercentProcessorTime: 3\n" - + "PercentUserTime: 0\n" - + "PoolNonpagedBytes: 19704\n" - + "PoolPagedBytes: 179368\n" - + "PriorityBase: 8\n" - + "PrivateBytes: 250654720\n" - + "ThreadCount: 20\n" - + "Timestamp_Object: \n" - + "Timestamp_PerfTime: \n" - + "Timestamp_Sys100NS: \n" - + "VirtualBytes: 3683155968\n" - + "VirtualBytesPeak: 3683155968\n" - + "WorkingSet: 40710144\n" - + "WorkingSetPeak: 45494272\n" - + "WorkingSetPrivate: 30330880\n"; - -} +package org.jutils.jprocesses.info; + +import com.profesorfalken.wmi4java.WMI4Java; +import com.profesorfalken.wmi4java.WMIClass; +import org.junit.Before; +import org.junit.Test; +import org.jutils.jprocesses.model.ProcessInfo; +import org.jutils.jprocesses.util.OSDetector; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.anyList; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.when; + +public class WindowsProcessesServiceTest { + + @Mock + WMI4Java wmi4Java; + WindowsProcessesService srv; + ProcessInfo processInfo1; + ProcessInfo processInfo2; + ProcessInfo processInfo3; + ProcessInfo processInfo4; + ProcessInfo processInfo5; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + srv = new WindowsProcessesService(wmi4Java); + processInfo1 = new ProcessInfo("3644", "00:09:568", "idea64.exe", "Denis", "4188632", "1056660", "100", "13:58:15", "8", "\"C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\bin\\idea64.exe\""); + processInfo2 = new ProcessInfo("6936", "00:01:85", "DOOMx64.exe", "Denis", "2429212", "1023348", "6", "14:47:12", "8", "\"D:\\SteamLibrary\\steamapps\\common\\DOOM\\DOOMx64.exe\""); + processInfo3 = new ProcessInfo("2876", "00:03:194", "chrome.exe", "Denis", "554672", "141348", "0", "11:54:00", "8", "\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\" --no-startup-window /prefetch:5"); + processInfo4 = new ProcessInfo("6700", "00:00:01", "java.exe", "Denis", "2006344", "83756", "2", "13:58:45", "8", "\"C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\bin\\java\" -Djava.awt.headless=true -Didea.version==2016.1.2 -Xmx512m -Didea.maven.embedder.version=3.0.5 -Dfile.encoding=windows-1251 -classpath \"C:/Program Files (x86)/JetBrains/IntelliJ IDEA Community Edition 2016.1/lib/resources_en.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\log4j.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\slf4j-api-1.7.10.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\slf4j-log4j12-1.7.10.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\jna.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\resources_en.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\util.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\oromatcher.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\annotations.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\snappy-in-java-0.3.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\trove4j.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\jna-platform.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\jdom.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\picocontainer.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\lucene-core-2.4.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven-server-api.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3-server-common.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3-server-lib\\archetype-catalog-2.2.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3-server-lib\\archetype-common-2.2.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3-server-lib\\maven-dependency-tree-1.2.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3-server-lib\\nexus-indexer-3.0.4.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3-server-lib\\nexus-indexer-artifact-1.0.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven30-server-impl.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\aether-api-1.13.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\aether-connector-wagon-1.13.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\aether-impl-1.13.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\aether-spi-1.13.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\aether-util-1.13.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\commons-cli-1.2.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\commons-io-2.2.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\commons-lang-2.6.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-aether-provider-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-artifact-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-compat-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-core-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-embedder-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-model-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-model-builder-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-plugin-api-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-repository-metadata-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-settings-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-settings-builder-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\plexus-cipher-1.7.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\plexus-component-annotations-1.5.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\plexus-interpolation-1.14.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\plexus-sec-dispatcher-1.3.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\plexus-utils-2.0.6.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\sisu-guava-0.9.9.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\sisu-guice-3.1.0-no_aop.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\sisu-inject-bean-2.3.0.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\sisu-inject-plexus-2.3.0.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\wagon-file-2.8.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\wagon-http-2.8-shaded.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\wagon-http-shared-2.8.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\wagon-provider-api-2.8.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\boot\\plexus-classworlds-2.4.jar\" org.jetbrains.idea.maven.server.RemoteMavenServer"); + processInfo5 = new ProcessInfo("8160", "00:00:00", "java.exe", "Denis", "3596832", "461036", "3", "15:18:48", "8", "\"C:\\Program Files\\Java\\jdk1.8.0_60\\bin\\java\" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:53995,suspend=y,server=n -ea -Didea.junit.sm_runner -Dfile.encoding=UTF-8 -classpath \"C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\idea_rt.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\junit\\lib\\junit-rt.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\charsets.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\deploy.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\javaws.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\jce.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\jfr.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\jfxswt.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\jsse.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\management-agent.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\plugin.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\resources.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\rt.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\access-bridge-64.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\cldrdata.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\dnsns.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\jaccess.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\jfxrt.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\localedata.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\nashorn.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\sunec.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\sunjce_provider.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\sunmscapi.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\sunpkcs11.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\zipfs.jar;D:\\dev\\jProcesses\\target\\test-classes;D:\\dev\\jProcesses\\target\\classes;C:\\Users\\Denis\\.m2\\repository\\junit\\junit\\4.10\\junit-4.10.jar;C:\\Users\\Denis\\.m2\\repository\\org\\hamcrest\\hamcrest-core\\1.1\\hamcrest-core-1.1.jar;C:\\Users\\Denis\\.m2\\repository\\com\\profesorfalken\\WMI4Java\\1.1\\WMI4Java-1.1.jar;C:\\Users\\Denis\\.m2\\repository\\com\\profesorfalken\\jPowerShell\\1.3\\jPowerShell-1.3.jar;C:\\Users\\Denis\\.m2\\repository\\org\\mockito\\mockito-all\\1.8.4\\mockito-all-1.8.4.jar\" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 org.jutils.jprocesses.info.WindowsProcessesServiceTest,testGetListWithName"); + when(wmi4Java.getRawWMIObjectOutput(eq(WMIClass.WIN32_PROCESS))).thenReturn(WMI_PROCESS1 + WMI_PROCESS2 + WMI_PROCESS3 + WMI_PROCESS4 + WMI_PROCESS5); + when(wmi4Java.getRawWMIObjectOutput(eq(WMIClass.WIN32_PERFFORMATTEDDATA_PERFPROC_PROCESS))).thenReturn(WMI_PROCESS1_PERF + WMI_PROCESS2_PERF + WMI_PROCESS3_PERF + WMI_PROCESS4_PERF + WMI_PROCESS5_PERF); + when(wmi4Java.VBSEngine()).thenReturn(wmi4Java); + } + + /** + * Test of getProcessList method, of class JProcesses. + */ + @Test + public void testGetList() { + if (OSDetector.isWindows()) { + List list = srv.getList(); + assertEquals(5, list.size()); + assertTrue(list.contains(processInfo1)); + assertTrue(list.contains(processInfo2)); + assertTrue(list.contains(processInfo3)); + assertTrue(list.contains(processInfo4)); + assertTrue(list.contains(processInfo5)); + } + } + + @Test + public void testGetListWithName() { + if (OSDetector.isWindows()) { + when(wmi4Java.properties(anyList())).thenReturn(wmi4Java); + when(wmi4Java.filters(anyList())).thenReturn(wmi4Java); + when(wmi4Java.filters(anyList())).thenReturn(wmi4Java); + when(wmi4Java.getRawWMIObjectOutput(eq(WMIClass.WIN32_PROCESS))).thenReturn(WMI_PROCESS4 + WMI_PROCESS5); + when(wmi4Java.getRawWMIObjectOutput(eq(WMIClass.WIN32_PERFFORMATTEDDATA_PERFPROC_PROCESS))).thenReturn(WMI_PROCESS4_PERF + WMI_PROCESS5_PERF); + List list = srv.getList("java.exe"); + assertEquals(2, list.size()); + assertTrue(list.contains(processInfo4)); + assertTrue(list.contains(processInfo5)); + } + } + + private static final String WMI_PROCESS1 = "Caption: idea64.exe\n" + + "CommandLine: \"C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\bin\\idea64.exe\" \n" + + "CreationClassName: Win32_Process\n" + + "CreationDate: 20160514135815.444566+180\n" + + "CSCreationClassName: Win32_ComputerSystem\n" + + "CSName: DENIS-PC\n" + + "Description: idea64.exe\n" + + "ExecutablePath: C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\bin\\idea64.exe\n" + + "ExecutionState: \n" + + "Handle: 3644\n" + + "HandleCount: 910\n" + + "InstallDate: \n" + + "KernelModeTime: 524007359\n" + + "MaximumWorkingSetSize: 1380\n" + + "MinimumWorkingSetSize: 200\n" + + "Name: idea64.exe\n" + + "OSCreationClassName: Win32_OperatingSystem\n" + + "OSName: Microsoft Windows 7 Ultimate |C:\\Windows|\\Device\\Harddisk0\\Partition2\n" + + "OtherOperationCount: 1934035\n" + + "OtherTransferCount: 35525870\n" + + "PageFaults: 2541972\n" + + "PageFileUsage: 1117360\n" + + "ParentProcessId: 500\n" + + "PeakPageFileUsage: 1133232\n" + + "PeakVirtualSize: 4297838592\n" + + "PeakWorkingSetSize: 1071420\n" + + "Priority: 8\n" + + "PrivatePageCount: 1144176640\n" + + "ProcessId: 3644\n" + + "QuotaNonPagedPoolUsage: 75\n" + + "QuotaPagedPoolUsage: 304\n" + + "QuotaPeakNonPagedPoolUsage: 81\n" + + "QuotaPeakPagedPoolUsage: 326\n" + + "ReadOperationCount: 178223\n" + + "ReadTransferCount: 661366634\n" + + "SessionId: 1\n" + + "Status: \n" + + "TerminationDate: \n" + + "ThreadCount: 64\n" + + "UserModeTime: 5684832441\n" + + "VirtualSize: 4289159168\n" + + "WindowsVersion: 6.1.7601\n" + + "WorkingSetSize: 1082019840\n" + + "WriteOperationCount: 4141\n" + + "WriteTransferCount: 1499671310\n"; + + private static final String WMI_PROCESS2 = "Caption: DOOMx64.exe\n" + + "CommandLine: \"D:\\SteamLibrary\\steamapps\\common\\DOOM\\DOOMx64.exe\"\n" + + "CreationClassName: Win32_Process\n" + + "CreationDate: 20160514144712.680566+180\n" + + "CSCreationClassName: Win32_ComputerSystem\n" + + "CSName: DENIS-PC\n" + + "Description: DOOMx64.exe\n" + + "ExecutablePath: D:\\SteamLibrary\\steamapps\\common\\DOOM\\DOOMx64.exe\n" + + "ExecutionState:\n" + + "Handle: 6936\n" + + "HandleCount: 1110\n" + + "InstallDate:\n" + + "KernelModeTime: 709960551\n" + + "MaximumWorkingSetSize: 628680\n" + + "MinimumWorkingSetSize: 628568\n" + + "Name: DOOMx64.exe\n" + + "OSCreationClassName: Win32_OperatingSystem\n" + + "OSName: Microsoft Windows 7 Ultimate |C:\\Windows|\\Device\\Harddisk0\\Partition2\n" + + "OtherOperationCount: 10494\n" + + "OtherTransferCount: 109196\n" + + "PageFaults: 386552\n" + + "PageFileUsage: 1585468\n" + + "ParentProcessId: 2548\n" + + "PeakPageFileUsage: 1585476\n" + + "PeakVirtualSize: 2490068992\n" + + "PeakWorkingSetSize: 1023348\n" + + "Priority: 8\n" + + "PrivatePageCount: 1623519232\n" + + "ProcessId: 6936\n" + + "QuotaNonPagedPoolUsage: 128\n" + + "QuotaPagedPoolUsage: 3330\n" + + "QuotaPeakNonPagedPoolUsage: 128\n" + + "QuotaPeakPagedPoolUsage: 3340\n" + + "ReadOperationCount: 2376\n" + + "ReadTransferCount: 2433886942\n" + + "SessionId: 1\n" + + "Status:\n" + + "TerminationDate:\n" + + "ThreadCount: 33\n" + + "UserModeTime: 857069494\n" + + "VirtualSize: 2487513088\n" + + "WindowsVersion: 6.1.7601\n" + + "WorkingSetSize: 1047908352\n" + + "WriteOperationCount: 210\n" + + "WriteTransferCount: 11894\n"; + private static final String WMI_PROCESS3 = "Caption: chrome.exe\n" + + "CommandLine: \"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\" --no-startup-window /prefetch:5\n" + + "CreationClassName: Win32_Process\n" + + "CreationDate: 20160514115400.098497+180\n" + + "CSCreationClassName: Win32_ComputerSystem\n" + + "CSName: DENIS-PC\n" + + "Description: chrome.exe\n" + + "ExecutablePath: C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\n" + + "ExecutionState:\n" + + "Handle: 2876\n" + + "HandleCount: 1265\n" + + "InstallDate:\n" + + "KernelModeTime: 661912243\n" + + "MaximumWorkingSetSize: 1380\n" + + "MinimumWorkingSetSize: 200\n" + + "Name: chrome.exe\n" + + "OSCreationClassName: Win32_OperatingSystem\n" + + "OSName: Microsoft Windows 7 Ultimate |C:\\Windows|\\Device\\Harddisk0\\Partition2\n" + + "OtherOperationCount: 493715\n" + + "OtherTransferCount: 18990227\n" + + "PageFaults: 1548415\n" + + "PageFileUsage: 104732\n" + + "ParentProcessId: 500\n" + + "PeakPageFileUsage: 138056\n" + + "PeakVirtualSize: 764755968\n" + + "PeakWorkingSetSize: 171256\n" + + "Priority: 8\n" + + "PrivatePageCount: 107245568\n" + + "ProcessId: 2876\n" + + "QuotaNonPagedPoolUsage: 65\n" + + "QuotaPagedPoolUsage: 852\n" + + "QuotaPeakNonPagedPoolUsage: 232\n" + + "QuotaPeakPagedPoolUsage: 1247\n" + + "ReadOperationCount: 1549009\n" + + "ReadTransferCount: 1148378511\n" + + "SessionId: 1\n" + + "Status:\n" + + "TerminationDate:\n" + + "ThreadCount: 39\n" + + "UserModeTime: 1948764492\n" + + "VirtualSize: 567984128\n" + + "WindowsVersion: 6.1.7601\n" + + "WorkingSetSize: 144740352\n" + + "WriteOperationCount: 1793811\n" + + "WriteTransferCount: 1503510606\n"; + private static final String WMI_PROCESS4 = "Caption: java.exe\n" + + "CommandLine: \"C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\bin\\java\" -Djava.awt.headless=true -Didea.version==2016.1.2 -Xmx512m -Didea.maven.embedder.version=3.0.5 -Dfile.encoding=windows-1251 -classpath \"C:/Program Files (x86)/JetBrains/IntelliJ IDEA Community Edition 2016.1/lib/resources_en.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\log4j.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\slf4j-api-1.7.10.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\slf4j-log4j12-1.7.10.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\jna.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\resources_en.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\util.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\oromatcher.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\annotations.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\snappy-in-java-0.3.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\trove4j.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\jna-platform.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\jdom.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\picocontainer.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\lucene-core-2.4.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven-server-api.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3-server-common.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3-server-lib\\archetype-catalog-2.2.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3-server-lib\\archetype-common-2.2.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3-server-lib\\maven-dependency-tree-1.2.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3-server-lib\\nexus-indexer-3.0.4.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3-server-lib\\nexus-indexer-artifact-1.0.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven30-server-impl.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\aether-api-1.13.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\aether-connector-wagon-1.13.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\aether-impl-1.13.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\aether-spi-1.13.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\aether-util-1.13.1.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\commons-cli-1.2.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\commons-io-2.2.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\commons-lang-2.6.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-aether-provider-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-artifact-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-compat-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-core-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-embedder-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-model-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-model-builder-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-plugin-api-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-repository-metadata-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-settings-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\maven-settings-builder-3.0.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\plexus-cipher-1.7.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\plexus-component-annotations-1.5.5.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\plexus-interpolation-1.14.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\plexus-sec-dispatcher-1.3.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\plexus-utils-2.0.6.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\sisu-guava-0.9.9.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\sisu-guice-3.1.0-no_aop.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\sisu-inject-bean-2.3.0.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\sisu-inject-plexus-2.3.0.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\wagon-file-2.8.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\wagon-http-2.8-shaded.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\wagon-http-shared-2.8.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\lib\\wagon-provider-api-2.8.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\maven\\lib\\maven3\\boot\\plexus-classworlds-2.4.jar\" org.jetbrains.idea.maven.server.RemoteMavenServer\n" + + "CreationClassName: Win32_Process\n" + + "CreationDate: 20160514135845.836304+180\n" + + "CSCreationClassName: Win32_ComputerSystem\n" + + "CSName: DENIS-PC\n" + + "Description: java.exe\n" + + "ExecutablePath: C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\bin\\java.exe\n" + + "ExecutionState: \n" + + "Handle: 6700\n" + + "HandleCount: 324\n" + + "InstallDate: \n" + + "KernelModeTime: 1716011\n" + + "MaximumWorkingSetSize: 1380\n" + + "MinimumWorkingSetSize: 200\n" + + "Name: java.exe\n" + + "OSCreationClassName: Win32_OperatingSystem\n" + + "OSName: Microsoft Windows 7 Ultimate |C:\\Windows|\\Device\\Harddisk0\\Partition2\n" + + "OtherOperationCount: 12619\n" + + "OtherTransferCount: 274107\n" + + "PageFaults: 34915\n" + + "PageFileUsage: 240784\n" + + "ParentProcessId: 3644\n" + + "PeakPageFileUsage: 245768\n" + + "PeakVirtualSize: 2058690560\n" + + "PeakWorkingSetSize: 94992\n" + + "Priority: 8\n" + + "PrivatePageCount: 246562816\n" + + "ProcessId: 6700\n" + + "QuotaNonPagedPoolUsage: 22\n" + + "QuotaPagedPoolUsage: 169\n" + + "QuotaPeakNonPagedPoolUsage: 26\n" + + "QuotaPeakPagedPoolUsage: 171\n" + + "ReadOperationCount: 9873\n" + + "ReadTransferCount: 11487969\n" + + "SessionId: 1\n" + + "Status: \n" + + "TerminationDate: \n" + + "ThreadCount: 24\n" + + "UserModeTime: 18252117\n" + + "VirtualSize: 2054496256\n" + + "WindowsVersion: 6.1.7601\n" + + "WorkingSetSize: 85766144\n" + + "WriteOperationCount: 118\n" + + "WriteTransferCount: 3848\n"; + + private static final String WMI_PROCESS5 = "Caption: java.exe\n" + + "CommandLine: \"C:\\Program Files\\Java\\jdk1.8.0_60\\bin\\java\" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:53995,suspend=y,server=n -ea -Didea.junit.sm_runner -Dfile.encoding=UTF-8 -classpath \"C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\lib\\idea_rt.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 2016.1\\plugins\\junit\\lib\\junit-rt.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\charsets.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\deploy.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\javaws.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\jce.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\jfr.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\jfxswt.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\jsse.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\management-agent.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\plugin.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\resources.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\rt.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\access-bridge-64.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\cldrdata.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\dnsns.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\jaccess.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\jfxrt.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\localedata.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\nashorn.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\sunec.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\sunjce_provider.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\sunmscapi.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\sunpkcs11.jar;C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\ext\\zipfs.jar;D:\\dev\\jProcesses\\target\\test-classes;D:\\dev\\jProcesses\\target\\classes;C:\\Users\\Denis\\.m2\\repository\\junit\\junit\\4.10\\junit-4.10.jar;C:\\Users\\Denis\\.m2\\repository\\org\\hamcrest\\hamcrest-core\\1.1\\hamcrest-core-1.1.jar;C:\\Users\\Denis\\.m2\\repository\\com\\profesorfalken\\WMI4Java\\1.1\\WMI4Java-1.1.jar;C:\\Users\\Denis\\.m2\\repository\\com\\profesorfalken\\jPowerShell\\1.3\\jPowerShell-1.3.jar;C:\\Users\\Denis\\.m2\\repository\\org\\mockito\\mockito-all\\1.8.4\\mockito-all-1.8.4.jar\" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 org.jutils.jprocesses.info.WindowsProcessesServiceTest,testGetListWithName\n" + + "CreationClassName: Win32_Process\n" + + "CreationDate: 20160514151848.264987+180\n" + + "CSCreationClassName: Win32_ComputerSystem\n" + + "CSName: DENIS-PC\n" + + "Description: java.exe\n" + + "ExecutablePath: C:\\Program Files\\Java\\jdk1.8.0_60\\bin\\java.exe\n" + + "ExecutionState: \n" + + "Handle: 8160\n" + + "HandleCount: 252\n" + + "InstallDate: \n" + + "KernelModeTime: 2808018\n" + + "MaximumWorkingSetSize: 1380\n" + + "MinimumWorkingSetSize: 200\n" + + "Name: java.exe\n" + + "OSCreationClassName: Win32_OperatingSystem\n" + + "OSName: Microsoft Windows 7 Ultimate |C:\\Windows|\\Device\\Harddisk0\\Partition2\n" + + "OtherOperationCount: 7038\n" + + "OtherTransferCount: 163028\n" + + "PageFaults: 118732\n" + + "PageFileUsage: 892380\n" + + "ParentProcessId: 3644\n" + + "PeakPageFileUsage: 894172\n" + + "PeakVirtualSize: 3683155968\n" + + "PeakWorkingSetSize: 462196\n" + + "Priority: 8\n" + + "PrivatePageCount: 913797120\n" + + "ProcessId: 8160\n" + + "QuotaNonPagedPoolUsage: 20\n" + + "QuotaPagedPoolUsage: 178\n" + + "QuotaPeakNonPagedPoolUsage: 119\n" + + "QuotaPeakPagedPoolUsage: 178\n" + + "ReadOperationCount: 4176\n" + + "ReadTransferCount: 6494968\n" + + "SessionId: 1\n" + + "Status: \n" + + "TerminationDate: \n" + + "ThreadCount: 20\n" + + "UserModeTime: 8268053\n" + + "VirtualSize: 3683155968\n" + + "WindowsVersion: 6.1.7601\n" + + "WorkingSetSize: 472100864\n" + + "WriteOperationCount: 14\n" + + "WriteTransferCount: 6635\n"; + + private static final String WMI_PROCESS1_PERF = "Caption: \n" + + "CreatingProcessID: 2548\n" + + "Description: \n" + + "ElapsedTime: 361\n" + + "Frequency_Object: \n" + + "Frequency_PerfTime: \n" + + "Frequency_Sys100NS: \n" + + "HandleCount: 1739\n" + + "IDProcess: 6936\n" + + "IODataBytesPersec: 3631675\n" + + "IODataOperationsPersec: 265\n" + + "IOOtherBytesPersec: 949\n" + + "IOOtherOperationsPersec: 296\n" + + "IOReadBytesPersec: 3630607\n" + + "IOReadOperationsPersec: 146\n" + + "IOWriteBytesPersec: 1068\n" + + "IOWriteOperationsPersec: 118\n" + + "Name: DOOMx64\n" + + "PageFaultsPersec: 0\n" + + "PageFileBytes: 2646224896\n" + + "PageFileBytesPeak: 2648330240\n" + + "PercentPrivilegedTime: 67\n" + + "PercentProcessorTime: 100\n" + + "PercentUserTime: 80\n" + + "PoolNonpagedBytes: 301176\n" + + "PoolPagedBytes: 2782520\n" + + "PriorityBase: 8\n" + + "PrivateBytes: 2646224896\n" + + "ThreadCount: 36\n" + + "Timestamp_Object: \n" + + "Timestamp_PerfTime: \n" + + "Timestamp_Sys100NS: \n" + + "VirtualBytes: 3179716608\n" + + "VirtualBytesPeak: 3231305728\n" + + "WorkingSet: 2166816768\n" + + "WorkingSetPeak: 2166833152\n" + + "WorkingSetPrivate: 1682055168\n"; + private static final String WMI_PROCESS2_PERF = "Caption: \n" + + "CreatingProcessID: 500\n" + + "Description: \n" + + "ElapsedTime: 3298\n" + + "Frequency_Object: \n" + + "Frequency_PerfTime: \n" + + "Frequency_Sys100NS: \n" + + "HandleCount: 917\n" + + "IDProcess: 3644\n" + + "IODataBytesPersec: 1867\n" + + "IODataOperationsPersec: 11\n" + + "IOOtherBytesPersec: 13672\n" + + "IOOtherOperationsPersec: 854\n" + + "IOReadBytesPersec: 1867\n" + + "IOReadOperationsPersec: 11\n" + + "IOWriteBytesPersec: 0\n" + + "IOWriteOperationsPersec: 0\n" + + "Name: idea64\n" + + "PageFaultsPersec: 3\n" + + "PageFileBytes: 1155874816\n" + + "PageFileBytesPeak: 1166286848\n" + + "PercentPrivilegedTime: 0\n" + + "PercentProcessorTime: 6\n" + + "PercentUserTime: 6\n" + + "PoolNonpagedBytes: 74096\n" + + "PoolPagedBytes: 310792\n" + + "PriorityBase: 8\n" + + "PrivateBytes: 1155874816\n" + + "ThreadCount: 65\n" + + "Timestamp_Object: \n" + + "Timestamp_PerfTime: \n" + + "Timestamp_Sys100NS: \n" + + "VirtualBytes: 4290207744\n" + + "VirtualBytesPeak: 4300398592\n" + + "WorkingSet: 1061761024\n" + + "WorkingSetPeak: 1097666560\n" + + "WorkingSetPrivate: 1050238976\n"; + private static final String WMI_PROCESS3_PERF = "Caption: \n" + + "CreatingProcessID: 500\n" + + "Description: \n" + + "ElapsedTime: 10753\n" + + "Frequency_Object: \n" + + "Frequency_PerfTime: \n" + + "Frequency_Sys100NS: \n" + + "HandleCount: 1259\n" + + "IDProcess: 2876\n" + + "IODataBytesPersec: 236304\n" + + "IODataOperationsPersec: 522\n" + + "IOOtherBytesPersec: 443\n" + + "IOOtherOperationsPersec: 106\n" + + "IOReadBytesPersec: 107811\n" + + "IOReadOperationsPersec: 174\n" + + "IOWriteBytesPersec: 128493\n" + + "IOWriteOperationsPersec: 348\n" + + "Name: chrome\n" + + "PageFaultsPersec: 0\n" + + "PageFileBytes: 107134976\n" + + "PageFileBytesPeak: 141369344\n" + + "PercentPrivilegedTime: 0\n" + + "PercentProcessorTime: 0\n" + + "PercentUserTime: 0\n" + + "PoolNonpagedBytes: 63448\n" + + "PoolPagedBytes: 870848\n" + + "PriorityBase: 8\n" + + "PrivateBytes: 107134976\n" + + "ThreadCount: 37\n" + + "Timestamp_Object: \n" + + "Timestamp_PerfTime: \n" + + "Timestamp_Sys100NS: \n" + + "VirtualBytes: 565362688\n" + + "VirtualBytesPeak: 764755968\n" + + "WorkingSet: 132898816\n" + + "WorkingSetPeak: 175366144\n" + + "WorkingSetPrivate: 86532096\n"; + private static final String WMI_PROCESS4_PERF = "Caption: \n" + + "CreatingProcessID: 3644\n" + + "Description: \n" + + "ElapsedTime: 4808\n" + + "Frequency_Object: \n" + + "Frequency_PerfTime: \n" + + "Frequency_Sys100NS: \n" + + "HandleCount: 325\n" + + "IDProcess: 6700\n" + + "IODataBytesPersec: 0\n" + + "IODataOperationsPersec: 0\n" + + "IOOtherBytesPersec: 0\n" + + "IOOtherOperationsPersec: 0\n" + + "IOReadBytesPersec: 0\n" + + "IOReadOperationsPersec: 0\n" + + "IOWriteBytesPersec: 0\n" + + "IOWriteOperationsPersec: 0\n" + + "Name: java\n" + + "PageFaultsPersec: 0\n" + + "PageFileBytes: 246562816\n" + + "PageFileBytesPeak: 251666432\n" + + "PercentPrivilegedTime: 0\n" + + "PercentProcessorTime: 2\n" + + "PercentUserTime: 0\n" + + "PoolNonpagedBytes: 23296\n" + + "PoolPagedBytes: 172744\n" + + "PriorityBase: 8\n" + + "PrivateBytes: 246562816\n" + + "ThreadCount: 24\n" + + "Timestamp_Object: \n" + + "Timestamp_PerfTime: \n" + + "Timestamp_Sys100NS: \n" + + "VirtualBytes: 2054496256\n" + + "VirtualBytesPeak: 2058690560\n" + + "WorkingSet: 85757952\n" + + "WorkingSetPeak: 97271808\n" + + "WorkingSetPrivate: 80453632\n"; + private static final String WMI_PROCESS5_PERF = "Caption: \n" + + "CreatingProcessID: 3644\n" + + "Description: \n" + + "ElapsedTime: 5\n" + + "Frequency_Object: \n" + + "Frequency_PerfTime: \n" + + "Frequency_Sys100NS: \n" + + "HandleCount: 245\n" + + "IDProcess: 8160\n" + + "IODataBytesPersec: 0\n" + + "IODataOperationsPersec: 0\n" + + "IOOtherBytesPersec: 0\n" + + "IOOtherOperationsPersec: 0\n" + + "IOReadBytesPersec: 0\n" + + "IOReadOperationsPersec: 0\n" + + "IOWriteBytesPersec: 0\n" + + "IOWriteOperationsPersec: 0\n" + + "Name: java#1\n" + + "PageFaultsPersec: 0\n" + + "PageFileBytes: 250654720\n" + + "PageFileBytesPeak: 255041536\n" + + "PercentPrivilegedTime: 0\n" + + "PercentProcessorTime: 3\n" + + "PercentUserTime: 0\n" + + "PoolNonpagedBytes: 19704\n" + + "PoolPagedBytes: 179368\n" + + "PriorityBase: 8\n" + + "PrivateBytes: 250654720\n" + + "ThreadCount: 20\n" + + "Timestamp_Object: \n" + + "Timestamp_PerfTime: \n" + + "Timestamp_Sys100NS: \n" + + "VirtualBytes: 3683155968\n" + + "VirtualBytesPeak: 3683155968\n" + + "WorkingSet: 40710144\n" + + "WorkingSetPeak: 45494272\n" + + "WorkingSetPrivate: 30330880\n"; + +} diff --git a/src/test/java/org/jutils/jprocesses/util/ProcessesUtilsTest.java b/src/test/java/org/jutils/jprocesses/util/ProcessesUtilsTest.java index 4d83ec1..ff610be 100644 --- a/src/test/java/org/jutils/jprocesses/util/ProcessesUtilsTest.java +++ b/src/test/java/org/jutils/jprocesses/util/ProcessesUtilsTest.java @@ -1,24 +1,24 @@ -package org.jutils.jprocesses.util; - -import org.junit.Test; - -import java.text.ParseException; -import java.util.Locale; - -import static org.junit.Assert.*; - -@SuppressWarnings("Since15") -public class ProcessesUtilsTest { - @Test - public void getCustomDateFormat() throws Exception { - assertEquals("10/23/2016 08:30:00", ProcessesUtils.parseUnixLongTimeToFullDate("oct 23 08:30:00 2016")); - try { - ProcessesUtils.parseUnixLongTimeToFullDate("23 okt 2016 08:30:00"); - fail(); - } catch (ParseException e) {} - ProcessesUtils.setCustomDateFormat("dd MMM yyyy HH:mm:ss"); - ProcessesUtils.setCustomLocale(Locale.forLanguageTag("no")); - assertEquals("10/23/2016 08:30:00", ProcessesUtils.parseUnixLongTimeToFullDate("23 okt 2016 08:30:00")); - } - +package org.jutils.jprocesses.util; + +import org.junit.Test; + +import java.text.ParseException; +import java.util.Locale; + +import static org.junit.Assert.*; + +@SuppressWarnings("Since15") +public class ProcessesUtilsTest { + @Test + public void getCustomDateFormat() throws Exception { + assertEquals("10/23/2016 08:30:00", ProcessesUtils.parseUnixLongTimeToFullDate("oct 23 08:30:00 2016")); + try { + ProcessesUtils.parseUnixLongTimeToFullDate("23 okt 2016 08:30:00"); + fail(); + } catch (ParseException e) {} + ProcessesUtils.setCustomDateFormat("dd MMM yyyy HH:mm:ss"); + ProcessesUtils.setCustomLocale(Locale.forLanguageTag("no")); + assertEquals("10/23/2016 08:30:00", ProcessesUtils.parseUnixLongTimeToFullDate("23 okt 2016 08:30:00")); + } + } \ No newline at end of file