tag:blogger.com,1999:blog-68663027722565726502024-02-20T00:03:59.461+00:00GarciaPL Notes - Java and OthersMost sophisticated informations and issues related with widely known IT sector and many more ;-)GarciaPLhttp://www.blogger.com/profile/08890456444783994039noreply@blogger.comBlogger144125tag:blogger.com,1999:blog-6866302772256572650.post-32462653626505597792020-11-12T19:34:00.003+00:002020-11-12T19:34:39.905+00:00Linux - Disk is Read Only. Windows 10 Dual Boot as a culprit<p> </p><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/CdLcJLKEMbY" width="320" youtube-src-id="CdLcJLKEMbY"></iframe></div><br /><p></p>GarciaPLhttp://www.blogger.com/profile/08890456444783994039noreply@blogger.com0tag:blogger.com,1999:blog-6866302772256572650.post-15513549342037210462020-11-12T19:27:00.002+00:002020-11-12T19:28:16.450+00:00JodaTime - week number of business date<p>If you would like to figure out week number in the month of your business date by using JodaTime in Java, please check below code snippet</p><p>Examples</p><p></p><ul style="text-align: left;"><li>Week number in the month for business date of 2020-10-07 is 2</li><li>Week number in the month for business date of 2020-10-21 is 4</li></ul><div><br /></div><div><br class="Apple-interchange-newline" /><span style="font-family: monospace;"><span style="white-space: pre;">LocalDate firstDayOfTheMonth = businessDate.dayOfMonth().withMinimumValue();
int weekOfMonth = Weeks.weeksBetween(firstDayOfTheMonth, businessDate.dayOfWeek().withMaximumValue().plusDays(1)).getWeeks();</span></span></div><p></p>GarciaPLhttp://www.blogger.com/profile/08890456444783994039noreply@blogger.com0tag:blogger.com,1999:blog-6866302772256572650.post-20372762009779513532020-07-02T14:26:00.004+01:002020-07-21T09:16:20.319+01:00IntelliJ Live TemplatesNew platform with IntelliJ Live Templates has been released! You can find it here <a href="https://intellijlivetemplates.netlify.app/" target="_blank">IntelliJ Live Templates</a><div><br /></div><div><div>Recently I had a desire to explore a little bit of how static websites are generated using modern stack like React. I choose at the time open-source framework called <a href="https://www.gatsbyjs.org">Gatsby</a> </div></div><div><br /></div><div><div>Gatsby has lots of advantages:</div><div>* Automatic routing based on your directory structure, so no extra code for configuring the router is needed</div><div>* Easily extensible by plugins</div><div>* Optimized for speed as only critical parts of your website are loaded</div><div>* Easy data integration with sources like CMSs, APIs, databases, file system (CSV, JSON, Markdown)</div></div><div><br /></div><div><div>Using that technology I had created a simple project called <b>IntelliJ Live Templates</b> which provides listings of live templates ready to be used by developers in <a href="https://www.jetbrains.com/idea/">IntelliJ IDEA</a>. Explanation of how live templates are used is placed on a project website under section About.</div><div><br /></div><div>Data to fill out the website is taken from the repository placed on Github in the form of Markdown files</div></div>GarciaPLhttp://www.blogger.com/profile/08890456444783994039noreply@blogger.com0tag:blogger.com,1999:blog-6866302772256572650.post-62042398987152894422020-04-17T19:30:00.006+01:002020-07-02T14:28:05.672+01:00DataGrip of IntelliJ - Ticket around new feature for SQL<div style="color: #1f2326; font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", Ubuntu, Cantarell, "Helvetica Neue", Arial, sans-serif; font-size: 14px; margin-bottom: 1em;">
What steps will reproduce the problem?</div>
<div style="color: #1f2326; font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", Ubuntu, Cantarell, "Helvetica Neue", Arial, sans-serif; font-size: 14px; margin-bottom: 1em; margin-top: 1em;">
When you create a new table with a constraint on column STATUS, it's difficult later on to amend that constraint as Oracle assigns a random name for it</div>
<pre class="wikicode prettyprint prettyprinted" style="border-radius: 3px; border: 1px solid rgb(136, 136, 136); color: var(--ring-secondary-color); font-family: var(--ring-font-family-monospace); font-size: 14px; line-height: 17px; margin-bottom: 3px; margin-top: 3px; max-height: 400px; overflow-x: auto; padding: 8px;"><code style="font-family: var(--ring-font-family-monospace);"><span class="pln" style="color: var(--ring-text-color);">CREATE TABLE MY_TABLE </span><span class="pun" style="color: var(--ring-secondary-color);">(</span><span class="pln" style="color: var(--ring-text-color);">
ID NUMERIC</span><span class="pun" style="color: var(--ring-secondary-color);">(</span><span class="lit" style="color: var(--ring-code-keyword-color);">19</span><span class="pun" style="color: var(--ring-secondary-color);">,</span><span class="lit" style="color: var(--ring-code-keyword-color);">0</span><span class="pun" style="color: var(--ring-secondary-color);">)</span><span class="pln" style="color: var(--ring-text-color);"> NOT NULL</span><span class="pun" style="color: var(--ring-secondary-color);">,</span><span class="pln" style="color: var(--ring-text-color);">
STATUS NVARCHAR2</span><span class="pun" style="color: var(--ring-secondary-color);">(</span><span class="lit" style="color: var(--ring-code-keyword-color);">20</span><span class="pun" style="color: var(--ring-secondary-color);">)</span><span class="pln" style="color: var(--ring-text-color);"> NOT NULL CHECK </span><span class="pun" style="color: var(--ring-secondary-color);">(</span><span class="pln" style="color: var(--ring-text-color);">STATUS IN </span><span class="pun" style="color: var(--ring-secondary-color);">(</span><span class="str" style="color: var(--ring-code-string-color);">'OPEN'</span><span class="pun" style="color: var(--ring-secondary-color);">,</span><span class="pln" style="color: var(--ring-text-color);"> </span><span class="str" style="color: var(--ring-code-string-color);">'CLOSED'</span><span class="pun" style="color: var(--ring-secondary-color);">));</span><span class="pln" style="color: var(--ring-text-color);">
</span><span class="pun" style="color: var(--ring-secondary-color);">);</span></code></pre>
<div style="color: #1f2326; font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", Ubuntu, Cantarell, "Helvetica Neue", Arial, sans-serif; font-size: 14px; margin-bottom: 1em; margin-top: 1em;">
What is the expected result?</div>
<div style="color: #1f2326; font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", Ubuntu, Cantarell, "Helvetica Neue", Arial, sans-serif; font-size: 14px; margin-bottom: 1em; margin-top: 1em;">
Getting a warning that it's better to create column constraint using a separate statement like</div>
<pre class="wikicode prettyprint prettyprinted" style="border-radius: 3px; border: 1px solid rgb(136, 136, 136); color: var(--ring-secondary-color); font-family: var(--ring-font-family-monospace); font-size: 14px; line-height: 17px; margin-top: 3px; max-height: 400px; overflow-x: auto; padding: 8px;"><code style="font-family: var(--ring-font-family-monospace);"><span class="pln" style="color: var(--ring-text-color);">ALTER TABLE MY_TABLE ADD CONSTRAINT STATUS_VALIDATION CHECK </span><span class="pun" style="color: var(--ring-secondary-color);">(</span><span class="pln" style="color: var(--ring-text-color);">STATUS IN </span><span class="pun" style="color: var(--ring-secondary-color);">(</span><span class="str" style="color: var(--ring-code-string-color);">'OPEN'</span><span class="pun" style="color: var(--ring-secondary-color);">,</span><span class="pln" style="color: var(--ring-text-color);"> </span><span class="str" style="color: var(--ring-code-string-color);">'CLOSED'</span><span class="pun" style="color: var(--ring-secondary-color);">));</span></code></pre>
<br />
<span style="font-size: large;">Link to a ticket - <a href="https://youtrack.jetbrains.com/issue/DBE-10549">https://youtrack.jetbrains.com/issue/DBE-10549</a></span>GarciaPLhttp://www.blogger.com/profile/08890456444783994039noreply@blogger.com0tag:blogger.com,1999:blog-6866302772256572650.post-14417515112678495812020-04-16T19:24:00.001+01:002020-04-16T19:24:28.495+01:00How drop unnamed column constraint in OracleIf constraint on column STATUS was created without a name during creating a table, Oracle will assign a random name for it. Unfortunately, we cannot modify the constraint directly.<br />
<br />
Steps involved of dropping unnamed constraint linked to column STATUS<br />
<ol>
<li>Duplicate STATUS field into a new field STATUS2</li>
<li>Define CHECK constraints on STATUS2</li>
<li>Migrate data from STATUS into STATUS2</li>
<li>Drop STATUS column</li>
<li>Rename STATUS2 to STATUS</li>
</ol>
<div>
<br /></div>
<div>
<br /></div>
<pre style="line-height: 125%; margin: 0;">ALTER TABLE MY_TABLE ADD STATUS2 NVARCHAR2(10) DEFAULT 'OPEN';
ALTER TABLE MY_TABLE ADD CONSTRAINT MY_TABLE_CHECK_STATUS CHECK (STATUS2 IN ('OPEN', 'CLOSED'));
UPDATE MY_TABLE SET STATUS2 = STATUS;
ALTER TABLE MY_TABLE DROP COLUMN STATUS;
ALTER TABLE MY_TABLE RENAME COLUMN STATUS2 TO STATUS;
</pre>
GarciaPLhttp://www.blogger.com/profile/08890456444783994039noreply@blogger.com0tag:blogger.com,1999:blog-6866302772256572650.post-39506946296967440282020-03-05T20:56:00.001+00:002020-03-05T20:56:03.022+00:00The system cannot find the path specified - AnacondaI have installed Anaconda recently on my Windows 10 at work. I have been playing with it for a while, but then I have decided to uninstall it. Without restarting my computer, after a few days, I have decided to install Anaconda once again as I have found that I need it at the end.<br />
<br />
I was astonished that after starting Power Shell or Cmder which emulates terminal as well, I was started getting an error listed below. It was very annoying as somehow some features like Scoop [1] and even Maven stopped working!<br />
<br />
<br />
<b>The system cannot find the path specified</b><br />
<br />
<br />
Unfortunately, Windows 10 was not able to tell me which path he struggles with at this point. I started digging in the web to find out that the main reason for whole fuss was Anaconda which supposes to be deleted completely after the restart of PC which did not happen obviously.<br />
<br />
<br />
Solution<br />
<br />
Go to <b>HKCU\Software\Microsoft\Command Processor\AutoRun </b><br />
<br />
using Register Editor and clear found value. I presume that whatever you are going to find might be related to Anaconda as it was so in my case.<br />
<br />
<br />
<br />
[1] Scoop - <a href="https://scoop.sh/">https://scoop.sh</a>GarciaPLhttp://www.blogger.com/profile/08890456444783994039noreply@blogger.com0tag:blogger.com,1999:blog-6866302772256572650.post-52927043366349818692019-08-24T23:02:00.000+01:002019-08-24T23:02:07.981+01:00Lombok - mandatory field in builder using builderMethodName@Builder(<b>builderMethodName = "mandatoryBuilder"</b>)<br />
@ToString<br />
public class MyClass {<br />
<br />
private String name;<br />
private String identifier;<br />
<br />
public String getName() {<br />
return name;<br />
}<br />
<br />
public String getIdentifier() {<br />
return identifier;<br />
}<br />
<br />
public static MyClassBuilder builder(String identifier) {<br />
return mandatoryBuilder().identifier(identifier);<br />
}<br />
<br />
}GarciaPLhttp://www.blogger.com/profile/08890456444783994039noreply@blogger.com0tag:blogger.com,1999:blog-6866302772256572650.post-35268560944546505622019-06-26T20:49:00.001+01:002019-06-26T20:49:30.324+01:00Tooltip does not show up for disabled button - ExtJSIf you are going to define tooltip for component in ExtJS (especially within version 6.7 and higher as I had chance to work with it) which might be disabled, unfortunately you will not be able to see. This component needs to be enabled that you might see your tooltip. Hopefully there is a way to fix it.<br />
<br />
Just enrich your component (button in this case) with <b>pointerEvents: 'all'</b><br />
<br />
var button = {<br />
xtype: 'button',<br />
tooltip: 'Your tooltip',<br />
text: 'Your Text',<br />
style: {<br />
pointerEvents: 'all'<br />
}<br />
};GarciaPLhttp://www.blogger.com/profile/08890456444783994039noreply@blogger.com0tag:blogger.com,1999:blog-6866302772256572650.post-69467713801101314932019-04-01T19:22:00.002+01:002019-04-01T19:22:29.026+01:00IntelliJ - Command line is too long<br /><br />Inside your .idea folder, change <b>workspace.xml</b> file add below property<br /><br /><b><property name="dynamic.classpath" value="true" /></b><br /><br />to<br /><br /><b><component name="PropertiesComponent"></b>GarciaPLhttp://www.blogger.com/profile/08890456444783994039noreply@blogger.com0tag:blogger.com,1999:blog-6866302772256572650.post-17630077450416412402019-01-04T20:26:00.001+00:002019-01-04T20:26:22.783+00:00Conda - use python package from pypiHave you ever tried to install package available in <b>pypi</b>, but not in official anaconda repository? There is a way to install it using Anaconda terminal.<br />
<br />
<ol>
<li>conda skeleton pypi PACKAGE</li>
<li>conda build PACKAGE</li>
</ol>
GarciaPLhttp://www.blogger.com/profile/08890456444783994039noreply@blogger.com0tag:blogger.com,1999:blog-6866302772256572650.post-33506390403957753692018-07-13T21:34:00.002+01:002018-07-13T21:34:50.819+01:00Python rstrip in JavaLast time I was working on migration of some code from Python into Java and I was trying to find a corresponding code in Java for a function called <b>rstrip</b> in Python. So, below you can see a code written in Python and solution written in Java produced of course using Guava.<br />
<div style="overflow: auto; width: auto;">
<pre style="line-height: 125%; margin: 0;">icb_code<span style="color: #333333;">.</span>rstrip(<span style="background-color: #fff0f0;">"0"</span>)
</pre>
</div>
<br />
<br />
<div style="overflow: auto; width: auto;">
<pre style="line-height: 125%; margin: 0;">CharMatcher trailingZeroMatcher <span style="color: #333333;">=</span> CharMatcher<span style="color: #333333;">.</span><span style="color: #0000cc;">is</span><span style="color: #333333;">(</span><span style="color: #0044dd;">'0'</span><span style="color: #333333;">);</span>
String trimmedInput <span style="color: #333333;">=</span> trailingZeroMatcher<span style="color: #333333;">.</span><span style="color: #0000cc;">trimTrailingFrom</span><span style="color: #333333;">(</span>input<span style="color: #333333;">);</span>
</pre>
</div>
GarciaPLhttp://www.blogger.com/profile/08890456444783994039noreply@blogger.com0tag:blogger.com,1999:blog-6866302772256572650.post-2379530989041074542018-07-11T22:43:00.001+01:002018-07-11T22:43:26.380+01:00Case Insensitive Map in JavaI needed recently to create a case-insensitive map in Java to query it with keys which might be uppercase or lowercase. I thought that it should be somewhere some kind of solution already available for developers. I found a class called CaseInsensitiveMap in Apache Commons Collections package (<a href="https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/map/CaseInsensitiveMap.html">https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/map/CaseInsensitiveMap.html</a>) but unfortunately, I found later on that it does not support generics which was needed in my case, that's why I decided to go with a custom solution for that problem which might be found below.<br />
<br />
<div style="overflow: auto; width: auto;">
<pre style="line-height: 125%; margin: 0;">Map<span style="color: #333333;"><</span>String<span style="color: #333333;">,</span> String<span style="color: #333333;">></span> record <span style="color: #333333;">=</span> getRecord<span style="color: #333333;">();</span>
TreeMap<span style="color: #333333;"><</span>String<span style="color: #333333;">,</span> String<span style="color: #333333;">></span> caseInsensitiveMap <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> TreeMap<span style="color: #333333;"><></span>getCaseInsensitiveComparator<span style="color: #333333;">());</span>
caseInsensitiveMap<span style="color: #333333;">.</span><span style="color: #0000cc;">putAll</span><span style="color: #333333;">(</span>record<span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">protected</span> Comparator<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> <span style="color: #0066bb; font-weight: bold;">getCaseInsensitiveComparator</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> Comparator<span style="color: #333333;">.</span><span style="color: #0000cc;">nullsFirst</span><span style="color: #333333;">(</span>String<span style="color: #333333;">.</span><span style="color: #0000cc;">CASE_INSENSITIVE_ORDER</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
</pre>
</div>
GarciaPLhttp://www.blogger.com/profile/08890456444783994039noreply@blogger.com0tag:blogger.com,1999:blog-6866302772256572650.post-59355386508413284892018-07-01T13:17:00.000+01:002018-07-01T13:17:36.382+01:00Splitting list by enum property to get map of enum and listLast time I had a problem with how to split the incoming list of a type File, to get a map of list of files by FileType, which is a property of File class. The easiest way to achieve it? <b>Reduce</b>.<br />
<br />
<b>Input</b> -> List<File> listOfFiles<br />
<b>Output</b> -> Map<FileType, List<File>><br />
<br />
<div style="overflow: auto; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">public</span> Map<span style="color: #333333;"><</span>FileType<span style="color: #333333;">,</span> List<span style="color: #333333;"><</span>File<span style="color: #333333;">>></span> <span style="color: #0066bb; font-weight: bold;">splitFilesByFileType</span><span style="color: #333333;">(</span>List<span style="color: #333333;"><</span>File<span style="color: #333333;">></span> listOfFiles<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> listOfFiles<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">()</span>
<span style="color: #333333;">.</span><span style="color: #0000cc;">reduce</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> HashMap<span style="color: #333333;"><>(),</span> <span style="color: #333333;">(</span>filesByFileType<span style="color: #333333;">,</span> file<span style="color: #333333;">)</span> <span style="color: #333333;">-></span> <span style="color: #333333;">{</span>
FileType fileType <span style="color: #333333;">=</span> file<span style="color: #333333;">.</span><span style="color: #0000cc;">getFileType</span><span style="color: #333333;">();</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>filesByFileType<span style="color: #333333;">.</span><span style="color: #0000cc;">containsKey</span><span style="color: #333333;">(</span>fileType<span style="color: #333333;">))</span> <span style="color: #333333;">{</span>
filesByFileType<span style="color: #333333;">.</span><span style="color: #0000cc;">get</span><span style="color: #333333;">(</span>fileType<span style="color: #333333;">).</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span>file<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">else</span> <span style="color: #333333;">{</span>
filesByFileType<span style="color: #333333;">.</span><span style="color: #0000cc;">putIfAbsent</span><span style="color: #333333;">(</span>fileType<span style="color: #333333;">,</span> Lists<span style="color: #333333;">.</span><span style="color: #0000cc;">newArrayList</span><span style="color: #333333;">(</span>file<span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">return</span> filesByFileType<span style="color: #333333;">;</span>
<span style="color: #333333;">},</span> <span style="color: #333333;">(</span>acc1<span style="color: #333333;">,</span> acc2<span style="color: #333333;">)</span> <span style="color: #333333;">-></span>
<span style="color: #333333;">{</span>
acc1<span style="color: #333333;">.</span><span style="color: #0000cc;">putAll</span><span style="color: #333333;">(</span>acc2<span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">return</span> acc1<span style="color: #333333;">;</span>
<span style="color: #333333;">});</span>
<span style="color: #333333;">}</span>
</pre>
</div>
GarciaPLhttp://www.blogger.com/profile/08890456444783994039noreply@blogger.com0tag:blogger.com,1999:blog-6866302772256572650.post-11648782022363853212018-05-13T19:22:00.000+01:002018-05-13T19:22:03.659+01:00IntelliJ IDEA - Ticket around new featureLast time I was thinking about one small feature which might be added into IntelliJ IDEA, that's why I created a task on their ticketing system, so hopefully, all of us might use it one day in the future.<br />
<br />
It's just an idea of a new feature which is basically an additional safety check. So, when a developer runs SQL script in IntelliJ IDEA like 'DELETE FROM tableName', it might show up confirmation if a developer is really sure about dropping data from a database. This dialog box might show up eventually the amount of data to be deleted and environment which is going to be affected. I hope that this feature might save a lot of developer's lives.<br />
<br />
Link to ticket <a href="https://youtrack.jetbrains.com/issue/DBE-6408">https://youtrack.jetbrains.com/issue/DBE-6408</a>GarciaPLhttp://www.blogger.com/profile/08890456444783994039noreply@blogger.com0tag:blogger.com,1999:blog-6866302772256572650.post-20326576336253755182018-04-13T21:14:00.002+01:002018-05-08T21:36:13.055+01:00Enum helper method - avoid equalsDo you still write those special if statements for enums like myEnum.equals(myEnum.TEST) ? If yes, I might be able to share with you a small tip how to avoid it! It will be very beneficial for you because of your code might express itself without a word! Moreover, you might avoid those imports of your enum fields in every class where the comparison is going to be defined by if statement. It will say you a lot of time in case of changing name of enum field.<br />
<br />
For instance, for the example defined below, only what you need to do in your code is -> if (operation.isCount()) {...} instead of if (operations.equals(Operation.COUNTA)) {...}<br />
<br />
Check this post on <b>Medium.com</b> -> <a href="https://medium.com/@lukaszciesluk/use-enum-utility-methods-in-java-avoid-equals-at-all-costs-2158571aca7c">Use enum utility methods in Java ! Avoid equals at all costs!</a><br />
<br />
<div style="overflow: auto; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.apache.commons.lang3.EnumUtils</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">enum</span> Operation <span style="color: #333333;">{</span>
COUNTA<span style="color: #333333;">,</span>
SUM<span style="color: #333333;">,</span></pre>
<pre style="line-height: 125%; margin: 0;"> NA;
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> Operation <span style="color: #0066bb; font-weight: bold;">parse</span><span style="color: #333333;">(</span>String input<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
Operation operation <span style="color: #333333;">=</span> EnumUtils<span style="color: #333333;">.</span><span style="color: #0000cc;">getEnum</span><span style="color: #333333;">(</span>Operation<span style="color: #333333;">.</span><span style="color: #0000cc;">class</span><span style="color: #333333;">,</span> input<span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>operation <span style="color: #333333;">==</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> NA<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">return</span> operation<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">boolean</span> <span style="color: #0066bb; font-weight: bold;">isCount</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">this</span> <span style="color: #333333;">==</span> COUNTA<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">boolean</span> <span style="color: #0066bb; font-weight: bold;">isSum</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">this</span> <span style="color: #333333;">==</span> SUM<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">boolean</span> <span style="color: #0066bb; font-weight: bold;">isCountOrSum</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">this</span> <span style="color: #333333;">==</span> COUNTA <span style="color: #333333;">||</span> <span style="color: #008800; font-weight: bold;">this</span> <span style="color: #333333;">==</span> SUM<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
GarciaPLhttp://www.blogger.com/profile/08890456444783994039noreply@blogger.com0tag:blogger.com,1999:blog-6866302772256572650.post-1850837315539220812018-04-06T21:19:00.002+01:002018-04-06T21:19:33.052+01:00Git move files - stop applying CRLF against my SQL file!Recently, I was trying to move some files, mostly .java's and.sql's, from one maven module into another one. We were just trying to merge two modules into one module, because of we thought that keeping two modules with pretty the same functionality is odd.<br />
<br />
The main point around this movement was to preserve the history of changes made to those files in the past. Everything was fine until we tried to deploy the application on the server. Of course, I forgot to mention that we were using Flyway as a database migration tool. Unexpectedly, one of the file's checksum was not the same as the checksum saved in the database for that SQL file (flyway_schema_history table). We were astonished because of the content of the file did not change at all... but Pull Request on BitBucket was saying that there is a difference. We checked the MD5 for that file before and after the move, it was the same, but for some reason the checksum detected by Flyway was different! To be honest, we spent a lot of time trying to understand why it's happening, and then I found this post made by Richard Tuin [1].<br />
<br />
The solution was just to revert moving of those SQL files up to the previous location and try to create a file called <b>.gitattributes</b>. We defined over there just a single line which was disabling CRLF for that single file for which checksum was not the same as this one stored in flyway schema history table. After moving SQL files once again, using git mv [2] command and uploading results on BitBucket, everything was fine!<br />
<br />
The content of .gitattributes for that single file should look as below :<br />
<br />
fileName.sql -crlf<br />
<br />
<br />
<b>References :</b><br />
<b>[1] <a href="https://www.rtuin.nl/2013/02/how-to-make-git-ignore-different-line-endings/">Richard Tuin - How to make git ignore different line endings</a></b><br />
<b>[2] <a href="https://git-scm.com/docs/git-mv">Git mv</a></b><br />
<b>[3] <a href="https://git-scm.com/docs/gitattributes">Git attributes</a></b><br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />GarciaPLhttp://www.blogger.com/profile/08890456444783994039noreply@blogger.com0tag:blogger.com,1999:blog-6866302772256572650.post-7086682589953203932018-04-02T17:02:00.002+01:002018-04-02T17:02:45.984+01:00Non-blocking IO / Reactive programming - explanationLast time I was looking for an explanation what this "<b>Non-Blocking IO</b>" means. It was hard to understand what it's not blocking and what exactly it does or does not. Moreover, I was trying to find information how this correlates with <b>reactive programming</b> in overall... and I found it! Please check article listed in References. Thanks Adam!<br />
<br />
In an old approach called threaded blocking IO, we bound every new connection to a new socket and create a new thread to process the data. This means that there is a single open connection which is managed by the single thread. When a client requests a connection from the server socket it sends back a new socket to communicate with. Now multiple connections can be handled until the limit of the maximum number of threads is reached. Each thread requires memory allocation, so while the concurrent connection goes up, the performance of the server goes down.<br />
<br />
Instead of binding the thread to a connection, a notification is sent when the data is ready to read from a buffer. NIO uses <b>buffers</b> instead of streams to read/write data because data is processed when it's already there, not when it's actually being received. With this mechanism, multiple connections can be handled by <b>one thread</b>. That's how finally no threads are blocked while waiting on the IO.<br />
The thread is only responsible for reading the buffers when it's notified about the arrival of any new data. This mechanism is called the 'event loop'. Following that logic, lots of concurrent connections can be handled with just a couple of threads. That's why NIO-based servers are so awesome and serve as our first building blocks.<br />
<br />
<b>Reactive programming</b> is about using asynchronous, non-blocking building blocks with a functional style of coding.<br />
<div>
<br /></div>
<b> <span style="font-family: "arial"; font-size: 13px; line-height: 18px; white-space: pre-wrap;">
References :<br />[1] <a href="https://www.kotlindevelopment.com/kotlin-webflux/">Kotlindevelopment.com - Developers guide to Webflux</a></span></b>
GarciaPLhttp://www.blogger.com/profile/08890456444783994039noreply@blogger.com0tag:blogger.com,1999:blog-6866302772256572650.post-72973224924219750762018-03-15T21:09:00.001+00:002018-04-02T13:04:54.375+01:00Compare two beans with DiffBuilderWould you like to compare properties of two, not related beans? Are you tired of using Comparable interface from Java ? You might use a class called <b>DiffBuilder</b> which is a part of Apache Commons !<br />
<br />
<div>
<div style="color: #222222; font-family: arial, sans-serif; font-size: small;">
DiffResult diffResult = new DiffBuilder(firstBean, secondBean, ToStringStyle.SHORT_PREFIX_STYLE)</div>
<div style="color: #222222; font-family: arial, sans-serif; font-size: small;">
.append("id", firstBean.getId(), secondBean.getId())</div>
<div style="color: #222222; font-family: arial, sans-serif; font-size: small;">
.append("type", firstBean.getType(), secondBean.getType())</div>
<div style="color: #222222; font-family: arial, sans-serif; font-size: small;">
.append("conditionName", firstBean.getOther(), secondBean.getOther())</div>
<div style="color: #222222; font-family: arial, sans-serif; font-size: small;">
<br /></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;">boolean hasDifference = diffResult.getNumberOfDiffs() != 0</span></div>
</div>
<br />
<br />
<span style="font-family: "arial"; font-size: 13px; line-height: 18px; white-space: pre-wrap;">
<b>Reference :</b>
<b>[1] <a href="https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/builder/DiffBuilder.html">DiffBuilder - Apache Commons</a></b>
</span>GarciaPLhttp://www.blogger.com/profile/08890456444783994039noreply@blogger.com0tag:blogger.com,1999:blog-6866302772256572650.post-52441966185688342492018-02-22T21:21:00.002+00:002018-02-22T21:21:48.340+00:00Remove password from your pdf payslipsWithin my previous employers, I was getting those payslips protected with a password. One day I thought what is going to happen if I would forget or lose the password! That's why I decided to write a small script in Python which might automate removing password from your payslip (or any other file).<br />
<br />
<br />
<div style="overflow: auto; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #888888;"># Requirements:</span>
<span style="color: #888888;"># pip install PyPDF2</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">os</span>
<span style="color: #008800; font-weight: bold;">from</span> <span style="color: #0e84b5; font-weight: bold;">PyPDF2</span> <span style="color: #008800; font-weight: bold;">import</span> PdfFileReader, PdfFileWriter
password <span style="color: #333333;">=</span> <span style="background-color: #fff0f0;">'password'</span>
directory <span style="color: #333333;">=</span> <span style="background-color: #fff0f0;">'/path/where/pdfs/are/stored'</span>
<span style="color: #008800; font-weight: bold;">def</span> <span style="color: #0066bb; font-weight: bold;">decrypt_pdf</span>(input_path, output_path, password):
<span style="color: #008800; font-weight: bold;">with</span> <span style="color: #007020;">open</span>(input_path, <span style="background-color: #fff0f0;">'rb'</span>) <span style="color: #008800; font-weight: bold;">as</span> input_file, \
<span style="color: #007020;">open</span>(output_path, <span style="background-color: #fff0f0;">'wb'</span>) <span style="color: #008800; font-weight: bold;">as</span> output_file:
reader <span style="color: #333333;">=</span> PdfFileReader(input_file)
reader<span style="color: #333333;">.</span>decrypt(password)
writer <span style="color: #333333;">=</span> PdfFileWriter()
<span style="color: #008800; font-weight: bold;">for</span> i <span style="color: black; font-weight: bold;">in</span> <span style="color: #007020;">range</span>(reader<span style="color: #333333;">.</span>getNumPages()):
writer<span style="color: #333333;">.</span>addPage(reader<span style="color: #333333;">.</span>getPage(i))
writer<span style="color: #333333;">.</span>write(output_file)
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #996633;">__name__</span> <span style="color: #333333;">==</span> <span style="background-color: #fff0f0;">'__main__'</span>:
newDirectory <span style="color: #333333;">=</span> directory <span style="color: #333333;">+</span> <span style="background-color: #fff0f0;">"/Decoded"</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: black; font-weight: bold;">not</span> os<span style="color: #333333;">.</span>path<span style="color: #333333;">.</span>exists(newDirectory):
os<span style="color: #333333;">.</span>makedirs(newDirectory)
<span style="color: #008800; font-weight: bold;">for</span> filename <span style="color: black; font-weight: bold;">in</span> os<span style="color: #333333;">.</span>listdir(directory):
<span style="color: #008800; font-weight: bold;">if</span> filename<span style="color: #333333;">.</span>endswith(<span style="background-color: #fff0f0;">".pdf"</span>):
fullFileName <span style="color: #333333;">=</span> os<span style="color: #333333;">.</span>path<span style="color: #333333;">.</span>join(directory, filename)
decrypt_pdf(fullFileName, newDirectory <span style="color: #333333;">+</span> <span style="background-color: #fff0f0;">"/"</span> <span style="color: #333333;">+</span> filename, password)
</pre>
</div>
<br />
<span style="font-family: "arial"; font-size: 13px; line-height: 18px; white-space: pre-wrap;">
<b>Reference :</b><br /><b>[1] <a href="https://pastebin.com/qbGeUjC4">Pastebin</a></b>
</span>GarciaPLhttp://www.blogger.com/profile/08890456444783994039noreply@blogger.com0tag:blogger.com,1999:blog-6866302772256572650.post-45440159794272184922018-02-18T16:13:00.002+00:002018-02-18T16:13:24.870+00:00Excel Power Query - Load JSON as a tableIf you would like to load JSON and present it as a table, it's quite easy if you have an Excel 2016 [1]. It's more complicated, once you have a lower version.<br />
<br />
First of all, try to install an additional plugin called <b>Power Query </b>[2].<br />
<br />
In the Power Query ribbon tab, click From Other Sources > Blank Query, then go to Advanced Editor and input below query string. Do not forget to change the path to your JSON.<br />
<br />
<div style="overflow: auto; width: auto;">
<pre style="line-height: 125%; margin: 0;">Click <span style="color: #333333;">-></span> Close <span style="color: #333333;">&</span> Load
<span style="color: #008800; font-weight: bold;">let</span>
Source <span style="color: #333333;">=</span> Json.Document(File.Contents(<span style="background-color: #fff0f0;">"Z:\Directory\input.json"</span>)),
AsTable <span style="color: #333333;">=</span> Table.FromRecords(Source)
<span style="color: #008800; font-weight: bold;">in</span>
AsTable
</pre>
</div>
<br />
<span style="font-family: "arial"; font-size: 13px; line-height: 18px; white-space: pre-wrap;">
Click on 'Close & Load'<br />
<b>Reference :</b><br /><b>[1] <a href="https://support.office.com/en-ie/article/connect-to-a-json-file-f65207ab-d957-4bf0-bec3-a08bb53cd4c0">Microsoft Support - Connect to a JSON file</a></b><br /><b>[2] <a href="https://www.microsoft.com/en-ie/download/details.aspx?id=39379">Power Query for Excel</a></b>
</span>GarciaPLhttp://www.blogger.com/profile/08890456444783994039noreply@blogger.com0tag:blogger.com,1999:blog-6866302772256572650.post-44653933042638900692018-02-18T16:00:00.002+00:002018-02-18T16:00:42.642+00:00Creating release notes based on git commit historyIf your JIRA does not support generating release notes because of for instance you do not fulfil Version field like in my case, there is a way to generate it based on git commit history.<br />
<br />
First of all, you need to install a package called <b>git-release-notes</b> [2] via npm. It will allow you to generate release note page from git commit history using the following command :<br />
<br />
<b>git-release-notes 2.2.2..HEAD markdown.ejs > changelog.md</b><br />
<b><br /></b>
Of course, 2.2.2 is a git tag.<br />
<br />
You might also use mentioned above template called markdown.ejs which has been written in EJS [3]. In my opinion, this template gives more control in terms how release notes will look like at the end. Of course, you might also depend on built-in template into git-release-notes module.<br />
<br />
<div style="overflow: auto; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #333333;"><%</span> commits.forEach(<span style="color: #008800; font-weight: bold;">function</span> (commit) { <span style="color: #333333;">%></span>
<span style="color: #333333;"><%</span> <span style="color: #008800; font-weight: bold;">if</span>(<span style="color: #333333;">!</span>commit.title.startsWith(<span style="background-color: #fff0f0;">'[maven-release-plugin]'</span>)) { <span style="color: #333333;">%></span>
<span style="color: #333333;"><%=</span> <span style="background-color: #fff0f0;">'http://jira.com/browse/'</span> <span style="color: #333333;">+</span> commit.title <span style="color: #333333;">-%></span>
<span style="color: #333333;"><%</span> } <span style="color: #333333;">%></span>
<span style="color: #333333;"><%</span> }) <span style="color: #333333;">%></span>
</pre>
<pre style="line-height: 125%; margin: 0;"><span style="color: #333333;">
</span></pre>
</div>
<br />
<b style="font-family: arial; font-size: 13px; white-space: pre-wrap;">References :</b><br />
<span style="font-family: "arial"; font-size: 13px; line-height: 18px; white-space: pre-wrap;"><b>[1] <a href="https://confluence.atlassian.com/adminjiraserver071/creating-release-notes-802592502.html">Confluence.atlassian.com - Creating release notes</a></b><br /><b>[2] <a href="https://www.npmjs.com/package/git-release-notes">NPM - git-release-notes</a></b><br /><b>[3] <a href="http://ejs.co/">Ejs.co</a></b>
</span>GarciaPLhttp://www.blogger.com/profile/08890456444783994039noreply@blogger.com0tag:blogger.com,1999:blog-6866302772256572650.post-71398231739189474852018-02-09T19:29:00.001+00:002018-02-09T19:29:53.647+00:00Removing duplicates by field/property in Java 8How to remove duplicates in Java 8 using field or property ? We might use reduce!
<div style="overflow:auto;width:auto;"><pre style="margin: 0; line-height: 125%"><span></span>List<span style="color: #333333"><</span>MyClass<span style="color: #333333">></span> distinctByField <span style="color: #333333">=</span> myList<span style="color: #333333">.</span><span style="color: #0000CC">stream</span><span style="color: #333333">().</span><span style="color: #0000CC">reduce</span><span style="color: #333333">(</span><span style="color: #008800; font-weight: bold">new</span> ArrayList<span style="color: #333333"><>(),</span> <span style="color: #333333">(</span>List<span style="color: #333333"><</span>MyClass<span style="color: #333333">></span> accumulator<span style="color: #333333">,</span> MyClass myClass<span style="color: #333333">)</span> <span style="color: #333333">-></span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">if</span> <span style="color: #333333">(</span>accumulator<span style="color: #333333">.</span><span style="color: #0000CC">stream</span><span style="color: #333333">().</span><span style="color: #0000CC">noneMatch</span><span style="color: #333333">(</span>item <span style="color: #333333">-></span> item<span style="color: #333333">.</span><span style="color: #0000CC">getField</span><span style="color: #333333">().</span><span style="color: #0000CC">equals</span><span style="color: #333333">(</span>myClass<span style="color: #333333">.</span><span style="color: #0000CC">getField</span><span style="color: #333333">())))</span> <span style="color: #333333">{</span>
accumulator<span style="color: #333333">.</span><span style="color: #0000CC">add</span><span style="color: #333333">(</span>MyClass<span style="color: #333333">);</span>
<span style="color: #333333">}</span>
<span style="color: #008800; font-weight: bold">return</span> accumulator<span style="color: #333333">;</span>
<span style="color: #333333">},</span> <span style="color: #333333">(</span>acc1<span style="color: #333333">,</span> acc2<span style="color: #333333">)</span> <span style="color: #333333">-></span> <span style="color: #333333">{</span>
acc1<span style="color: #333333">.</span><span style="color: #0000CC">addAll</span><span style="color: #333333">(</span>acc2<span style="color: #333333">);</span>
<span style="color: #008800; font-weight: bold">return</span> acc1<span style="color: #333333">;</span>
<span style="color: #333333">});</span>
</pre></div>
GarciaPLhttp://www.blogger.com/profile/08890456444783994039noreply@blogger.com0tag:blogger.com,1999:blog-6866302772256572650.post-532909534680769182017-11-20T20:59:00.001+00:002017-11-20T20:59:55.199+00:00Technical Debt - Identifying and Managing<iframe allowfullscreen="" frameborder="0" height="485" marginheight="0" marginwidth="0" scrolling="no" src="//www.slideshare.net/slideshow/embed_code/key/4Ska9Z4WnwjGUi" style="border-width: 1px; border: 1px solid #ccc; margin-bottom: 5px; max-width: 100%;" width="595"> </iframe> <br />
<div style="margin-bottom: 5px;">
<strong> <a href="https://www.slideshare.net/zazworka/identifying-and-managing-technical-debt" target="_blank" title="Identifying and Managing Technical Debt">Identifying and Managing Technical Debt</a> </strong> from <strong><a href="https://www.slideshare.net/zazworka" target="_blank">zazworka</a></strong> </div>
GarciaPLhttp://www.blogger.com/profile/08890456444783994039noreply@blogger.com0tag:blogger.com,1999:blog-6866302772256572650.post-52641742911648592772017-10-01T12:36:00.001+01:002017-10-01T12:40:29.706+01:00Client Backend - Project for interviewRecently I was creating small distributed application called <b>ClientBackend</b> just for interview purpose. Solution consists of thee microservices :<br />
<ul>
<li>Client (received request from API Gateway and sends it to Backend)</li>
<li>Backend (performs operation defined in request)</li>
<li>Discovery (Spring Eureka)</li>
<li>API Gateway (Spring Zuul)</li>
</ul>
<div>
<span style="font-family: "arial"; font-size: 13px; line-height: 18px; white-space: pre-wrap;">
Technology stack :
</span>
</div>
<div>
<ul>
<li>Java 8</li>
<li>Spring Boot</li>
<li>Hystrix</li>
<li>Eureka</li>
<li>Feign</li>
<li>Zuul</li>
<li>TypeOf</li>
</ul>
</div>
<div>
<br /></div>
<div>
<span style="font-family: "arial"; font-size: 13px; line-height: 18px; white-space: pre-wrap;">More info about the solution <a href="https://github.com/GarciaPL/ClientBackend">https://github.com/GarciaPL/ClientBackend</a></span></div>
GarciaPLhttp://www.blogger.com/profile/08890456444783994039noreply@blogger.com0tag:blogger.com,1999:blog-6866302772256572650.post-65180721693870938872017-08-11T15:48:00.000+01:002017-08-11T15:48:13.203+01:00Hamcrest Custom Exception MatcherSometimes asserting custom exception is difficult. Sometimes it's not, for instance when you are using AssertJ, it's very quite easy to achieve. In this post I would like to highlight how to achieve that using Hamcrest matcher.<br />
<br />
Below you might find a BaseExceptionMatcher which can be easily extended for our needs. Let's assume that we have an exception with four properties like errorType, errorCode, status and description.<br />
<br />
<div style="overflow: auto; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">com.my.package.ErrorCode</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">com.my.package.ErrorType</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">com.my.package.base.BaseException</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">com.my.package.MessageType</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.hamcrest.Description</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.hamcrest.TypeSafeMatcher</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">javax.ws.rs.core.Response</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">abstract</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">BaseExceptionMatcher</span> <span style="color: #008800; font-weight: bold;">extends</span> TypeSafeMatcher<span style="color: #333333;"><</span>BaseException<span style="color: #333333;">></span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">protected</span> ErrorType expectedErrorType<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">protected</span> ErrorCode expectedErrorCode<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">protected</span> Response<span style="color: #333333;">.</span><span style="color: #0000cc;">Status</span> expectedStatus<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">protected</span> String expectedDescription<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #0066bb; font-weight: bold;">BaseExceptionMatcher</span><span style="color: #333333;">(</span>ErrorCode expectedErrorCode<span style="color: #333333;">,</span> Response<span style="color: #333333;">.</span><span style="color: #0000cc;">Status</span> expectedStatus<span style="color: #333333;">,</span> ErrorType expectedErrorType<span style="color: #333333;">,</span> String expectedDescription<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">expectedErrorCode</span> <span style="color: #333333;">=</span> expectedErrorCode<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">expectedStatus</span> <span style="color: #333333;">=</span> expectedStatus<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">expectedErrorType</span> <span style="color: #333333;">=</span> expectedErrorType<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">expectedDescription</span> <span style="color: #333333;">=</span> expectedDescription<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #555555; font-weight: bold;">@Override</span>
<span style="color: #008800; font-weight: bold;">protected</span> <span style="color: #333399; font-weight: bold;">boolean</span> <span style="color: #0066bb; font-weight: bold;">matchesSafely</span><span style="color: #333333;">(</span>BaseException item<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> item<span style="color: #333333;">.</span><span style="color: #0000cc;">getStatus</span><span style="color: #333333;">().</span><span style="color: #0000cc;">equals</span><span style="color: #333333;">(</span>expectedStatus<span style="color: #333333;">)</span> <span style="color: #333333;">&&</span>
item<span style="color: #333333;">.</span><span style="color: #0000cc;">getError</span><span style="color: #333333;">().</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">allMatch</span><span style="color: #333333;">(</span>p <span style="color: #333333;">-></span>
p<span style="color: #333333;">.</span><span style="color: #0000cc;">getType</span><span style="color: #333333;">().</span><span style="color: #0000cc;">equals</span><span style="color: #333333;">(</span>expectedErrorType<span style="color: #333333;">.</span><span style="color: #0000cc;">name</span><span style="color: #333333;">())</span> <span style="color: #333333;">&&</span> p<span style="color: #333333;">.</span><span style="color: #0000cc;">getCode</span><span style="color: #333333;">().</span><span style="color: #0000cc;">equals</span><span style="color: #333333;">(</span>expectedErrorCode<span style="color: #333333;">.</span><span style="color: #0000cc;">name</span><span style="color: #333333;">()))</span> <span style="color: #333333;">&&</span>
item<span style="color: #333333;">.</span><span style="color: #0000cc;">getError</span><span style="color: #333333;">().</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">allMatch</span><span style="color: #333333;">(</span>p <span style="color: #333333;">-></span> p<span style="color: #333333;">.</span><span style="color: #0000cc;">getDescription</span><span style="color: #333333;">().</span><span style="color: #0000cc;">contains</span><span style="color: #333333;">(</span>expectedDescription<span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #555555; font-weight: bold;">@Override</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">describeTo</span><span style="color: #333333;">(</span>Description description<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
description<span style="color: #333333;">.</span><span style="color: #0000cc;">appendText</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Response status : "</span><span style="color: #333333;">)</span>
<span style="color: #333333;">.</span><span style="color: #0000cc;">appendValue</span><span style="color: #333333;">(</span>expectedStatus<span style="color: #333333;">.</span><span style="color: #0000cc;">getReasonPhrase</span><span style="color: #333333;">()</span> <span style="color: #333333;">+</span> <span style="background-color: #fff0f0;">" ("</span> <span style="color: #333333;">+</span> expectedStatus<span style="color: #333333;">.</span><span style="color: #0000cc;">getStatusCode</span><span style="color: #333333;">()</span> <span style="color: #333333;">+</span> <span style="background-color: #fff0f0;">")"</span><span style="color: #333333;">)</span>
<span style="color: #333333;">.</span><span style="color: #0000cc;">appendText</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">" | ErrorType : "</span><span style="color: #333333;">).</span><span style="color: #0000cc;">appendValue</span><span style="color: #333333;">(</span>expectedErrorType<span style="color: #333333;">.</span><span style="color: #0000cc;">name</span><span style="color: #333333;">())</span>
<span style="color: #333333;">.</span><span style="color: #0000cc;">appendText</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">" | ErrorCode : "</span><span style="color: #333333;">).</span><span style="color: #0000cc;">appendValue</span><span style="color: #333333;">(</span>expectedErrorCode<span style="color: #333333;">.</span><span style="color: #0000cc;">name</span><span style="color: #333333;">())</span>
<span style="color: #333333;">.</span><span style="color: #0000cc;">appendText</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">" | Description : "</span><span style="color: #333333;">).</span><span style="color: #0000cc;">appendValue</span><span style="color: #333333;">(</span>expectedDescription<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #555555; font-weight: bold;">@Override</span>
<span style="color: #008800; font-weight: bold;">protected</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">describeMismatchSafely</span><span style="color: #333333;">(</span>BaseException item<span style="color: #333333;">,</span> Description mismatchDescription<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
mismatchDescription<span style="color: #333333;">.</span><span style="color: #0000cc;">appendText</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Exception contains response status : "</span><span style="color: #333333;">)</span>
<span style="color: #333333;">.</span><span style="color: #0000cc;">appendValue</span><span style="color: #333333;">(</span>item<span style="color: #333333;">.</span><span style="color: #0000cc;">getStatus</span><span style="color: #333333;">().</span><span style="color: #0000cc;">getReasonPhrase</span><span style="color: #333333;">()</span> <span style="color: #333333;">+</span> <span style="background-color: #fff0f0;">"("</span> <span style="color: #333333;">+</span> item<span style="color: #333333;">.</span><span style="color: #0000cc;">getStatus</span><span style="color: #333333;">().</span><span style="color: #0000cc;">getStatusCode</span><span style="color: #333333;">()</span> <span style="color: #333333;">+</span> <span style="background-color: #fff0f0;">")"</span><span style="color: #333333;">)</span>
<span style="color: #333333;">.</span><span style="color: #0000cc;">appendText</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">" | ErrorType : "</span><span style="color: #333333;">).</span><span style="color: #0000cc;">appendValue</span><span style="color: #333333;">(</span>getMessageType<span style="color: #333333;">(</span>item<span style="color: #333333;">).</span><span style="color: #0000cc;">getType</span><span style="color: #333333;">())</span>
<span style="color: #333333;">.</span><span style="color: #0000cc;">appendText</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">" | ErrorCode : "</span><span style="color: #333333;">).</span><span style="color: #0000cc;">appendValue</span><span style="color: #333333;">(</span>getMessageType<span style="color: #333333;">(</span>item<span style="color: #333333;">).</span><span style="color: #0000cc;">getCode</span><span style="color: #333333;">())</span>
<span style="color: #333333;">.</span><span style="color: #0000cc;">appendText</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">" | Description : "</span><span style="color: #333333;">).</span><span style="color: #0000cc;">appendValue</span><span style="color: #333333;">(</span>getMessageType<span style="color: #333333;">(</span>item<span style="color: #333333;">).</span><span style="color: #0000cc;">getDescription</span><span style="color: #333333;">());</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">private</span> MessageType <span style="color: #0066bb; font-weight: bold;">getMessageType</span><span style="color: #333333;">(</span>BaseException item<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> item<span style="color: #333333;">.</span><span style="color: #0000cc;">getError</span><span style="color: #333333;">().</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">findFirst</span><span style="color: #333333;">().</span><span style="color: #0000cc;">orElseGet</span><span style="color: #333333;">(()</span> <span style="color: #333333;">-></span> <span style="color: #333333;">{</span>
MessageType messageType <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> MessageType<span style="color: #333333;">();</span>
messageType<span style="color: #333333;">.</span><span style="color: #0000cc;">setType</span><span style="color: #333333;">(</span>ErrorType<span style="color: #333333;">.</span><span style="color: #0000cc;">APPLICATION</span><span style="color: #333333;">.</span><span style="color: #0000cc;">name</span><span style="color: #333333;">());</span>
messageType<span style="color: #333333;">.</span><span style="color: #0000cc;">setCode</span><span style="color: #333333;">(</span>ErrorCode<span style="color: #333333;">.</span><span style="color: #0000cc;">INTERNAL_SERVER_ERROR</span><span style="color: #333333;">.</span><span style="color: #0000cc;">name</span><span style="color: #333333;">());</span>
messageType<span style="color: #333333;">.</span><span style="color: #0000cc;">setDescription</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Matcher Error"</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">return</span> messageType<span style="color: #333333;">;</span>
<span style="color: #333333;">});</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
<span style="font-family: "arial"; font-size: 13px; line-height: 18px; white-space: pre-wrap;">
Now we can create a concrete exception class which might reuse that above abstract class.
</span><br />
<div style="overflow:auto;width:auto;"><pre style="margin: 0; line-height: 125%"><span></span><span style="color: #008800; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">my.package.ErrorCode</span><span style="color: #333333">;</span>
<span style="color: #008800; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">my.package.ErrorType</span><span style="color: #333333">;</span>
<span style="color: #008800; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">my.package.BaseExceptionMatcher</span><span style="color: #333333">;</span>
<span style="color: #008800; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">javax.ws.rs.core.Response</span><span style="color: #333333">;</span>
<span style="color: #008800; font-weight: bold">public</span> <span style="color: #008800; font-weight: bold">class</span> <span style="color: #BB0066; font-weight: bold">MyExceptionMatcher</span> <span style="color: #008800; font-weight: bold">extends</span> BaseExceptionMatcher <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">public</span> <span style="color: #0066BB; font-weight: bold">MyExceptionMatcher</span><span style="color: #333333">(</span>ErrorType expectedErrorType<span style="color: #333333">,</span>
ErrorCode expectedErrorCode<span style="color: #333333">,</span>
Response<span style="color: #333333">.</span><span style="color: #0000CC">Status</span> expectedStatus<span style="color: #333333">,</span>
String expectedDescription<span style="color: #333333">)</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">super</span><span style="color: #333333">(</span>expectedErrorCode<span style="color: #333333">,</span> expectedStatus<span style="color: #333333">,</span> expectedErrorType<span style="color: #333333">,</span> expectedDescription<span style="color: #333333">);</span>
<span style="color: #333333">}</span>
<span style="color: #333333">}</span>
</pre></div>
<span style="font-family: "arial"; font-size: 13px; line-height: 18px; white-space: pre-wrap;">
So now you are ready to reuse that matcher in JUnit test. Only what you need to do is define special rule in your test as below.
</span><br />
<div style="overflow:auto;width:auto;"><pre style="margin: 0; line-height: 125%"><span></span><span style="color: #555555; font-weight: bold">@Rule</span>
<span style="color: #008800; font-weight: bold">public</span> ExpectedException expectedException <span style="color: #333333">=</span> ExpectedException<span style="color: #333333">.</span><span style="color: #0000CC">none</span><span style="color: #333333">();</span>
</pre></div>
<span style="font-family: "arial"; font-size: 13px; line-height: 18px; white-space: pre-wrap;">
Last thing is just write a test which might throw an exception and reuse exception matcher written above.
</span><br/>
<div style="overflow:auto;width:auto;"><pre style="margin: 0; line-height: 125%"><span></span><span style="color: #008800; font-weight: bold">public</span> <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">testFailure</span><span style="color: #333333">()</span> <span style="color: #008800; font-weight: bold">throws</span> MyException <span style="color: #333333">{</span>
expectedException<span style="color: #333333">.</span><span style="color: #0000CC">expect</span><span style="color: #333333">(</span><span style="color: #008800; font-weight: bold">new</span> MyExceptionMatcher<span style="color: #333333">(</span>ErrorType<span style="color: #333333">.</span><span style="color: #0000CC">APPLICATION</span><span style="color: #333333">,</span> ErrorCode<span style="color: #333333">.</span><span style="color: #0000CC">DOCUMENT_NOT_FOUND</span><span style="color: #333333">,</span> Response<span style="color: #333333">.</span><span style="color: #0000CC">Status</span><span style="color: #333333">.</span><span style="color: #0000CC">NOT_FOUND</span><span style="color: #333333">,</span> <span style="background-color: #fff0f0">"Document not found"</span><span style="color: #333333">));</span>
<span style="color: #888888">//do smth to fail</span>
<span style="color: #333333">}</span>
</pre></div>GarciaPLhttp://www.blogger.com/profile/08890456444783994039noreply@blogger.com1