Search in sources :

Example 11 with ObjectInput

use of org.apache.dubbo.common.serialize.ObjectInput in project dubbo by alibaba.

the class RedisProtocolTest method testAuthRedis.

@Test
public void testAuthRedis() {
    // default db.index=0
    Invoker<IDemoService> refer = PROTOCOL.refer(IDemoService.class, registryUrl.addParameter("max.idle", 10).addParameter("max.active", 20));
    IDemoService demoService = this.PROXY.getProxy(refer);
    String value = demoService.get("key");
    assertThat(value, is(nullValue()));
    demoService.set("key", "newValue");
    value = demoService.get("key");
    assertThat(value, is("newValue"));
    demoService.delete("key");
    value = demoService.get("key");
    assertThat(value, is(nullValue()));
    refer.destroy();
    // change db.index=1
    String password = "123456";
    int database = 1;
    this.registryUrl = this.registryUrl.setPassword(password).addParameter("db.index", database);
    refer = PROTOCOL.refer(IDemoService.class, registryUrl.addParameter("max.idle", 10).addParameter("max.active", 20));
    demoService = this.PROXY.getProxy(refer);
    demoService.set("key", "newValue");
    value = demoService.get("key");
    assertThat(value, is("newValue"));
    // jedis gets the result comparison
    JedisPool pool = new JedisPool(new GenericObjectPoolConfig(), "localhost", registryUrl.getPort(), 2000, password, database, (String) null);
    try (Jedis jedis = pool.getResource()) {
        byte[] valueByte = jedis.get("key".getBytes());
        Serialization serialization = ExtensionLoader.getExtensionLoader(Serialization.class).getExtension(this.registryUrl.getParameter(Constants.SERIALIZATION_KEY, "java"));
        ObjectInput oin = serialization.deserialize(this.registryUrl, new ByteArrayInputStream(valueByte));
        String actual = (String) oin.readObject();
        assertThat(value, is(actual));
    } catch (Exception e) {
        Assertions.fail("jedis gets the result comparison is error!");
    } finally {
        pool.destroy();
    }
    demoService.delete("key");
    value = demoService.get("key");
    assertThat(value, is(nullValue()));
    refer.destroy();
}
Also used : Serialization(org.apache.dubbo.common.serialize.Serialization) Jedis(redis.clients.jedis.Jedis) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) JedisPool(redis.clients.jedis.JedisPool) ObjectInput(org.apache.dubbo.common.serialize.ObjectInput) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException) JedisDataException(redis.clients.jedis.exceptions.JedisDataException) IOException(java.io.IOException) RpcException(org.apache.dubbo.rpc.RpcException) Test(org.junit.jupiter.api.Test)

Example 12 with ObjectInput

use of org.apache.dubbo.common.serialize.ObjectInput in project dubbo by alibaba.

the class TransportCodec method decode.

@Override
public Object decode(Channel channel, ChannelBuffer buffer) throws IOException {
    InputStream input = new ChannelBufferInputStream(buffer);
    ObjectInput objectInput = getSerialization(channel).deserialize(channel.getUrl(), input);
    Object object = decodeData(channel, objectInput);
    if (objectInput instanceof Cleanable) {
        ((Cleanable) objectInput).cleanup();
    }
    return object;
}
Also used : ChannelBufferInputStream(org.apache.dubbo.remoting.buffer.ChannelBufferInputStream) InputStream(java.io.InputStream) ChannelBufferInputStream(org.apache.dubbo.remoting.buffer.ChannelBufferInputStream) ObjectInput(org.apache.dubbo.common.serialize.ObjectInput) Cleanable(org.apache.dubbo.common.serialize.Cleanable)

Example 13 with ObjectInput

use of org.apache.dubbo.common.serialize.ObjectInput in project dubbo by alibaba.

the class AbstractSerializationTest method test_LinkedHashMap.

@Test
public void test_LinkedHashMap() throws Exception {
    LinkedHashMap<String, String> data = new LinkedHashMap<String, String>();
    data.put("1", "a");
    data.put("2", "b");
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeObject(data);
    objectOutput.flushBuffer();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
    Object read = deserialize.readObject();
    assertTrue(read instanceof LinkedHashMap);
    @SuppressWarnings("unchecked") String key1 = ((LinkedHashMap<String, String>) read).entrySet().iterator().next().getKey();
    assertEquals("1", key1);
    assertEquals(data, read);
    try {
        deserialize.readObject();
        fail();
    } catch (IOException expected) {
    }
}
Also used : ObjectOutput(org.apache.dubbo.common.serialize.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(org.apache.dubbo.common.serialize.ObjectInput) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test)

Example 14 with ObjectInput

use of org.apache.dubbo.common.serialize.ObjectInput in project dubbo by alibaba.

the class AbstractSerializationTest method test_Byte.

@Test
public void test_Byte() throws Exception {
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeByte((byte) 123);
    objectOutput.flushBuffer();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
    assertEquals((byte) 123, deserialize.readByte());
    try {
        deserialize.readByte();
        fail();
    } catch (IOException expected) {
    }
}
Also used : ObjectOutput(org.apache.dubbo.common.serialize.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(org.apache.dubbo.common.serialize.ObjectInput) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 15 with ObjectInput

use of org.apache.dubbo.common.serialize.ObjectInput in project dubbo by alibaba.

the class AbstractSerializationTest method test_MediaContent_WithType_badStream.

@Test
public void test_MediaContent_WithType_badStream() throws Exception {
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeObject(mediaContent);
    objectOutput.flushBuffer();
    byte[] byteArray = byteArrayOutputStream.toByteArray();
    for (int i = 0; i < byteArray.length; i++) {
        if (i % 3 == 0) {
            byteArray[i] = (byte) ~byteArray[i];
        }
    }
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
    try {
        ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
        // local variable, convenient for debug
        @SuppressWarnings("unused") Object read = deserialize.readObject(MediaContent.class);
        fail();
    } catch (IOException expected) {
        System.out.println(expected);
    }
}
Also used : ObjectOutput(org.apache.dubbo.common.serialize.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(org.apache.dubbo.common.serialize.ObjectInput) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Aggregations

ObjectInput (org.apache.dubbo.common.serialize.ObjectInput)84 ByteArrayInputStream (java.io.ByteArrayInputStream)76 Test (org.junit.jupiter.api.Test)74 ObjectOutput (org.apache.dubbo.common.serialize.ObjectOutput)73 IOException (java.io.IOException)42 AbstractSerializationPersonFailTest (org.apache.dubbo.common.serialize.base.AbstractSerializationPersonFailTest)7 AbstractSerializationPersonOkTest (org.apache.dubbo.common.serialize.base.AbstractSerializationPersonOkTest)7 InputStream (java.io.InputStream)6 HashMap (java.util.HashMap)3 Cleanable (org.apache.dubbo.common.serialize.Cleanable)3 BigPerson (org.apache.dubbo.common.serialize.model.person.BigPerson)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 LinkedHashMap (java.util.LinkedHashMap)2 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)2 URL (org.apache.dubbo.common.URL)2 Serialization (org.apache.dubbo.common.serialize.Serialization)2 BizException (org.apache.dubbo.common.serialize.model.BizException)2 BizExceptionNoDefaultConstructor (org.apache.dubbo.common.serialize.model.BizExceptionNoDefaultConstructor)2 Request (org.apache.dubbo.remoting.exchange.Request)2 Response (org.apache.dubbo.remoting.exchange.Response)2