Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package local.laer.app.knapsack.support; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.base.Verify; import com.google.common.io.CharStreams; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.logging.Logger; import local.laer.app.knapsack.domain.support.Matrix; import org.hamcrest.CoreMatchers; import org.junit.Test; import static org.junit.Assert.*; import org.skyscreamer.jsonassert.JSONAssert; /** * * @author Lars Eriksson (larsq.eriksson@gmail.com) */ public class OffsetIntMatrixTest { private final static Logger LOG = Logger.getLogger(OffsetBinaryMatrixTest.class.getPackage().getName()); @Test public void testSerialize() throws JsonProcessingException { OffsetIntMatrix matrix = new OffsetIntMatrix(1, 1, 5, 6, 0); matrix.mutableTransform(IntFunctions.fillWith(5)); ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(matrix); LOG.info(String.format("json: %s", json)); } private String readResource(String resource) throws IOException { InputStream in = getClass().getResourceAsStream(resource); Verify.verifyNotNull(in); return CharStreams.toString(new InputStreamReader(in)); } @Test public void testDeserialize() throws IOException { String json = readResource("/fixtures/IntMatrix.json"); try { Matrix m = new ObjectMapper().readValue(json, Matrix.class); OffsetIntMatrix converted = OffsetIntMatrixParser.valueOf(m); assertThat(converted.cols(), CoreMatchers.is(4)); assertThat(converted.rows(), CoreMatchers.is(7)); assertThat(converted.getInt(5, 5), CoreMatchers.is(1)); assertThat(converted.getInt(6, 5), CoreMatchers.is(2)); assertThat(converted.getInt(7, 5), CoreMatchers.is(3)); assertThat(converted.getInt(8, 5), CoreMatchers.is(4)); assertThat(converted.getInt(9, 5), CoreMatchers.is(5)); assertThat(converted.getInt(10, 5), CoreMatchers.is(0)); assertThat(converted.getInt(11, 5), CoreMatchers.is(0)); assertThat(converted.getInt(5, 6), CoreMatchers.is(1)); assertThat(converted.getInt(6, 6), CoreMatchers.is(2)); assertThat(converted.getInt(7, 6), CoreMatchers.is(3)); assertThat(converted.getInt(8, 6), CoreMatchers.is(4)); assertThat(converted.getInt(9, 6), CoreMatchers.is(5)); assertThat(converted.getInt(10, 6), CoreMatchers.is(0)); assertThat(converted.getInt(11, 6), CoreMatchers.is(0)); assertThat(converted.getInt(5, 7), CoreMatchers.is(0)); assertThat(converted.getInt(6, 7), CoreMatchers.is(0)); assertThat(converted.getInt(7, 7), CoreMatchers.is(0)); assertThat(converted.getInt(8, 7), CoreMatchers.is(0)); assertThat(converted.getInt(9, 7), CoreMatchers.is(0)); assertThat(converted.getInt(10, 7), CoreMatchers.is(0)); assertThat(converted.getInt(11, 7), CoreMatchers.is(0)); assertThat(converted.getInt(5, 8), CoreMatchers.is(0)); assertThat(converted.getInt(6, 8), CoreMatchers.is(0)); assertThat(converted.getInt(7, 8), CoreMatchers.is(1)); assertThat(converted.getInt(8, 8), CoreMatchers.is(2)); assertThat(converted.getInt(9, 8), CoreMatchers.is(3)); assertThat(converted.getInt(10, 8), CoreMatchers.is(4)); assertThat(converted.getInt(11, 8), CoreMatchers.is(5)); } catch (Exception e) { LOG.severe(e.getMessage()); throw e; } } }