@@ -74,9 +74,10 @@ public void testInvalidCommandLine() throws ParseException {
7474 " -c,--config <arg> Defines the configuration directory. Defaults to './config'." + ls +
7575 " -d,--dryrun Defines 'dryrun' status, i.e. just test the replacement and report problems." + ls +
7676 " -h,--help Prints the tool usage." + ls +
77- " -p,--path <arg> Observation path segment to be replaced." + ls +
77+ " -p,--path <arg> Observation path segment to be replaced or truncated ." + ls +
7878 " -r,--replace <arg> Observation path segment replacement." + ls +
79- " -s,--segments <arg> Number of segments to consider for paths missing the search expression (default: 4)" + ls , err .toString ());
79+ " -s,--segments <arg> Number of segments to consider for paths missing the search expression (default: 4)" + ls +
80+ " -t,--truncate Command to truncate path segment." + ls , err .toString ());
8081 } finally {
8182 System .setOut (_out );
8283 System .setErr (_err );
@@ -141,12 +142,57 @@ public void testCorrectPaths_Postgres_alterSomePaths() throws IOException, Parse
141142 }
142143
143144 @ Test
144- public void testCorrectPaths_H2_alterSomePaths () throws IOException , ParseException , SQLException {
145+ public void testTruncatePaths_MongoDb_alterSomePaths () throws IOException , ParseException , SQLException {
146+ TestUtil .writeDatabaseProperties_MongoDb (configDir );
147+ TestUtil .writeSystemConfig (configDir );
148+ final BasicDataSource dataSource = TestUtil .getDataSource_MongoDb ();
149+
150+ runTest_truncatePath (dataSource );
151+ }
152+
153+ @ Test
154+ public void testTruncatePaths_Postgres_alterSomePaths () throws IOException , ParseException , SQLException {
155+ TestUtil .writeDatabaseProperties_Postgres (configDir );
156+ TestUtil .writeSystemConfig (configDir );
157+ final BasicDataSource dataSource = TestUtil .getDataSource_Postgres ();
158+
159+ runTest_truncatePath (dataSource );
160+ }
161+
162+ @ Test
163+ public void testTruncatePaths_H2_alterSomePaths () throws IOException , ParseException , SQLException {
145164 TestUtil .writeDatabaseProperties_H2 (configDir );
146165 TestUtil .writeSystemConfig (configDir );
147166 final BasicDataSource dataSource = TestUtil .getDatasource_H2 ();
148167
149- runTest_alterSomePaths (dataSource );
168+ runTest_truncatePath (dataSource );
169+ }
170+
171+ @ Test
172+ public void testTruncatePaths_MongoDb_innerSegment () throws IOException , ParseException , SQLException {
173+ TestUtil .writeDatabaseProperties_MongoDb (configDir );
174+ TestUtil .writeSystemConfig (configDir );
175+ final BasicDataSource dataSource = TestUtil .getDataSource_MongoDb ();
176+
177+ runTest_truncatePath_innerSegment (dataSource );
178+ }
179+
180+ @ Test
181+ public void testTruncatePaths_Postgres_innerSegment () throws IOException , ParseException , SQLException {
182+ TestUtil .writeDatabaseProperties_Postgres (configDir );
183+ TestUtil .writeSystemConfig (configDir );
184+ final BasicDataSource dataSource = TestUtil .getDataSource_Postgres ();
185+
186+ runTest_truncatePath_innerSegment (dataSource );
187+ }
188+
189+ @ Test
190+ public void testTruncatePaths_H2_innerSegment () throws IOException , ParseException , SQLException {
191+ TestUtil .writeDatabaseProperties_H2 (configDir );
192+ TestUtil .writeSystemConfig (configDir );
193+ final BasicDataSource dataSource = TestUtil .getDatasource_H2 ();
194+
195+ runTest_truncatePath_innerSegment (dataSource );
150196 }
151197
152198 @ Test
@@ -268,6 +314,66 @@ private void runTest_alterSomePaths(BasicDataSource dataSource) throws SQLExcept
268314 }
269315 }
270316
317+ private void runTest_truncatePath (BasicDataSource dataSource ) throws SQLException , ParseException {
318+ final Storage storage = initializeStorage (dataSource );
319+
320+ for (int i = 0 ; i < 12 ; i ++) {
321+ final SatelliteObservation observation = TestData .createSatelliteObservation (geometryFactory );
322+ final String obsPath = TestUtil .assembleFileSystemPath (new String []{"archive" , "correct" , "the_file_number_" + i }, true );
323+ observation .setDataFilePath (obsPath );
324+ storage .insert (observation );
325+ }
326+
327+ try {
328+ final String cutPath = TestUtil .assembleFileSystemPath (new String []{"archive" , "correct" }, true );
329+
330+ final String [] args = new String []{"-c" , configDir .getAbsolutePath (),
331+ "-p" , cutPath ,
332+ "-t" };
333+
334+ DbMaintenanceToolMain .main (args );
335+
336+ final List <SatelliteObservation > observations = storage .get ();
337+ assertEquals (12 , observations .size ());
338+ for (SatelliteObservation satelliteObservation : observations ) {
339+ assertFalse (satelliteObservation .getDataFilePath ().toString ().contains (cutPath ));
340+ }
341+ } finally {
342+ storage .clear ();
343+ storage .close ();
344+ }
345+ }
346+
347+ private void runTest_truncatePath_innerSegment (BasicDataSource dataSource ) throws SQLException , ParseException {
348+ final Storage storage = initializeStorage (dataSource );
349+
350+ for (int i = 0 ; i < 12 ; i ++) {
351+ final SatelliteObservation observation = TestData .createSatelliteObservation (geometryFactory );
352+ final String obsPath = TestUtil .assembleFileSystemPath (new String []{"archive" , "correct" , "the_file_number_" + i }, true );
353+ observation .setDataFilePath (obsPath );
354+ storage .insert (observation );
355+ }
356+
357+ try {
358+ final String cutPath = TestUtil .assembleFileSystemPath (new String []{"correct" }, true );
359+
360+ final String [] args = new String []{"-c" , configDir .getAbsolutePath (),
361+ "-p" , cutPath ,
362+ "-t" };
363+
364+ DbMaintenanceToolMain .main (args );
365+
366+ final List <SatelliteObservation > observations = storage .get ();
367+ assertEquals (12 , observations .size ());
368+ for (SatelliteObservation satelliteObservation : observations ) {
369+ assertFalse (satelliteObservation .getDataFilePath ().toString ().contains (cutPath ));
370+ }
371+ } finally {
372+ storage .clear ();
373+ storage .close ();
374+ }
375+ }
376+
271377 private void runTest_dryRun_allOk (BasicDataSource dataSource ) throws SQLException , ParseException {
272378 final String sep = System .lineSeparator ();
273379 final Storage storage = initializeStorage (dataSource );
0 commit comments