Skip to content

Commit

Permalink
BasicHelperNG / BasicWrapperNG with no serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Mar 8, 2024
1 parent c57acf0 commit d218b00
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- BasicWrapperNG with no serialization

## [8.5.1] - 2024-03-08

### Added
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.fugerit.java.core.db.daogen;

import java.io.Serializable;
import java.util.function.BooleanSupplier;

public class BasicHelper implements Serializable {

Expand All @@ -9,10 +10,14 @@ public class BasicHelper implements Serializable {
*/
private static final long serialVersionUID = 7545568256714803873L;

protected static final boolean UNSOPPORTED_OPERATION = true;
private static final boolean UNSUPPORTED_OPERATION = true;

public static void throwUnsupported( String message ) {
if ( UNSOPPORTED_OPERATION ) {
throwUnsupported( message, () -> UNSUPPORTED_OPERATION );
}

public static void throwUnsupported(String message, BooleanSupplier isUnsupported) {
if ( isUnsupported.getAsBoolean() ) {
throw new UnsupportedOperationException( message );
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.fugerit.java.core.db.daogen;

import org.fugerit.java.core.lang.helpers.Wrapper;
import org.fugerit.java.core.lang.helpers.WrapperHelper;

public class BasicWrapperNG<T> implements Wrapper<T> {

private T wrapped;

@Override
public T unwrapModel() {
return wrapped;
}

@Override
public void wrapModel(T wrapped) {
this.wrapped = wrapped;
}

public BasicWrapperNG(T wrapped) {
super();
this.wrapped = wrapped;
}

@Override
public String toString() {
return this.getClass().getSimpleName()+"[wraps:"+unwrapModel().toString()+"]";
}

@Override
public T unwrap() {
return WrapperHelper.unwrap( this.wrapped );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static Object fullSerializationTest( Object value ) throws IOException {
public static Object deserialize( byte[] data ) throws IOException {
Object res = null;
try ( ObjectInputStream ois = new ObjectInputStream( new ByteArrayInputStream( data ) ) ) {
res = HelperIOException.get( () -> ois.readObject() );
res = HelperIOException.get( ois::readObject );
log.info( "deserializeTest, read object : {}", res );
}
return res;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
package test.org.fugerit.java.core.db.dao.daogen;

import java.io.Serializable;
import java.math.BigDecimal;

import lombok.extern.slf4j.Slf4j;
import org.fugerit.java.core.db.dao.DAOException;
import org.fugerit.java.core.db.dao.DAORuntimeException;
import org.fugerit.java.core.db.dao.FieldList;
import org.fugerit.java.core.db.daogen.BasicDaoResult;
import org.fugerit.java.core.db.daogen.BasicDataFacade;
import org.fugerit.java.core.db.daogen.BasicWrapper;
import org.fugerit.java.core.db.daogen.CloseableDAOContextSC;
import org.fugerit.java.core.db.daogen.DataEntityInfo;
import org.fugerit.java.core.db.daogen.DataEntityUtils;
import org.fugerit.java.core.db.daogen.QueryHelper;
import org.fugerit.java.core.db.daogen.*;
import org.fugerit.java.core.function.SafeFunction;
import org.junit.Assert;
import org.junit.Test;

import lombok.extern.slf4j.Slf4j;
import test.org.fugerit.java.BasicTest;
import test.org.fugerit.java.core.db.TestBasicDBHelper;
import test.org.fugerit.java.core.db.dao.ModelUser;

import java.io.Serializable;
import java.math.BigDecimal;

@Slf4j
public class TestBasicDataFacade extends TestBasicDBHelper implements Serializable {

Expand Down Expand Up @@ -85,8 +78,10 @@ public void testBasicDataFacade2() {
log.info( "test 1 : {}", userWrapper.unwrap() );
log.info( "test 2 : {}", userWrapper.unwrapModel() );
log.info( "test 3 : {}", HELPER.fullSerializationTest( userWrapper ) );
log.info( "test 4 : {}", userWrapper );
userWrapper.wrapModel( userTest );
Assert.assertThrows( UnsupportedOperationException.class , () -> BasicWrapper.throwUnsupported( "message" ) );
userWrapper.wrapModel( userTest );
// test DataEntityUtils
DataEntityUtils.unwrap( facade );
Assert.assertThrows( DAOException.class , () -> DataEntityUtils.unwrap( userTest ) );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package test.org.fugerit.java.core.db.dao.daogen;

import lombok.extern.slf4j.Slf4j;
import org.fugerit.java.core.db.daogen.BasicHelper;
import org.fugerit.java.core.db.daogen.BasicWrapperNG;
import org.fugerit.java.core.function.SafeFunction;
import org.junit.Assert;
import org.junit.Test;
import test.org.fugerit.java.BasicTest;
import test.org.fugerit.java.core.db.dao.ModelUser;

import java.io.NotSerializableException;

@Slf4j
public class TestBasicWrapperNG {

private static final BasicTest HELPER = new BasicTest();

@Test
public void testBasicWrapperNG() {
SafeFunction.apply( () -> {
ModelUser userTest = new ModelUser();
// wrapper ng
BasicWrapperNG<ModelUser> userWrapperNG = new BasicWrapperNG<>( userTest );
log.info( "test 1 : {}", userWrapperNG.unwrap() );
log.info( "test 2 : {}", userWrapperNG.unwrapModel() );
log.info( "test 3 : {}", userWrapperNG );
userWrapperNG.wrapModel( userTest );
Assert.assertThrows(NotSerializableException.class, () -> HELPER.fullSerializationTest( userWrapperNG ));
BasicHelper.throwUnsupported( "test 0", () -> false ); // will not throw unsupported operation
Assert.assertThrows( UnsupportedOperationException.class, () -> BasicHelper.throwUnsupported( "test 1" ) );
} );
}

}

0 comments on commit d218b00

Please sign in to comment.