View Javadoc
1   package fr.ifremer.dali.dao;
2   
3   /*
4    * #%L
5    * Dali :: Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import com.google.common.collect.Lists;
27  import fr.ifremer.dali.config.DaliConfiguration;
28  import fr.ifremer.dali.config.DaliConfigurationOption;
29  import fr.ifremer.dali.service.DaliServiceLocator;
30  import fr.ifremer.quadrige3.core.security.SecurityContextHelper;
31  import fr.ifremer.quadrige3.core.service.ServiceLocator;
32  import fr.ifremer.quadrige3.core.test.DatabaseResource;
33  import org.junit.runner.Description;
34  
35  import java.util.Arrays;
36  import java.util.List;
37  
38  public class DaliDatabaseResource extends DatabaseResource {
39  
40      public static final String MODULE_NAME = "dali-core";
41      public static final String HSQLDB_SRC_DATABASE_DIRECTORY = String.format(HSQLDB_SRC_DATABASE_DIRECTORY_PATTERN, MODULE_NAME);
42  
43      private final DaliDatabaseFixtures fixtures = new DaliDatabaseFixtures();
44  
45      public static DaliDatabaseResource readDb() {
46          return new DaliDatabaseResource("");
47      }
48  
49      public static DaliDatabaseResource readDb(String dbName, boolean enableServices) {
50          if (!enableServices) {
51              return new DaliDatabaseResource(dbName, "beanRefFactoryWithNoService.xml", "beanRefFactoryWithNoService", false);
52          }
53          return new DaliDatabaseResource(dbName);
54      }
55  
56      public static DaliDatabaseResource writeDb() {
57          return new DaliDatabaseResource("", true);
58      }
59  
60      public static DaliDatabaseResource writeDb(String dbName) {
61          return new DaliDatabaseResource(dbName, true);
62      }
63  
64      public static DaliDatabaseResource writeDb(String dbName, boolean enableServices) {
65          if (!enableServices) {
66              return new DaliDatabaseResource(dbName, "beanRefFactoryWithNoService.xml", "beanRefFactoryWithNoService", true);
67          }
68          return new DaliDatabaseResource(dbName, true);
69      }
70  
71      public static DaliDatabaseResource noDb() {
72          return new DaliDatabaseResource(
73                  "", "beanRefFactoryWithNoDb.xml", "beanRefFactoryWithNoDb");
74      }
75  
76      protected DaliDatabaseResource(String dbName) {
77          super(dbName, null, null, false);
78      }
79  
80      protected DaliDatabaseResource(String dbName, boolean writeDb) {
81          super(dbName, null, null, writeDb);
82      }
83  
84      protected DaliDatabaseResource(String dbName, String beanFactoryReferenceLocation,
85                                     String beanRefFactoryReferenceId) {
86          super(dbName, beanFactoryReferenceLocation, beanRefFactoryReferenceId, false);
87      }
88  
89      protected DaliDatabaseResource(String dbName, String beanFactoryReferenceLocation,
90                                     String beanRefFactoryReferenceId,
91                                     boolean writeDb) {
92          super(dbName, beanFactoryReferenceLocation, beanRefFactoryReferenceId, writeDb);
93      }
94  
95      @Override
96      protected String getConfigFilesPrefix() {
97          return MODULE_NAME + "-test";
98      }
99  
100     @Override
101     protected String getI18nBundleName() {
102         return MODULE_NAME + "-i18n";
103     }
104 
105     @Override
106     protected String getModuleDirectory() {
107         return MODULE_NAME;
108     }
109 
110     @Override
111     public String getBuildEnvironment() {
112         return BUILD_ENVIRONMENT_DEFAULT;
113     }
114 
115     public DaliDatabaseFixtures getFixtures() {
116         return fixtures;
117     }
118 
119     @Override
120     protected void before(Description description) throws Throwable {
121         // Call inherited method
122         super.before(description);
123 
124         // Initialize default context
125         if (getBeanFactoryReferenceLocation() == null) {
126             ServiceLocator.setInstance(DaliServiceLocator.instance());
127         }
128 
129         // Set Authentication (fake user)
130         SecurityContextHelper.authenticate("demo", "demo");
131 
132         DaliServiceLocator.instance().getDataContext().setRecorderPersonId(1001);
133         DaliServiceLocator.instance().getDataContext().setRecorderDepartmentId(2);
134 
135         log.debug(DaliConfiguration.getInstance().getJdbcUrl());
136     }
137 
138     @Override
139     protected String[] getConfigArgs() {
140         List<String> configArgs = Lists.newArrayList(Arrays.asList(super.getConfigArgs()));
141         configArgs.addAll(Lists.newArrayList(
142                 "--option", DaliConfigurationOption.BASEDIR.getKey(), getResourceDirectory().getAbsolutePath()
143 //                , "--option", QuadrigeCoreConfigurationOption.AUTHENTICATION_DISABLED.getKey(), Boolean.toString(true)
144                 , "--option", DaliConfigurationOption.DB_ENUMERATION_RESOURCE.getKey(), "classpath*:quadrige3-db-enumerations.properties,classpath*:dali-db-test-enumerations.properties"
145         ));
146         return configArgs.toArray(new String[configArgs.size()]);
147     }
148 
149     /**
150      * Convenience methods that could be override to initialize other configuration
151      *
152      * @param configFilename configuration file name
153      */
154     @Override
155     protected void initConfiguration(String configFilename) {
156         String[] configArgs = getConfigArgs();
157         DaliConfiguration daliConfiguration = new DaliConfiguration(configFilename, configArgs);
158         DaliConfiguration.setInstance(daliConfiguration);
159     }
160 
161 }