WrapperSetupHelper { static HashMap<String, Integer> getNumberOfAttributes(final IComponent spoutOrBolt, final Collection<String> rawOutputs) throws IllegalArgumentException { final SetupOutputFieldsDeclarer declarer = new SetupOutputFieldsDeclarer(); spoutOrBolt.declareOutputFields(declarer); for (Entry<String, Integer> schema : declarer.outputSchemas.entrySet()) { int declaredNumberOfAttributes = schema.getValue(); if ((declaredNumberOfAttributes < 0) || (declaredNumberOfAttributes > 25)) { throw new IllegalArgumentException( "Provided bolt declares non supported number of output attributes. Must be in range [0;25] but " + "was " + declaredNumberOfAttributes); } if (rawOutputs != null && rawOutputs.contains(schema.getKey())) { if (declaredNumberOfAttributes != 1) { throw new IllegalArgumentException( "Ouput type is requested to be raw type, but provided bolt declares more then one output " + "attribute."); } schema.setValue(-1); } } return declarer.outputSchemas; }    }
@Test public void testEmptyDeclarerBolt() { IComponent boltOrSpout; if (this.r.nextBoolean()) { boltOrSpout = mock(IRichSpout.class); } else { boltOrSpout = mock(IRichBolt.class); } Assert.assertEquals(new HashMap<String, Integer>(), WrapperSetupHelper.getNumberOfAttributes(boltOrSpout, null)); }
@Test(expected = IllegalArgumentException.class) public void testRawType() throws Exception { IComponent boltOrSpout; if (this.r.nextBoolean()) { boltOrSpout = mock(IRichSpout.class); } else { boltOrSpout = mock(IRichBolt.class); } final SetupOutputFieldsDeclarer declarer = new SetupOutputFieldsDeclarer(); declarer.declare(new Fields("dummy1", "dummy2")); PowerMockito.whenNew(SetupOutputFieldsDeclarer.class).withNoArguments().thenReturn(declarer); WrapperSetupHelper.getNumberOfAttributes(boltOrSpout, Sets.newHashSet(new String[] { Utils.DEFAULT_STREAM_ID })); }
@Test(expected = IllegalArgumentException.class) public void testToManyAttributes() throws Exception { IComponent boltOrSpout; if (this.r.nextBoolean()) { boltOrSpout = mock(IRichSpout.class); } else { boltOrSpout = mock(IRichBolt.class); } final SetupOutputFieldsDeclarer declarer = new SetupOutputFieldsDeclarer(); final String[] schema = new String[26]; for (int i = 0; i < schema.length; ++i) { schema[i] = "a" + i; } declarer.declare(new Fields(schema)); PowerMockito.whenNew(SetupOutputFieldsDeclarer.class).withNoArguments().thenReturn(declarer); WrapperSetupHelper.getNumberOfAttributes(boltOrSpout, null); }