From d748648b235bd7dc95bfb2672ee156e0eb132bd8 Mon Sep 17 00:00:00 2001 From: Jonathan Ellis Date: Fri, 7 Aug 2009 23:29:50 +0000 Subject: [PATCH] fix broken casts. patch by Bill de hOra; reviewed by jbellis for CASSANDRA-338 git-svn-id: https://svn.apache.org/repos/asf/incubator/cassandra/trunk@802267 13f79535-47bb-0310-9956-ffa450edef68 --- .../cassandra/service/StorageService.java | 6 +- .../cassandra/service/StorageServiceTest.java | 56 +++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 test/unit/org/apache/cassandra/service/StorageServiceTest.java diff --git a/src/java/org/apache/cassandra/service/StorageService.java b/src/java/org/apache/cassandra/service/StorageService.java index 21481c4f17..1d0e12588e 100644 --- a/src/java/org/apache/cassandra/service/StorageService.java +++ b/src/java/org/apache/cassandra/service/StorageService.java @@ -683,9 +683,11 @@ public final class StorageService implements IEndPointStateChangeSubscriber, Sto { EndPoint target = new EndPoint(host, DatabaseDescriptor.getStoragePort()); /* Set up the stream manager with the files that need to streamed */ - StreamManager.instance(target).addFilesToStream((StreamContextManager.StreamContext[]) streamContexts.toArray()); + final StreamContextManager.StreamContext[] contexts = streamContexts.toArray(new StreamContextManager.StreamContext[streamContexts.size()]); + StreamManager.instance(target).addFilesToStream(contexts); /* Send the bootstrap initiate message */ - BootstrapInitiateMessage biMessage = new BootstrapInitiateMessage((StreamContextManager.StreamContext[]) streamContexts.toArray()); + final StreamContextManager.StreamContext[] bootContexts = streamContexts.toArray(new StreamContextManager.StreamContext[streamContexts.size()]); + BootstrapInitiateMessage biMessage = new BootstrapInitiateMessage(bootContexts); Message message = BootstrapInitiateMessage.makeBootstrapInitiateMessage(biMessage); if (logger_.isDebugEnabled()) logger_.debug("Sending a bootstrap initiate message to " + target + " ..."); diff --git a/test/unit/org/apache/cassandra/service/StorageServiceTest.java b/test/unit/org/apache/cassandra/service/StorageServiceTest.java new file mode 100644 index 0000000000..ebcc2a4af8 --- /dev/null +++ b/test/unit/org/apache/cassandra/service/StorageServiceTest.java @@ -0,0 +1,56 @@ +/* +* 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.cassandra.service; + +import org.junit.Test; +import org.junit.Assert; +import org.apache.cassandra.net.io.StreamContextManager; + +import java.util.List; +import java.util.ArrayList; + +public class StorageServiceTest { + + + @Test + public void testImpossibleCast() { + List streamContexts = new ArrayList(); + try { + StreamContextManager.StreamContext[] arr = (StreamContextManager.StreamContext[]) streamContexts.toArray(); + Assert.fail("expected ClassCastException from Object[] to StreamContextManager.StreamContext[]"); + + } catch (ClassCastException e) { + Assert.assertTrue(true); + } + } + + + @Test + public void testPossibleCast() { + + List streamContexts = new ArrayList(); + StreamContextManager.StreamContext[] contexts = streamContexts.toArray(new StreamContextManager.StreamContext[streamContexts.size()]); + Assert.assertTrue(contexts.length == 0); + + StreamContextManager.StreamContext streamContext = new StreamContextManager.StreamContext("foofile", 0, "fooTable"); + streamContexts.add(streamContext); + contexts = streamContexts.toArray(new StreamContextManager.StreamContext[streamContexts.size()]); + Assert.assertTrue(contexts.length == 1); + } +}