killbill-uncached

Details

diff --git a/account/src/test/java/com/ning/billing/account/glue/AccountModuleMock.java b/account/src/test/java/com/ning/billing/account/glue/AccountModuleMock.java
new file mode 100644
index 0000000..422a159
--- /dev/null
+++ b/account/src/test/java/com/ning/billing/account/glue/AccountModuleMock.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2010-2011 Ning, Inc.
+ *
+ * Ning licenses this file to you 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 com.ning.billing.account.glue;
+
+import org.skife.config.ConfigurationObjectFactory;
+import org.skife.jdbi.v2.DBI;
+
+import com.ning.billing.dbi.DBIProvider;
+import com.ning.billing.dbi.DbiConfig;
+
+public class AccountModuleMock extends AccountModule {
+
+    protected void installDBI() {
+        bind(DBI.class).toProvider(DBIProvider.class).asEagerSingleton();
+        final DbiConfig config = new ConfigurationObjectFactory(System.getProperties()).build(DbiConfig.class);
+        bind(DbiConfig.class).toInstance(config);
+    }
+
+    @Override
+    protected void configure() {
+        installDBI();
+        super.configure();
+
+    }
+}
diff --git a/util/src/test/java/com/ning/billing/dbi/DbiConfig.java b/util/src/test/java/com/ning/billing/dbi/DbiConfig.java
new file mode 100644
index 0000000..feb6ebd
--- /dev/null
+++ b/util/src/test/java/com/ning/billing/dbi/DbiConfig.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2010-2011 Ning, Inc.
+ *
+ * Ning licenses this file to you 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 com.ning.billing.dbi;
+
+import org.skife.config.Config;
+import org.skife.config.Default;
+import org.skife.config.Description;
+import org.skife.config.TimeSpan;
+
+public interface DbiConfig
+{
+    @Description("The jdbc url for the database")
+    @Config("com.ning.billing.dbi.jdbc.url")
+    @Default("jdbc:mysql://127.0.0.1:3306/killbill")
+    String getJdbcUrl();
+
+    @Description("The jdbc user name for the database")
+    @Config("com.ning.billing.dbi.jdbc.user")
+    @Default("root")
+    String getUsername();
+
+    @Description("The jdbc password for the database")
+    @Config("com.ning.billing.dbi.jdbc.password")
+    @Default("root")
+    String getPassword();
+
+    @Description("The minimum allowed number of idle connections to the database")
+    @Config("com.ning.billing.dbi.jdbc.minIdle")
+    @Default("1")
+    int getMinIdle();
+
+    @Description("The maximum allowed number of active connections to the database")
+    @Config("com.ning.billing.dbi.jdbc.maxActive")
+    @Default("10")
+    int getMaxActive();
+
+    @Description("How long to wait before a connection attempt to the database is considered timed out")
+    @Config("com.ning.billing.dbi.jdbc.connectionTimeout")
+    @Default("10s")
+    TimeSpan getConnectionTimeout();
+}
\ No newline at end of file
diff --git a/util/src/test/java/com/ning/billing/dbi/DBIProvider.java b/util/src/test/java/com/ning/billing/dbi/DBIProvider.java
new file mode 100644
index 0000000..a4a7b61
--- /dev/null
+++ b/util/src/test/java/com/ning/billing/dbi/DBIProvider.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2010-2011 Ning, Inc.
+ *
+ * Ning licenses this file to you 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 com.ning.billing.dbi;
+
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.jolbox.bonecp.BoneCPConfig;
+import com.jolbox.bonecp.BoneCPDataSource;
+import com.ning.jdbi.metrics.JdbiGroupStrategy;
+import com.ning.jdbi.metrics.MetricsTimingCollector;
+import com.ning.jdbi.metrics.SqlJdbiGroupStrategy;
+import com.yammer.metrics.core.MetricsRegistry;
+import org.skife.jdbi.v2.DBI;
+import org.skife.jdbi.v2.TimingCollector;
+import org.skife.jdbi.v2.logging.Log4JLog;
+import org.skife.jdbi.v2.tweak.SQLLog;
+
+import java.util.concurrent.TimeUnit;
+
+public class DBIProvider implements Provider<DBI>
+{
+    private final MetricsRegistry metricsRegistry;
+    private final DbiConfig config;
+
+    @Inject
+    public DBIProvider(final MetricsRegistry metricsRegistry, final DbiConfig config)
+    {
+        this.metricsRegistry = metricsRegistry;
+        this.config = config;
+    }
+
+    @Override
+    public DBI get()
+    {
+        final BoneCPConfig dbConfig = new BoneCPConfig();
+        dbConfig.setJdbcUrl(config.getJdbcUrl());
+        dbConfig.setUsername(config.getUsername());
+        dbConfig.setPassword(config.getPassword());
+        dbConfig.setMinConnectionsPerPartition(config.getMinIdle());
+        dbConfig.setMaxConnectionsPerPartition(config.getMaxActive());
+        dbConfig.setConnectionTimeout(config.getConnectionTimeout().getPeriod(), config.getConnectionTimeout().getUnit());
+        dbConfig.setPartitionCount(1);
+        dbConfig.setDefaultTransactionIsolation("READ_COMMITTED");
+        dbConfig.setDisableJMX(false);
+
+        final BoneCPDataSource ds = new BoneCPDataSource(dbConfig);
+        final DBI dbi = new DBI(ds);
+        final SQLLog log = new Log4JLog();
+        dbi.setSQLLog(log);
+
+        final JdbiGroupStrategy jdbiGroupStrategy = new SqlJdbiGroupStrategy();
+        final TimingCollector timingCollector = new MetricsTimingCollector(metricsRegistry, jdbiGroupStrategy, TimeUnit.MILLISECONDS, TimeUnit.SECONDS);
+        dbi.setTimingCollector(timingCollector);
+
+        return dbi;
+    }
+}
\ No newline at end of file
diff --git a/util/src/test/java/com/ning/billing/util/clock/ClockMock.java b/util/src/test/java/com/ning/billing/util/clock/ClockMock.java
new file mode 100644
index 0000000..7e41d1c
--- /dev/null
+++ b/util/src/test/java/com/ning/billing/util/clock/ClockMock.java
@@ -0,0 +1,134 @@
+/*
+ * Copyright 2010-2011 Ning, Inc.
+ *
+ * Ning licenses this file to you 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 com.ning.billing.util.clock;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+
+import com.ning.billing.catalog.api.IDuration;
+
+// STEPH should really be in tests but not accessible from other sub modules
+public class ClockMock extends Clock {
+
+    private enum DeltaType {
+        DELTA_NONE,
+        DELTA_DURATION,
+        DELTA_ABS
+    }
+
+    private long deltaFromRealityMs;
+    private List<IDuration> deltaFromRealityDuration;
+    private long deltaFromRealitDurationEpsilon;
+    private DeltaType deltaType;
+
+    public ClockMock() {
+        deltaType = DeltaType.DELTA_NONE;
+        deltaFromRealityMs = 0;
+        deltaFromRealitDurationEpsilon = 0;
+        deltaFromRealityDuration = null;
+    }
+
+    @Override
+    public synchronized DateTime getNow(DateTimeZone tz) {
+        return adjust(super.getNow(tz));
+    }
+
+    @Override
+    public synchronized DateTime getUTCNow() {
+        return getNow(DateTimeZone.UTC);
+    }
+
+    public synchronized void setDeltaFromReality(IDuration delta, long epsilon) {
+        deltaType = DeltaType.DELTA_DURATION;
+        deltaFromRealityDuration = new ArrayList<IDuration>();
+        deltaFromRealityDuration.add(delta);
+        deltaFromRealitDurationEpsilon = epsilon;
+        deltaFromRealityMs = 0;
+    }
+
+    public synchronized void addDeltaFromReality(IDuration delta) {
+        if (deltaType != DeltaType.DELTA_DURATION) {
+            throw new RuntimeException("ClockMock should be set with type DELTA_DURATION");
+        }
+        deltaFromRealityDuration.add(delta);
+    }
+
+    public synchronized void setDeltaFromReality(long delta) {
+        deltaType = DeltaType.DELTA_ABS;
+        deltaFromRealityDuration = null;
+        deltaFromRealitDurationEpsilon = 0;
+        deltaFromRealityMs = delta;
+    }
+
+    public synchronized void resetDeltaFromReality() {
+        deltaType = DeltaType.DELTA_NONE;
+        deltaFromRealityDuration = null;
+        deltaFromRealitDurationEpsilon = 0;
+        deltaFromRealityMs = 0;
+    }
+
+    private DateTime adjust(DateTime realNow) {
+        switch(deltaType) {
+            case DELTA_NONE:
+                return realNow;
+            case DELTA_ABS:
+                return adjustFromAbsolute(realNow);
+            case DELTA_DURATION:
+                return adjustFromDuration(realNow);
+            default:
+                return null;
+        }
+    }
+
+    private DateTime adjustFromDuration(DateTime input) {
+
+        DateTime result = input;
+        for (IDuration cur : deltaFromRealityDuration) {
+
+            int length = cur.getLength();
+            switch (cur.getUnit()) {
+            case DAYS:
+                result = result.plusDays(cur.getLength());
+                break;
+
+            case MONTHS:
+                result = result.plusMonths(cur.getLength());
+                break;
+
+            case YEARS:
+                result = result.plusYears(cur.getLength());
+                break;
+
+            case UNLIMITED:
+            default:
+                throw new RuntimeException("ClockMock is adjusting an unlimtited time period");
+            }
+        }
+        if (deltaFromRealitDurationEpsilon != 0) {
+            result = result.plus(deltaFromRealitDurationEpsilon);
+        }
+        return result;
+    }
+
+    private DateTime adjustFromAbsolute(DateTime input) {
+        return input.plus(deltaFromRealityMs);
+    }
+
+}