Added test, fixed NPE
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/d20e43fc Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/d20e43fc Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/d20e43fc Branch: refs/heads/sql-store-cmp Commit: d20e43fc5f99893ff25f329f144d2cf14c9f5990 Parents: 2476bb1 Author: Alexey Goncharuk <alexey.goncha...@gmail.com> Authored: Sun Jan 24 20:31:59 2016 +0300 Committer: Alexey Goncharuk <alexey.goncha...@gmail.com> Committed: Wed Feb 3 17:13:41 2016 +0300 ---------------------------------------------------------------------- .../processors/cache/CacheObjectAdapter.java | 3 + .../cache/IgniteCacheObjectPutSelfTest.java | 177 +++++++++++++++++++ 2 files changed, 180 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/d20e43fc/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectAdapter.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectAdapter.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectAdapter.java index 21873d4..28a95d8 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectAdapter.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectAdapter.java @@ -71,6 +71,9 @@ public abstract class CacheObjectAdapter implements CacheObject, Externalizable /** {@inheritDoc} */ @Override public boolean putValue(ByteBuffer buf, CacheObjectContext ctx) throws IgniteCheckedException { + if (valBytes == null) + valueBytes(ctx); + if (buf.remaining() < valBytes.length + 5) return false; http://git-wip-us.apache.org/repos/asf/ignite/blob/d20e43fc/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheObjectPutSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheObjectPutSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheObjectPutSelfTest.java new file mode 100644 index 0000000..2dc18f5 --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheObjectPutSelfTest.java @@ -0,0 +1,177 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.ignite.internal.processors.cache; + +import java.io.Serializable; +import java.nio.ByteBuffer; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.internal.IgniteEx; +import org.apache.ignite.internal.processors.cacheobject.IgniteCacheObjectProcessor; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; + +/** + * + */ +public class IgniteCacheObjectPutSelfTest extends GridCommonAbstractTest { + /** */ + public static final String CACHE_NAME = "partitioned"; + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(gridName); + + CacheConfiguration ccfg = new CacheConfiguration(); + + ccfg.setName(CACHE_NAME); + + cfg.setCacheConfiguration(ccfg); + + cfg.setMarshaller(null); + + return cfg; + } + + /** {@inheritDoc} */ + @Override protected void beforeTestsStarted() throws Exception { + super.beforeTestsStarted(); + + startGrid(0); + } + + /** {@inheritDoc} */ + @Override protected void afterTestsStopped() throws Exception { + stopAllGrids(); + } + + /** + * @throws Exception If failed. + */ + public void testPrimitiveValues() throws Exception { + IgniteEx ignite = grid(0); + + IgniteCache<Object, Object> cache = ignite.cache(CACHE_NAME); + + for (int i = 0; i < 10; i++) + cache.put(i, String.valueOf(i)); + + IgniteCacheObjectProcessor co = ignite.context().cacheObjects(); + GridCacheAdapter<Object, Object> iCache = ignite.context().cache().internalCache(CACHE_NAME); + CacheObjectContext coCtx = iCache.context().cacheObjectContext(); + + ByteBuffer buf = ByteBuffer.allocate(2048); + + for (int i = 0; i < 10; i++) { + KeyCacheObject key = co.toCacheKeyObject(coCtx, i, false); + + GridCacheEntryEx entry = iCache.peekEx(key); + + assertNotNull(entry); + + assertTrue(entry.key().putValue(buf, coCtx)); + assertTrue(entry.valueBytes().putValue(buf, coCtx)); + } + + buf.flip(); + + for (int i = 0; i < 10; i++) { + CacheObject co1 = co.toCacheObject(coCtx, buf); + + assertEquals(i, co1.value(coCtx, false)); + + CacheObject co2 = co.toCacheObject(coCtx, buf); + + assertEquals(String.valueOf(i), co2.value(coCtx, false)); + } + } + + /** + * @throws Exception If failed. + */ + public void testClassValues() throws Exception { + IgniteEx ignite = grid(0); + + IgniteCache<Object, Object> cache = ignite.cache(CACHE_NAME); + + for (int i = 0; i < 10; i++) + cache.put(new TestValue(i), new TestValue(i)); + + IgniteCacheObjectProcessor co = ignite.context().cacheObjects(); + GridCacheAdapter<Object, Object> iCache = ignite.context().cache().internalCache(CACHE_NAME); + CacheObjectContext coCtx = iCache.context().cacheObjectContext(); + + ByteBuffer buf = ByteBuffer.allocate(2048); + + for (int i = 0; i < 10; i++) { + KeyCacheObject key = co.toCacheKeyObject(coCtx, new TestValue(i), false); + + GridCacheEntryEx entry = iCache.peekEx(key); + + assertNotNull(entry); + + assertTrue(entry.key().putValue(buf, coCtx)); + assertTrue(entry.valueBytes().putValue(buf, coCtx)); + } + + buf.flip(); + + for (int i = 0; i < 10; i++) { + CacheObject co1 = co.toCacheObject(coCtx, buf); + + assertEquals(new TestValue(i), co1.value(coCtx, false)); + + CacheObject co2 = co.toCacheObject(coCtx, buf); + + assertEquals(new TestValue(i), co2.value(coCtx, false)); + } + } + + /** + * + */ + private static class TestValue implements Serializable { + /** */ + private int val; + + /** + * @param val Value. + */ + private TestValue(int val) { + this.val = val; + } + + /** {@inheritDoc} */ + @Override public boolean equals(Object o) { + if (this == o) + return true; + + if (!(o instanceof TestValue)) + return false; + + TestValue value = (TestValue)o; + + return val == value.val; + } + + /** {@inheritDoc} */ + @Override public int hashCode() { + return val; + } + } +}