applies tidies for imageChooser (JCLOUDS-331) as suggested in pull #202

This commit is contained in:
Alex Heneveld 2013-11-15 09:19:21 +00:00 committed by Andrew Phillips
parent 029e718435
commit 9a7336bb0e
4 changed files with 81 additions and 64 deletions

View File

@ -96,7 +96,6 @@ public class EC2TemplateBuilderImplTest extends TemplateBuilderImplTest {
return "us-east-1/"+uniqueLabel; return "us-east-1/"+uniqueLabel;
} }
@Override @Override
@Test @Test
public void testHardwareWithImageIdPredicateOnlyAcceptsImageWhenLocationNull() { public void testHardwareWithImageIdPredicateOnlyAcceptsImageWhenLocationNull() {

View File

@ -146,9 +146,16 @@ public class TemplateBuilderImplTest {
verify(defaultTemplate, optionsProvider, templateBuilderProvider); verify(defaultTemplate, optionsProvider, templateBuilderProvider);
} }
/**
* Resolves images using a default TemplateBuilder, optionally applying a customizer to that
* TemplateBuilder where the customizer may e.g. apply an imageChooser
*/
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected void doTestResolveImages(Supplier<Set<? extends Image>> images, Image expectedBest, protected void doTestResolveImagesWithTemplateBuilderCustomizer(
Function<TemplateBuilderImpl, TemplateBuilderImpl> builderCustomisation) { Supplier<Set<? extends Image>> inputImages,
Function<TemplateBuilderImpl, TemplateBuilderImpl> templateBuilderPerTestCustomizer,
Image expectedBest) {
Hardware hardware = new HardwareBuilder().id("hardwareId").build(); Hardware hardware = new HardwareBuilder().id("hardwareId").build();
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
@ -161,30 +168,40 @@ public class TemplateBuilderImplTest {
replay(defaultTemplate, optionsProvider, templateBuilderProvider); replay(defaultTemplate, optionsProvider, templateBuilderProvider);
TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region, TemplateBuilderImpl template = createTemplateBuilder(null, locations, inputImages, hardwares, region,
optionsProvider, templateBuilderProvider); optionsProvider, templateBuilderProvider);
template = builderCustomisation.apply(template); template = templateBuilderPerTestCustomizer.apply(template);
assertEquals(template.resolveImage(hardware, images.get()), expectedBest); assertEquals(template.resolveImage(hardware, inputImages.get()), expectedBest);
verify(defaultTemplate, optionsProvider, templateBuilderProvider); verify(defaultTemplate, optionsProvider, templateBuilderProvider);
} }
protected void doTestResolveImages(Supplier<Set<? extends Image>> inputImages, Image expectedBest) {
doTestResolveImagesWithTemplateBuilderCustomizer(
inputImages,
Functions.<TemplateBuilderImpl>identity(),
expectedBest);
}
public void testResolveImagesSimple() { public void testResolveImagesSimple() {
doTestResolveImages(Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of( doTestResolveImages(
image, image64bit)), Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of(image, image64bit)),
image64bit, Functions.<TemplateBuilderImpl>identity()); image64bit);
} }
public void testResolveImagesPrefersNull() { public void testResolveImagesPrefersNull() {
// preferring null has been the default behaviour; not sure if this is ideal // preferring null has been the default behaviour; not sure if this is ideal
// (would make more sense to prefer nonNull) but don't change behaviour by default // (would make more sense to prefer nonNull) but don't change behaviour by default
doTestResolveImages(Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of( doTestResolveImages(
image, imageArchNull, image64bit)), Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of(image, imageArchNull, image64bit)),
imageArchNull, Functions.<TemplateBuilderImpl>identity()); imageArchNull);
} }
public void testResolveImagesCustomSorterPreferringNonNull() { public void testResolveImagesCustomSorterPreferringNonNull() {
// preferring null has been the default behaviour;
// see comments in TemplateBuilderImpl.DEFAULT_IMAGE_ORDERING
final Ordering<Image> sorterPreferringNonNullArch = new Ordering<Image>() { final Ordering<Image> sorterPreferringNonNullArch = new Ordering<Image>() {
@Override @Override
public int compare(Image left, Image right) { public int compare(Image left, Image right) {
@ -198,30 +215,28 @@ public class TemplateBuilderImplTest {
assertTrue(TemplateBuilderImpl.DEFAULT_IMAGE_ORDERING.compare(image64bit, imageArchNull) < 0, "wrong default image ordering"); assertTrue(TemplateBuilderImpl.DEFAULT_IMAGE_ORDERING.compare(image64bit, imageArchNull) < 0, "wrong default image ordering");
assertTrue(sorterPreferringNonNullArch.compare(image64bit, imageArchNull) > 0, "wrong custom image ordering"); assertTrue(sorterPreferringNonNullArch.compare(image64bit, imageArchNull) > 0, "wrong custom image ordering");
// preferring null has been the default behaviour; Function<TemplateBuilderImpl, TemplateBuilderImpl> tbCustomiser = new Function<TemplateBuilderImpl, TemplateBuilderImpl>() {
// see comments in TemplateBuilderImpl.DEFAULT_IMAGE_ORDERING
doTestResolveImages(Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of(
image, imageArchNull, image64bit)),
image64bit, new Function<TemplateBuilderImpl,TemplateBuilderImpl>() {
@Override @Override
public TemplateBuilderImpl apply(TemplateBuilderImpl input) { public TemplateBuilderImpl apply(TemplateBuilderImpl input) {
return input.imageChooser(input.imageChooserFromOrdering(sorterPreferringNonNullArch)); return input.imageChooser(input.imageChooserFromOrdering(sorterPreferringNonNullArch));
} }
}); };
doTestResolveImagesWithTemplateBuilderCustomizer(
Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of(image, imageArchNull, image64bit)),
tbCustomiser, image64bit);
} }
public void testResolveImagesPrefersImageBecauseNameIsLastAlphabetically() { public void testResolveImagesPrefersImageBecauseNameIsLastAlphabetically() {
// preferring that which comes later alphabetically is the default behaviour; // preferring that which comes later alphabetically is the default behaviour;
// see comments in TemplateBuilderImpl.DEFAULT_IMAGE_ORDERING // see comments in TemplateBuilderImpl.DEFAULT_IMAGE_ORDERING
doTestResolveImages(Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of( doTestResolveImages(
imageNameAlt, image)), Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of(imageNameAlt, image)),
image, Functions.<TemplateBuilderImpl>identity()); image);
} }
public void testResolveImagesCustomSorterPreferringAltImage() { public void testResolveImagesCustomSorterPreferringAltImage() {
doTestResolveImages(Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of( Function<TemplateBuilderImpl, TemplateBuilderImpl> tbSortWithAlternate = new Function<TemplateBuilderImpl, TemplateBuilderImpl>() {
imageNameAlt, image, imageArchNull, image64bit)),
imageNameAlt, new Function<TemplateBuilderImpl,TemplateBuilderImpl>() {
@Override @Override
public TemplateBuilderImpl apply(TemplateBuilderImpl input) { public TemplateBuilderImpl apply(TemplateBuilderImpl input) {
return input.imageChooser(input.imageChooserFromOrdering(new Ordering<Image>() { return input.imageChooser(input.imageChooserFromOrdering(new Ordering<Image>() {
@ -235,13 +250,14 @@ public class TemplateBuilderImplTest {
} }
})); }));
} }
}); };
doTestResolveImagesWithTemplateBuilderCustomizer(
Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of(imageNameAlt, image, imageArchNull, image64bit)),
tbSortWithAlternate, imageNameAlt);
} }
public void testResolveImagesCustomChooserPreferringAltImage() { public void testResolveImagesCustomChooserPreferringAltImage() {
doTestResolveImages(Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of( Function<TemplateBuilderImpl, TemplateBuilderImpl> tbChooseAlternate = new Function<TemplateBuilderImpl, TemplateBuilderImpl>() {
imageNameAlt, image, imageArchNull, image64bit)),
imageNameAlt, new Function<TemplateBuilderImpl,TemplateBuilderImpl>() {
@Override @Override
public TemplateBuilderImpl apply(TemplateBuilderImpl input) { public TemplateBuilderImpl apply(TemplateBuilderImpl input) {
return input.imageChooser(new Function<Iterable<? extends Image>, Image>() { return input.imageChooser(new Function<Iterable<? extends Image>, Image>() {
@ -256,10 +272,12 @@ public class TemplateBuilderImplTest {
} }
}); });
} }
}); };
doTestResolveImagesWithTemplateBuilderCustomizer(
Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of(imageNameAlt, image, imageArchNull, image64bit)),
tbChooseAlternate, imageNameAlt);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Test @Test
public void testArchWins() { public void testArchWins() {