dubbo.apache.org/dubbo-go/v3@v3.1.1/config/config_loader_options_test.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package config
    19  
    20  import (
    21  	"strings"
    22  	"testing"
    23  )
    24  
    25  import (
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  import (
    30  	"dubbo.apache.org/dubbo-go/v3/common/constant/file"
    31  )
    32  
    33  func TestCheckGenre(t *testing.T) {
    34  
    35  	err := checkFileSuffix("abc")
    36  	assert.NotNil(t, err)
    37  
    38  	err = checkFileSuffix("zc")
    39  	assert.NotNil(t, err)
    40  
    41  	err = checkFileSuffix("json")
    42  	assert.Nil(t, err)
    43  }
    44  
    45  func TestFileGenre(t *testing.T) {
    46  	conf := NewLoaderConf(WithPath("../config/testdata/config/properties/application.properties"))
    47  	assert.Equal(t, conf.suffix, "properties")
    48  }
    49  
    50  func TestRootConfig(t *testing.T) {
    51  	rc := NewRootConfigBuilder().SetApplication(NewApplicationConfigBuilder().SetName("test-app").Build()).Build()
    52  	conf := NewLoaderConf(WithRootConfig(rc))
    53  	assert.Equal(t, conf.rc.Application.Name, "test-app")
    54  }
    55  
    56  func TestNewLoaderConf_WithBytes(t *testing.T) {
    57  	str := `dubbo.application.name=dubbo-go
    58  dubbo.application.module=local
    59  dubbo.services.HelloService.registry=nacos,zk`
    60  
    61  	conf := NewLoaderConf(WithBytes([]byte(str)), WithGenre("properties"))
    62  
    63  	assert.NotNil(t, conf)
    64  	assert.NotNil(t, conf.bytes)
    65  }
    66  
    67  func TestNewLoaderConf_WithSuffix(t *testing.T) {
    68  	conf := NewLoaderConf(
    69  		WithSuffix(file.JSON),
    70  		WithPath("../config/testdata/config/properties/application.properties"),
    71  	)
    72  
    73  	assert.Equal(t, conf.suffix, string(file.PROPERTIES))
    74  }
    75  
    76  func TestResolverFilePath(t *testing.T) {
    77  	name, suffix := resolverFilePath("../config/application.properties")
    78  	assert.Equal(t, name, "application")
    79  	assert.Equal(t, suffix, "properties")
    80  }
    81  
    82  func TestResolverFilePath_Illegal_Path(t *testing.T) {
    83  	name, suffix := resolverFilePath("application.properties")
    84  	assert.Equal(t, name, "application")
    85  	assert.Equal(t, suffix, "properties")
    86  }
    87  
    88  func TestResolverFilePath_Illegal_Path_Name(t *testing.T) {
    89  	name, suffix := resolverFilePath("application")
    90  	assert.Equal(t, name, "application")
    91  	assert.Equal(t, suffix, string(file.YAML))
    92  }
    93  
    94  func Test_getActiveFilePath(t *testing.T) {
    95  	conf := NewLoaderConf(
    96  		WithSuffix(file.JSON),
    97  		WithPath("../config/testdata/config/properties/application.properties"),
    98  	)
    99  
   100  	filePath := conf.getActiveFilePath("dev")
   101  
   102  	assert.Equal(t, strings.HasSuffix(filePath, "application-dev.properties"), true)
   103  
   104  	exists := pathExists(filePath)
   105  	assert.Equal(t, exists, false)
   106  	exists = pathExists("application.properties")
   107  	assert.Equal(t, exists, false)
   108  
   109  }