Skip to content

ItemStreamSupport component name collision detection #339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 3.0.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2013 the original author or authors.
* Copyright 2006-2014 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.
Expand All @@ -16,17 +16,20 @@
package org.springframework.batch.item;

import org.springframework.batch.item.util.ExecutionContextUserSupport;
import org.springframework.util.ClassUtils;

/**
* Empty method implementation of {@link ItemStream}.
*
* @author Dave Syer
* @author Dean de Bree
*
* @author Jimmy Praet
*/
public abstract class ItemStreamSupport implements ItemStream {

private final ExecutionContextUserSupport executionContextUserSupport = new ExecutionContextUserSupport();

private String collisionDetectionKey = ClassUtils.getShortName(getClass());

/**
* No-op.
Expand All @@ -37,25 +40,33 @@ public void close() {
}

/**
* No-op.
* @see org.springframework.batch.item.ItemStream#open(ExecutionContext)
*/
@Override
public void open(ExecutionContext executionContext) {
executionContext.putInt(collisionDetectionKey, System.identityHashCode(this));
}

/**
* Return empty {@link ExecutionContext}.
* Checks for ExecutionContext component name collisions.
*
* @see org.springframework.batch.item.ItemStream#update(ExecutionContext)
*/
@Override
public void update(ExecutionContext executionContext) {
if (executionContext.containsKey(collisionDetectionKey) &&
executionContext.getInt(collisionDetectionKey) != System.identityHashCode(this)) {
throw new IllegalStateException(String.format("ExecutionContext key collision detected. "
+ "You are updating multiple ItemStream components of type '%s' within the same step, "
+ "without specifying a unique name.", getClass().getName()));
}
}

/**
* The name of the component which will be used as a stem for keys in the
* {@link ExecutionContext}. Subclasses should provide a default value, e.g.
* the short form of the class name.
* the short form of the class name. When using multiple components of the same
* class in a single step, make sure you are specifying a unique name for each component.
*
* @param name the name for the component
*/
Expand All @@ -65,6 +76,7 @@ public void setName(String name) {

protected void setExecutionContextName(String name) {
executionContextUserSupport.setName(name);
collisionDetectionKey = ClassUtils.getShortName(getClass()) + "." + name;
}

public String getExecutionContextKey(String key) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2014 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 org.springframework.batch.item;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
* @author Jimmy Praet
*/
public class ItemStreamSupportTests {

private ItemStreamSupport itemStreamSupport;

private ExecutionContext executionContext;

@Before
public void setUp() {
itemStreamSupport = new TestItemStreamSupport();
executionContext = new ExecutionContext();
}

@Test
public void testGetExecutionContextKey() {
Assert.assertEquals("TestItemStreamSupport.foo", itemStreamSupport.getExecutionContextKey("foo"));
}

@Test
public void testOpen() {
itemStreamSupport.open(executionContext);
Assert.assertEquals(1, executionContext.size());
}

@Test
public void testUpdateBeforeOpen() {
itemStreamSupport.update(executionContext);
Assert.assertEquals(0, executionContext.size());
}

@Test
public void testUpdateAfterOpen() {
itemStreamSupport.open(executionContext);
itemStreamSupport.update(executionContext);
Assert.assertEquals(1, executionContext.size());
}

@Test(expected=IllegalStateException.class)
public void testExecutionContextKeyCollision() {
ItemStreamSupport other = new TestItemStreamSupport();
itemStreamSupport.open(executionContext);
other.open(executionContext);
itemStreamSupport.update(executionContext);
other.update(executionContext);
}

@Test
public void testExecutionContextNoKeyCollision() {
ItemStreamSupport other = new TestItemStreamSupport();
other.setName("unique_name");
itemStreamSupport.open(executionContext);
other.open(executionContext);
itemStreamSupport.update(executionContext);
other.update(executionContext);
}

private static final class TestItemStreamSupport extends ItemStreamSupport {

public TestItemStreamSupport() {
setName("TestItemStreamSupport");
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2014 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.
Expand Down Expand Up @@ -564,7 +564,7 @@ public void testDefaultStreamContext() throws Exception {
writer.open(executionContext);
writer.update(executionContext);
assertNotNull(executionContext);
assertEquals(2, executionContext.entrySet().size());
assertEquals(3, executionContext.entrySet().size());
assertEquals(0, executionContext.getLong(ClassUtils.getShortName(FlatFileItemWriter.class) + ".current.count"));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2014 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.
Expand Down Expand Up @@ -34,6 +34,8 @@ public class CompositeItemStreamTests extends TestCase {
private CompositeItemStream manager = new CompositeItemStream();

private List<String> list = new ArrayList<String>();

private ExecutionContext executionContext = new ExecutionContext();

public void testRegisterAndOpen() {
ItemStreamSupport stream = new ItemStreamSupport() {
Expand All @@ -44,7 +46,7 @@ public void open(ExecutionContext executionContext) {
}
};
manager.register(stream);
manager.open(null);
manager.open(executionContext);
assertEquals(1, list.size());
}

Expand All @@ -58,7 +60,7 @@ public void open(ExecutionContext executionContext) {
};
manager.register(stream);
manager.register(stream);
manager.open(null);
manager.open(executionContext);
assertEquals(1, list.size());
}

Expand All @@ -70,7 +72,7 @@ public void update(ExecutionContext executionContext) {
list.add("bar");
}
});
manager.update(null);
manager.update(executionContext);
assertEquals(1, list.size());
}

Expand All @@ -94,9 +96,9 @@ public void open(ExecutionContext executionContext) {
list.add("bar");
}
} });
manager.open(null);
manager.open(executionContext);
manager.close();
manager.open(null);
manager.open(executionContext);
assertEquals(2, list.size());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2014 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.
Expand Down Expand Up @@ -123,7 +123,7 @@ public void testUpdateWithMax() throws Exception {
context.putInt("foo.read.count.max", 1);
reader.open(context);
reader.update(context);
assertEquals(2, context.size());
assertEquals(3, context.size());
}

private static class ItemCountingItemStreamItemReader extends AbstractItemCountingItemStreamItemReader<String> {
Expand Down