summaryrefslogtreecommitdiff
path: root/platform/platform-tests/testSrc/com/intellij/openapi/components/impl/ApplicationStoreTest.java
blob: 719635a19a305842ce311026697e06217106e69c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package com.intellij.openapi.components.impl;

import com.intellij.application.options.PathMacrosImpl;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.*;
import com.intellij.openapi.components.impl.stores.*;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.CharsetToolkit;
import com.intellij.testFramework.LightPlatformLangTestCase;
import com.intellij.util.xmlb.XmlSerializerUtil;
import gnu.trove.THashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

public class ApplicationStoreTest extends LightPlatformLangTestCase {
  private File testAppConfig;
  private MyComponentStore componentStore;

  @Override
  public void setUp() throws Exception {
    super.setUp();

    String testAppConfigPath = System.getProperty("test.app.config.path");
    if (testAppConfigPath == null) {
      testAppConfig = FileUtil.createTempDirectory("testAppSettings", null);
    }
    else {
      testAppConfig = new File(FileUtil.expandUserHome(testAppConfigPath));
    }
    FileUtil.delete(testAppConfig);

    componentStore = new MyComponentStore(testAppConfig.getAbsolutePath());
  }

  @Override
  public void tearDown() throws Exception {
    try {
      Disposer.dispose(componentStore);
      componentStore = null;
    }
    finally {
      try {
        super.tearDown();
      }
      finally {
        FileUtil.delete(testAppConfig);
      }
    }
  }

  public void testStreamProviderSaveIfSeveralStoragesConfigured() throws Exception {
    SeveralStoragesConfigured component = new SeveralStoragesConfigured();

    MyStreamProvider streamProvider = new MyStreamProvider();
    componentStore.getStateStorageManager().setStreamProvider(streamProvider);

    componentStore.initComponent(component, false);
    component.foo = "newValue";
    StoreUtil.doSave(componentStore);

    assertThat(streamProvider.data.get(RoamingType.PER_USER).get(StoragePathMacros.APP_CONFIG + "/proxy.settings.xml"), equalTo("<application>\n" +
                                                                                                                                "  <component name=\"HttpConfigurable\">\n" +
                                                                                                                                "    <option name=\"foo\" value=\"newValue\" />\n" +
                                                                                                                                "  </component>\n" +
                                                                                                                                "</application>"));
  }

  public void testLoadFromStreamProvider() throws Exception {
    SeveralStoragesConfigured component = new SeveralStoragesConfigured();

    MyStreamProvider streamProvider = new MyStreamProvider();
    THashMap<String, String> map = new THashMap<String, String>();
    map.put(StoragePathMacros.APP_CONFIG + "/proxy.settings.xml", "<application>\n" +
                                                                  "  <component name=\"HttpConfigurable\">\n" +
                                                                  "    <option name=\"foo\" value=\"newValue\" />\n" +
                                                                  "  </component>\n" +
                                                                  "</application>");
    streamProvider.data.put(RoamingType.PER_USER, map);

    componentStore.getStateStorageManager().setStreamProvider(streamProvider);
    componentStore.initComponent(component, false);
    assertThat(component.foo, equalTo("newValue"));
  }

  private static class MyStreamProvider extends StreamProvider {
    public final Map<RoamingType, Map<String, String>> data = new THashMap<RoamingType, Map<String, String>>();

    @Override
    public void saveContent(@NotNull String fileSpec,
                            @NotNull byte[] content,
                            int size,
                            @NotNull RoamingType roamingType,
                            boolean async) {
      getMap(roamingType).put(fileSpec, new String(content, 0, size, CharsetToolkit.UTF8_CHARSET));
    }

    private Map<String, String> getMap(@NotNull RoamingType roamingType) {
      Map<String, String> map = data.get(roamingType);
      if (map == null) {
        map = new THashMap<String, String>();
        data.put(roamingType, map);
      }
      return map;
    }

    @Nullable
    @Override
    public InputStream loadContent(@NotNull String fileSpec, @NotNull RoamingType roamingType) throws IOException {
      String data = getMap(roamingType).get(fileSpec);
      return data == null ? null : new ByteArrayInputStream(data.getBytes(CharsetToolkit.UTF8_CHARSET));
    }

    @Override
    public void delete(@NotNull String fileSpec, @NotNull RoamingType roamingType) {
      Map<String, String> map = data.get(roamingType);
      if (map != null) {
        map.remove(fileSpec);
      }
    }
  }

  class MyComponentStore extends ComponentStoreImpl implements Disposable {
    private final StateStorageManager stateStorageManager;

    MyComponentStore(@NotNull final String testAppConfigPath) {
      TrackingPathMacroSubstitutor macroSubstitutor = new ApplicationPathMacroManager().createTrackingSubstitutor();
      stateStorageManager = new StateStorageManagerImpl(macroSubstitutor, "application", this, ApplicationManager.getApplication().getPicoContainer()) {
        @Override
        protected StorageData createStorageData(String storageSpec) {
          return new FileBasedStorage.FileStorageData("application");
        }

        @Nullable
        @Override
        protected String getOldStorageSpec(Object component, final String componentName, final StateStorageOperation operation) {
          return null;
        }

        @Override
        protected String getVersionsFilePath() {
          return testAppConfigPath + "/options/appComponentVersions.xml";
        }

        @Override
        protected TrackingPathMacroSubstitutor getMacroSubstitutor(@NotNull final String fileSpec) {
          if (fileSpec.equals(StoragePathMacros.APP_CONFIG + "/" + PathMacrosImpl.EXT_FILE_NAME + ".xml")) {
            return null;
          }
          return super.getMacroSubstitutor(fileSpec);
        }
      };

      stateStorageManager.addMacro(StoragePathMacros.APP_CONFIG, testAppConfigPath);
    }

    @Override
    public void load() throws IOException, StateStorageException {
    }

    @NotNull
    @Override
    public StateStorageManager getStateStorageManager() {
      return stateStorageManager;
    }

    @Override
    public void dispose() {
    }

    @Nullable
    @Override
    protected StateStorage getDefaultsStorage() {
      return null;
    }
  }

  static class SeveralStoragesConfiguredStorageChooser implements StateStorageChooser<SeveralStoragesConfigured> {
    @Override
    public Storage[] selectStorages(Storage[] storages, SeveralStoragesConfigured component, StateStorageOperation operation) {
      if (operation == StateStorageOperation.WRITE) {
        for (Storage storage : storages) {
          if (storage.file().equals(StoragePathMacros.APP_CONFIG + "/proxy.settings.xml")) {
            return new Storage[]{storage};
          }
        }
      }
      return storages;
    }
  }

  @State(
    name = "HttpConfigurable",
    storages = {
      // we use two storages due to backward compatibility, see http://crucible.labs.intellij.net/cru/CR-IC-5142
      @Storage(file = StoragePathMacros.APP_CONFIG + "/other.xml"),
      @Storage(file = StoragePathMacros.APP_CONFIG + "/proxy.settings.xml")
    },
    storageChooser = SeveralStoragesConfiguredStorageChooser.class
  )
  static class SeveralStoragesConfigured implements PersistentStateComponent<SeveralStoragesConfigured> {
    public String foo = "defaultValue";

    @Nullable
    @Override
    public SeveralStoragesConfigured getState() {
      return this;
    }

    @Override
    public void loadState(SeveralStoragesConfigured state) {
      XmlSerializerUtil.copyBean(state, this);
    }
  }
}