context)
42 | {
43 | LOG.info("Update {} executed", this.getTargetVersion());
44 | context.getStorageManager().setRoot("Hello World! @ " + new Date() + " Update 1.1");
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/micro-migration/src/main/java/software/xdev/micromigration/eclipsestore/MigrationEmbeddedStorage.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.eclipsestore;
17 |
18 | import java.nio.file.Path;
19 | import java.util.Objects;
20 |
21 | import org.eclipse.store.afs.nio.types.NioFileSystem;
22 | import org.eclipse.store.storage.embedded.types.EmbeddedStorage;
23 | import org.eclipse.store.storage.embedded.types.EmbeddedStorageFoundation;
24 | import org.eclipse.store.storage.embedded.types.EmbeddedStorageManager;
25 | import org.eclipse.store.storage.types.Storage;
26 | import org.eclipse.store.storage.types.StorageConfiguration;
27 |
28 | import software.xdev.micromigration.migrater.MicroMigrater;
29 |
30 |
31 | /**
32 | * Provides static utility calls to create the {@link MigrationEmbeddedStorageManager} for updateable datastores.
33 | * Basically a wrapper for the utility class {@link EmbeddedStorage}.
34 | */
35 | public final class MigrationEmbeddedStorage
36 | {
37 | /**
38 | * Creates a {@link MigrationEmbeddedStorageManager} with the given {@link MicroMigrater}. Uses the
39 | * {@link EmbeddedStorageFoundation#New()} configuration for the actual {@link EmbeddedStorageManager}.
40 | * Warning "resource" is suppressed because it is used and closed in the
41 | * {@link MigrationEmbeddedStorageManager}.
42 | *
43 | * @param migrater which is used as source for the migration scripts
44 | * @return the created storage manager with the given migrater
45 | */
46 | @SuppressWarnings("java:S2095")
47 | public static MigrationEmbeddedStorageManager start(final MicroMigrater migrater)
48 | {
49 | Objects.requireNonNull(migrater);
50 | return new MigrationEmbeddedStorageManager(
51 | createStorageManager(),
52 | migrater
53 | ).start();
54 | }
55 |
56 | /**
57 | * Creates a {@link MigrationEmbeddedStorageManager} with the given {@link MicroMigrater}. Uses the
58 | * {@link EmbeddedStorageFoundation#New()} configuration for the actual {@link EmbeddedStorageManager}.
59 | *
Warning "resource" is suppressed because it is used and closed in the
60 | * {@link MigrationEmbeddedStorageManager}.
61 | *
62 | * @param storageDirectory is used as the base directory for the datastore
63 | * @param migrater which is used as source for the migration scripts
64 | * @return the created storage manager with the given migrater
65 | */
66 | @SuppressWarnings("java:S2095")
67 | public static MigrationEmbeddedStorageManager start(
68 | final Path storageDirectory,
69 | final MicroMigrater migrater
70 | )
71 | {
72 | Objects.requireNonNull(migrater);
73 | Objects.requireNonNull(storageDirectory);
74 |
75 | return new MigrationEmbeddedStorageManager(
76 | createStorageManager(storageDirectory),
77 | migrater
78 | ).start();
79 | }
80 |
81 | private static EmbeddedStorageManager createStorageManager(final Path storageDirectory)
82 | {
83 | final NioFileSystem fileSystem = NioFileSystem.New();
84 | return EmbeddedStorageFoundation.New()
85 | .setConfiguration(
86 | StorageConfiguration.Builder()
87 | .setStorageFileProvider(
88 | Storage.FileProviderBuilder(fileSystem)
89 | .setDirectory(fileSystem.ensureDirectoryPath(storageDirectory.toAbsolutePath().toString()))
90 | .createFileProvider()
91 | )
92 | .createConfiguration()
93 | )
94 | .createEmbeddedStorageManager();
95 | }
96 |
97 | private static EmbeddedStorageManager createStorageManager()
98 | {
99 | return EmbeddedStorageFoundation.New()
100 | .setConfiguration(
101 | StorageConfiguration.Builder().createConfiguration()
102 | )
103 | .createEmbeddedStorageManager();
104 | }
105 |
106 | private MigrationEmbeddedStorage()
107 | {
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/micro-migration/src/main/java/software/xdev/micromigration/eclipsestore/MigrationEmbeddedStorageManager.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.eclipsestore;
17 |
18 | import org.eclipse.store.storage.embedded.types.EmbeddedStorageManager;
19 |
20 | import software.xdev.micromigration.migrater.MicroMigrater;
21 | import software.xdev.micromigration.version.Versioned;
22 | import software.xdev.micromigration.versionagnostic.VersionAgnosticMigrationEmbeddedStorageManager;
23 |
24 |
25 | /**
26 | * Specific implementation of the {@link VersionAgnosticMigrationEmbeddedStorageManager} for one specific version.
27 | * @see VersionAgnosticMigrationEmbeddedStorageManager
28 | */
29 | public class MigrationEmbeddedStorageManager
30 | extends VersionAgnosticMigrationEmbeddedStorageManager
31 | {
32 | /**
33 | * @param nativeManager which will be used as the underlying storage manager. Almost all methods are only rerouted
34 | * to this native manager. Only {@link #start()}, {@link #root()} and {@link #setRoot(Object)}
35 | * are intercepted and a {@link Versioned} is placed between the requests.
36 | * @param migrater which is used as source for the migration scripts
37 | */
38 | public MigrationEmbeddedStorageManager(
39 | final EmbeddedStorageManager nativeManager,
40 | final MicroMigrater migrater
41 | )
42 | {
43 | super(new TunnelingEmbeddedStorageManager(nativeManager), migrater);
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/micro-migration/src/main/java/software/xdev/micromigration/eclipsestore/MigrationManager.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.eclipsestore;
17 |
18 | import java.util.function.Consumer;
19 | import java.util.function.Supplier;
20 |
21 | import org.eclipse.store.storage.embedded.types.EmbeddedStorageManager;
22 |
23 | import software.xdev.micromigration.migrater.MicroMigrater;
24 | import software.xdev.micromigration.scripts.VersionAgnosticMigrationScript;
25 | import software.xdev.micromigration.version.MigrationVersion;
26 | import software.xdev.micromigration.version.Versioned;
27 | import software.xdev.micromigration.versionagnostic.VersionAgnosticMigrationManager;
28 |
29 |
30 | /**
31 | * Specific implementation of the {@link VersionAgnosticMigrationManager}.
32 | *
33 | * @see VersionAgnosticMigrationManager
34 | */
35 | public class MigrationManager extends VersionAgnosticMigrationManager
36 | {
37 | /**
38 | * Much more complicated constructor than
39 | * {@link MigrationManager#MigrationManager(Versioned, MicroMigrater, EmbeddedStorageManager)}
40 | *
41 | * @param currentVersionGetter which supplies the current version of the object to update.
42 | * @param currentVersionSetter which sets the new version of the object in some membervariable. This Consumer is
43 | * not
44 | * supposed to store the version, but only save it in some membervariable to be stored
45 | * after.
46 | * @param currentVersionStorer which is supposed to store the new version of the object somewhere in the datastore.
47 | * @param migrater does the actual migration with the given {@link VersionAgnosticMigrationScript}
48 | * @param storageManager for the {@link VersionAgnosticMigrationScript}s to use. Is not used for the storing
49 | * of the new version.
50 | */
51 | public MigrationManager(
52 | final Supplier currentVersionGetter,
53 | final Consumer currentVersionSetter,
54 | final Consumer currentVersionStorer,
55 | final MicroMigrater migrater,
56 | final EmbeddedStorageManager storageManager
57 | )
58 | {
59 | super(
60 | currentVersionGetter,
61 | currentVersionSetter,
62 | currentVersionStorer,
63 | migrater,
64 | new MigrationEmbeddedStorageManager(storageManager, migrater));
65 | }
66 |
67 | /**
68 | * Simple Constructor.
69 | *
70 | * @param versionedObject which provides getter and setter for the current version. This object will be stored
71 | * after
72 | * the {@link VersionAgnosticMigrationScript}s are executed.
73 | * @param migrater does the actual migration with the given {@link VersionAgnosticMigrationScript}
74 | * @param storageManager for the {@link VersionAgnosticMigrationScript}s to use. Is not used for the storing of
75 | * the
76 | * new version.
77 | */
78 | public MigrationManager(
79 | final Versioned versionedObject,
80 | final MicroMigrater migrater,
81 | final EmbeddedStorageManager storageManager
82 | )
83 | {
84 | super(versionedObject, migrater, new MigrationEmbeddedStorageManager(storageManager, migrater));
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/micro-migration/src/main/java/software/xdev/micromigration/eclipsestore/MigrationScript.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.eclipsestore;
17 |
18 | import software.xdev.micromigration.scripts.Context;
19 | import software.xdev.micromigration.scripts.VersionAgnosticMigrationScript;
20 |
21 |
22 | /**
23 | * Interface for scripts to migrate / update datastores.
24 | *
25 | * One script is supposed to bring a datastore from a lower version to the target version. After the
26 | * {@link VersionAgnosticMigrationScript#migrate(Context)} method is called, the target version is reached.
27 | *
28 | * This is a shorthand for {@link VersionAgnosticMigrationScript}
29 | */
30 | public interface MigrationScript extends VersionAgnosticMigrationScript
31 | {
32 | }
33 |
--------------------------------------------------------------------------------
/micro-migration/src/main/java/software/xdev/micromigration/migrater/AbstractMigrater.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.migrater;
17 |
18 | import java.time.Clock;
19 | import java.time.LocalDateTime;
20 | import java.util.ArrayList;
21 | import java.util.List;
22 | import java.util.Objects;
23 | import java.util.TreeSet;
24 | import java.util.function.Consumer;
25 |
26 | import software.xdev.micromigration.notification.ScriptExecutionNotificationWithScriptReference;
27 | import software.xdev.micromigration.scripts.Context;
28 | import software.xdev.micromigration.scripts.VersionAgnosticMigrationScript;
29 | import software.xdev.micromigration.version.MigrationVersion;
30 | import software.xdev.micromigration.versionagnostic.VersionAgnosticMigrationEmbeddedStorageManager;
31 |
32 |
33 | /**
34 | * Provides the basic functionality to apply {@link VersionAgnosticMigrationScript}s to a datastore.
35 | */
36 | public abstract class AbstractMigrater implements MicroMigrater
37 | {
38 | private final List> notificationConsumers =
39 | new ArrayList<>();
40 | private Clock clock = Clock.systemDefaultZone();
41 |
42 | @Override
43 | public void registerNotificationConsumer(
44 | final Consumer notificationConsumer)
45 | {
46 | this.notificationConsumers.add(notificationConsumer);
47 | }
48 |
49 | @Override
50 | public > MigrationVersion migrateToNewest(
51 | final MigrationVersion fromVersion,
52 | final E storageManager,
53 | final Object root
54 | )
55 | {
56 | Objects.requireNonNull(storageManager);
57 |
58 | final TreeSet extends VersionAgnosticMigrationScript, ?>> sortedScripts = this.getSortedScripts();
59 | if(!sortedScripts.isEmpty())
60 | {
61 | return this.migrateToVersion(
62 | fromVersion,
63 | this.getSortedScripts().last().getTargetVersion(),
64 | storageManager,
65 | root
66 | );
67 | }
68 | return fromVersion;
69 | }
70 |
71 | @SuppressWarnings("unchecked")
72 | @Override
73 | public > MigrationVersion migrateToVersion(
74 | final MigrationVersion fromVersion,
75 | final MigrationVersion targetVersion,
76 | final E storageManager,
77 | final Object objectToMigrate
78 | )
79 | {
80 | Objects.requireNonNull(targetVersion);
81 | Objects.requireNonNull(storageManager);
82 |
83 | MigrationVersion updateVersionWhichWasExecuted = fromVersion;
84 | for(final VersionAgnosticMigrationScript, ?> script : this.getSortedScripts())
85 | {
86 | final VersionAgnosticMigrationScript, E> castedScript = (VersionAgnosticMigrationScript, E>)script;
87 | if((fromVersion == null || MigrationVersion.COMPARATOR.compare(fromVersion, script.getTargetVersion()) < 0)
88 | && MigrationVersion.COMPARATOR.compare(script.getTargetVersion(), targetVersion) <= 0)
89 | {
90 | LocalDateTime startDate = null;
91 | final MigrationVersion versionBeforeUpdate = updateVersionWhichWasExecuted;
92 | if(!this.notificationConsumers.isEmpty())
93 | {
94 | startDate = LocalDateTime.now(this.clock);
95 | }
96 | updateVersionWhichWasExecuted =
97 | this.migrateWithScript(castedScript, storageManager, objectToMigrate);
98 | if(!this.notificationConsumers.isEmpty())
99 | {
100 | final ScriptExecutionNotificationWithScriptReference scriptNotification =
101 | new ScriptExecutionNotificationWithScriptReference(
102 | script,
103 | versionBeforeUpdate,
104 | updateVersionWhichWasExecuted,
105 | startDate,
106 | LocalDateTime.now(this.clock)
107 | );
108 | this.notificationConsumers.forEach(consumer -> consumer.accept(scriptNotification));
109 | }
110 | }
111 | }
112 | return updateVersionWhichWasExecuted;
113 | }
114 |
115 | @SuppressWarnings("unchecked")
116 | private > MigrationVersion migrateWithScript(
117 | final VersionAgnosticMigrationScript script,
118 | final E storageManager,
119 | final Object objectToMigrate
120 | )
121 | {
122 | final T castedObjectToMigrate = (T)objectToMigrate;
123 | script.migrate(new Context<>(castedObjectToMigrate, storageManager));
124 | return script.getTargetVersion();
125 | }
126 |
127 | /**
128 | * Checks if the given {@link VersionAgnosticMigrationScript} is not already registered in the
129 | * {@link #getSortedScripts()}.
130 | *
131 | * @param scriptToCheck It's target version is checked, if it is not already registered.
132 | * @throws VersionAlreadyRegisteredException if script is already registered.
133 | */
134 | protected void checkIfVersionIsAlreadyRegistered(final VersionAgnosticMigrationScript, ?> scriptToCheck)
135 | {
136 | // Check if same version is not already registered
137 | for(final VersionAgnosticMigrationScript, ?> alreadyRegisteredScript : this.getSortedScripts())
138 | {
139 | if(MigrationVersion.COMPARATOR.compare(
140 | alreadyRegisteredScript.getTargetVersion(),
141 | scriptToCheck.getTargetVersion()) == 0)
142 | {
143 | // Two scripts with the same version are not allowed to get registered.
144 | throw new VersionAlreadyRegisteredException(
145 | alreadyRegisteredScript.getTargetVersion(),
146 | alreadyRegisteredScript,
147 | scriptToCheck
148 | );
149 | }
150 | }
151 | }
152 |
153 | /**
154 | * Change used clock for notifications from {@link #registerNotificationConsumer(Consumer)}.
155 | *
156 | * @param clock is used when a
157 | * {@link software.xdev.micromigration.notification.ScriptExecutionNotificationWithoutScriptReference}
158 | * is created
159 | * @return self
160 | */
161 | public AbstractMigrater withClock(final Clock clock)
162 | {
163 | this.clock = clock;
164 | return this;
165 | }
166 | }
167 |
--------------------------------------------------------------------------------
/micro-migration/src/main/java/software/xdev/micromigration/migrater/ExplicitMigrater.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.migrater;
17 |
18 | import java.util.TreeSet;
19 |
20 | import software.xdev.micromigration.scripts.VersionAgnosticMigrationScript;
21 |
22 |
23 | /**
24 | * Contains all the available scripts to migrate the datastore to a certain version.
25 | *
26 | * This class needs explicit scripts which are then included in the migration process.
27 | */
28 | public class ExplicitMigrater extends AbstractMigrater
29 | {
30 | private final TreeSet> sortedScripts = new TreeSet<>(
31 | VersionAgnosticMigrationScript.COMPARATOR);
32 |
33 | /**
34 | * @param scripts are all the scripts that are executed, if the current version is lower than this of the
35 | * script
36 | * Versions of the scripts must be unique. That means that no version is allowed multiple times in
37 | * the migrater.
38 | * @throws VersionAlreadyRegisteredException if two scripts have the same version
39 | */
40 | @SuppressWarnings("PMD.UseArraysAsList")
41 | public ExplicitMigrater(final VersionAgnosticMigrationScript, ?>... scripts)
42 | {
43 | for(final VersionAgnosticMigrationScript, ?> script : scripts)
44 | {
45 | this.checkIfVersionIsAlreadyRegistered(script);
46 | this.sortedScripts.add(script);
47 | }
48 | }
49 |
50 | @Override
51 | public TreeSet> getSortedScripts()
52 | {
53 | return this.sortedScripts;
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/micro-migration/src/main/java/software/xdev/micromigration/migrater/MicroMigrater.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.migrater;
17 |
18 | import java.util.TreeSet;
19 | import java.util.function.Consumer;
20 |
21 | import software.xdev.micromigration.notification.ScriptExecutionNotificationWithScriptReference;
22 | import software.xdev.micromigration.scripts.Context;
23 | import software.xdev.micromigration.scripts.VersionAgnosticMigrationScript;
24 | import software.xdev.micromigration.version.MigrationVersion;
25 | import software.xdev.micromigration.versionagnostic.VersionAgnosticMigrationEmbeddedStorageManager;
26 |
27 |
28 | /**
29 | * Executes all the available scripts to migrate the datastore to a certain version.
30 | */
31 | public interface MicroMigrater
32 | {
33 | /**
34 | * @return all the contained {@link VersionAgnosticMigrationScript}s, sorted by their {@link MigrationVersion}
35 | * ascending.
36 | */
37 | @SuppressWarnings("java:S1452")
38 | TreeSet extends VersionAgnosticMigrationScript, ?>> getSortedScripts();
39 |
40 | /**
41 | * Executes all the scripts that are available to the migrater. Only scripts with a higher target version than the
42 | * given fromVersion are executed. Scripts are executed one after another from the lowest to the highest
43 | * version.
44 | *
45 | * Example:
46 | * Current version is 1.0.0 Scripts for v1.1.0, v2.0.0 and v1.2.1 are available Scripts are chain executed
47 | * like v1.1.0 then v1.2.1 then v2.0.0
48 | *
49 | * @param fromVersion is the current version of the datastore. Scripts for lower versions then the fromVersion
50 | * are not executed.
51 | * @param storageManager is relayed to the scripts {@link VersionAgnosticMigrationScript#migrate(Context)} method.
52 | * This way the script can call
53 | * {@link VersionAgnosticMigrationEmbeddedStorageManager#store(Object)} or another method on
54 | * the storage manager.
55 | * @param root is relayed to the scripts {@link VersionAgnosticMigrationScript#migrate(Context)} method.
56 | * This way the script can change something within the root object.
57 | * @param the {@link VersionAgnosticMigrationEmbeddedStorageManager} which contains the migrating
58 | * object
59 | * @return the target version of the last executed script
60 | */
61 | > MigrationVersion migrateToNewest(
62 | MigrationVersion fromVersion,
63 | E storageManager,
64 | Object root
65 | );
66 |
67 | /**
68 | * Executes all the scripts that are available to the migrater until the given targetVersion is reached. Only
69 | * scripts with a higher target version than the given fromVersion are executed. Scripts are executed one after
70 | * another from the lowest to the highest version.
71 | *
72 | * Example:
73 | * Current version is 1.0.0 Scripts for v1.1.0, v2.0.0 and v1.2.1 are available Scripts are chain executed
74 | * like v1.1.0 then v1.2.1 then v2.0.0
75 | *
76 | * @param fromVersion is the current version of the datastore. Scripts for lower versions then the fromVersion
77 | * are not executed.
78 | * @param targetVersion is the highest allowed script version. Scripts which have a higher version won't be
79 | * exectued.
80 | * @param storageManager is relayed to the scripts {@link VersionAgnosticMigrationScript#migrate(Context)} method.
81 | * This way the script can call EmbeddedStorageManager#store or another method on the
82 | * storage
83 | * manager.
84 | * @param objectToMigrate is relayed to the scripts {@link VersionAgnosticMigrationScript#migrate(Context)} method.
85 | * This way the script can change something within the object to migrate.
86 | * @param the {@link VersionAgnosticMigrationEmbeddedStorageManager} which contains the migrating
87 | * object
88 | * @return the target version of the last executed script
89 | */
90 | > MigrationVersion migrateToVersion(
91 | MigrationVersion fromVersion,
92 | MigrationVersion targetVersion,
93 | E storageManager,
94 | Object objectToMigrate
95 | );
96 |
97 | /**
98 | * Registers a callback to take action when a script is executed.
99 | *
100 | * @param notificationConsumer is executed when a script is used from this migrater.
101 | */
102 | void registerNotificationConsumer(
103 | Consumer notificationConsumer);
104 | }
105 |
--------------------------------------------------------------------------------
/micro-migration/src/main/java/software/xdev/micromigration/migrater/VersionAlreadyRegisteredException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.migrater;
17 |
18 | import java.util.Objects;
19 |
20 | import software.xdev.micromigration.scripts.VersionAgnosticMigrationScript;
21 | import software.xdev.micromigration.version.MigrationVersion;
22 |
23 |
24 | /**
25 | * Exception that should be used if two scripts with the same version exist.
26 | */
27 | @SuppressWarnings({"java:S1948", "java:S1452"})
28 | public class VersionAlreadyRegisteredException extends RuntimeException
29 | {
30 | /**
31 | * The already registered script with the same version
32 | */
33 | private final MigrationVersion alreadyRegisteredVersion;
34 | /**
35 | * The version of the already registered script
36 | */
37 | private final VersionAgnosticMigrationScript, ?> alreadyRegisteredScript;
38 | /**
39 | * The script with the same version as {@link #alreadyRegisteredScript}, which should be registered as well
40 | */
41 | private final VersionAgnosticMigrationScript, ?> newScriptToRegister;
42 |
43 | /**
44 | * @param alreadyRegisteredVersion The version of the already registered script
45 | * @param alreadyRegisteredScript The already registered script with the same version
46 | * @param newScriptToRegister The script with the same version as alreadyRegisteredScript, which should be
47 | * registered as well
48 | */
49 | public VersionAlreadyRegisteredException(
50 | final MigrationVersion alreadyRegisteredVersion,
51 | final VersionAgnosticMigrationScript, ?> alreadyRegisteredScript,
52 | final VersionAgnosticMigrationScript, ?> newScriptToRegister
53 | )
54 | {
55 | super("Version " + alreadyRegisteredVersion.toString()
56 | + " is already registered. Versions must be unique within the migrater.");
57 | this.alreadyRegisteredVersion = Objects.requireNonNull(alreadyRegisteredVersion);
58 | this.alreadyRegisteredScript = Objects.requireNonNull(alreadyRegisteredScript);
59 | this.newScriptToRegister = Objects.requireNonNull(newScriptToRegister);
60 | }
61 |
62 | /**
63 | * @return the version of the already registered script
64 | */
65 | public MigrationVersion getAlreadyRegisteredVersion()
66 | {
67 | return this.alreadyRegisteredVersion;
68 | }
69 |
70 | /**
71 | * @return the already registered script with the same version
72 | */
73 | public VersionAgnosticMigrationScript, ?> getAlreadyRegisteredScript()
74 | {
75 | return this.alreadyRegisteredScript;
76 | }
77 |
78 | /**
79 | * @return the script with the same version as {@link #getAlreadyRegisteredScript()}, which should be registered as
80 | * well
81 | */
82 | public VersionAgnosticMigrationScript, ?> getNewScriptToRegister()
83 | {
84 | return this.newScriptToRegister;
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/micro-migration/src/main/java/software/xdev/micromigration/migrater/reflection/ReflectiveMigrater.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.migrater.reflection;
17 |
18 | import java.io.File;
19 | import java.io.IOException;
20 | import java.io.UncheckedIOException;
21 | import java.lang.reflect.InvocationTargetException;
22 | import java.lang.reflect.Modifier;
23 | import java.net.URL;
24 | import java.util.ArrayList;
25 | import java.util.Enumeration;
26 | import java.util.List;
27 | import java.util.TreeSet;
28 |
29 | import software.xdev.micromigration.migrater.AbstractMigrater;
30 | import software.xdev.micromigration.scripts.VersionAgnosticMigrationScript;
31 |
32 |
33 | /**
34 | * Contains all the available scripts to migrate the datastore to a certain version.
35 | *
36 | * Searches all implementation of {@link VersionAgnosticMigrationScript} in the specified package and it's the sub
37 | * packages.
38 | *
39 | *
40 | * Reflection source: https://stackoverflow.com/a/520344
41 | *
42 | * @author AB
43 | */
44 | public class ReflectiveMigrater extends AbstractMigrater
45 | {
46 | private static final String CLASS_EXTENSION = ".class";
47 |
48 | private final TreeSet> sortedScripts = new TreeSet<>(
49 | VersionAgnosticMigrationScript.COMPARATOR);
50 |
51 | /**
52 | * @param packagePath defines the package in which {@link VersionAgnosticMigrationScript}s will be searched. Also
53 | * searches through all sub packages of packagePath
54 | * @throws ScriptInstantiationException if a class in the given package could not be instantiated
55 | */
56 | @SuppressWarnings("unchecked")
57 | public ReflectiveMigrater(final String packagePath)
58 | {
59 | getClasses(packagePath)
60 | .stream()
61 | .filter(c -> !Modifier.isAbstract(c.getModifiers()))
62 | .filter(VersionAgnosticMigrationScript.class::isAssignableFrom)
63 | .map(c -> (Class extends VersionAgnosticMigrationScript>)c)
64 | .map(this::instantiateClass)
65 | .forEach(instantiateScript -> {
66 | this.checkIfVersionIsAlreadyRegistered(instantiateScript);
67 | this.sortedScripts.add(instantiateScript);
68 | });
69 | }
70 |
71 | @SuppressWarnings("rawtypes")
72 | private VersionAgnosticMigrationScript, ?> instantiateClass(
73 | final Class extends VersionAgnosticMigrationScript> scriptClass)
74 | {
75 | try
76 | {
77 | return scriptClass.getDeclaredConstructor().newInstance();
78 | }
79 | catch(final InstantiationException
80 | | IllegalAccessException
81 | | IllegalArgumentException
82 | | InvocationTargetException
83 | | NoSuchMethodException
84 | | SecurityException e
85 | )
86 | {
87 | throw new ScriptInstantiationException("Could not instantiate class " + scriptClass.getName(), e);
88 | }
89 | }
90 |
91 | /**
92 | * Scans all classes accessible from the context class loader which belong to the given package and subpackages.
93 | *
94 | * @param packageName The base package
95 | * @return The classes
96 | */
97 | private static List> getClasses(final String packageName)
98 | {
99 | try
100 | {
101 | final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
102 | assert classLoader != null;
103 | final String path = packageName.replace('.', '/');
104 | final Enumeration resources = classLoader.getResources(path);
105 | final List dirs = new ArrayList<>();
106 | while(resources.hasMoreElements())
107 | {
108 | final URL resource = resources.nextElement();
109 | dirs.add(new File(resource.getFile()));
110 | }
111 | final ArrayList> classes = new ArrayList<>();
112 | for(final File directory : dirs)
113 | {
114 | classes.addAll(findClasses(directory, packageName));
115 | }
116 | return classes;
117 | }
118 | catch(final ClassNotFoundException e)
119 | {
120 | throw new IllegalStateException("Unable to find class", e);
121 | }
122 | catch(final IOException ioe)
123 | {
124 | throw new UncheckedIOException(ioe);
125 | }
126 | }
127 |
128 | /**
129 | * Recursive method used to find all classes in a given directory and subdirs.
130 | *
131 | * @param directory The base directory
132 | * @param packageName The package name for classes found inside the base directory
133 | * @return The classes
134 | */
135 | private static List> findClasses(final File directory, final String packageName)
136 | throws ClassNotFoundException
137 | {
138 | if(!directory.exists())
139 | {
140 | return new ArrayList<>();
141 | }
142 |
143 | final List> classes = new ArrayList<>();
144 | for(final File file : directory.listFiles())
145 | {
146 | if(file.isDirectory())
147 | {
148 | assert !file.getName().contains(".");
149 | classes.addAll(findClasses(file, packageName + "." + file.getName()));
150 | }
151 | else if(file.getName().endsWith(CLASS_EXTENSION))
152 | {
153 | classes.add(Class.forName(
154 | packageName + '.' + file.getName()
155 | .substring(0, file.getName().length() - CLASS_EXTENSION.length())));
156 | }
157 | }
158 | return classes;
159 | }
160 |
161 | @Override
162 | public TreeSet> getSortedScripts()
163 | {
164 | return this.sortedScripts;
165 | }
166 | }
167 |
--------------------------------------------------------------------------------
/micro-migration/src/main/java/software/xdev/micromigration/migrater/reflection/ScriptInstantiationException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.migrater.reflection;
17 |
18 | /**
19 | * Holds information about exceptions if a script class can not be instantiated.
20 | */
21 | public class ScriptInstantiationException extends RuntimeException
22 | {
23 | /**
24 | * @param message for the exception
25 | * @param cause of the exception
26 | */
27 | public ScriptInstantiationException(final String message, final Throwable cause)
28 | {
29 | super(message, cause);
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/micro-migration/src/main/java/software/xdev/micromigration/notification/AbstractScriptExecutionNotification.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.notification;
17 |
18 | import java.time.LocalDateTime;
19 |
20 | import software.xdev.micromigration.migrater.MicroMigrater;
21 | import software.xdev.micromigration.version.MigrationVersion;
22 |
23 |
24 | /**
25 | * Contains data about the execution of a script by a {@link MicroMigrater}.
26 | */
27 | public abstract class AbstractScriptExecutionNotification
28 | {
29 | private final MigrationVersion sourceVersion;
30 | private final MigrationVersion targetVersion;
31 | private final LocalDateTime startDate;
32 | private final LocalDateTime endDate;
33 |
34 | /**
35 | * @param sourceVersion original version of the object before executing the script
36 | * @param targetVersion version of the object after executing the script
37 | * @param startDate time when the script was started
38 | * @param endDate time when the script has finished
39 | */
40 | protected AbstractScriptExecutionNotification(
41 | final MigrationVersion sourceVersion,
42 | final MigrationVersion targetVersion,
43 | final LocalDateTime startDate,
44 | final LocalDateTime endDate
45 | )
46 | {
47 | super();
48 | this.sourceVersion = sourceVersion;
49 | this.targetVersion = targetVersion;
50 | this.startDate = startDate;
51 | this.endDate = endDate;
52 | }
53 |
54 | /**
55 | * @return the original version of the object before executing the script
56 | */
57 | public MigrationVersion getSourceVersion()
58 | {
59 | return this.sourceVersion;
60 | }
61 |
62 | /**
63 | * @return the version of the object after executing the script
64 | */
65 | public MigrationVersion getTargetVersion()
66 | {
67 | return this.targetVersion;
68 | }
69 |
70 | /**
71 | * @return the time when the script was started
72 | */
73 | public LocalDateTime getStartDate()
74 | {
75 | return this.startDate;
76 | }
77 |
78 | /**
79 | * @return time when the script has finished
80 | */
81 | public LocalDateTime getEndDate()
82 | {
83 | return this.endDate;
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/micro-migration/src/main/java/software/xdev/micromigration/notification/ScriptExecutionNotificationWithScriptReference.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.notification;
17 |
18 | import java.time.LocalDateTime;
19 |
20 | import software.xdev.micromigration.migrater.MicroMigrater;
21 | import software.xdev.micromigration.scripts.VersionAgnosticMigrationScript;
22 | import software.xdev.micromigration.version.MigrationVersion;
23 |
24 |
25 | /**
26 | * Contains data about the execution of a script by a {@link MicroMigrater}.
27 | */
28 | public class ScriptExecutionNotificationWithScriptReference extends AbstractScriptExecutionNotification
29 | {
30 | private final VersionAgnosticMigrationScript, ?> executedScript;
31 |
32 | /**
33 | * @param executedScript script that was executed
34 | * @param sourceVersion original version of the object before executing the script
35 | * @param targetVersion version of the object after executing the script
36 | * @param startDate time when the script was started
37 | * @param endDate time when the script has finished
38 | */
39 | public ScriptExecutionNotificationWithScriptReference(
40 | final VersionAgnosticMigrationScript, ?> executedScript,
41 | final MigrationVersion sourceVersion,
42 | final MigrationVersion targetVersion,
43 | final LocalDateTime startDate,
44 | final LocalDateTime endDate
45 | )
46 | {
47 | super(
48 | sourceVersion,
49 | targetVersion,
50 | startDate,
51 | endDate
52 | );
53 | this.executedScript = executedScript;
54 | }
55 |
56 | /**
57 | * @return the script that was executed
58 | */
59 | @SuppressWarnings("java:S1452")
60 | public VersionAgnosticMigrationScript, ?> getExecutedScript()
61 | {
62 | return this.executedScript;
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/micro-migration/src/main/java/software/xdev/micromigration/notification/ScriptExecutionNotificationWithoutScriptReference.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.notification;
17 |
18 | import software.xdev.micromigration.scripts.VersionAgnosticMigrationScript;
19 |
20 | /**
21 | * Same as {@link ScriptExecutionNotificationWithScriptReference} but instead of referencing
22 | * the {@link VersionAgnosticMigrationScript} directly, only the name of the script is
23 | * extracted through the class name.
24 | *
25 | * "Why?!" - If you want to persist say a history of your applied scripts in your database and
26 | * you reference your scripts directly, these classes are referenced in your datastore.
27 | * That shouldn't be a problem. Except when you refactor or delete these scripts.
28 | * Usually what's really important is the name of the script.
29 | */
30 | public class ScriptExecutionNotificationWithoutScriptReference extends AbstractScriptExecutionNotification
31 | {
32 | private final String executedScriptName;
33 |
34 | /**
35 | * @param originalNotification where the reference to the script is deleted and the class name is extracted.
36 | */
37 | public ScriptExecutionNotificationWithoutScriptReference(
38 | final ScriptExecutionNotificationWithScriptReference originalNotification)
39 | {
40 | super(
41 | originalNotification.getSourceVersion(),
42 | originalNotification.getTargetVersion(),
43 | originalNotification.getStartDate(),
44 | originalNotification.getEndDate()
45 | );
46 | this.executedScriptName = originalNotification.getExecutedScript().getClass().getSimpleName();
47 | }
48 |
49 | /**
50 | * @return the name of the script that was extracted.
51 | */
52 | public String getExecutedScriptName()
53 | {
54 | return this.executedScriptName;
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/micro-migration/src/main/java/software/xdev/micromigration/scripts/Context.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.scripts;
17 |
18 | /**
19 | * Container that holds necessary information for the execution of an {@link VersionAgnosticMigrationScript}
20 | */
21 | public class Context
22 | {
23 | private final T migratingObject;
24 | private final E storageManager;
25 |
26 | /**
27 | * @param migratingObject that must be migrated to a new version
28 | * @param storageManager where the migratingObject is stored
29 | */
30 | public Context(
31 | final T migratingObject,
32 | final E storageManager
33 | )
34 | {
35 | super();
36 | this.migratingObject = migratingObject;
37 | this.storageManager = storageManager;
38 | }
39 |
40 | /**
41 | * @return the current object where the migration is executed upon
42 | */
43 | public T getMigratingObject()
44 | {
45 | return this.migratingObject;
46 | }
47 |
48 | /**
49 | * @return the responsible storage manager for the migrating object
50 | */
51 | public E getStorageManager()
52 | {
53 | return this.storageManager;
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/micro-migration/src/main/java/software/xdev/micromigration/scripts/ReflectiveVersionMigrationScript.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.scripts;
17 |
18 | import java.util.ArrayList;
19 | import java.util.List;
20 |
21 | import software.xdev.micromigration.version.MigrationVersion;
22 | import software.xdev.micromigration.versionagnostic.VersionAgnosticMigrationEmbeddedStorageManager;
23 |
24 |
25 | /**
26 | * Script which creates the target version of the script through the class name.
27 | *
28 | * Class name has to be in the scheme:
29 | *
30 | * vM_Classname vM_m_Classname vM_m_m_Classname
31 | *
32 | * Where v
is short for version and is a constant (just a char),
33 | * M
is a integer for the major version,
34 | * m
is a integer for the minor version
35 | * Classname
is a custom String that the user can choose.
36 | * This scheme can basically be extended infinetly. For example: v1_1_2_2_MyUpdateScript
37 | *
38 | * Therefore the character _
can only be used as a seperator of versions and may not be used for other
39 | * purposes.
40 | *
41 | * If the class name has the wrong format, an {@link Error} is thrown.
42 | */
43 | public abstract class ReflectiveVersionMigrationScript<
44 | T,
45 | E extends VersionAgnosticMigrationEmbeddedStorageManager, ?>>
46 | implements VersionAgnosticMigrationScript
47 | {
48 | private static final char PREFIX = 'v';
49 | private static final String VERSION_SEPERATOR = "_";
50 | private static final String WRONG_FORMAT_ERROR_MESSAGE =
51 | "Script has invalid class name. Either rename the class to a valid script class name, or implement method "
52 | + "getTargetVersion().";
53 |
54 | private final MigrationVersion version;
55 |
56 | /**
57 | * @throws Error if the class name has the wrong format
58 | */
59 | protected ReflectiveVersionMigrationScript()
60 | {
61 | this.version = this.createTargetVersionFromClassName();
62 | }
63 |
64 | private MigrationVersion createTargetVersionFromClassName()
65 | {
66 | final String implementationClassName = this.getClass().getSimpleName();
67 | if(PREFIX != implementationClassName.charAt(0))
68 | {
69 | throw new IllegalArgumentException(WRONG_FORMAT_ERROR_MESSAGE);
70 | }
71 | final String implementationClassNameWithoutPrefix = implementationClassName.substring(1);
72 | final String[] classNameParts = implementationClassNameWithoutPrefix.split(VERSION_SEPERATOR);
73 | if(classNameParts.length < 2)
74 | {
75 | throw new IllegalArgumentException(WRONG_FORMAT_ERROR_MESSAGE);
76 | }
77 | try
78 | {
79 | final List versionNumbers = new ArrayList<>();
80 | for(int i = 0; i < classNameParts.length - 1; i++)
81 | {
82 | versionNumbers.add(Integer.parseInt(classNameParts[i]));
83 | }
84 | return new MigrationVersion(versionNumbers);
85 | }
86 | catch(final NumberFormatException e)
87 | {
88 | throw new IllegalArgumentException(WRONG_FORMAT_ERROR_MESSAGE, e);
89 | }
90 | }
91 |
92 | @Override
93 | public MigrationVersion getTargetVersion()
94 | {
95 | return this.version;
96 | }
97 | }
98 |
--------------------------------------------------------------------------------
/micro-migration/src/main/java/software/xdev/micromigration/scripts/SimpleMigrationScript.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.scripts;
17 |
18 | import java.util.function.Consumer;
19 |
20 | import software.xdev.micromigration.version.MigrationVersion;
21 | import software.xdev.micromigration.versionagnostic.VersionAgnosticMigrationEmbeddedStorageManager;
22 |
23 |
24 | /**
25 | * Provides a simple way to create a migration script with the necessary version and {@link Consumer}.
26 | */
27 | public class SimpleMigrationScript>
28 | extends SimpleTypedMigrationScript
29 | {
30 | /**
31 | * @param targetVersion to which the script is updating the object
32 | * @param consumer which consumes the object and updates it to the target version
33 | */
34 | public SimpleMigrationScript(
35 | final MigrationVersion targetVersion,
36 | final Consumer> consumer
37 | )
38 | {
39 | super(targetVersion, consumer);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/micro-migration/src/main/java/software/xdev/micromigration/scripts/SimpleTypedMigrationScript.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.scripts;
17 |
18 | import java.util.Objects;
19 | import java.util.function.Consumer;
20 |
21 | import software.xdev.micromigration.version.MigrationVersion;
22 | import software.xdev.micromigration.versionagnostic.VersionAgnosticMigrationEmbeddedStorageManager;
23 |
24 |
25 | /**
26 | * Provides a simple way to create a migration script with the necessary version and {@link Consumer}.
27 | */
28 | public class SimpleTypedMigrationScript>
29 | implements VersionAgnosticMigrationScript
30 | {
31 | private final MigrationVersion version;
32 | private final Consumer> consumer;
33 |
34 | /**
35 | * @param version of the datastore after this script is executed
36 | * @param consumer which is executed to reach the given datastore version
37 | */
38 | public SimpleTypedMigrationScript(
39 | final MigrationVersion version,
40 | final Consumer> consumer
41 | )
42 | {
43 | Objects.requireNonNull(version);
44 | Objects.requireNonNull(consumer);
45 | this.version = version;
46 | this.consumer = consumer;
47 | }
48 |
49 | @Override
50 | public MigrationVersion getTargetVersion()
51 | {
52 | return this.version;
53 | }
54 |
55 | @Override
56 | public void migrate(final Context context)
57 | {
58 | this.consumer.accept(context);
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/micro-migration/src/main/java/software/xdev/micromigration/scripts/VersionAgnosticMigrationScript.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.scripts;
17 |
18 | import java.util.Comparator;
19 |
20 | import software.xdev.micromigration.version.MigrationVersion;
21 | import software.xdev.micromigration.versionagnostic.VersionAgnosticMigrationEmbeddedStorageManager;
22 |
23 |
24 | /**
25 | * Interface for scripts to migrate / update datastores.
26 | *
27 | * One script is supposed to bring a datastore from a lower version to the target version. After the
28 | * {@link VersionAgnosticMigrationScript#migrate(Context)} method is called, the target version is reached.
29 | */
30 | public interface VersionAgnosticMigrationScript>
31 | {
32 | /**
33 | * @return the version of the datastore after this script is executed.
34 | */
35 | MigrationVersion getTargetVersion();
36 |
37 | /**
38 | * Execute logic to migrate the given datastore to a newer version of the store. After executing the
39 | * {@link #getTargetVersion()} is reached.
40 | *
41 | * @param context that holds necessary data for the migration
42 | */
43 | void migrate(Context context);
44 |
45 | /**
46 | * Provides a {@link Comparator} that compares the {@link #getTargetVersion()} of the given scripts
47 | */
48 | Comparator> COMPARATOR =
49 | (o1, o2) -> MigrationVersion.COMPARATOR.compare(o1.getTargetVersion(), o2.getTargetVersion());
50 | }
51 |
--------------------------------------------------------------------------------
/micro-migration/src/main/java/software/xdev/micromigration/version/MigrationVersion.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.version;
17 |
18 | import java.util.Arrays;
19 | import java.util.Comparator;
20 | import java.util.List;
21 |
22 |
23 | /**
24 | * Defines one version of the EclipseStore datastore.
25 | */
26 | public class MigrationVersion
27 | {
28 | private final int[] versions;
29 |
30 | /**
31 | * @param versions as integers. For example 1.0.2 would be an array of [1,0,2]
32 | */
33 | public MigrationVersion(final int... versions)
34 | {
35 | if(versions == null || versions.length == 0)
36 | {
37 | this.versions = new int[]{0};
38 | }
39 | else
40 | {
41 | this.versions = versions;
42 | }
43 | }
44 |
45 | /**
46 | * @param versionsAsList as integers. For example 1.0.2 would be a list of [1,0,2]
47 | */
48 | public MigrationVersion(final List versionsAsList)
49 | {
50 | if(versionsAsList == null || versionsAsList.isEmpty())
51 | {
52 | this.versions = new int[]{0};
53 | }
54 | else
55 | {
56 | final int[] versionsAsArray = new int[versionsAsList.size()];
57 | for(int i = 0; i < versionsAsArray.length; i++)
58 | {
59 | versionsAsArray[i] = versionsAsList.get(i);
60 | }
61 | this.versions = versionsAsArray;
62 | }
63 | }
64 |
65 | /**
66 | * @return versions as an array of integers. For example 1.0.2 would be an array of [1,0,2]
67 | */
68 | public int[] getVersions()
69 | {
70 | return this.versions;
71 | }
72 |
73 | @Override
74 | public String toString()
75 | {
76 | final StringBuilder sb = new StringBuilder("v");
77 | for(final int version : this.versions)
78 | {
79 | sb.append(version).append('.');
80 | }
81 | sb.deleteCharAt(sb.length() - 1);
82 | return sb.toString();
83 | }
84 |
85 | @Override
86 | public int hashCode()
87 | {
88 | final int prime = 31;
89 | int result = 1;
90 | result = prime * result + Arrays.hashCode(this.versions);
91 | return result;
92 | }
93 |
94 | @Override
95 | public boolean equals(final Object obj)
96 | {
97 | if(this == obj)
98 | {
99 | return true;
100 | }
101 | if(obj == null)
102 | {
103 | return false;
104 | }
105 | if(this.getClass() != obj.getClass())
106 | {
107 | return false;
108 | }
109 | final MigrationVersion other = (MigrationVersion)obj;
110 | return Arrays.equals(this.versions, other.versions);
111 | }
112 |
113 | /**
114 | * Provides a {@link Comparator} that compares the {@link #getVersions()} of the given versions
115 | */
116 | public static final Comparator COMPARATOR =
117 | Comparator.comparing(MigrationVersion::getVersions, Arrays::compare);
118 | }
119 |
--------------------------------------------------------------------------------
/micro-migration/src/main/java/software/xdev/micromigration/version/Versioned.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.version;
17 |
18 | /**
19 | * Interface used by the MigrationManagers for easier versioning of objects.
20 | */
21 | public interface Versioned
22 | {
23 | /**
24 | * @param version to set the current version of the object
25 | */
26 | void setVersion(MigrationVersion version);
27 |
28 | /**
29 | * @return the current version of the object
30 | */
31 | MigrationVersion getVersion();
32 | }
33 |
--------------------------------------------------------------------------------
/micro-migration/src/main/java/software/xdev/micromigration/version/VersionedAndKeeperOfHistory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.version;
17 |
18 | import java.util.List;
19 |
20 | import software.xdev.micromigration.notification.ScriptExecutionNotificationWithoutScriptReference;
21 |
22 | /**
23 | * Interface used by the MigrationManagers for easier versioning of objects
24 | * and to keep and read the migration history.
25 | */
26 | public interface VersionedAndKeeperOfHistory extends Versioned
27 | {
28 | /**
29 | * Adds the information about the executed script to the history book.
30 | * @param executedScriptInformation information about the executed script
31 | */
32 | void addExecutedScript(ScriptExecutionNotificationWithoutScriptReference executedScriptInformation);
33 |
34 | /**
35 | * @return the complete migration history. That means information about every executed script.
36 | */
37 | List getMigrationHistory();
38 | }
39 |
--------------------------------------------------------------------------------
/micro-migration/src/main/java/software/xdev/micromigration/version/VersionedObject.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.version;
17 |
18 | import java.util.Objects;
19 |
20 |
21 | /**
22 | * Simple container to hold a specific object and a correlating version for it.
23 | *
24 | * @param type of the object that's contained
25 | */
26 | public class VersionedObject implements Versioned
27 | {
28 | private MigrationVersion currentVersion;
29 | private T actualObject;
30 |
31 | /**
32 | * @param actualObject set the actual object which is versioned
33 | */
34 | public VersionedObject(final T actualObject)
35 | {
36 | this.actualObject = actualObject;
37 | this.currentVersion = new MigrationVersion(0);
38 | }
39 |
40 | @Override
41 | public void setVersion(final MigrationVersion version)
42 | {
43 | Objects.requireNonNull(version);
44 | this.currentVersion = version;
45 | }
46 |
47 | @Override
48 | public MigrationVersion getVersion()
49 | {
50 | return this.currentVersion;
51 | }
52 |
53 | /**
54 | * @param actualObject which is versioned
55 | */
56 | public void setObject(final T actualObject)
57 | {
58 | this.actualObject = actualObject;
59 | }
60 |
61 | /**
62 | * @return the actual object which is versioned
63 | */
64 | public T getObject()
65 | {
66 | return this.actualObject;
67 | }
68 |
69 | @Override
70 | public String toString()
71 | {
72 | return this.currentVersion.toString() + "\n" + this.actualObject;
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/micro-migration/src/main/java/software/xdev/micromigration/version/VersionedObjectWithHistory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.version;
17 |
18 | import java.util.ArrayList;
19 | import java.util.List;
20 | import java.util.Objects;
21 |
22 | import software.xdev.micromigration.notification.ScriptExecutionNotificationWithoutScriptReference;
23 |
24 |
25 | /**
26 | * This class is inserted as the root of the datastore and contains only the current version, the actual
27 | * root object and the history of executed scripts.
28 | */
29 | public class VersionedObjectWithHistory extends VersionedObject implements VersionedAndKeeperOfHistory
30 | {
31 | private final List migrationHistory;
32 |
33 | /**
34 | * @param actualRoot which is stored in the datastore and defined by the user
35 | */
36 | public VersionedObjectWithHistory(final Object actualRoot)
37 | {
38 | super(actualRoot);
39 | this.migrationHistory = new ArrayList<>();
40 | }
41 |
42 | @Override
43 | public void addExecutedScript(final ScriptExecutionNotificationWithoutScriptReference executedScriptInformation)
44 | {
45 | this.migrationHistory.add(Objects.requireNonNull(executedScriptInformation));
46 | }
47 |
48 | @Override
49 | public List getMigrationHistory()
50 | {
51 | return this.migrationHistory;
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/micro-migration/src/main/java/software/xdev/micromigration/version/VersionedRoot.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.version;
17 |
18 | import java.util.Objects;
19 |
20 |
21 | /**
22 | * This class is inserted as the root of the datastore and contains only the current version and the actual root
23 | * object.
24 | */
25 | public class VersionedRoot implements Versioned
26 | {
27 | private MigrationVersion currentVersion;
28 | private Object actualRoot;
29 |
30 | /**
31 | * @param actualRoot which is stored in the datastore and defined by the user
32 | */
33 | public VersionedRoot(final Object actualRoot)
34 | {
35 | this.actualRoot = actualRoot;
36 | this.currentVersion = new MigrationVersion(0);
37 | }
38 |
39 | @Override
40 | public void setVersion(final MigrationVersion version)
41 | {
42 | Objects.requireNonNull(version);
43 | this.currentVersion = version;
44 | }
45 |
46 | @Override
47 | public MigrationVersion getVersion()
48 | {
49 | return this.currentVersion;
50 | }
51 |
52 | /**
53 | * @param actualRoot which is stored in the datastore and defined by the user
54 | */
55 | public void setRoot(final Object actualRoot)
56 | {
57 | this.actualRoot = actualRoot;
58 | }
59 |
60 | /**
61 | * @return the actual root, that's defined by the user
62 | */
63 | public Object getRoot()
64 | {
65 | return this.actualRoot;
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/micro-migration/src/main/java/software/xdev/micromigration/version/VersionedRootWithHistory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.version;
17 |
18 | import java.util.ArrayList;
19 | import java.util.List;
20 | import java.util.Objects;
21 |
22 | import software.xdev.micromigration.notification.ScriptExecutionNotificationWithoutScriptReference;
23 |
24 |
25 | /**
26 | * This class is inserted as the root of the datastore and contains only the current version, the actual root object
27 | * and the history of executed scripts.
28 | */
29 | public class VersionedRootWithHistory extends VersionedRoot implements VersionedAndKeeperOfHistory
30 | {
31 | private final List migrationHistory;
32 |
33 | /**
34 | * @param actualRoot which is stored in the datastore and defined by the user
35 | */
36 | public VersionedRootWithHistory(final Object actualRoot)
37 | {
38 | super(actualRoot);
39 | this.migrationHistory = new ArrayList<>();
40 | }
41 |
42 | @Override
43 | public void addExecutedScript(final ScriptExecutionNotificationWithoutScriptReference executedScriptInformation)
44 | {
45 | this.migrationHistory.add(Objects.requireNonNull(executedScriptInformation));
46 | }
47 |
48 | /**
49 | * @return the complete migration history. That means information about every executed script.
50 | */
51 | @Override
52 | public List getMigrationHistory()
53 | {
54 | return this.migrationHistory;
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/micro-migration/src/main/java/software/xdev/micromigration/versionagnostic/VersionAgnosticMigrationEmbeddedStorageManager.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.versionagnostic;
17 |
18 | import java.util.List;
19 | import java.util.Objects;
20 |
21 | import software.xdev.micromigration.migrater.MicroMigrater;
22 | import software.xdev.micromigration.notification.ScriptExecutionNotificationWithoutScriptReference;
23 | import software.xdev.micromigration.version.MigrationVersion;
24 | import software.xdev.micromigration.version.Versioned;
25 | import software.xdev.micromigration.version.VersionedRoot;
26 | import software.xdev.micromigration.version.VersionedRootWithHistory;
27 |
28 |
29 | /**
30 | * Wrapper class for the {@link org.eclipse.store.storage.embedded.types.EmbeddedStorageManager} interface.
31 | *
32 | * Basically it intercepts storing the root object and places a {@link Versioned} in front of it. This means the root
33 | * object of the datastore is then versioned. Internally uses the {@link VersionAgnosticMigrationManager} to do the
34 | * actual migration.
35 | *
36 | * {@code VersionAgnostic} because it should be independent of the actual implementation used.
37 | *
38 | * @param class of itself to be able to return the actual class and not just a generic class
39 | * @param The actually used EmbeddedStorageManager
40 | */
41 | public abstract class VersionAgnosticMigrationEmbeddedStorageManager
42 | implements AutoCloseable
43 | {
44 | private final MicroMigrater migrater;
45 | private VersionedRootWithHistory versionRoot;
46 | private final VersionAgnosticTunnelingEmbeddedStorageManager tunnelingManager;
47 |
48 | /**
49 | * @param tunnelingManager which will be used as the underlying storage manager. Almost all methods are only
50 | * rerouted to this native manager. Only {@link #start()}, {@link #root()} and
51 | * {@link #setRoot(Object)} are intercepted and a {@link Versioned} is placed between the
52 | * requests.
53 | * @param migrater which is used as source for the migration scripts
54 | */
55 | protected VersionAgnosticMigrationEmbeddedStorageManager(
56 | final VersionAgnosticTunnelingEmbeddedStorageManager tunnelingManager,
57 | final MicroMigrater migrater
58 | )
59 | {
60 | this.tunnelingManager = Objects.requireNonNull(tunnelingManager);
61 | this.migrater = Objects.requireNonNull(migrater);
62 | }
63 |
64 | /**
65 | * @return the native EmbeddedStorageManager
66 | */
67 | public E getNativeStorageManager()
68 | {
69 | return this.getTunnelingManager().getNativeStorageManager();
70 | }
71 |
72 | /**
73 | * @return the used {@link VersionAgnosticTunnelingEmbeddedStorageManager}
74 | */
75 | protected VersionAgnosticTunnelingEmbeddedStorageManager getTunnelingManager()
76 | {
77 | return this.tunnelingManager;
78 | }
79 |
80 | /**
81 | * Checks if the root object is of the instance of {@link Versioned}. If it is not, the root will be replaced with
82 | * the versioned root and the actual root object will be put inside the versioned root.
83 | *
84 | * After starting the storage manager, all the available update scripts are executed in order until the newest
85 | * version of the datastore is reached.
86 | *
87 | * @return itself
88 | */
89 | @SuppressWarnings({"unchecked", "rawtypes"})
90 | public T start()
91 | {
92 | this.tunnelingManager.start();
93 | if(this.tunnelingManager.root() instanceof final VersionedRootWithHistory versionedRootWithHistory)
94 | {
95 | this.versionRoot = versionedRootWithHistory;
96 | }
97 | else
98 | {
99 | // Build VersionedRootWithHistory around actual root, set by user.
100 | this.versionRoot = new VersionedRootWithHistory(this.tunnelingManager.root());
101 | this.tunnelingManager.setRoot(this.versionRoot);
102 | this.tunnelingManager.storeRoot();
103 | }
104 | new VersionAgnosticMigrationManager(
105 | this.versionRoot,
106 | this.migrater,
107 | this
108 | ).migrate(this.versionRoot.getRoot());
109 | return (T)this;
110 | }
111 |
112 | /**
113 | * @return current version that's managed
114 | */
115 | public MigrationVersion getCurrentVersion()
116 | {
117 | return this.versionRoot.getVersion();
118 | }
119 |
120 | /**
121 | * @return the actual root object
122 | */
123 | public Object root()
124 | {
125 | return this.versionRoot.getRoot();
126 | }
127 |
128 | /**
129 | * @return the actual root object
130 | */
131 | public List getMigrationHistory()
132 | {
133 | return this.versionRoot.getMigrationHistory();
134 | }
135 |
136 | /**
137 | * Sets the actual root element (not the versioned root)
138 | *
139 | * @param newRoot to set
140 | * @return the set object
141 | */
142 | public Object setRoot(final Object newRoot)
143 | {
144 | this.versionRoot.setRoot(newRoot);
145 | return newRoot;
146 | }
147 |
148 | /**
149 | * Stores the {@link VersionedRoot} and the actual root object.
150 | *
151 | * @return what EmbeddedStorageManager#storeRoot returns
152 | */
153 | public long storeRoot()
154 | {
155 | this.tunnelingManager.store(this.versionRoot);
156 | return this.tunnelingManager.store(this.versionRoot.getRoot());
157 | }
158 |
159 | /**
160 | * Stores the objectToStore
161 | *
162 | * @param objectToStore which is stored
163 | * @return what EmbeddedStorageManager#store returns
164 | */
165 | public long store(final Object objectToStore)
166 | {
167 | return this.tunnelingManager.store(objectToStore);
168 | }
169 |
170 | /**
171 | * Shuts down the datastore.
172 | *
173 | * @return what EmbeddedStorageManager#storeRoot shutdown
174 | */
175 | public boolean shutdown()
176 | {
177 | return this.tunnelingManager.shutdown();
178 | }
179 |
180 | /**
181 | * Closes the datastore.
182 | */
183 | @Override
184 | public void close()
185 | {
186 | this.tunnelingManager.close();
187 | }
188 | }
189 |
--------------------------------------------------------------------------------
/micro-migration/src/main/java/software/xdev/micromigration/versionagnostic/VersionAgnosticTunnelingEmbeddedStorageManager.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.versionagnostic;
17 |
18 | /**
19 | * Wrapper class for the {@link org.eclipse.store.storage.embedded.types.EmbeddedStorageManager} interface.
20 | *
21 | * It's simply an interface to not directly depend on the underlying framework, but still use its functionality.
22 | *
23 | *
24 | * {@code VersionAgnostic} because it should be independent of the actual implementation used.
25 | *
26 | *
27 | * @param Represents the actually used EmbeddedStorageManager
28 | */
29 | public interface VersionAgnosticTunnelingEmbeddedStorageManager
30 | extends AutoCloseable
31 | {
32 | /**
33 | * Simply relais the method-call to the EmbeddedStorageManager
34 | *
35 | * @return what the actual EmbeddedStorageManager returns
36 | */
37 | T start();
38 |
39 | /**
40 | * Simply relais the method-call to the EmbeddedStorageManager
41 | *
42 | * @return what the actual EmbeddedStorageManager returns
43 | */
44 | Object root();
45 |
46 | /**
47 | * Simply relais the method-call to the EmbeddedStorageManager
48 | *
49 | * @param newRoot whatever the actual EmbeddedStorageManager uses this for
50 | * @return what the actual EmbeddedStorageManager returns
51 | */
52 | Object setRoot(Object newRoot);
53 |
54 | /**
55 | * Simply relais the method-call to the EmbeddedStorageManager
56 | *
57 | * @return what the actual EmbeddedStorageManager returns
58 | */
59 | long storeRoot();
60 |
61 | /**
62 | * Simply relais the method-call to the EmbeddedStorageManager
63 | *
64 | * @return what the actual EmbeddedStorageManager returns
65 | */
66 | boolean shutdown();
67 |
68 | /**
69 | * Simply relais the method-call to the EmbeddedStorageManager
70 | *
71 | * @return what the actual EmbeddedStorageManager returns
72 | */
73 | boolean isAcceptingTasks();
74 |
75 | /**
76 | * Simply relais the method-call to the EmbeddedStorageManager
77 | *
78 | * @return what the actual EmbeddedStorageManager returns
79 | */
80 | boolean isRunning();
81 |
82 | /**
83 | * Simply relais the method-call to the EmbeddedStorageManager
84 | *
85 | * @return what the actual EmbeddedStorageManager returns
86 | */
87 | boolean isStartingUp();
88 |
89 | /**
90 | * Simply relais the method-call to the EmbeddedStorageManager
91 | *
92 | * @return what the actual EmbeddedStorageManager returns
93 | */
94 | boolean isShuttingDown();
95 |
96 | /**
97 | * Simply relais the method-call to the EmbeddedStorageManager
98 | */
99 | void checkAcceptingTasks();
100 |
101 | /**
102 | * Simply relais the method-call to the EmbeddedStorageManager
103 | *
104 | * @return what the actual EmbeddedStorageManager returns
105 | */
106 | long initializationTime();
107 |
108 | /**
109 | * Simply relais the method-call to the EmbeddedStorageManager
110 | *
111 | * @return what the actual EmbeddedStorageManager returns
112 | */
113 | long operationModeTime();
114 |
115 | /**
116 | * Simply relais the method-call to the EmbeddedStorageManager
117 | *
118 | * @return what the actual EmbeddedStorageManager returns
119 | */
120 | boolean isActive();
121 |
122 | /**
123 | * Simply relais the method-call to the EmbeddedStorageManager
124 | *
125 | * @param nanoTimeBudget whatever the actual EmbeddedStorageManager uses this for
126 | * @return what the actual EmbeddedStorageManager returns
127 | */
128 | boolean issueGarbageCollection(long nanoTimeBudget);
129 |
130 | /**
131 | * Simply relais the method-call to the EmbeddedStorageManager
132 | *
133 | * @param nanoTimeBudget whatever the actual EmbeddedStorageManager uses this for
134 | * @return what the actual EmbeddedStorageManager returns
135 | */
136 | boolean issueFileCheck(long nanoTimeBudget);
137 |
138 | /**
139 | * Simply relais the method-call to the EmbeddedStorageManager
140 | *
141 | * @param instance whatever the actual EmbeddedStorageManager uses this for
142 | * @return what the actual EmbeddedStorageManager returns
143 | */
144 | long store(Object instance);
145 |
146 | /**
147 | * @return the actual EmbeddedStorageManager
148 | */
149 | T getNativeStorageManager();
150 |
151 | /**
152 | * Simply relais the method-call to the EmbeddedStorageManager
153 | */
154 | @Override
155 | void close();
156 | }
157 |
--------------------------------------------------------------------------------
/micro-migration/src/test/java/software/xdev/micromigration/eclipsestore/integration/IntroduceMigrationOnExistingDatastoreTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.eclipsestore.integration;
17 |
18 | import static org.junit.jupiter.api.Assertions.assertEquals;
19 |
20 | import java.nio.file.Path;
21 |
22 | import org.eclipse.store.storage.embedded.types.EmbeddedStorage;
23 | import org.eclipse.store.storage.embedded.types.EmbeddedStorageManager;
24 | import org.junit.jupiter.api.Test;
25 | import org.junit.jupiter.api.io.TempDir;
26 |
27 | import software.xdev.micromigration.eclipsestore.MigrationEmbeddedStorage;
28 | import software.xdev.micromigration.eclipsestore.MigrationEmbeddedStorageManager;
29 | import software.xdev.micromigration.eclipsestore.util.MicroMigrationScriptDummy;
30 | import software.xdev.micromigration.migrater.ExplicitMigrater;
31 | import software.xdev.micromigration.version.MigrationVersion;
32 |
33 |
34 | class IntroduceMigrationOnExistingDatastoreTest
35 | {
36 | static final String ROOT = "OriginalRoot";
37 |
38 | @Test
39 | void checkIntroducingMigrationOnExistingDatastoreMigrationEmbeddedStorageManager(@TempDir final Path storageFolder)
40 | {
41 | try(final EmbeddedStorageManager storageManager = EmbeddedStorage.start(storageFolder))
42 | {
43 | storageManager.setRoot(ROOT);
44 | storageManager.storeRoot();
45 | }
46 |
47 | final ExplicitMigrater migrater = new ExplicitMigrater(
48 | new MicroMigrationScriptDummy(new MigrationVersion(1))
49 | );
50 | try(final MigrationEmbeddedStorageManager migrationStorageManager = MigrationEmbeddedStorage.start(
51 | storageFolder,
52 | migrater))
53 | {
54 | assertEquals(ROOT, migrationStorageManager.root());
55 | assertEquals(1, migrationStorageManager.getCurrentVersion().getVersions()[0]);
56 | }
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/micro-migration/src/test/java/software/xdev/micromigration/eclipsestore/integration/MigrationScriptWithNullSourceVersionTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.eclipsestore.integration;
17 |
18 | import static org.junit.jupiter.api.Assertions.assertEquals;
19 | import static org.junit.jupiter.api.Assertions.assertNull;
20 |
21 | import java.nio.file.Path;
22 |
23 | import org.eclipse.store.storage.embedded.types.EmbeddedStorage;
24 | import org.eclipse.store.storage.embedded.types.EmbeddedStorageManager;
25 | import org.junit.jupiter.api.Test;
26 | import org.junit.jupiter.api.io.TempDir;
27 |
28 | import software.xdev.micromigration.eclipsestore.MigrationEmbeddedStorageManager;
29 | import software.xdev.micromigration.eclipsestore.MigrationManager;
30 | import software.xdev.micromigration.migrater.ExplicitMigrater;
31 | import software.xdev.micromigration.scripts.SimpleTypedMigrationScript;
32 | import software.xdev.micromigration.scripts.VersionAgnosticMigrationScript;
33 | import software.xdev.micromigration.version.MigrationVersion;
34 | import software.xdev.micromigration.version.Versioned;
35 | import software.xdev.micromigration.version.VersionedObject;
36 |
37 |
38 | class MigrationScriptWithNullSourceVersionTest
39 | {
40 | public static class EmptyVersionedRoot implements Versioned
41 | {
42 | private MigrationVersion version;
43 |
44 | @Override
45 | public void setVersion(final MigrationVersion version)
46 | {
47 | this.version = version;
48 | }
49 |
50 | @Override
51 | public MigrationVersion getVersion()
52 | {
53 | return this.version;
54 | }
55 | }
56 |
57 | @Test
58 | void updateFromEmptyVersion(@TempDir final Path storageFolder)
59 | {
60 | // First run without any migration script
61 | try(final EmbeddedStorageManager storageManager = this.startEmbeddedStorageManagerWithPath(storageFolder))
62 | {
63 | final EmptyVersionedRoot firstRoot = new EmptyVersionedRoot();
64 | storageManager.setRoot(firstRoot);
65 | storageManager.storeRoot();
66 | assertNull(firstRoot.getVersion());
67 | }
68 |
69 | // Run with one migration script
70 | final VersionAgnosticMigrationScript, MigrationEmbeddedStorageManager> firstScript =
71 | new SimpleTypedMigrationScript<>(
72 | new MigrationVersion(1),
73 | context -> {
74 | }
75 | );
76 |
77 | try(final EmbeddedStorageManager storageManager = this.startEmbeddedStorageManagerWithPath(storageFolder))
78 | {
79 | new MigrationManager(
80 | (Versioned)storageManager.root(),
81 | new ExplicitMigrater(firstScript),
82 | storageManager
83 | )
84 | .migrate(storageManager.root());
85 | final EmptyVersionedRoot currentRoot = (EmptyVersionedRoot)storageManager.root();
86 | assertEquals(new MigrationVersion(1), currentRoot.getVersion());
87 | }
88 | }
89 |
90 | @Test
91 | void updateWithNoScripts(@TempDir final Path storageFolder)
92 | {
93 | // First run without any migration script
94 | try(final EmbeddedStorageManager storageManager = this.startEmbeddedStorageManagerWithPath(storageFolder))
95 | {
96 | final EmptyVersionedRoot firstRoot = new EmptyVersionedRoot();
97 | storageManager.setRoot(firstRoot);
98 | storageManager.storeRoot();
99 | assertNull(firstRoot.getVersion());
100 | }
101 |
102 | try(final EmbeddedStorageManager storageManager = this.startEmbeddedStorageManagerWithPath(storageFolder))
103 | {
104 | new MigrationManager(
105 | (Versioned)storageManager.root(),
106 | new ExplicitMigrater(),
107 | storageManager
108 | )
109 | .migrate(storageManager.root());
110 | final EmptyVersionedRoot currentRoot = (EmptyVersionedRoot)storageManager.root();
111 | assertNull(currentRoot.getVersion());
112 | }
113 | }
114 |
115 | private EmbeddedStorageManager startEmbeddedStorageManagerWithPath(final Path storageFolder)
116 | {
117 | return EmbeddedStorage.start(storageFolder);
118 | }
119 | }
120 |
--------------------------------------------------------------------------------
/micro-migration/src/test/java/software/xdev/micromigration/eclipsestore/integration/MultipleScriptsTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.eclipsestore.integration;
17 |
18 | import static org.junit.jupiter.api.Assertions.assertEquals;
19 |
20 | import java.nio.file.Path;
21 |
22 | import org.junit.jupiter.api.Assertions;
23 | import org.junit.jupiter.api.Test;
24 | import org.junit.jupiter.api.io.TempDir;
25 |
26 | import software.xdev.micromigration.eclipsestore.MigrationEmbeddedStorage;
27 | import software.xdev.micromigration.eclipsestore.MigrationEmbeddedStorageManager;
28 | import software.xdev.micromigration.migrater.ExplicitMigrater;
29 | import software.xdev.micromigration.migrater.VersionAlreadyRegisteredException;
30 | import software.xdev.micromigration.scripts.SimpleTypedMigrationScript;
31 | import software.xdev.micromigration.scripts.VersionAgnosticMigrationScript;
32 | import software.xdev.micromigration.version.MigrationVersion;
33 |
34 |
35 | class MultipleScriptsTest
36 | {
37 | @Test
38 | void checkMigrationWithTwoScriptsAtOnceMigrationEmbeddedStorageManager(@TempDir final Path storageFolder)
39 | {
40 | final VersionAgnosticMigrationScript firstScript =
41 | new SimpleTypedMigrationScript<>(
42 | new MigrationVersion(1),
43 | context -> context.getStorageManager().setRoot(1)
44 | );
45 | final VersionAgnosticMigrationScript secondScript =
46 | new SimpleTypedMigrationScript<>(
47 | new MigrationVersion(2),
48 | context -> context.getStorageManager().setRoot(2)
49 | );
50 | final ExplicitMigrater migrater = new ExplicitMigrater(firstScript, secondScript);
51 | try(final MigrationEmbeddedStorageManager migrationStorageManager = MigrationEmbeddedStorage.start(
52 | storageFolder,
53 | migrater))
54 | {
55 | assertEquals(2, migrationStorageManager.root());
56 | assertEquals(new MigrationVersion(2), migrationStorageManager.getCurrentVersion());
57 | }
58 | }
59 |
60 | @Test
61 | void checkMigrationWithTwoScriptsWithSameVersion()
62 | {
63 | final VersionAgnosticMigrationScript firstScript =
64 | new SimpleTypedMigrationScript<>(
65 | new MigrationVersion(1),
66 | context -> context.getStorageManager().setRoot(1)
67 | );
68 | final VersionAgnosticMigrationScript secondScript =
69 | new SimpleTypedMigrationScript<>(
70 | new MigrationVersion(1),
71 | context -> context.getStorageManager().setRoot(2)
72 | );
73 | Assertions.assertThrows(VersionAlreadyRegisteredException.class, () ->
74 | new ExplicitMigrater(firstScript, secondScript)
75 | );
76 | }
77 |
78 | @Test
79 | void checkMigrationWithThreeScriptsWithSameVersion()
80 | {
81 | final VersionAgnosticMigrationScript firstScript =
82 | new SimpleTypedMigrationScript<>(
83 | new MigrationVersion(1),
84 | context -> context.getStorageManager().setRoot(1)
85 | );
86 | final VersionAgnosticMigrationScript secondScript =
87 | new SimpleTypedMigrationScript<>(
88 | new MigrationVersion(2),
89 | context -> context.getStorageManager().setRoot(2)
90 | );
91 | final VersionAgnosticMigrationScript thirdScript =
92 | new SimpleTypedMigrationScript<>(
93 | new MigrationVersion(1),
94 | context -> context.getStorageManager().setRoot(3)
95 | );
96 | Assertions.assertThrows(VersionAlreadyRegisteredException.class, () ->
97 | new ExplicitMigrater(firstScript, secondScript, thirdScript)
98 | );
99 | }
100 | }
101 |
--------------------------------------------------------------------------------
/micro-migration/src/test/java/software/xdev/micromigration/eclipsestore/integration/StoreStuffInMigrationStorageManagerTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.eclipsestore.integration;
17 |
18 | import static org.junit.jupiter.api.Assertions.assertEquals;
19 | import static org.junit.jupiter.api.Assertions.assertNotNull;
20 |
21 | import java.nio.file.Path;
22 |
23 | import org.junit.jupiter.api.Test;
24 | import org.junit.jupiter.api.io.TempDir;
25 |
26 | import software.xdev.micromigration.eclipsestore.MigrationEmbeddedStorage;
27 | import software.xdev.micromigration.eclipsestore.MigrationEmbeddedStorageManager;
28 | import software.xdev.micromigration.migrater.ExplicitMigrater;
29 | import software.xdev.micromigration.scripts.SimpleTypedMigrationScript;
30 | import software.xdev.micromigration.scripts.VersionAgnosticMigrationScript;
31 | import software.xdev.micromigration.version.MigrationVersion;
32 |
33 |
34 | class StoreStuffInMigrationStorageManagerTest
35 | {
36 | static class RootClass
37 | {
38 | private final ChildClass child = new ChildClass();
39 | }
40 |
41 |
42 | static class ChildClass
43 | {
44 | private int i;
45 | }
46 |
47 | @Test
48 | void checkStoringSomethingAfterUpdating(@TempDir final Path storageFolder)
49 | {
50 | final VersionAgnosticMigrationScript script =
51 | new SimpleTypedMigrationScript<>(
52 | new MigrationVersion(1),
53 | context -> {
54 | }
55 | );
56 | final ExplicitMigrater migrater = new ExplicitMigrater(script);
57 | // Create new store and change stored object
58 | try(final MigrationEmbeddedStorageManager migrationStorageManager = MigrationEmbeddedStorage.start(
59 | storageFolder,
60 | migrater))
61 | {
62 | migrationStorageManager.setRoot(new RootClass());
63 | migrationStorageManager.storeRoot();
64 | final RootClass storedRoot = (RootClass)migrationStorageManager.root();
65 | assertEquals(0, storedRoot.child.i);
66 | ((RootClass)migrationStorageManager.root()).child.i = 1;
67 | migrationStorageManager.store(storedRoot.child);
68 | assertEquals(1, storedRoot.child.i);
69 | }
70 | // Check if stored object is correct
71 | try(final MigrationEmbeddedStorageManager migrationStorageManager = MigrationEmbeddedStorage.start(
72 | storageFolder,
73 | migrater))
74 | {
75 | final RootClass storedRoot = (RootClass)migrationStorageManager.root();
76 | assertNotNull(storedRoot);
77 | assertEquals(1, storedRoot.child.i);
78 | }
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/micro-migration/src/test/java/software/xdev/micromigration/eclipsestore/migrater/ExplicitMigraterTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.eclipsestore.migrater;
17 |
18 | import static org.junit.jupiter.api.Assertions.assertEquals;
19 |
20 | import java.nio.file.Path;
21 |
22 | import org.junit.jupiter.api.Assertions;
23 | import org.junit.jupiter.api.Test;
24 | import org.junit.jupiter.api.io.TempDir;
25 |
26 | import software.xdev.micromigration.eclipsestore.MigrationEmbeddedStorage;
27 | import software.xdev.micromigration.eclipsestore.MigrationEmbeddedStorageManager;
28 | import software.xdev.micromigration.eclipsestore.util.MicroMigrationScriptDummy;
29 | import software.xdev.micromigration.migrater.ExplicitMigrater;
30 | import software.xdev.micromigration.scripts.SimpleTypedMigrationScript;
31 | import software.xdev.micromigration.version.MigrationVersion;
32 |
33 |
34 | class ExplicitMigraterTest
35 | {
36 |
37 | @Test
38 | void checkGetSortedScriptsEmpty()
39 | {
40 | final ExplicitMigrater migrater = new ExplicitMigrater();
41 | assertEquals(0, migrater.getSortedScripts().size());
42 | }
43 |
44 | @Test
45 | void checkGetSortedScriptsSorted()
46 | {
47 | final ExplicitMigrater migrater = new ExplicitMigrater(
48 | new MicroMigrationScriptDummy(new MigrationVersion(1)),
49 | new MicroMigrationScriptDummy(new MigrationVersion(2))
50 | );
51 | assertEquals(1, migrater.getSortedScripts().first().getTargetVersion().getVersions()[0]);
52 | assertEquals(2, migrater.getSortedScripts().last().getTargetVersion().getVersions()[0]);
53 | }
54 |
55 | @Test
56 | void checkGetSortedScriptsUnsorted()
57 | {
58 | final ExplicitMigrater migrater = new ExplicitMigrater(
59 | new MicroMigrationScriptDummy(new MigrationVersion(2)),
60 | new MicroMigrationScriptDummy(new MigrationVersion(1))
61 | );
62 | assertEquals(1, migrater.getSortedScripts().first().getTargetVersion().getVersions()[0]);
63 | assertEquals(2, migrater.getSortedScripts().last().getTargetVersion().getVersions()[0]);
64 | }
65 |
66 | @Test
67 | void checkWrongTypedVersionedScript(@TempDir final Path storageFolder)
68 | {
69 | try(final MigrationEmbeddedStorageManager storageManager = MigrationEmbeddedStorage.start(
70 | storageFolder,
71 | new ExplicitMigrater()))
72 | {
73 | storageManager.setRoot("SomeString");
74 | storageManager.storeRoot();
75 | }
76 | final ExplicitMigrater migrater = new ExplicitMigrater(
77 | new SimpleTypedMigrationScript(
78 | new MigrationVersion(1),
79 | context -> context.getStorageManager().setRoot(context.getMigratingObject() + 1)
80 | )
81 | );
82 | Assertions.assertThrows(
83 | ClassCastException.class,
84 | () -> MigrationEmbeddedStorage.start(storageFolder, migrater));
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/micro-migration/src/test/java/software/xdev/micromigration/eclipsestore/util/MicroMigrationScriptDummy.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.eclipsestore.util;
17 |
18 | import software.xdev.micromigration.eclipsestore.MigrationEmbeddedStorageManager;
19 | import software.xdev.micromigration.scripts.Context;
20 | import software.xdev.micromigration.scripts.VersionAgnosticMigrationScript;
21 | import software.xdev.micromigration.version.MigrationVersion;
22 |
23 |
24 | public class MicroMigrationScriptDummy
25 | implements VersionAgnosticMigrationScript
26 | {
27 | private final MigrationVersion version;
28 |
29 | public MicroMigrationScriptDummy(final MigrationVersion version)
30 | {
31 | this.version = version;
32 | }
33 |
34 | @Override
35 | public MigrationVersion getTargetVersion()
36 | {
37 | return this.version;
38 | }
39 |
40 | @Override
41 | public void migrate(final Context context)
42 | {
43 | // Don't do anything.
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/micro-migration/src/test/java/software/xdev/micromigration/migrater/reflection/ReflectiveMigraterTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.migrater.reflection;
17 |
18 | import static org.junit.jupiter.api.Assertions.assertEquals;
19 |
20 | import org.junit.jupiter.api.Assertions;
21 | import org.junit.jupiter.api.Test;
22 |
23 | import software.xdev.micromigration.migrater.reflection.scripts.abstractReflectiveSuperClass.v1_ValidScript;
24 |
25 |
26 | class ReflectiveMigraterTest
27 | {
28 | @Test
29 | void testValidScript()
30 | {
31 | final ReflectiveMigrater migrater =
32 | new ReflectiveMigrater("software.xdev.micromigration.migrater.reflection.scripts.valid");
33 | assertEquals(1, migrater.getSortedScripts().size());
34 | assertEquals(
35 | software.xdev.micromigration.migrater.reflection.scripts.valid.ValidScript.class,
36 | migrater.getSortedScripts().first().getClass()
37 | );
38 | }
39 |
40 | @Test
41 | void testValidScriptWithIrrelevantClasses()
42 | {
43 | final ReflectiveMigrater migrater =
44 | new ReflectiveMigrater("software.xdev.micromigration.migrater.reflection.scripts"
45 | + ".moreClassesIncludingValid");
46 | assertEquals(1, migrater.getSortedScripts().size());
47 | assertEquals(
48 | software.xdev.micromigration.migrater.reflection.scripts.moreClassesIncludingValid.ValidScript.class,
49 | migrater.getSortedScripts().first().getClass()
50 | );
51 | }
52 |
53 | @Test
54 | void testValidScriptWithSubpackages()
55 | {
56 | final ReflectiveMigrater migrater =
57 | new ReflectiveMigrater("software.xdev.micromigration.migrater.reflection.scripts.includeSubPackages");
58 | assertEquals(2, migrater.getSortedScripts().size());
59 | assertEquals(
60 | software.xdev.micromigration.migrater.reflection.scripts.includeSubPackages.ValidScript.class,
61 | migrater.getSortedScripts().first().getClass()
62 | );
63 | assertEquals(
64 | software.xdev.micromigration.migrater.reflection.scripts.includeSubPackages.subpackage
65 | .ValidScriptInSubpackage.class,
66 | migrater.getSortedScripts().last().getClass()
67 | );
68 | }
69 |
70 | @Test
71 | void testPackageWithNoScript()
72 | {
73 | final ReflectiveMigrater migrater =
74 | new ReflectiveMigrater("software.xdev.micromigration.migrater.reflection.scripts.packageNotExisting");
75 | assertEquals(0, migrater.getSortedScripts().size());
76 | }
77 |
78 | @Test
79 | void testExceptionThrowingScript()
80 | {
81 | Assertions.assertThrows(ScriptInstantiationException.class, () -> {
82 | new ReflectiveMigrater("software.xdev.micromigration.migrater.reflection.scripts.exceptionThrowing");
83 | });
84 | }
85 |
86 | @Test
87 | void testErrorThrowingScript()
88 | {
89 | Assertions.assertThrows(ScriptInstantiationException.class, () -> {
90 | new ReflectiveMigrater("software.xdev.micromigration.migrater.reflection.scripts.errorThrowing");
91 | });
92 | }
93 |
94 | @Test
95 | void testNoCorrectConstructor()
96 | {
97 | Assertions.assertThrows(ScriptInstantiationException.class, () -> {
98 | new ReflectiveMigrater("software.xdev.micromigration.migrater.reflection.scripts.noCorrectConstructor");
99 | });
100 | }
101 |
102 | @Test
103 | void testAbstractSuperClass()
104 | {
105 | final ReflectiveMigrater migrater =
106 | new ReflectiveMigrater("software.xdev.micromigration.migrater.reflection.scripts.abstractSuperClass");
107 | assertEquals(1, migrater.getSortedScripts().size());
108 | assertEquals(
109 | software.xdev.micromigration.migrater.reflection.scripts.abstractSuperClass.ValidScript.class,
110 | migrater.getSortedScripts().first().getClass()
111 | );
112 | }
113 |
114 | @Test
115 | void testReflectiveVersion()
116 | {
117 | final ReflectiveMigrater migrater =
118 | new ReflectiveMigrater("software.xdev.micromigration.migrater.reflection.scripts.reflectiveVersion");
119 | assertEquals(1, migrater.getSortedScripts().size());
120 | assertEquals(
121 | software.xdev.micromigration.migrater.reflection.scripts.reflectiveVersion.v1_ValidScript.class,
122 | migrater.getSortedScripts().first().getClass()
123 | );
124 | }
125 |
126 | @Test
127 | void testReflectiveSuperClass()
128 | {
129 | final ReflectiveMigrater migrater =
130 | new ReflectiveMigrater(
131 | "software.xdev.micromigration.migrater.reflection.scripts.abstractReflectiveSuperClass");
132 | assertEquals(1, migrater.getSortedScripts().size());
133 | assertEquals(
134 | v1_ValidScript.class,
135 | migrater.getSortedScripts().first().getClass()
136 | );
137 | }
138 | }
139 |
--------------------------------------------------------------------------------
/micro-migration/src/test/java/software/xdev/micromigration/migrater/reflection/scripts/abstractReflectiveSuperClass/AbstractScript.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.migrater.reflection.scripts.abstractReflectiveSuperClass;
17 |
18 | import software.xdev.micromigration.eclipsestore.MigrationEmbeddedStorageManager;
19 | import software.xdev.micromigration.scripts.ReflectiveVersionMigrationScript;
20 |
21 |
22 | public abstract class AbstractScript extends ReflectiveVersionMigrationScript
23 | {
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/micro-migration/src/test/java/software/xdev/micromigration/migrater/reflection/scripts/abstractReflectiveSuperClass/v1_ValidScript.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.migrater.reflection.scripts.abstractReflectiveSuperClass;
17 |
18 | import software.xdev.micromigration.eclipsestore.MigrationEmbeddedStorageManager;
19 | import software.xdev.micromigration.scripts.Context;
20 |
21 |
22 | @SuppressWarnings("checkstyle:TypeName")
23 | public class v1_ValidScript extends AbstractScript
24 | {
25 | @Override
26 | public void migrate(final Context context)
27 | {
28 | // Do nothing
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/micro-migration/src/test/java/software/xdev/micromigration/migrater/reflection/scripts/abstractSuperClass/AbstractScript.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.migrater.reflection.scripts.abstractSuperClass;
17 |
18 | import software.xdev.micromigration.eclipsestore.MigrationEmbeddedStorageManager;
19 | import software.xdev.micromigration.scripts.VersionAgnosticMigrationScript;
20 | import software.xdev.micromigration.version.MigrationVersion;
21 |
22 |
23 | public abstract class AbstractScript implements VersionAgnosticMigrationScript
24 | {
25 | @Override
26 | public MigrationVersion getTargetVersion()
27 | {
28 | return new MigrationVersion(1);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/micro-migration/src/test/java/software/xdev/micromigration/migrater/reflection/scripts/abstractSuperClass/ValidScript.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.migrater.reflection.scripts.abstractSuperClass;
17 |
18 | import software.xdev.micromigration.eclipsestore.MigrationEmbeddedStorageManager;
19 | import software.xdev.micromigration.scripts.Context;
20 |
21 |
22 | public class ValidScript extends AbstractScript
23 | {
24 | @Override
25 | public void migrate(final Context context)
26 | {
27 | // Do nothing
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/micro-migration/src/test/java/software/xdev/micromigration/migrater/reflection/scripts/errorThrowing/ErrorThrowingScript.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.migrater.reflection.scripts.errorThrowing;
17 |
18 | import software.xdev.micromigration.eclipsestore.MigrationEmbeddedStorageManager;
19 | import software.xdev.micromigration.scripts.Context;
20 | import software.xdev.micromigration.scripts.VersionAgnosticMigrationScript;
21 | import software.xdev.micromigration.version.MigrationVersion;
22 |
23 |
24 | public class ErrorThrowingScript implements VersionAgnosticMigrationScript
25 | {
26 | public ErrorThrowingScript()
27 | {
28 | throw new Error();
29 | }
30 |
31 | @Override
32 | public MigrationVersion getTargetVersion()
33 | {
34 | return new MigrationVersion(1);
35 | }
36 |
37 | @Override
38 | public void migrate(final Context context)
39 | {
40 | // Do nothing
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/micro-migration/src/test/java/software/xdev/micromigration/migrater/reflection/scripts/exceptionThrowing/ExceptionThrowingScript.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.migrater.reflection.scripts.exceptionThrowing;
17 |
18 | import software.xdev.micromigration.eclipsestore.MigrationEmbeddedStorageManager;
19 | import software.xdev.micromigration.scripts.Context;
20 | import software.xdev.micromigration.scripts.VersionAgnosticMigrationScript;
21 | import software.xdev.micromigration.version.MigrationVersion;
22 |
23 |
24 | public class ExceptionThrowingScript implements VersionAgnosticMigrationScript
25 | {
26 | public ExceptionThrowingScript() throws Exception
27 | {
28 | throw new Exception();
29 | }
30 |
31 | @Override
32 | public MigrationVersion getTargetVersion()
33 | {
34 | return new MigrationVersion(1);
35 | }
36 |
37 | @Override
38 | public void migrate(final Context context)
39 | {
40 | // Do nothing
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/micro-migration/src/test/java/software/xdev/micromigration/migrater/reflection/scripts/includeSubPackages/ValidScript.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.migrater.reflection.scripts.includeSubPackages;
17 |
18 | import software.xdev.micromigration.eclipsestore.MigrationEmbeddedStorageManager;
19 | import software.xdev.micromigration.scripts.Context;
20 | import software.xdev.micromigration.scripts.VersionAgnosticMigrationScript;
21 | import software.xdev.micromigration.version.MigrationVersion;
22 |
23 |
24 | public class ValidScript implements VersionAgnosticMigrationScript
25 | {
26 | @Override
27 | public MigrationVersion getTargetVersion()
28 | {
29 | return new MigrationVersion(1);
30 | }
31 |
32 | @Override
33 | public void migrate(final Context context)
34 | {
35 | // Do nothing
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/micro-migration/src/test/java/software/xdev/micromigration/migrater/reflection/scripts/includeSubPackages/subpackage/ValidScriptInSubpackage.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.migrater.reflection.scripts.includeSubPackages.subpackage;
17 |
18 | import software.xdev.micromigration.eclipsestore.MigrationEmbeddedStorageManager;
19 | import software.xdev.micromigration.scripts.Context;
20 | import software.xdev.micromigration.scripts.VersionAgnosticMigrationScript;
21 | import software.xdev.micromigration.version.MigrationVersion;
22 |
23 |
24 | public class ValidScriptInSubpackage implements VersionAgnosticMigrationScript
25 | {
26 | @Override
27 | public MigrationVersion getTargetVersion()
28 | {
29 | return new MigrationVersion(2);
30 | }
31 |
32 | @Override
33 | public void migrate(final Context context)
34 | {
35 | // Do nothing
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/micro-migration/src/test/java/software/xdev/micromigration/migrater/reflection/scripts/moreClassesIncludingValid/IrrelevantClass.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.migrater.reflection.scripts.moreClassesIncludingValid;
17 |
18 | public class IrrelevantClass
19 | {
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/micro-migration/src/test/java/software/xdev/micromigration/migrater/reflection/scripts/moreClassesIncludingValid/ValidScript.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.migrater.reflection.scripts.moreClassesIncludingValid;
17 |
18 | import software.xdev.micromigration.eclipsestore.MigrationEmbeddedStorageManager;
19 | import software.xdev.micromigration.scripts.Context;
20 | import software.xdev.micromigration.scripts.VersionAgnosticMigrationScript;
21 | import software.xdev.micromigration.version.MigrationVersion;
22 |
23 |
24 | public class ValidScript implements VersionAgnosticMigrationScript
25 | {
26 | @Override
27 | public MigrationVersion getTargetVersion()
28 | {
29 | return new MigrationVersion(1);
30 | }
31 |
32 | @Override
33 | public void migrate(final Context context)
34 | {
35 | // Do nothing
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/micro-migration/src/test/java/software/xdev/micromigration/migrater/reflection/scripts/noCorrectConstructor/NoCorrectConstructorScript.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.migrater.reflection.scripts.noCorrectConstructor;
17 |
18 | import java.util.logging.Logger;
19 |
20 | import software.xdev.micromigration.eclipsestore.MigrationEmbeddedStorageManager;
21 | import software.xdev.micromigration.scripts.Context;
22 | import software.xdev.micromigration.scripts.VersionAgnosticMigrationScript;
23 | import software.xdev.micromigration.version.MigrationVersion;
24 |
25 |
26 | public class NoCorrectConstructorScript
27 | implements VersionAgnosticMigrationScript
28 | {
29 | private final String argument;
30 |
31 | public NoCorrectConstructorScript(final String argument)
32 | {
33 | this.argument = argument;
34 | }
35 |
36 | @Override
37 | public MigrationVersion getTargetVersion()
38 | {
39 | return new MigrationVersion(1);
40 | }
41 |
42 | @Override
43 | public void migrate(final Context context)
44 | {
45 | Logger.getGlobal().info(this.argument);
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/micro-migration/src/test/java/software/xdev/micromigration/migrater/reflection/scripts/reflectiveVersion/v1_ValidScript.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.migrater.reflection.scripts.reflectiveVersion;
17 |
18 | import software.xdev.micromigration.eclipsestore.MigrationEmbeddedStorageManager;
19 | import software.xdev.micromigration.scripts.Context;
20 | import software.xdev.micromigration.scripts.ReflectiveVersionMigrationScript;
21 |
22 |
23 | @SuppressWarnings("checkstyle:TypeName")
24 | public class v1_ValidScript extends ReflectiveVersionMigrationScript
25 | {
26 | @Override
27 | public void migrate(final Context context)
28 | {
29 | // Do nothing
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/micro-migration/src/test/java/software/xdev/micromigration/migrater/reflection/scripts/valid/ValidScript.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.migrater.reflection.scripts.valid;
17 |
18 | import software.xdev.micromigration.eclipsestore.MigrationEmbeddedStorageManager;
19 | import software.xdev.micromigration.scripts.Context;
20 | import software.xdev.micromigration.scripts.VersionAgnosticMigrationScript;
21 | import software.xdev.micromigration.version.MigrationVersion;
22 |
23 |
24 | public class ValidScript implements VersionAgnosticMigrationScript
25 | {
26 | @Override
27 | public MigrationVersion getTargetVersion()
28 | {
29 | return new MigrationVersion(1);
30 | }
31 |
32 | @Override
33 | public void migrate(final Context context)
34 | {
35 | // Do nothing
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/micro-migration/src/test/java/software/xdev/micromigration/scripts/ReflectiveVersionMigrationScriptTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.scripts;
17 |
18 | import org.junit.jupiter.api.Assertions;
19 | import org.junit.jupiter.api.Test;
20 |
21 | import software.xdev.micromigration.version.MigrationVersion;
22 | import software.xdev.micromigration.versionagnostic.VersionAgnosticMigrationEmbeddedStorageManager;
23 |
24 |
25 | @SuppressWarnings({"checkstyle:TypeName", "checkstyle:MethodName"})
26 | class ReflectiveVersionMigrationScriptTest
27 | {
28 | public static class v1_CorrectClassName extends ReflectiveVersionMigrationScriptDummy
29 | {
30 | }
31 |
32 | @Test
33 | void correctName_v1_CorrectClassName()
34 | {
35 | Assertions.assertEquals(new MigrationVersion(1), new v1_CorrectClassName().getTargetVersion());
36 | }
37 |
38 | public static class v1_1_CorrectClassName extends ReflectiveVersionMigrationScriptDummy
39 | {
40 | }
41 |
42 | @Test
43 | void correctName_v1_1_CorrectClassName()
44 | {
45 | Assertions.assertEquals(new MigrationVersion(1, 1), new v1_1_CorrectClassName().getTargetVersion());
46 | }
47 |
48 | public static class v1_1_1_CorrectClassName extends ReflectiveVersionMigrationScriptDummy
49 | {
50 | }
51 |
52 | @Test
53 | void correctName_v1_1_1_CorrectClassName()
54 | {
55 | Assertions.assertEquals(new MigrationVersion(1, 1, 1), new v1_1_1_CorrectClassName().getTargetVersion());
56 | }
57 |
58 | public static class v10_1_1_CorrectClassName extends ReflectiveVersionMigrationScriptDummy
59 | {
60 | }
61 |
62 | @Test
63 | void correctName_v10_1_1_CorrectClassName()
64 | {
65 | Assertions.assertEquals(new MigrationVersion(10, 1, 1), new v10_1_1_CorrectClassName().getTargetVersion());
66 | }
67 |
68 | public static class v10_10_1_CorrectClassName extends ReflectiveVersionMigrationScriptDummy
69 | {
70 | }
71 |
72 | @Test
73 | void correctName_v10_10_1_CorrectClassName()
74 | {
75 | Assertions.assertEquals(new MigrationVersion(10, 10, 1), new v10_10_1_CorrectClassName().getTargetVersion());
76 | }
77 |
78 | public static class v10_10_10_CorrectClassName extends ReflectiveVersionMigrationScriptDummy
79 | {
80 | }
81 |
82 | @Test
83 | void correctName_v10_10_10_CorrectClassName()
84 | {
85 | Assertions.assertEquals(new MigrationVersion(10, 10, 10), new v10_10_10_CorrectClassName().getTargetVersion());
86 | }
87 |
88 | public static class a1_InvalidClassName extends ReflectiveVersionMigrationScriptDummy
89 | {
90 | }
91 |
92 | @Test
93 | void invalidName_a1_InvalidClassName()
94 | {
95 | Assertions.assertThrows(IllegalArgumentException.class, a1_InvalidClassName::new);
96 | }
97 |
98 | public static class foo1_InvalidClassName extends ReflectiveVersionMigrationScriptDummy
99 | {
100 | }
101 |
102 | @Test
103 | void invalidName_foo1_InvalidClassName()
104 | {
105 | Assertions.assertThrows(IllegalArgumentException.class, foo1_InvalidClassName::new);
106 | }
107 |
108 | public static class InvalidClassName extends ReflectiveVersionMigrationScriptDummy
109 | {
110 | }
111 |
112 | @Test
113 | void invalidName_InvalidClassName()
114 | {
115 | Assertions.assertThrows(IllegalArgumentException.class, InvalidClassName::new);
116 | }
117 |
118 | public static class InvalidClassName_v1 extends ReflectiveVersionMigrationScriptDummy
119 | {
120 | }
121 |
122 | @Test
123 | void invalidName_InvalidClassName_v1()
124 | {
125 | Assertions.assertThrows(IllegalArgumentException.class, InvalidClassName_v1::new);
126 | }
127 |
128 | public static class v1_k_InvalidClassName extends ReflectiveVersionMigrationScriptDummy
129 | {
130 | }
131 |
132 | @Test
133 | void invalidName_v1_k_InvalidClassName()
134 | {
135 | Assertions.assertThrows(IllegalArgumentException.class, v1_k_InvalidClassName::new);
136 | }
137 |
138 | public static class v1_k_2_InvalidClassName extends ReflectiveVersionMigrationScriptDummy
139 | {
140 | }
141 |
142 | @Test
143 | void invalidName_v1_k_2_InvalidClassName()
144 | {
145 | Assertions.assertThrows(IllegalArgumentException.class, v1_k_2_InvalidClassName::new);
146 | }
147 |
148 | public static class v2147483648_InvalidClassName extends ReflectiveVersionMigrationScriptDummy
149 | {
150 | }
151 |
152 | @Test
153 | void invalidName_v2147483648_InvalidClassName()
154 | {
155 | Assertions.assertThrows(IllegalArgumentException.class, v2147483648_InvalidClassName::new);
156 | }
157 |
158 | public static class v___InvalidClassName extends ReflectiveVersionMigrationScriptDummy
159 | {
160 | }
161 |
162 | @Test
163 | void invalidName_v___InvalidClassName()
164 | {
165 | Assertions.assertThrows(IllegalArgumentException.class, v___InvalidClassName::new);
166 | }
167 |
168 | public static class ReflectiveVersionMigrationScriptDummy
169 | extends ReflectiveVersionMigrationScript>
171 | {
172 | @Override
173 | public void migrate(
174 | final Context> context)
175 | {
176 | // Dummy
177 | }
178 | }
179 | }
180 |
--------------------------------------------------------------------------------
/micro-migration/src/test/java/software/xdev/micromigration/version/MigrationVersionTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2021 XDEV Software (https://xdev.software)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package software.xdev.micromigration.version;
17 |
18 | import static org.junit.jupiter.api.Assertions.assertEquals;
19 |
20 | import org.junit.jupiter.api.Test;
21 |
22 |
23 | class MigrationVersionTest
24 | {
25 | @Test
26 | void checkToStringv1()
27 | {
28 | final MigrationVersion version = new MigrationVersion(1);
29 | assertEquals("v1", version.toString());
30 | }
31 |
32 | @Test
33 | void checkToStringv11()
34 | {
35 | final MigrationVersion version = new MigrationVersion(1, 1);
36 | assertEquals("v1.1", version.toString());
37 | }
38 |
39 | @Test
40 | void checkToStringv111()
41 | {
42 | final MigrationVersion version = new MigrationVersion(1, 1, 1);
43 | assertEquals("v1.1.1", version.toString());
44 | }
45 |
46 | @Test
47 | void checkToStringv0()
48 | {
49 | final MigrationVersion version = new MigrationVersion(0);
50 | assertEquals("v0", version.toString());
51 | }
52 |
53 | @Test
54 | void checkToStringvNull()
55 | {
56 | final MigrationVersion version = new MigrationVersion();
57 | assertEquals("v0", version.toString());
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | software.xdev
8 | micro-migration-root
9 | 3.0.3-SNAPSHOT
10 | pom
11 |
12 |
13 | XDEV Software
14 | https://xdev.software
15 |
16 |
17 |
18 | micro-migration
19 | micro-migration-demo
20 |
21 |
22 |
23 | UTF-8
24 | UTF-8
25 |
26 |
27 |
28 |
29 | Apache-2.0
30 | https://www.apache.org/licenses/LICENSE-2.0.txt
31 | repo
32 |
33 |
34 |
35 |
36 |
37 | checkstyle
38 |
39 |
40 |
41 | org.apache.maven.plugins
42 | maven-checkstyle-plugin
43 | 3.6.0
44 |
45 |
46 | com.puppycrawl.tools
47 | checkstyle
48 | 10.24.0
49 |
50 |
51 |
52 | .config/checkstyle/checkstyle.xml
53 | true
54 |
55 |
56 |
57 |
58 | check
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 | pmd
68 |
69 |
70 |
71 | org.apache.maven.plugins
72 | maven-pmd-plugin
73 | 3.26.0
74 |
75 | true
76 | true
77 |
78 | .config/pmd/ruleset.xml
79 |
80 |
81 |
82 |
83 | net.sourceforge.pmd
84 | pmd-core
85 | 7.13.0
86 |
87 |
88 | net.sourceforge.pmd
89 | pmd-java
90 | 7.13.0
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 | org.apache.maven.plugins
101 | maven-jxr-plugin
102 | 3.6.0
103 |
104 |
105 |
106 |
107 |
108 |
109 |
--------------------------------------------------------------------------------
/renovate.json5:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "https://docs.renovatebot.com/renovate-schema.json",
3 | "rebaseWhen": "behind-base-branch",
4 | "packageRules": [
5 | {
6 | "description": "Group Eclipse Store",
7 | "matchPackagePatterns": [
8 | "^org.eclipse.store"
9 | ],
10 | "datasources": [
11 | "maven"
12 | ],
13 | "groupName": "org.eclipse.store"
14 | },
15 | {
16 | "description": "Ignore project internal dependencies",
17 | "packagePattern": "^software.xdev:micro-migration",
18 | "datasources": [
19 | "maven"
20 | ],
21 | "enabled": false
22 | },
23 | {
24 | "description": "Group net.sourceforge.pmd",
25 | "matchPackagePatterns": [
26 | "^net.sourceforge.pmd"
27 | ],
28 | "datasources": [
29 | "maven"
30 | ],
31 | "groupName": "net.sourceforge.pmd"
32 | }
33 | ]
34 | }
35 |
--------------------------------------------------------------------------------