Skip to content

Commit

Permalink
Fixing test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
mihxil committed Nov 8, 2024
1 parent a4abb8a commit 80b94e7
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package nl.vpro.hibernate;

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.*;
import java.time.Duration;

import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.UserType;

/**
* @author Michiel Meeuwissen
* @since 0.36
*/
public class DurationToLongType implements UserType<Duration> {

public static final DurationToLongType INSTANCE = new DurationToLongType();


public DurationToLongType() {
super();
}


@Override
public int getSqlType() {
return Types.BIGINT;
}

@Override
public Class<Duration> returnedClass() {
return Duration.class;
}

@Override
public boolean equals(Duration x, Duration y) throws HibernateException {
if (x == null) {
return y == null;
}
return x.equals(y);

}

@Override
public int hashCode(Duration x) throws HibernateException {
if (x == null) {
return 0;
}
return x.hashCode();
}

@Override
public Duration nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException {
BigDecimal ts = rs.getBigDecimal(position);
if (ts == null) {
return null;
}
return Duration.ofMillis(ts.toBigInteger().longValue());
}


@Override
public void nullSafeSet(PreparedStatement st, Duration value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException {
if (value == null) {
st.setNull(index, Types.TIMESTAMP);
} else {
st.setBigDecimal(index, BigDecimal.valueOf(value.toMillis()));
}
}

@Override
public Duration deepCopy(Duration value) throws HibernateException {
return value;
}

@Override
public boolean isMutable() {
return false;
}

@Override
public Serializable disassemble(Duration value) throws HibernateException {
return value;
}

@Override
public Duration assemble(Serializable cached, Object owner) throws HibernateException {
return (Duration) cached;
}

@Override
public Duration replace(Duration original, Duration target, Object owner) throws HibernateException {
return original;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package nl.vpro.javax.cache;

import java.util.concurrent.TimeUnit;

import javax.cache.expiry.Duration;
import javax.cache.expiry.ExpiryPolicy;

public class CustomExpiryPolicy implements ExpiryPolicy {

private java.time.Duration expiry = java.time.Duration.ofMinutes(5);
@Override
public Duration getExpiryForCreation() {
return new Duration(TimeUnit.MILLISECONDS, expiry.toMillis());
}

@Override
public Duration getExpiryForAccess() {
return new Duration(TimeUnit.MILLISECONDS, expiry.toMillis());
}

@Override
public Duration getExpiryForUpdate() {
return new Duration(TimeUnit.MILLISECONDS, expiry.toMillis());
}
}
15 changes: 15 additions & 0 deletions vpro-shared-util/src/main/java/nl/vpro/util/TextUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,21 @@ public static boolean isValid(@NonNull String input, boolean aggressive) {
}
}

public static final Pattern VALID_XML = Pattern.compile("[^\\x09\\x0A\\x0D\\x20-\\xD7FF\\xE000-\\xFFFD\\x10000-x10FFFF]*");

@PolyNull
public static String makeValidXmlText(@PolyNull String input) {
if (input == null) {
return null;
}

return input.replaceAll("[\\x{0}-\\x{8}]|[\\x{B}-\\x{C}]|[\\x{E}-\\x{1F}]|[\\x{D800}-\\x{DFFF}]|[\\x{FFFE}-\\x{FFFF}]", "");
}

public static boolean isValidXmlText(String input) {
return VALID_XML.matcher(input).matches();
}

private static boolean jsoupNodeValid(Node n) {
if (n instanceof Element e) {
if (ALL.contains(e.tagName().toLowerCase())) {
Expand Down
11 changes: 6 additions & 5 deletions vpro-shared-util/src/test/java/nl/vpro/util/TextUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
*/
package nl.vpro.util;

import java.lang.reflect.*;
import java.util.Arrays;
import java.util.Locale;
import java.util.stream.Stream;

import org.checkerframework.checker.nullness.qual.PolyNull;
import org.jsoup.Jsoup;
import org.jsoup.safety.Safelist;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.*;

import java.lang.reflect.*;
import java.util.Arrays;
import java.util.Locale;
import java.util.stream.Stream;


import static java.nio.charset.StandardCharsets.UTF_8;
import static nl.vpro.util.TextUtil.*;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down

0 comments on commit 80b94e7

Please sign in to comment.