Skip to content

Commit 77fcdfc

Browse files
committed
Fix the errors in test when running in Windows
1 parent 140893f commit 77fcdfc

File tree

6 files changed

+113
-84
lines changed

6 files changed

+113
-84
lines changed

src/main/java/org/silverpeas/dbbuilder/util/Configuration.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ public class Configuration {
4444
private static final String TEMP_FILES_SUBDIR = "temp";
4545
private static final String DIR_CONTRIBUTIONFILESROOT = Configuration.getHome()
4646
+ File.separator + DBREPOSITORY_SUBDIR + File.separator + CONTRIB_FILES_SUBDIR;
47-
// Répertoire racine des DB Pieces Contribution File
47+
// Répertoire racine des DB Pieces Contribution File
4848
private static final String DIR_DBPIECESFILESROOT =
4949
getHome() + File.separator + DBREPOSITORY_SUBDIR;
50-
// Répertoire temp
50+
// Répertoire temp
5151
private static final String DIR_TEMP = getHome() + File.separator + TEMP_FILES_SUBDIR;
5252

5353
/**
@@ -62,12 +62,8 @@ public static Properties loadResource(String propertyName) throws IOException {
6262
InputStream in = Configuration.class.getClassLoader().getResourceAsStream(propertyName);
6363
try {
6464
if (in == null) {
65-
String path = propertyName.replace('/', File.separatorChar);
66-
path = path.replace('\\', File.separatorChar);
67-
if (!path.startsWith(File.separator)) {
68-
path = File.separatorChar + path;
69-
}
70-
File configurationFile = new File(getHome() + File.separatorChar + "properties" + path);
65+
String path = getPropertyPath(propertyName);
66+
File configurationFile = new File(path);
7167
if (configurationFile.exists()) {
7268
in = new FileInputStream(configurationFile);
7369
}
@@ -114,11 +110,8 @@ public static Properties loadResource(File resource) throws IOException {
114110
*/
115111
public static File[] listResources(final String directoryPath, final FileFilter filter) throws
116112
IOException {
117-
String path = directoryPath;
118-
if (!directoryPath.startsWith("/")) {
119-
path = File.separatorChar + directoryPath;
120-
}
121-
File dir = new File(getHome() + File.separatorChar + "properties" + path);
113+
String path = getPropertyPath(directoryPath);
114+
File dir = new File(path);
122115
if (!dir.exists() || !dir.isDirectory()) {
123116
throw new IOException("The path '" + path + "' doesn't refer a directory!");
124117
}
@@ -132,15 +125,15 @@ public boolean accept(File file) {
132125
}
133126

134127
public static boolean isExist(final String resourcePath) {
135-
String path = resourcePath;
136-
if (!resourcePath.startsWith("/")) {
137-
path = File.separatorChar + resourcePath;
128+
String path = Configuration.class.getClassLoader().getResource(resourcePath).getPath();
129+
if (!new File(path).exists()) {
130+
path = getPropertyPath(resourcePath);
131+
return new File(path).exists();
138132
}
139-
File resource = new File(getHome() + File.separator + "properties" + path);
140-
return resource.exists();
133+
return true;
141134
}
142135

143-
// Récupère le répertoire racine d'installation
136+
// Récupère le répertoire racine d'installation
144137
public static String getHome() {
145138
if (dbbuilderHome == null) {
146139
if (!System.getProperties().containsKey(HOME_KEY)) {
@@ -162,7 +155,7 @@ public static String getPiecesFilesDir() {
162155
return DIR_DBPIECESFILESROOT;
163156
}
164157

165-
// Récupère le répertoire data d'installation
158+
// Récupère le répertoire data d'installation
166159
public static String getData() {
167160
if (dbbuilderData == null) {
168161
if (System.getProperties().containsKey(DATA_KEY)) {
@@ -184,6 +177,15 @@ public static String getLogDir() {
184177
return getHome() + File.separator + LOG_FILES_SUBDIR;
185178
}
186179

180+
private static String getPropertyPath(String propertyPath) {
181+
String path = propertyPath.replace('/', File.separatorChar);
182+
path = path.replace('\\', File.separatorChar);
183+
if (!path.startsWith(File.separator)) {
184+
path = File.separatorChar + path;
185+
}
186+
return getHome() + File.separatorChar + "properties" + path;
187+
}
188+
187189
private Configuration() {
188190
}
189191
}

src/main/java/org/silverpeas/migration/sqldomain/PasswordSizeIncrease.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,41 +42,40 @@
4242
/**
4343
* DB migration to change the type size (a varchar) of the password field upto 123 characters in the
4444
* for the table of users in the customer's domains.
45-
*
4645
* @author mmoquillon
4746
*/
4847
public class PasswordSizeIncrease extends DbBuilderDynamicPart {
4948

50-
// request to fetch all the SQL domains of the customer
49+
// request to fetch all the SQL domains of the customer
5150
static final String DOMAINS_SQL =
5251
"SELECT * FROM ST_DOMAIN WHERE className='com.stratelia.silverpeas.domains.sqldriver.SQLDriver'";
53-
// the domain property that gives the user table in the domain database
52+
// the domain property that gives the user table in the domain database
5453
static final String TABLE_OF_USERS = "database.SQLUserTableName";
55-
// the domain property that gives the password field in the user table
54+
// the domain property that gives the password field in the user table
5655
static final String PASSWORD_FIELD = "database.SQLUserPasswordColumnName";
57-
// the domain property that gives the JDBC driver to use for connecting to the database
56+
// the domain property that gives the JDBC driver to use for connecting to the database
5857
static final String SQL_DRIVER = "database.SQLClassName";
59-
// the domain property that gives the URL of the database
58+
// the domain property that gives the URL of the database
6059
static final String DATABASE_URL = "database.SQLJDBCUrl";
61-
// the domain property that gives the login to open a connection with the database
60+
// the domain property that gives the login to open a connection with the database
6261
static final String DATABASE_LOGIN = "database.SQLAccessLogin";
63-
// the domain property that gives the password associated with the login
62+
// the domain property that gives the password associated with the login
6463
static final String DATABASE_PASSWORD = "database.SQLAccessPasswd";
65-
// the H2 database defined by its JDBC driver
64+
// the H2 database defined by its JDBC driver
6665
static final String H2_DATABASE = "org.h2.Driver";
67-
// the PostgreSQL database defined by its JDBC driver
66+
// the PostgreSQL database defined by its JDBC driver
6867
static final String POSTGRESQL_DATABASE = "org.postgresql.Driver";
69-
// the MS-SQLServer database defined by the its open-source JDBC driver
68+
// the MS-SQLServer database defined by the its open-source JDBC driver
7069
static final String MSSQL_DATABASE = "net.sourceforge.jtds.jdbc.Driver";
71-
// the Oracle database defined by its proprietary JDBC driver
70+
// the Oracle database defined by its proprietary JDBC driver
7271
static final String ORACLE_DATABASE = "oracle.jdbc.driver.OracleDriver";
73-
// the H2 SQL instruction to change the type of the password field in the user table
72+
// the H2 SQL instruction to change the type of the password field in the user table
7473
static final String H2_SQL = "ALTER TABLE {0} ALTER COLUMN {1} varchar(123)";
75-
// the PostgreSQL SQL instruction to change the type of the password field in the user table
74+
// the PostgreSQL SQL instruction to change the type of the password field in the user table
7675
static final String POSTGRESQL_SQL = "ALTER TABLE {0} ALTER COLUMN {1} TYPE varchar(123)";
77-
// the MS-SQLServer SQL instruction to change the type of the password field in the user table
76+
// the MS-SQLServer SQL instruction to change the type of the password field in the user table
7877
static final String MSSQL_SQL = "ALTER TABLE {0} ALTER COLUMN {1} varchar(123)";
79-
// the Oracle SQL instruction to change the type of the password field in the user table
78+
// the Oracle SQL instruction to change the type of the password field in the user table
8079
static final String ORACLE_SQL = "ALTER TABLE {0} MODIFY ({1} varchar(123))";
8180
private Connection sharedConnection;
8281
private Console console;
@@ -88,9 +87,10 @@ public void migrate() throws Exception {
8887
String name = resourcePath.replace('.', File.separatorChar) + ".properties";
8988
Properties resource = Configuration.loadResource(name);
9089
if (resource.isEmpty()) {
91-
// the resource isn't located at com/stratelia/silverpeas/domains but at the new
92-
// properties location org/silverpeas/domains
93-
name = name.replaceFirst("com/stratelia/", "org/");
90+
// the resource isn't located at com/stratelia/silverpeas/domains but at the new
91+
// properties location org/silverpeas/domains
92+
name = name.replace("com" + File.separatorChar + "stratelia" + File.separatorChar,
93+
"org" + File.separatorChar);
9494
resource = Configuration.loadResource(name);
9595
}
9696
if (!resource.isEmpty()) {
@@ -208,7 +208,6 @@ private Connection getSharedConnection() {
208208
/**
209209
* Sets a connection to be shared by all of the domains to be updated. This is for SQL domains
210210
* defined within the same data source.
211-
*
212211
* @param connection a shared connection to SQL data source.
213212
*/
214213
protected void setSharedConnection(Connection connection) {

src/test/java/org/silverpeas/SilverpeasSettings/SilverpeasSettingsTest.java

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.util.Collection;
2828
import java.util.List;
2929
import java.util.Properties;
30-
3130
import org.apache.commons.io.FileUtils;
3231
import org.jdom.Document;
3332
import org.jdom.Element;
@@ -40,11 +39,12 @@
4039
import org.junit.Before;
4140
import org.junit.BeforeClass;
4241
import org.junit.Test;
43-
4442
import org.silverpeas.applicationbuilder.XmlDocument;
4543
import org.silverpeas.file.GestionVariables;
44+
import org.silverpeas.test.SystemInitializationForTests;
4645

4746
import static java.io.File.separatorChar;
47+
4848
import static org.silverpeas.SilverpeasSettings.SilverpeasSettings.*;
4949

5050
/**
@@ -53,17 +53,17 @@
5353
*/
5454
public class SilverpeasSettingsTest {
5555

56-
private static final String resourcesDir = System.getProperty("basedir") + separatorChar +
57-
"target" + separatorChar + "test-classes" + separatorChar;
56+
private static final String OPERATING_SYSTEM = System.getProperty("os.name").toLowerCase();
57+
private static final String SILVERPEAS_HOME = "silverpeas.home";
58+
private static String resourcesDir;
5859

5960
public SilverpeasSettingsTest() {
6061
}
6162

6263
@BeforeClass
6364
public static void setUpClass() throws Exception {
64-
System.setProperty("silverpeas.home", resourcesDir);
65-
System.setProperty("SILVERPEAS_HOME", resourcesDir);
66-
System.setProperty("JBOSS_HOME", resourcesDir);
65+
SystemInitializationForTests.initialize();
66+
resourcesDir = System.getProperty(SILVERPEAS_HOME);
6767
}
6868

6969
@AfterClass
@@ -87,11 +87,11 @@ public void tearDown() {
8787
public void testLoadGlobalVariables() throws Exception {
8888
SAXBuilder builder = new SAXBuilder();
8989
Document doc = builder.build(new File(
90-
resourcesDir + "expected" + separatorChar + "MergedSettings.xml"));
90+
resourcesDir + File.separatorChar + "expected" + separatorChar + "MergedSettings.xml"));
9191
// Get the root element
9292
Element root = doc.getRootElement();
9393
GestionVariables gv = SilverpeasSettings.loadGlobalVariables(new File(
94-
resourcesDir + "expected"), root);
94+
resourcesDir + File.separatorChar + "expected"), root);
9595
Assert.assertEquals("test@silverpeas.com", gv.getValue("ADMIN_EMAIL"));
9696
Assert.assertEquals("http://www.silverpeas.com", gv.getValue("URL_SERVER"));
9797
Assert.assertEquals("5432", gv.getValue("SQL_LISTEN_PORT_POSTGRES"));
@@ -100,7 +100,7 @@ public void testLoadGlobalVariables() throws Exception {
100100

101101
@Test
102102
public void testLoadConfiguration() throws Exception {
103-
File dir = new File(resourcesDir + "xml");
103+
File dir = new File(resourcesDir + File.separatorChar + "xml");
104104
GestionVariables properties = SilverpeasSettings.loadConfiguration(dir);
105105
Assert.assertEquals("c:/toto", properties.getValue("SILVERPEAS_HOME"));
106106
Assert.assertEquals("http://localhost:8000", properties.getValue("URL_SERVER"));
@@ -115,15 +115,15 @@ public void testLoadConfiguration() throws Exception {
115115
*/
116116
@Test
117117
public void testMergeConfigurationFiles() throws Exception {
118-
File dirXml = new File(resourcesDir + "xml");
118+
File dirXml = new File(resourcesDir + File.separatorChar + "xml");
119119
XmlDocument fileXml = new XmlDocument(dirXml, SilverpeasSettings.SILVERPEAS_SETTINGS);
120120
fileXml.load();
121121
SilverpeasSettings.mergeConfigurationFiles(fileXml, dirXml);
122122
fileXml.setOutputEncoding("UTF-8");
123123
XMLOutputter output = new XMLOutputter(Format.getPrettyFormat());
124124

125-
File tempDir = new File(System.getProperty("basedir") + separatorChar + "target" +
126-
separatorChar + "temp");
125+
File tempDir = new File(System.getProperty("basedir") + separatorChar + "target" + separatorChar
126+
+ "temp");
127127
tempDir.mkdirs();
128128
File mergedFile = new File(tempDir, "merge_result.xml");
129129
OutputStream out = new FileOutputStream(mergedFile);
@@ -132,8 +132,8 @@ public void testMergeConfigurationFiles() throws Exception {
132132
@SuppressWarnings("unchecked")
133133
List<String> resultLines = FileUtils.readLines(mergedFile, "UTF-8");
134134
@SuppressWarnings("unchecked")
135-
List<String> expectedtLines = FileUtils.readLines(new File(resourcesDir + "expected" +
136-
separatorChar + "MergedSettings.xml"), "UTF-8");
135+
List<String> expectedtLines = FileUtils.readLines(new File(resourcesDir + File.separatorChar
136+
+ "expected" + separatorChar + "MergedSettings.xml"), "UTF-8");
137137
Assert.assertNotNull(resultLines);
138138
Assert.assertEquals(expectedtLines.size(), resultLines.size());
139139
for (int i = 0; i < resultLines.size(); i++) {
@@ -145,9 +145,9 @@ public void testMergeConfigurationFiles() throws Exception {
145145
public void xmlFileTransformation() throws Exception {
146146
GestionVariables gestion = new GestionVariables(new Properties());
147147
gestion.addVariable("JBOSS_LISTEN_PORT", "9500");
148-
gestion.addVariable("SILVERPEAS_DATA_HOME_DEPENDANT", resourcesDir + "data");
149-
gestion.addVariable("SILVERPEAS_HOME_DEPENDANT", resourcesDir + "toto");
150-
String dir = resourcesDir + "transform" + separatorChar;
148+
gestion.addVariable("SILVERPEAS_DATA_HOME_DEPENDANT", resourcesDir + "/data");
149+
gestion.addVariable("SILVERPEAS_HOME_DEPENDANT", resourcesDir + "/toto");
150+
String dir = resourcesDir + File.separatorChar + "transform" + separatorChar;
151151
Element element = new Element(XML_FILE_TAG);
152152
element.setAttribute(FILE_NAME_ATTRIB, "jbossweb-tomcat55.sar/server.xml");
153153
Collection<Element> values = new ArrayList<Element>(2);
@@ -159,29 +159,32 @@ public void xmlFileTransformation() throws Exception {
159159
values = new ArrayList<Element>(2);
160160
values.add(getValueElement("@docBase", "${SILVERPEAS_DATA_HOME_DEPENDANT}/data/weblib"));
161161
values.add(getValueElement("@path", "/weblib"));
162-
element.addContent(getParameterElement("/Server/Service[@name='jboss.web']/Engine[@name='jboss.web']/" +
163-
"Host[@name='localhost']/Context[@path='/weblib']", "update", values));
162+
element.
163+
addContent(getParameterElement("/Server/Service[@name='jboss.web']/Engine[@name='jboss.web']/"
164+
+ "Host[@name='localhost']/Context[@path='/weblib']", "update", values));
164165
values = new ArrayList<Element>(2);
165166
values.add(getValueElement("@docBase", "${SILVERPEAS_DATA_HOME_DEPENDANT}/data/website"));
166167
values.add(getValueElement("@path", "/website"));
167-
element.addContent(getParameterElement("//Service[@name='jboss.web']/Engine[@name='jboss.web']" +
168-
"/Host[@name='localhost']/Context[@path='/website']", "update", values));
168+
element.addContent(getParameterElement("//Service[@name='jboss.web']/Engine[@name='jboss.web']"
169+
+ "/Host[@name='localhost']/Context[@path='/website']", "update", values));
169170
values = new ArrayList<Element>(2);
170171
values.add(getValueElement("@docBase", "${SILVERPEAS_HOME_DEPENDANT}/help/fr"));
171172
values.add(getValueElement("@path", "/help_fr"));
172-
element.addContent(getParameterElement("/Server/Service[@name='jboss.web']/Engine[@name='jboss.web']/" +
173-
"Host[@name='localhost']/Context[@path='/help_fr']", "update", values));
173+
element.
174+
addContent(getParameterElement("/Server/Service[@name='jboss.web']/Engine[@name='jboss.web']/"
175+
+ "Host[@name='localhost']/Context[@path='/help_fr']", "update", values));
174176
xmlfile(dir, element, gestion);
175177
@SuppressWarnings("unchecked")
176-
List<String> resultLines = FileUtils.readLines(new File(resourcesDir + "transform" +
177-
separatorChar + "jbossweb-tomcat55.sar" + separatorChar + "server.xml"), "UTF-8");
178+
List<String> resultLines = FileUtils.readLines(new File(resourcesDir + File.separatorChar
179+
+ "transform" + separatorChar + "jbossweb-tomcat55.sar" + separatorChar + "server.xml"),
180+
"UTF-8");
178181
@SuppressWarnings("unchecked")
179-
List<String> expectedtLines = FileUtils.readLines(new File(resourcesDir + "expected_transform" +
180-
separatorChar + "server.xml"), "UTF-8");
182+
List<String> expectedtLines = FileUtils.readLines(new File(resourcesDir + File.separatorChar
183+
+ "expected_transform" + separatorChar + "server.xml"), "UTF-8");
181184
Assert.assertNotNull(resultLines);
182185
Assert.assertEquals(expectedtLines.size(), resultLines.size());
183186
for (int i = 0; i < resultLines.size(); i++) {
184-
Assert.assertEquals(expectedtLines.get(i), resultLines.get(i));
187+
Assert.assertEquals(replaceInvalidWindowsChars(expectedtLines.get(i)), resultLines.get(i));
185188
}
186189
}
187190

@@ -199,4 +202,13 @@ protected Element getParameterElement(String key, String mode, Collection<Elemen
199202
element.setContent(values);
200203
return element;
201204
}
205+
206+
protected String replaceInvalidWindowsChars(String path) {
207+
String fixedPath = path;
208+
if (OPERATING_SYSTEM.contains("win") && path.contains("\\")) {
209+
// we are in windows and in some version of this os ${basedir} add two \ after the drive label
210+
fixedPath = path.replace("\\\\", "\\");
211+
}
212+
return fixedPath;
213+
}
202214
}

src/test/java/org/silverpeas/file/ModifPropertiesTest.java

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,50 @@
2424
package org.silverpeas.file;
2525

2626
import java.io.File;
27+
import java.io.IOException;
28+
import java.util.List;
29+
import java.util.Properties;
2730
import org.apache.commons.io.FileUtils;
2831
import org.junit.Test;
32+
2933
import static org.hamcrest.Matchers.is;
3034
import static org.junit.Assert.assertThat;
3135

3236
public class ModifPropertiesTest {
3337

38+
static final String PROPERTIES_PATH = "org/silverpeas/converter/openoffice.properties";
39+
static final String EXPECTED_PROPERTIES_PATH =
40+
"org/silverpeas/converter/expected_openoffice.properties";
41+
static final String OPENOFFICE_HOST_KEY = "openoffice.host";
42+
static final String OPENOFFICE_PORT_KEY = "openoffice.port";
43+
static final String ADDED_ENTRY_KEY = "added.entry";
44+
3445
public ModifPropertiesTest() {
3546
}
3647

3748
/**
38-
* Test of executeModification method, of class ModifProperties.
39-
* Test for adding a new property, keeping the propertyies order and the comments.
49+
* Test of executeModification method, of class ModifProperties. Test for adding a new property,
50+
* keeping the properties order and the comments.
4051
*/
4152
@Test
4253
public void testExecuteModification() throws Exception {
43-
String path = this.getClass().getClassLoader().getResource(
44-
"org/silverpeas/converter/openoffice.properties").getPath();
54+
String path = this.getClass().getClassLoader().getResource(PROPERTIES_PATH).getPath();
4555
ModifProperties instance = new ModifProperties(path);
46-
instance.addModification(new ElementModif("openoffice.host", "localhost"));
47-
instance.addModification(new ElementModif("added.entry", "new entry"));
56+
instance.addModification(new ElementModif(OPENOFFICE_HOST_KEY, "localhost"));
57+
instance.addModification(new ElementModif(ADDED_ENTRY_KEY, "new entry"));
4858
instance.executeModification();
49-
String result = FileUtils.readFileToString(new File(path));
50-
String expectedResult = FileUtils.readFileToString(new File(this.getClass().getClassLoader().
51-
getResource("org/silverpeas/converter/expected_openoffice.properties").getPath()));
59+
60+
String expectedPath = this.getClass().getClassLoader().getResource(EXPECTED_PROPERTIES_PATH).
61+
getPath();
62+
List<String> result = FileUtils.readLines(new File(path), "UTF-8");
63+
List<String> expectedResult = FileUtils.readLines(new File(expectedPath), "UTF-8");
5264
assertThat(result, is(expectedResult));
5365

5466
}
67+
68+
private Properties loadProperties(String path) throws IOException {
69+
Properties props = new Properties();
70+
props.load(getClass().getClassLoader().getResourceAsStream(path));
71+
return props;
72+
}
5573
}

0 commit comments

Comments
 (0)