Skip to content

Instantly share code, notes, and snippets.

View sleberknight's full-sized avatar

Scott Leberknight sleberknight

  • Fortitude Technologies
  • Leesburg, VA
View GitHub Profile
@sleberknight
sleberknight / IntelliJEnumSourceBugTest.java
Last active January 26, 2019 02:14
IntelliJ incorrectly reporting "Field is never used" in JUnit 5 test using @EnumSource
package com.fortitudetec.junit.jupiter;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
/**
* Tested on:
* IntelliJ 2018.3.2, 2018.3.3
* macOS Mojave 10.14.2
*/
@sleberknight
sleberknight / IntelliJExplicitTypeCanBeReplacedByDiamondTest.java
Last active April 3, 2019 02:47
IntelliJ incorrectly reporting "explicit type argument can be replaced by <>" for JAX-RS GenericType
package com.fortitudetec.junit.jupiter;
import io.dropwizard.testing.junit5.DropwizardClientExtension;
import io.dropwizard.testing.junit5.DropwizardExtensionsSupport;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
@sleberknight
sleberknight / ServiceInfo.java
Created August 18, 2020 15:13
ServiceInfo interface (from registry-client)
package org.kiwiproject.registry.config;
import java.util.Optional;
public interface ServiceInfo {
int getApplicationPort();
Optional<Integer> getApplicationSecurePort();
int getAdminPort();
RegistryInfo getRegistryInfo();
@sleberknight
sleberknight / isEmpty.java
Created August 20, 2020 01:18
Reflection fails on public methods inside List created using List.of
jshell> var lst = List.of()
lst ==> []
jshell> var m = lst.getClass().getMethod("isEmpty")
m ==> public boolean java.util.ImmutableCollections$ListN.isEmpty()
jshell> var result = m.invoke(lst)
| Exception java.lang.IllegalAccessException: class REPL.$JShell$12B cannot access a member of class java.util.ImmutableCollections$ListN (in module java.base) with modifiers "public"
| at Reflection.newIllegalAccessException (Reflection.java:361)
| at AccessibleObject.checkAccess (AccessibleObject.java:591)
@sleberknight
sleberknight / StaticImports.java
Last active December 8, 2020 01:57
Messing with static imports in Java
package com.acme.examples;
import static com.acme.examples.ClassA.method1;
import static com.acme.examples.ClassA.method2;
import static com.acme.examples.ClassA.method3;
import static com.acme.examples.ClassB.method1;
import static com.acme.examples.ClassB.method2;
import static com.acme.examples.ClassB.method3;
public class StaticImports {
@sleberknight
sleberknight / ReReTrying.java
Created December 10, 2020 23:58
Re-retrying 4.0.0 pre-release example
package org.kiwiproject.examples;
import com.github.rholder.retry.AttemptTimeLimiters;
import com.github.rholder.retry.RetryException;
import com.github.rholder.retry.Retryer;
import com.github.rholder.retry.RetryerBuilder;
import com.github.rholder.retry.StopStrategies;
import com.github.rholder.retry.WaitStrategies;
import lombok.extern.slf4j.Slf4j;
@sleberknight
sleberknight / ReRetryingRun.java
Last active December 11, 2020 19:53
Re-retrying 4.0.0 pre-release example using Retryer#run
package org.kiwiproject.examples;
import com.github.rholder.retry.AttemptTimeLimiters;
import com.github.rholder.retry.RetryException;
import com.github.rholder.retry.Retryer;
import com.github.rholder.retry.RetryerBuilder;
import com.github.rholder.retry.StopStrategies;
import com.github.rholder.retry.WaitStrategies;
import lombok.extern.slf4j.Slf4j;
@sleberknight
sleberknight / uuid_validation.cpp
Created April 9, 2021 19:38
Simple C++ UUID regex validation
#include <iostream>
#include <regex>
#include <string>
#include <getopt.h>
using namespace std;
bool validate_uuid(const std::string& s)
{
static const std::regex e("^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$", std::regex_constants::icase);
@sleberknight
sleberknight / verify-checksum.sh
Created April 29, 2021 14:40
Verify SHA-256 checksum with a single command
#!/bin/bash
file_to_verify=$1
expected_checksum=$2
diff <(sha256sum $file_to_verify | cut -f 1 -d \ ) <(echo $expected_checksum)
@sleberknight
sleberknight / ReceiveMetadata.java
Created July 1, 2021 22:15
Are we not properly closing the Responses?
public Response receiveMetadata(String metadata) {
var response = client.sendMetadata(metadata);
// Here, successful is from KiwiResponses, and this method does NOT close the Response and
// since we are not consuming the response entity, could this cause the problem?
if (successful(response)) {
return Response.ok.build();
}