@Rodrigo: Thanks for the tip: I will try that @Adrian: Can we use OSGI with Guice?
On Sat, Jun 27, 2009 at 9:17 AM, Adrian Cole <[email protected]> wrote: > Well, this is why I am currently working on osgi. Switching modules seems > to imply some coupling you probably don't want. > > That said, without osgi, here's what we do: parse a list of modules, adding > defaults if no overrides are present. How you add additional modules could > be a varargs factory method (Module ... modules), > Class.forName(overrideModuleName), etc. > > Here's an example how we inspect modules to satisfy the requirements of our > code. > > private Injector createInjector() { > > useDefaultPortIfNotPresent(properties); > > addLoggingModuleIfNotPresent(modules); > > addS3ParserModuleIfNotPresent(modules); > > addS3ConnectionModuleIfNotPresent(modules); > > addHttpModuleIfNeededAndNotPresent(modules); > > modules.add(new AbstractModule() { > @Override > > protected void configure() { > Names.bindProperties(binder(), checkNotNull(properties, > "properties")); > } > }); > modules.add(new S3ContextModule()); > > return Guice.createInjector(modules); > > } > > I hope this helps. > -Adrian > > > On Sat, Jun 27, 2009 at 9:45 AM, Karthik Krishnan <[email protected] > > wrote: > >> >> Adrian: Thanks for your reply: For unit tests, I am mocking behavior >> for the dao class. Ideally I would like to use DM based datasources >> for my integration tests and JNDI based data sources for my >> application to unit test the entire work flow. >> >> In Spring it is so easy to do this. I just swap one xml with another >> and let Spring framework handling the switch seamlessly. Idon't know >> how to get Guice to do it for me without having to change the module >> instance. >> >> On Jun 26, 11:51 pm, Adrian Cole <[email protected]> wrote: >> > Hi, Krishnan, >> > >> > I'd extract MyDao's interface and move the other code to >> DataSourceMyDao. >> > For unit tests, you can mock the behaviour in MyDao specific to that >> test. >> > pita perhaps, but fairly normal. Creating a more versatile StubMyDao >> would >> > involve some work. In jclouds, this proved useful, as it helps you load >> > test your code, as opposed to your db. >> > >> > All this said, you could always fire up an in-process db like derby or >> > hsqldb. Less work, but it isn't really unit testing at that point ;) >> > >> > my 2p. >> > -Adrian >> > jclouds <http://code.google.com/p/jclouds> >> > >> > On Sat, Jun 27, 2009 at 7:57 AM, Karthik Krishnan >> > <[email protected]>wrote: >> > >> > >> > >> > > Hi Guice Gurus, >> > >> > > I am just playing with Guice 2.x and I am stuck with an issue that I >> > > can not resolve. Any help would be appreciated. >> > >> > > I have a service class that takes DAO class as a constructor argument >> > > that connects to the data base. The dao class accepts a data source as >> > > a constructor argument. I have created two modules : the first one >> > > injects a JNDI based data source, the other DriverManager based data >> > > source. I would like >> > >> > > public class MyService { >> > >> > > private final MyDao myDao; >> > >> > > public MyService(MyDao myDao) { >> > > this.myDao = myDao; >> > > } >> > >> > > .... >> > >> > > } >> > >> > > public class MyDao { >> > > private final DataSource dataSource; >> > >> > > @Inject >> > > public MyDao(DataSource dataSource) { >> > > this.dataSource = dataSource; >> > > } >> > >> > > .... >> > > } >> > >> > > My modules: >> > >> > > public class JndiDataSourceModule extends AbstractModule { >> > >> > > public void configure() { >> > > bind(DataSource.class).toProvider(JndiIntegration.fromJndi >> > > (DataSource.class, "myDatasource")); >> > > } >> > > } >> > >> > > public class DriverManagerDataSourceModule extends AbstractModule { >> > > @Provides >> > > DataSource getDataSource() { >> > > return DriverManagerDataSource(driverClass, url, username, >> > > password); >> > > } >> > >> > > .... >> > > } >> > >> > > I can not figure out how to inject DriverManager based DataSource for >> > > unit tests and JNDI based datasource in my application. I was thinking >> > > of passing modules in the constructor of my service class but I don't >> > > think I would be able to mock my MyDao for exceptional conditions. >> > > Any help would be appreciated. >> >> > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/google-guice?hl=en -~----------~----~----~----~------~----~------~--~---
