@Override protected TileSerializer<List<Pair<S, T>>> create () throws ConfigurationException { return new PairArrayAvroSerializer<S, T>(_keyType, _valueType, TileSerializerFactory.getCodecFactory(this)); } }
@Test public void testPairArraySerializer () throws Exception { TileSerializer<List<Pair<String, Integer>>> serialD = new PairArrayAvroSerializer<>(String.class, Integer.class, CodecFactory.nullCodec()); Assert.assertTrue(result instanceof PairArrayAvroSerializer); PairArrayAvroSerializer<?, ?> serialR = (PairArrayAvroSerializer<?, ?>) result; Assert.assertEquals(new TypeDescriptor(List.class, new TypeDescriptor(Pair.class, new TypeDescriptor(String.class), new TypeDescriptor(Integer.class))), serialR.getBinTypeDescription()); @SuppressWarnings({ "unchecked", "rawtypes" }) PairArrayAvroSerializer<String, Integer> serialRD = (PairArrayAvroSerializer) result; serialRD.serialize(tile, tbaos2); tbaos1.flush(); tbaos1.close(); TileData<List<Pair<String, Integer>>> out1 = serialRD.deserialize(tile.getDefinition(), tbais1);
@Test public void testSerializerTypeing () { Assert.assertEquals(new TypeDescriptor(Double.class), new PrimitiveAvroSerializer<>(Double.class, CodecFactory.nullCodec()).getBinTypeDescription()); Assert.assertEquals(new TypeDescriptor(List.class, new TypeDescriptor(Double.class)), new PrimitiveArrayAvroSerializer<>(Double.class, CodecFactory.nullCodec()).getBinTypeDescription()); Assert.assertEquals(new TypeDescriptor(List.class, new TypeDescriptor(Pair.class, new TypeDescriptor(String.class), new TypeDescriptor(Integer.class))), new PairArrayAvroSerializer<>(String.class, Integer.class, CodecFactory.nullCodec()).getBinTypeDescription()); } }
@Test public void testReadWordScoreTile () throws IOException, JSONException { // Create a tile to test TileSerializer<List<Pair<String, Double>>> serializer = new PairArrayAvroSerializer<>(String.class, Double.class, CodecFactory.nullCodec()); TileIndex index = new TileIndex(0, 0, 0, 1, 1); DenseTileData<List<Pair<String, Double>>> tile = new DenseTileData<>(index); List<Pair<String, Double>> bin = new ArrayList<>(); bin.add(new Pair<String, Double>("abc", 1.0)); bin.add(new Pair<String, Double>("def", 1.5)); bin.add(new Pair<String, Double>("ghi", 2.0)); bin.add(new Pair<String, Double>("jkl", 2.25)); tile.setBin(0, 0, bin); ByteArrayOutputStream baos = new ByteArrayOutputStream(); serializer.serialize(tile, baos); baos.flush(); baos.close(); byte[] serializedTileData = baos.toByteArray(); // Now try to convert that to JSON. JSONObject result = AvroJSONConverter.convert(new ByteArrayInputStream(serializedTileData)); System.out.println(result.toString()); } }
@SafeVarargs final <S, T> void testRoundTrip(Class<? extends S> keyType, Class<? extends T> valueType, int arraySize, Pair<S, T>... data) throws Exception { TileSerializer<List<Pair<S, T>>> serializer = new PairArrayAvroSerializer<>(keyType, valueType, CodecFactory.nullCodec());