Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7339f1b09bd3e42d3e6506a50ea9eb2df6b57aa4 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2011 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.orcs.db.mock.internal;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import org.eclipse.osee.framework.core.data.IDatabaseInfo;
import org.eclipse.osee.framework.database.IOseeDatabaseService;
import org.eclipse.osee.framework.database.core.IDatabaseInfoProvider;
import org.eclipse.osee.framework.database.core.OseeConnection;
import org.eclipse.osee.framework.h2.H2DbServer;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.network.PortUtil;
import org.eclipse.osee.orcs.db.mock.OseeDatabase;
import org.junit.Assert;
import org.junit.runners.model.FrameworkMethod;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceRegistration;

/**
 * @author Roberto E. Escobar
 */
public class TestDatabase {

   private final FrameworkMethod method;
   private final Object target;

   private ServiceRegistration<?> registration;
   private File tempFolder;
   private final String connectionId;

   public TestDatabase(String connectionId, FrameworkMethod method, Object target) {
      this.connectionId = connectionId;
      this.method = method;
      this.target = target;
   }

   private File createTempFolder(FrameworkMethod method, Object target) {
      String tempDir = System.getProperty("user.home");
      String folderName =
         String.format("%s_%s_%s", target.getClass().getSimpleName(), method.getName(), Lib.getDateTimeString());
      File tempFolder = new File(tempDir, folderName);
      tempFolder.mkdir();
      return tempFolder;
   }

   public void initialize() throws Exception {
      Bundle bundle = FrameworkUtil.getBundle(OseeDatabase.class);
      Assert.assertNotNull("Bundle cannot be null", bundle);
      int state = bundle.getState();
      if (state != Bundle.STARTING || state != Bundle.ACTIVE) {
         bundle.start();
      }

      tempFolder = createTempFolder(method, target);
      Assert.assertNotNull("TempFolder cannot be null", tempFolder);

      addResource(tempFolder, bundle, "data/h2.zip");
      addResource(tempFolder, bundle, "data/binary_data.zip");

      checkExist(tempFolder, "h2");
      checkExist(tempFolder, "attr");

      String dbPath = getDbHomePath(tempFolder, "h2");

      int port = PortUtil.getInstance().getConsecutiveValidPorts(2);
      int webPort = port + 1;

      IDatabaseInfo databaseInfo = new DbInfo(connectionId, port, dbPath);
      TestDbProvider provider = new TestDbProvider(databaseInfo);

      System.setProperty("osee.db.embedded.server", "");
      System.setProperty("osee.application.server.data", tempFolder.getAbsolutePath());
      registerProvider(provider);

      IOseeDatabaseService dbService = OsgiUtil.getService(IOseeDatabaseService.class);
      Assert.assertNotNull(dbService);

      H2DbServer.startServer("0.0.0.0", port, webPort);

      OseeConnection connection = dbService.getConnection();
      try {
         Assert.assertNotNull(connection);
      } finally {
         connection.close();
      }
   }

   private void registerProvider(IDatabaseInfoProvider service) {
      BundleContext context = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
      registration = context.registerService(IDatabaseInfoProvider.class, service, null);
   }

   private String getDbHomePath(File tempFolder, String dbFolder) {
      return String.format("~/%s/%s", tempFolder.getName(), dbFolder);
   }

   private void checkExist(File tempFolder, String name) {
      File toCheck = new File(tempFolder, name);
      Assert.assertTrue(String.format("%s does not exist", name), toCheck.exists());
   }

   private void addResource(File targetDirectory, Bundle bundle, String resource) throws IOException {
      URL resourceURL = bundle.getResource(resource);
      InputStream inputStream = null;
      try {
         inputStream = new BufferedInputStream(resourceURL.openStream());
         Lib.decompressStream(inputStream, targetDirectory);
      } finally {
         Lib.close(inputStream);
      }
   }

   public void cleanup() {
      //TODO issue shutdown command to server;
      if (registration != null) {
         registration.unregister();
      }

      System.setProperty("osee.application.server.data", "");
      System.setProperty("osee.db.embedded.server", "");
      H2DbServer.stopServer();
      Runtime.getRuntime().addShutdownHook(new Thread() {
         @Override
         public void run() {
            if (tempFolder != null) {
               Lib.deleteDir(tempFolder);
            }
         }
      });
   }
}

Back to the top