UserPropertiesMigrationImpl implements UserPropertiesMigration { @SuppressWarnings("unchecked") @Override public <T extends UserProperties> T migrate(UserProperties properties, Class<T> target) { if (target.isAssignableFrom(properties.getClass())) return (T) properties; if (UserPropertiesV1.class.isAssignableFrom(properties.getClass())) { if (target == UserPropertiesV2.class) { return (T) getV2Properties((UserPropertiesV1) properties); } else if (target == UserPropertiesV3.class) { properties = getV2Properties((UserPropertiesV1) properties); return (T) getV3Properties((UserPropertiesV2) properties); } else if (target == UserPropertiesV4.class) { properties = getV2Properties((UserPropertiesV1) properties); properties = getV3Properties((UserPropertiesV2) properties); return (T) getV4Properties((UserPropertiesV3) properties); } } if (UserPropertiesV2.class.isAssignableFrom(properties.getClass())) { if (target == UserPropertiesV1.class) { return (T) getV1Properties((UserPropertiesV2) properties); } else if (target == UserPropertiesV3.class) { return (T) getV3Properties((UserPropertiesV2) properties); } else if (target == UserPropertiesV4.class) { properties = getV3Properties((UserPropertiesV2) properties); return (T) getV4Properties((UserPropertiesV3) properties); } } if (UserPropertiesV3.class.isAssignableFrom(properties.getClass())) { if (target == UserPropertiesV1.class) { properties = getV2PropertiesFromV3((UserPropertiesV3) properties); return (T) getV1Properties((UserPropertiesV2) properties); } else if (target == UserPropertiesV2.class) { return (T) getV2PropertiesFromV3((UserPropertiesV3) properties); } else if (target == UserPropertiesV4.class) { return (T) getV4Properties((UserPropertiesV3) properties); } } if (UserPropertiesV4.class.isAssignableFrom(properties.getClass())) { if (target == UserPropertiesV1.class) { properties = getV3PropertiesFromV4((UserPropertiesV4) properties); properties = getV2PropertiesFromV3((UserPropertiesV3) properties); return (T) getV1Properties((UserPropertiesV2) properties); } else if (target == UserPropertiesV2.class) { properties = getV3PropertiesFromV4((UserPropertiesV4) properties); return (T) getV2PropertiesFromV3((UserPropertiesV3) properties); } else if (target == UserPropertiesV3.class) { return (T) getV3PropertiesFromV4((UserPropertiesV4) properties); } } throw new IllegalStateException("can't convert properties: from=" + properties.getClass() + " to=" + target); }  @Autowired void setUserDao(UserDao userDao); @PreDestroy void stop(); @Override boolean needsMigration(UserProperties properties, Class<?> target); @SuppressWarnings("unchecked") @Override T migrate(UserProperties properties,
      Class<T> target); @Override void startUserPropertiesBulkMigration(
      Class<T> target); @Override UserPropertiesMigrationStatus getUserPropertiesBulkMigrationStatus();  }
@Test public void testV1ToV2Migration() { UserPropertiesV1 v1 = new UserPropertiesV1(); v1.setDefaultLocationLat(47.0); v1.setDefaultLocationLon(-122.0); v1.setDefaultLocationName("Seattle"); v1.setLastSelectedStopId("1_29214"); v1.setRememberPreferencesEnabled(true); v1.setBookmarkedStopIds(Arrays.asList("1_29214", "1_75403")); UserPropertiesV1 result = _service.migrate(v1, UserPropertiesV1.class); assertTrue(v1 == result); UserPropertiesV2 v2 = _service.migrate(v1, UserPropertiesV2.class); assertTrue(v2.isRememberPreferencesEnabled()); assertEquals(47.0, v2.getDefaultLocationLat(), 0.0); assertEquals(-122.0, v2.getDefaultLocationLon(), 0.0); assertEquals("Seattle", v2.getDefaultLocationName()); List<Bookmark> bookmarks = v2.getBookmarks(); assertEquals(2, bookmarks.size()); Bookmark bookmark = bookmarks.get(0); assertEquals(0,bookmark.getId()); assertNull(bookmark.getName()); assertEquals(Arrays.asList("1_29214"), bookmark.getStopIds()); assertTrue(bookmark.getRouteFilter().getRouteIds().isEmpty()); bookmark = bookmarks.get(1); assertEquals(1,bookmark.getId()); assertNull(bookmark.getName()); assertEquals(Arrays.asList("1_75403"), bookmark.getStopIds()); assertTrue(bookmark.getRouteFilter().getRouteIds().isEmpty()); }
@Test public void testV2ToV1Migration() { UserPropertiesV2 v2 = new UserPropertiesV2(); v2.setDefaultLocationLat(47.0); v2.setDefaultLocationLon(-122.0); v2.setDefaultLocationName("Seattle"); v2.setRememberPreferencesEnabled(true); Bookmark b1 = new Bookmark(0,null,Arrays.asList("1_29214"),new RouteFilter()); Bookmark b2 = new Bookmark(1,null,Arrays.asList("1_75403", "1_75414"), new RouteFilter()); v2.setBookmarks(Arrays.asList(b1, b2)); UserPropertiesV2 result = _service.migrate(v2, UserPropertiesV2.class); assertTrue(v2 == result); UserPropertiesV1 v1 = _service.migrate(v2, UserPropertiesV1.class); assertTrue(v1.isRememberPreferencesEnabled()); assertNull(v1.getLastSelectedStopId()); assertEquals(47.0, v1.getDefaultLocationLat(), 0.0); assertEquals(-122.0, v1.getDefaultLocationLon(), 0.0); assertEquals("Seattle", v1.getDefaultLocationName()); assertEquals(Arrays.asList("1_29214", "1_75403", "1_75414"), v1.getBookmarkedStopIds()); }
@Test public void testV2ToV3Migration() { UserPropertiesV2 v2 = new UserPropertiesV2(); v2.setDefaultLocationLat(47.0); v2.setDefaultLocationLon(-122.0); v2.setDefaultLocationName("Seattle"); v2.setRememberPreferencesEnabled(true); Bookmark b1 = new Bookmark(0,null,Arrays.asList("1_29214"),new RouteFilter()); Bookmark b2 = new Bookmark(1,null,Arrays.asList("1_75403", "1_75414"), new RouteFilter()); v2.setBookmarks(Arrays.asList(b1, b2)); UserPropertiesV2 result = _service.migrate(v2, UserPropertiesV2.class); assertTrue(v2 == result); UserPropertiesV3 v3 = _service.migrate(v2, UserPropertiesV3.class); assertTrue(v3.isRememberPreferencesEnabled()); assertEquals(47.0, v3.getDefaultLocationLat(), 0.0); assertEquals(-122.0, v3.getDefaultLocationLon(), 0.0); assertEquals("Seattle", v3.getDefaultLocationName()); List<Bookmark> bookmarks = v3.getBookmarks(); assertEquals(2, bookmarks.size()); Bookmark bookmark = bookmarks.get(0); assertEquals(0,bookmark.getId()); assertNull(bookmark.getName()); assertEquals(Arrays.asList("1_29214"), bookmark.getStopIds()); assertTrue(bookmark.getRouteFilter().getRouteIds().isEmpty()); bookmark = bookmarks.get(1); assertEquals(1,bookmark.getId()); assertNull(bookmark.getName()); assertEquals(Arrays.asList("1_75403", "1_75414"), bookmark.getStopIds()); assertTrue(bookmark.getRouteFilter().getRouteIds().isEmpty()); }
@Test public void testV3ToV2Migration() { UserPropertiesV3 v3 = new UserPropertiesV3(); v3.setDefaultLocationLat(47.0); v3.setDefaultLocationLon(-122.0); v3.setDefaultLocationName("Seattle"); v3.setRememberPreferencesEnabled(true); Bookmark b1 = new Bookmark(0,null,Arrays.asList("1_29214"),new RouteFilter()); Bookmark b2 = new Bookmark(1,null,Arrays.asList("1_75403", "1_75414"), new RouteFilter()); v3.setBookmarks(Arrays.asList(b1, b2)); UserPropertiesV3 result = _service.migrate(v3, UserPropertiesV3.class); assertTrue(v3 == result); UserPropertiesV2 v2 = _service.migrate(v3, UserPropertiesV2.class); assertTrue(v2.isRememberPreferencesEnabled()); assertEquals(47.0, v2.getDefaultLocationLat(), 0.0); assertEquals(-122.0, v2.getDefaultLocationLon(), 0.0); assertEquals("Seattle", v2.getDefaultLocationName()); List<Bookmark> bookmarks = v2.getBookmarks(); assertEquals(2, bookmarks.size()); Bookmark bookmark = bookmarks.get(0); assertEquals(0,bookmark.getId()); assertNull(bookmark.getName()); assertEquals(Arrays.asList("1_29214"), bookmark.getStopIds()); assertTrue(bookmark.getRouteFilter().getRouteIds().isEmpty()); bookmark = bookmarks.get(1); assertEquals(1,bookmark.getId()); assertNull(bookmark.getName()); assertEquals(Arrays.asList("1_75403", "1_75414"), bookmark.getStopIds()); assertTrue(bookmark.getRouteFilter().getRouteIds().isEmpty()); }