Skip to content

Commit

Permalink
#380 Correction for ModelFinder with multiple databases
Browse files Browse the repository at this point in the history
  • Loading branch information
ericbn committed Mar 9, 2015
1 parent 61f22af commit ee22d7f
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 23 deletions.
40 changes: 18 additions & 22 deletions activejdbc/src/main/java/org/javalite/activejdbc/ModelFinder.java
@@ -1,20 +1,18 @@
/*
Copyright 2009-2014 Igor Polevoy
Copyright 2009-2015 Igor Polevoy
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
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
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.
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.javalite.activejdbc;


Expand All @@ -33,17 +31,15 @@ public class ModelFinder {

protected static void findModels(String dbName) throws IOException, ClassNotFoundException {
//this is for static instrumentation. In case of dynamic, the modelClassNames will already be filled.
if(modelClassNames.isEmpty()){
List<String> models = Registry.instance().getConfiguration().getModelNames(dbName);
if (models != null && !models.isEmpty()) {
for (String model : models) {
modelFound(model);
}
} else {
throw new InitException("you are trying to work with models, but no models are found. Maybe you have " +
"no models in project, or you did not instrument the models. It is expected that you have " +
"a file activejdbc_models.properties on classpath");
List<String> models = Registry.instance().getConfiguration().getModelNames(dbName);
if (models != null && !models.isEmpty()) {
for (String model : models) {
modelFound(model);
}
} else {
throw new InitException("you are trying to work with models, but no models are found. Maybe you have " +
"no models in project, or you did not instrument the models. It is expected that you have " +
"a file activejdbc_models.properties on classpath");
}
}

Expand Down
68 changes: 68 additions & 0 deletions activejdbc/src/test/java/org/javalite/activejdbc/OtherDbTest.java
@@ -0,0 +1,68 @@
/*
Copyright 2009-2015 Igor Polevoy
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.javalite.activejdbc;


import java.sql.SQLException;
import java.util.List;
import org.javalite.activejdbc.test.ActiveJDBCTest;
import org.junit.Test;

import org.javalite.activejdbc.test_models.OtherDbModel;
import org.javalite.activejdbc.test_models.User;
import org.junit.After;
import org.junit.Before;

/**
* @author Eric Nielsen
*/
public class OtherDbTest extends ActiveJDBCTest {

@Before
public void setUp() throws SQLException {
DB db = new DB("test");
db.open("org.h2.Driver", "jdbc:h2:mem:other;DB_CLOSE_DELAY=-1", "sa", "");
db.exec("DROP TABLE IF EXISTS other_db_models;");
db.exec("CREATE TABLE other_db_models (id int(11) NOT NULL auto_increment PRIMARY KEY, name VARCHAR(56));");
db.connection().setAutoCommit(false);
deleteAndPopulateTable("users");
db.exec("INSERT INTO other_db_models (id, name) VALUES(1, 'Foo');");
db.exec("INSERT INTO other_db_models (id, name) VALUES(2, 'Bar');");
}

@After
public void tearDown() throws SQLException {
DB db = new DB("test");
try {
db.connection().rollback();
} finally {
db.close();
}
}

@Test
public void shouldFindFOtherDb() {
List<User> users = User.findAll().orderBy("id");
List<OtherDbModel> others = OtherDbModel.findAll().orderBy("id");
the(users.size()).shouldBeEqual(2);
the(users.get(0).get("first_name")).shouldBeEqual("Marilyn");
the(users.get(1).get("first_name")).shouldBeEqual("John");
the(others.size()).shouldBeEqual(2);
the(others.get(0).get("name")).shouldBeEqual("Foo");
the(others.get(1).get("name")).shouldBeEqual("Bar");
}
}
Expand Up @@ -7,5 +7,5 @@
* @author Igor Polevoy: 4/23/12 12:08 PM
*/
@DbName("test")
public class OtherDBModel extends Model {
public class OtherDbModel extends Model {
}

0 comments on commit ee22d7f

Please sign in to comment.