    001    package org.picocontainer.parameters;
    002
    003    import static org.junit.Assert.assertEquals;
    004    import static org.junit.Assert.assertFalse;
    005    import static org.junit.Assert.assertNotNull;
    006    import static org.junit.Assert.assertNull;
    007
    008    import java.io.File;
    009    import java.util.List;
    010
    011    import org.junit.Test;
    012    import org.picocontainer.Characteristics;
    013    import org.picocontainer.DefaultPicoContainer;
    014    import org.picocontainer.MutablePicoContainer;
    015    import org.picocontainer.NameBinding;
    016    import org.picocontainer.PicoContainer;
    017    import org.picocontainer.adapters.NullCA;
    018
    019    /**
    020     * test that config parameter does the right job
    021     *
    022     * @author k.pribluda
    023     *
    024     */
    025    public class ConfigParameterTestCase {
    026
    027
    028            // defaultparameter name, just for convenience
    029            NameBinding paramNameBinding = new NameBinding() {
    030                    public String getName() {
    031                            return "gloo.blum";
    032                    }
    033
    034            };
    035
    036            @Test public void testThatNoEntryIsWorkingProperly() throws Exception {
    037                    PicoContainer container = new DefaultPicoContainer();
    038                    ComponentParameter parameter = new ComponentParameter("gloo.blum");
    039
    040                    // shall be not resolvable
    041                    assertFalse(parameter.resolve(container, null, null, String.class,
    042                                               paramNameBinding, false, null).isResolved());
    043
    044                    // shall resolve instance as null
    045                    assertNull(parameter.resolve(container, null, null, String.class,
    046                                                 paramNameBinding, false, null).resolveInstance());
    047            }
    048
    049            @Test public void testThatNotStringEntryIsNotResolved() throws Exception {
    050                    MutablePicoContainer container = new DefaultPicoContainer();
    051                    container.addComponent("gloo.blum", new Integer(239));
    052
    053                    ComponentParameter parameter = new ComponentParameter("gloo.blum");
    054
    055                    // shall be not resolvable
    056                    assertFalse(parameter.resolve(container, null, null, String.class,
    057                                               paramNameBinding, false, null).isResolved());
    058
    059                    // shall resolve instance as null
    060                    assertNull(parameter.resolve(container, null, null, String.class,
    061                                                 paramNameBinding, false, null).resolveInstance());
    062
    063            }
    064
    065            /**
    066             * shall resolve as ddifferent classes
    067             *
    068             * @throws Exception
    069             */
    070            @Test public void testThatResolvedSuccessfully() throws Exception {
    071                    MutablePicoContainer container = new DefaultPicoContainer();
    072                    container.addComponent("gloo.blum", "239");
    073
    074                    ComponentParameter parameter = new ComponentParameter("gloo.blum");
    075
    076                    assertEquals(new Integer(239), parameter.resolve(container,
    077                                    new NullCA(Integer.class), null, Integer.class, paramNameBinding, false, null).resolveInstance());
    078                    assertEquals("239", parameter.resolve(container, new NullCA(String.class),
    079                    null, String.class, paramNameBinding, false, null).resolveInstance());
    080            }
    081
    082            /**
    083             * shall bomb properly if no suitable converter found
    084             *
    085             */
    086            @Test public void testThatUnavailableConverterProducesCorrectException() {
    087                    MutablePicoContainer container = new DefaultPicoContainer();
    088                    container.addComponent("gloo.blum", "239");
    089
    090                    ComponentParameter parameter = new ComponentParameter("gloo.blum");
    091
    092    //              try {
    093    //                      Object foo = parameter.resolveInstance(container, null, List.class, paramNameBinding, false);
    094    //                      fail("failed to bomb on unavailable converter");
    095    //              } catch (ConfigParameter.NoConverterAvailableException ex) {
    096    //                      // that's anticipated
    097    //              }
    098                Object foo = parameter.resolve(container, null, null, List.class, paramNameBinding, false, null).resolveInstance();
    099            assertNull(foo);
    100
    101        }
    102
    103            @Test public void testComponentInstantiation() {
    104                    DefaultPicoContainer properties = new DefaultPicoContainer();
    105                    properties.addComponent("numericProperty", "239");
    106                    properties.addComponent("doubleProperty", "17.95");
    107                    properties.addComponent("stringProperty", "foo.bar");
    108
    109                    DefaultPicoContainer container = new DefaultPicoContainer(properties);
    110                    container.addComponent("configured", ExternallyConfiguredComponent.class,
    111                                                    new ComponentParameter("numericProperty"),
    112                                                    // resolves as string
    113                                                    new ComponentParameter("stringProperty"),
    114                                                    // resolves as file
    115                                                    new ComponentParameter("stringProperty"),
    116                                                    // resolves as double
    117                                                    new ComponentParameter("doubleProperty")
    118
    119                                    );
    120
    121
    122                    ExternallyConfiguredComponent component = (ExternallyConfiguredComponent) container.getComponent("configured");
    123
    124                    assertNotNull(component);
    125                    assertEquals(239,component.getLongValue());
    126                    assertEquals("foo.bar",component.getStringParameter());
    127                    assertEquals(new File("foo.bar"),component.getFileParameter());
    128                    assertEquals(17.95,component.getDoubleParameter(),0);
    129            }
    130
    131        @Test public void testComponentInstantiationViaParamNameAssociations() {
    132            DefaultPicoContainer properties = new DefaultPicoContainer();
    133            properties.addConfig("longValue", "239");
    134            properties.addConfig("doubleParameter", "17.95");
    135            properties.addConfig("stringParameter", "foo.bar");
    136            properties.addConfig("fileParameter", "bar.txt");
    137
    138            DefaultPicoContainer container = new DefaultPicoContainer(properties);
    139            container.as(Characteristics.USE_NAMES).addComponent(ExternallyConfiguredComponent.class);
    140
    141            ExternallyConfiguredComponent component = container.getComponent(ExternallyConfiguredComponent.class);
    142
    143            assertNotNull(component);
    144            assertEquals(239,component.getLongValue());
    145            assertEquals("foo.bar",component.getStringParameter());
    146            assertEquals(new File("bar.txt"),component.getFileParameter());
    147            assertEquals(17.95,component.getDoubleParameter(),0);
    148        }
    149
    150
    151
    152            /**
    153             * test component to show automatic conversion
    154             *
    155             * @author ko5tik
    156             */
    157
    158            public static class ExternallyConfiguredComponent {
    159                    long longValue;
    160
    161                    String stringParameter;
    162
    163                    File fileParameter;
    164
    165                    double doubleParameter;
    166
    167                    public ExternallyConfiguredComponent(long longValue, String stringParameter, File fileParameter, double doubleParameter) {
    168                            super();
    169                            this.longValue = longValue;
    170                            this.stringParameter = stringParameter;
    171                            this.fileParameter = fileParameter;
    172                            this.doubleParameter = doubleParameter;
    173                    }
    174
    175                    public double getDoubleParameter() {
    176                            return doubleParameter;
    177                    }
    178
    179                    public File getFileParameter() {
    180                            return fileParameter;
    181                    }
    182
    183                    public long getLongValue() {
    184                            return longValue;
    185                    }
    186
    187                    public String getStringParameter() {
    188                            return stringParameter;
    189                    }
    190
    191                    public void setDoubleParameter(double doubleParameter) {
    192                            this.doubleParameter = doubleParameter;
    193                    }
    194
    195                    public void setFileParameter(File fileParameter) {
    196                            this.fileParameter = fileParameter;
    197                    }
    198
    199                    public void setLongValue(long longValue) {
    200                            this.longValue = longValue;
    201                    }
    202
    203                    public void setStringParameter(String stringParameter) {
    204                            this.stringParameter = stringParameter;
    205                    }
    206
    207            }
    208
    209    }