/* * Copyright 2002-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.baidu.bjf.remoting.protobuf.idlproxy; import java.io.IOException; import java.util.Map; import org.junit.Assert; import org.junit.Test; import com.baidu.bjf.remoting.protobuf.IDLProxyObject; import com.baidu.bjf.remoting.protobuf.ProtobufIDLGenerator; import com.baidu.bjf.remoting.protobuf.ProtobufIDLProxy; import com.baidu.bjf.remoting.protobuf.complex.AddressBookProtosPOJO; import com.baidu.bjf.remoting.protobuf.complex.AddressBookProtosPOJOWithDefault; import com.baidu.bjf.remoting.protobuf.complex.PersonPOJO; import com.baidu.bjf.remoting.protobuf.complex.PersonPOJOWithDefault; /** * Test class for {@link ProtobufIDLProxy}. * * @author xiemalin * @since 1.0.8 */ public class ProtobufIDLProxyPressureTest { /** The times. */ int times = 500; /** * Test pressure with cached. * * @throws IOException Signals that an I/O exception has occurred. */ @Test public void testPressureWithCached() throws IOException { String code = ProtobufIDLGenerator.getIDL(AddressBookProtosPOJOWithDefault.class); Map<String, IDLProxyObject> idlProxyObjects = ProtobufIDLProxy.create(code, false); IDLProxyObject idlProxyObject = idlProxyObjects.get(AddressBookProtosPOJOWithDefault.class.getSimpleName()); testPressureOfCacheControl(idlProxyObject, true); } /** * Test pressure without cached. * * @throws IOException Signals that an I/O exception has occurred. */ @Test public void testPressureWithoutCached() throws IOException { String code = ProtobufIDLGenerator.getIDL(AddressBookProtosPOJO.class); Map<String, IDLProxyObject> idlProxyObjects = ProtobufIDLProxy.create(code); IDLProxyObject idlProxyObject = idlProxyObjects.get(AddressBookProtosPOJO.class.getSimpleName()); testPressureOfCacheControl(idlProxyObject, false); } /** * Test simple pressure with cached. * * @throws IOException Signals that an I/O exception has occurred. */ @Test public void testSimplePressureWithCached() throws IOException { String code = ProtobufIDLGenerator.getIDL(PersonPOJOWithDefault.class); Map<String, IDLProxyObject> idlProxyObjects = ProtobufIDLProxy.create(code); IDLProxyObject idlProxyObject = idlProxyObjects.get(PersonPOJOWithDefault.class.getSimpleName()); testSimpleObjectPressureOfCacheControl(idlProxyObject, true); } /** * Test simple pressure without cached. * * @throws IOException Signals that an I/O exception has occurred. */ @Test public void testSimplePressureWithoutCached() throws IOException { String code = ProtobufIDLGenerator.getIDL(PersonPOJO.class); Map<String, IDLProxyObject> idlProxyObjects = ProtobufIDLProxy.create(code); IDLProxyObject idlProxyObject = idlProxyObjects.get(PersonPOJO.class.getSimpleName()); testSimpleObjectPressureOfCacheControl(idlProxyObject, false); } /** * Test simple object pressure of cache control. * * @param idlProxyObject the idl proxy object * @param cached the cached * @throws IOException Signals that an I/O exception has occurred. */ private void testSimpleObjectPressureOfCacheControl(IDLProxyObject idlProxyObject, boolean cached) throws IOException { idlProxyObject.setCached(cached); for (int i = 0; i < times; i++) { idlProxyObject.put("name", "hello"); idlProxyObject.put("id", i); try { idlProxyObject.put("notexitfield", null); Assert.fail(); } catch (Exception e) { Assert.assertNotNull(e); } byte[] bb = idlProxyObject.encode(); IDLProxyObject newObject = idlProxyObject.decode(bb); Assert.assertEquals("hello", newObject.get("name")); Assert.assertEquals(i, newObject.get("id")); } } /** * Test pressure of cache control. * * @param idlProxyObject the idl proxy object * @param cached the cached * @throws IOException Signals that an I/O exception has occurred. */ private void testPressureOfCacheControl(IDLProxyObject idlProxyObject, boolean cached) throws IOException { idlProxyObject.setCached(cached); for (int i = 0; i < times; i++) { idlProxyObject.put("name", "hello"); idlProxyObject.put("list.name", "yes"); idlProxyObject.put("list.id", 10); /* * try { idlProxyObject.put("notexitfield", null); Assert.fail(); } * catch (Exception e) { Assert.assertNotNull(e); } */ byte[] bb = idlProxyObject.encode(); IDLProxyObject newObject = idlProxyObject.decode(bb); Assert.assertEquals("hello", newObject.get("name")); Assert.assertEquals("yes", newObject.get("list.name")); } } }